Hierarchical Clustering
Hierarchical clustering builds a nested family of clusterings of points . The family runs from single points up to one cluster. The usual agglomerative version works bottom-up. It starts with every point in its own cluster. At each step it merges the two clusters and with the smallest cluster distance . After merges, one cluster remains. The method needs a point distance , usually Euclidean. It also needs a linkage, the rule that turns point distances into a cluster distance:
- Single linkage: , the closest pair. It tends to chain, so a run of close points can join two distant groups.
- Complete linkage: , the farthest pair. It gives compact clusters of similar diameter, and one outlier can distort it.
- Average linkage: , the mean over all pairs. Its behavior falls between the two above.
- Ward linkage: , where is the mean of cluster and its size. This is the increase in the within-cluster sum of squares caused by the merge. Ward linkage therefore minimizes the k-means objective greedily.
The dendrogram draws the merges as a tree. Its leaves are the points, and each merge is a bar at height . A horizontal cut at height keeps the merges below , and the branches it crosses are the clusters. Let be the merge heights. Any cut with gives clusters. The user chooses after the fit, often at a large gap between successive heights. The four linkages above never merge lower than an earlier merge, so the heights increase up the tree.
The method stores all point distances, so it needs memory. A naive version scans all pairs at every merge and takes time. Priority queues bring this to . Divisive clustering works top-down, splitting one cluster recursively, for example with 2-means at each split. Like k-means, the method needs features on comparable scales.
Intuition
The name describes the output. The clusters form a hierarchy. Each cluster at a fine level sits inside one cluster at every coarser level. “Agglomerative” means built by gathering. The algorithm gathers the two most similar groups and repeats. K-means returns one partition for one fixed . Hierarchical clustering returns the partitions for every from down to in one run. The dendrogram shows all of them, and the user picks the level that suits the task.
The linkage decides which clusters count as closest, and it changes the result. Single linkage asks whether any member of one group is near any member of the other. It follows chains of neighbors and can trace long or curved groups. Complete linkage asks whether every member of one group is near every member of the other. It keeps clusters tight. Ward linkage asks how much the merge would spread the points around their mean. Like k-means, it tends to give round clusters of similar size. A Gaussian mixture model is the model-based alternative when soft assignments are needed.
Properties
(Reading Merge Heights) The height where two points first share a cluster measures how different the method considers them.
This height is the cophenetic distance of the two points. Points that join low are similar, and a long vertical branch marks a well-separated cluster. The left-to-right order of the leaves carries little meaning. Each of the merges can swap its two children, so drawings show the same tree. Two neighboring leaves may join only at the top. Reading heights also needs a linkage whose heights increase. Centroid linkage, which compares cluster means, can break this. On an equilateral triangle with side , it first merges two corners at height . Their mean lies from the third corner, so the next merge sits lower. Such an inversion draws a merge below its children, and cuts at different heights then disagree.
(Single Linkage Chains) Single linkage merges points along the minimum spanning tree, so it finds connected groups of any shape.
The minimum spanning tree joins all points with the smallest total edge length. Gower and Ross (1969) showed that single linkage adds its edges in increasing order. The clusters at height are therefore the connected groups of the graph that links every pair closer than . Single linkage can recover rings and crescents, which k-means splits with flat boundaries. One chain of noise points can also join two real clusters. Complete and Ward linkage resist chaining, but they cut long, thin clusters into pieces. The SLINK algorithm (Sibson 1973) computes single linkage in time.
(Ward Matches K-Means) The Ward merge costs add up to the k-means objective of each cut.
Each single point has a within-cluster sum of squares of zero. Each Ward merge adds exactly its cost to . The -cluster cut therefore has , where is the cost of merge . Ward’s method takes the cheapest merge at each step and never undoes one. Its -cluster cut is a good k-means partition but not always the best one. A common recipe uses the Ward cut to start Lloyd’s algorithm, which then refines it. Software such as SciPy reports the height , which equals the Euclidean distance when both clusters are single points. Ward linkage compares means, so it assumes Euclidean distance.
(Quadratic Memory) The distance matrix limits hierarchical clustering to tens of thousands of points on one machine.
With , the distances take about GB in double precision. The nearest-neighbor chain algorithm (Murtagh 1983) runs in time for single, complete, average and Ward linkage. Memory therefore sets the limit. For larger data, practitioners first run k-means with a few hundred centers and then cluster the centers. Distances also lose contrast as grows, as described in 7.2. Curse of Dimensionality. The merge heights then bunch together, and the dendrogram shows no clear gap. Reducing the data with PCA first helps.
(Clustering Assets) In finance, the distance lets hierarchical clustering group assets whose returns move together.
Here is the correlation of the returns of assets and . Standardize each return series to mean and variance over periods. The Euclidean distance between two such series, divided by , equals . It is for , for and for . It obeys the triangle inequality, which does not. Mantegna (1999) built the single-linkage tree of US stocks with this distance. Its branches matched industry sectors. López de Prado (2016) built hierarchical risk parity on such a tree. That method orders the assets by the tree and splits weight between groups in inverse proportion to their variance. It never inverts the covariance matrix, which mean-variance optimization needs.
Examples
(Easy) Campsites on a Trail
A ranger maps five campsites along a river trail, at kilometers , , , and . The ranger clusters them with single linkage and with complete linkage.
List the merges and their heights for each linkage. Do the dendrograms agree, and what does each give for ? Both linkages first merge the closest pair, , at height . With single linkage, the site at is from the nearer member and joins at height . The site at is from the site at , so it joins at height . The site at joins last, at height . Each step adds one site to a growing chain. With complete linkage, the site at is from the farther member and joins at height . Next, the sites at and are apart. The site at is from the site at , so forms first, at height . The last merge happens at height , the full span. The trees differ in shape and height. Single linkage merges at , and complete linkage at . For , single linkage cuts between and and gives and . Complete linkage cuts between and and gives and .
(Medium) Grouping Five Stocks
A fund holds five stocks: a chipmaker, a software firm, a water utility, an electric utility and a gold miner. Their daily returns over two years have the correlation matrix below, with the stocks in that order.
An analyst uses the distance and average linkage.Build the dendrogram. Where does the cut for go, and which does the largest gap suggest? The two tech stocks are apart, and the two utilities are apart. These are the two smallest distances. The first merges therefore form the tech pair at and the utility pair at . The four distances between the pairs are , , and . Their mean, , is the height of the third merge. The gold miner is from every stock, so it joins last, at . The cut for goes between and . It leaves the gold miner alone against the other four. The gaps between successive heights are , and . The largest gap lies between and , so it suggests . The three clusters are the tech pair, the utility pair and the gold miner. Single and complete linkage give the same tree here, with third merge heights of and .
See Also
- 6.1. K-Means, the flat method whose objective Ward linkage minimizes greedily.
- 6.2. Gaussian Mixture Models, the model-based alternative with soft assignments.
- 6. Clustering, for the other clustering methods.
- 7.2. Curse of Dimensionality, for why distances lose contrast with many features.
- 4.3b Modern Portfolio Theory, for the covariance-based allocation that tree-based portfolios avoid inverting.