Hierarchical Agglomerative Clustering
Start with every point as its own cluster, repeatedly glue the two closest together, and record the whole history as a tree. You get every possible number of clusters at once and choose the granularity afterwards by cutting the tree.
Prerequisites: K-Means Clustering, Correlation
K-Means Clustering makes you commit before you start: how many groups are there? Often you have no idea, and worse, the honest answer is "it depends how closely you look." Two energy names are one group at fine resolution and part of a much larger cyclical bloc at coarse resolution. Both statements are true. Hierarchical clustering refuses to choose — it builds the entire nested family of groupings, from singletons up to one big blob, and lets you pick the resolution afterwards.
Think of administrative geography. Villages merge into districts, districts into counties, counties into states. Nobody had to declare in advance that the country has exactly seventeen regions; the structure is a tree and you read it at whatever level you need. Agglomerative clustering builds exactly that tree, bottom-up, by a rule so simple it fits in one line: repeatedly merge the two closest clusters.
The algorithm and the one real choice
Start with clusters, one per point. Compute all pairwise distances. Find the smallest, merge that pair, record the distance at which they merged, and recompute distances from the new cluster to everything else. Repeat times and you are down to one cluster. Plotting each merge as a horizontal bar at its merge height gives a dendrogram.
The only genuine decision is what "distance between two clusters" means once a cluster holds more than one point. That rule is called the linkage:
- Single linkage — the distance between the two closest members. Finds long, stringy shapes; prone to chaining.
- Complete linkage — the distance between the two furthest members. Produces compact, similarly sized clusters.
- Average linkage — the mean over all cross-pairs. A middle course, and the usual default for correlation data.
- Ward linkage — merge whichever pair increases total within-cluster variance least. Behaves much like k-means, but hierarchically.
Worked example 1: four assets
Correlations between four names: , , , , , . Correlation is not a distance, so convert it with the standard transform , which turns perfect correlation into zero distance and independence into :
and the four cross pairs come out at , , , .
Merge 1. The smallest entry is , so A and B join at height . Merge 2. The smallest remaining is , so C and D join at height . Merge 3. Only two clusters are left. Under complete linkage their distance is the largest cross-pair, , so they join there. (Under single linkage the same merge would sit at — the tree's shape is identical here, only its heights move.)
Cut the tree anywhere between and — say at — and you get exactly two clusters, and . Cut at and you get three: , , .
The dendrogram is the output, not a picture of it. You get all from to in one fit, and choosing becomes choosing a cut height — a decision about how much dissimilarity you are willing to call "the same group."
Worked example 2: linkage changes the answer
Five points on a line: .
Under single linkage, and merge at distance . The nearest member of to point is , one unit away, so joins at height as well; then joins at height for the same reason. The blob has grown by stepping stones — this is chaining. Finally joins at . Heights: .
Under complete linkage, and merge at , and and merge at . Now and are at , while each is from , so the two pairs merge at and joins last at . Heights: .
Cut both trees at height . Single linkage says and ; complete linkage says , and . Same five numbers, same algorithm, different structure — because the linkage rule is part of the model.
What this means in practice
The headline use in finance is portfolio construction. Hierarchical Risk Parity (López de Prado) clusters assets on correlation distance, reorders the covariance matrix so related names sit adjacent, and allocates down the tree — sidestepping the matrix inversion that makes Pitfalls of Mean-Variance Optimization so fragile. The same dendrogram is also how you discover that the official sector labels disagree with how things actually trade.
Cost is the practical limit: you need the full pairwise distance matrix, which is memory, so a few thousand items is comfortable and a million is not. Below that, hierarchical clustering is usually the more informative first look, because the tree shows you whether there is structure before you commit to a number.
Merges are greedy and permanent — an early mistake propagates all the way up, and unlike k-means there is no reassignment step to undo it. Worse, the algorithm always returns a beautiful tree, even on pure noise. Before reading meaning into a dendrogram, check that it survives resampling and that the merge heights actually jump somewhere; a tree with evenly spaced merges is telling you there are no clusters.
Related concepts
Practice in interviews
Further reading
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (Ch. 14.3)
- Mantegna, Hierarchical Structure in Financial Markets (1999)
- López de Prado, Building Diversified Portfolios that Outperform Out-of-Sample (2016)