Quant Memo
Core

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 nn 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 nn 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 n1n-1 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.
single · nearest pair complete · furthest pair
Same two groups, two different answers to "how far apart are they?". The linkage rule, not the data, decides which merges happen first.

Worked example 1: four assets

Correlations between four names: ρAB=0.90\rho_{AB} = 0.90, ρCD=0.80\rho_{CD} = 0.80, ρAC=0.30\rho_{AC} = 0.30, ρAD=0.20\rho_{AD} = 0.20, ρBC=0.35\rho_{BC} = 0.35, ρBD=0.25\rho_{BD} = 0.25. Correlation is not a distance, so convert it with the standard transform d=2(1ρ)d = \sqrt{2(1-\rho)}, which turns perfect correlation into zero distance and independence into 2\sqrt{2}:

dAB=0.20=0.447,dCD=0.40=0.632d_{AB} = \sqrt{0.20} = 0.447, \quad d_{CD} = \sqrt{0.40} = 0.632

and the four cross pairs come out at dAC=1.183d_{AC} = 1.183, dAD=1.265d_{AD} = 1.265, dBC=1.140d_{BC} = 1.140, dBD=1.225d_{BD} = 1.225.

Merge 1. The smallest entry is 0.4470.447, so A and B join at height 0.4470.447. Merge 2. The smallest remaining is 0.6320.632, so C and D join at height 0.6320.632. Merge 3. Only two clusters are left. Under complete linkage their distance is the largest cross-pair, max(1.183,1.265,1.140,1.225)=1.265\max(1.183, 1.265, 1.140, 1.225) = 1.265, so they join there. (Under single linkage the same merge would sit at 1.1401.140 — the tree's shape is identical here, only its heights move.)

Cut the tree anywhere between 0.6320.632 and 1.2651.265 — say at 1.01.0 — and you get exactly two clusters, {A,B}\{A, B\} and {C,D}\{C, D\}. Cut at 0.50.5 and you get three: {A,B}\{A,B\}, {C}\{C\}, {D}\{D\}.

0 0.5 1.0 cut → 2 clusters 0.45 0.63 1.27 A B C D correlation distance on the vertical axis
The dendrogram stores every grouping at once. A horizontal cut turns the tree into a flat clustering; slide the cut down and clusters split.

The dendrogram is the output, not a picture of it. You get all kk from 11 to nn in one fit, and choosing kk 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: 1,2,3,4,101, 2, 3, 4, 10.

Under single linkage, 11 and 22 merge at distance 11. The nearest member of {1,2}\{1,2\} to point 33 is 22, one unit away, so 33 joins at height 11 as well; then 44 joins at height 11 for the same reason. The blob has grown by stepping stones — this is chaining. Finally 1010 joins at min(104)=6\min(|10-4|) = 6. Heights: 1,1,1,61, 1, 1, 6.

Under complete linkage, 11 and 22 merge at 11, and 33 and 44 merge at 11. Now {1,2}\{1,2\} and {3,4}\{3,4\} are at max(14,13,24,23)=3\max(|1-4|, |1-3|, |2-4|, |2-3|) = 3, while each is 99 from 1010, so the two pairs merge at 33 and 1010 joins last at 99. Heights: 1,1,3,91, 1, 3, 9.

Cut both trees at height 22. Single linkage says {1,2,3,4}\{1,2,3,4\} and {10}\{10\}; complete linkage says {1,2}\{1,2\}, {3,4}\{3,4\} and {10}\{10\}. 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 O(n2)O(n^2) 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)
ShareTwitterLinkedIn