# Hierarchical Clustering

In data mining and statistics, hierarchical clustering (also called hierarchical cluster analysis or HCA) is a method of cluster analysis which seeks to build a hierarchy of clusters. Strategies for hierarchical clustering generally fall into two types:

Type Description
Agglomerative This is a bottom-up approach: each observation starts in its own cluster, and pairs of clusters are merged as one moves up the hierarchy
Divisive This is a top-down approach: all observations start in one cluster, and splits are performed recursively as one moves down the hierarchy

In general, the merges and splits are determined in a greedy manner. The results of hierarchical clustering are usually presented in a dendrogram.

## Cluster dissimilarity

In order to decide which clusters should be combined (for agglomerative), or where a cluster should be split (for divisive), a measure of dissimilarity between sets of observations is required. In most methods of hierarchical clustering, this is achieved by use of an appropriate metric (a measure of distance between pairs of observations), and a linkage criterion which specifies the dissimilarity of sets as a function of the pairwise distances of observations in the sets.

### Metric

The choice of an appropriate metric will influence the shape of the clusters, as some elements may be relatively closer to one another under one metric than another. For example, in two dimensions, under the Manhattan distance metric, the distance between the origin (0,0) and (0.5, 0.5) is the same as the distance between the origin and (0, 1), while under the Euclidean distance metric the latter is strictly greater.

Some commonly used metrics for hierarchical clustering are:

Names Formula
Euclidean distance $\|a-b \|_2 = \sqrt{\sum_i (a_i-b_i)^2}$
Squared Euclidean distance $\|a-b \|_2^2 = \sum_i (a_i-b_i)^2$
Manhattan (or city block ) distance $\|a-b \|_1 = \sum_i |a_i-b_i|$
Maximum distance (or Chebyshev distance) $\|a-b \|_\infty = \max_i |a_i-b_i|$
Mahalanobis distance $\sqrt{(a-b)^{\top}S^{-1}(a-b)}$ where S is the Covariance matrix

For text or other non-numeric data, metrics such as the Hamming distance or Levenshtein distance are often used.

Euclidean and Manhattan distances are the special cases of generalized Minkowski distance with p = 1 (for Manhattan) and p = 2 (for Euclidean).

Several other dissimilarity measures exist. Particularly, correlation-based distances - Pearson, Eisen cosine, Spearman, Kendall correlation distances, which are widely used for gene expression data analyses. Correlation-based distance is defined by subtracting the correlation coefficient from 1. Strictly speaking, correlation-based distances cannot be used as metric, while the square root of it can be.

A review of cluster analysis in health psychology research found that the most common distance measure in published studies in that research area is the Euclidean distance or the squared Euclidean distance.

The linkage criterion determines the distance between sets of observations as a function of the pairwise distances between observations.

Some commonly used linkage criteria between two sets of observations $A$ and $B$ are:

Names Formula
Maximum or complete-linkage clustering $\max \, \{\, d(a,b) : a \in A,\, b \in B \,\}.$
Minimum or single-linkage clustering $\min \, \{\, d(a,b) : a \in A,\, b \in B \,\}..$
Unweighted average linkage clustering (or UPGMA) $\frac{1}{|A|\cdot|B|} \sum_{a \in A }\sum_{ b \in B} d(a,b).$
Weighted average linkage clustering (or WPGMA) $d(i \cup j, k) = \frac{d(i, k) + d(j, k)}{2}.$
Centroid linkage clustering, or UPGMC $d(c_A,c_B)$ where $c_A$ and $c_B$ are the centroids of clusters $A$ and $B$, respectively.
Minimum energy clustering $\frac {2}{nm}\sum_{i,j=1}^{n,m} \|a_i- b_j\|_2 - \frac {1}{n^2}\sum_{i,j=1}^{n} \|a_i-a_j\|_2 - \frac{1}{m^2}\sum_{i,j=1}^{m} \|b_i-b_j\|_2$

Complete-linkage clustering is one of several methods of agglomerative hierarchical clustering. At the beginning of the process, each element is in a cluster of its own. The clusters are then sequentially combined into larger clusters until all elements end up being in the same cluster. The method is also known as farthest neighbour clustering. The result of the clustering can be visualized as a dendrogram, which shows the sequence of cluster fusion and the distance at which each fusion took place.

### Clustering procedure

At each step, the two clusters separated by the shortest distance are combined. The definition of 'shortest distance' is what differentiates between the different agglomerative clustering methods. In complete-linkage clustering, the link between two clusters contains all element pairs, and the distance between clusters equals the distance between those two elements (one in each cluster) that are farthest away from each other. The shortest of these links that remains at any step causes the fusion of the two clusters whose elements are involved.

Mathematically, the complete linkage function — the distance $D(X,Y)$ between clusters $X$ and $Y$ — is described by the following expression :

[$]D(X,Y)= \max_{x\in X, y\in Y} d(x,y)[$]

where $d(x,y)$ is the distance between elements $x \in X$ and $y \in Y$, and $X$ and $Y$ are two sets of elements (clusters).

### Algorithms

#### Naive scheme

The following algorithm is an agglomerative scheme that erases rows and columns in a proximity matrix as old clusters are merged into new ones. The $N \times N$ proximity matrix $D$ contains all distances $d(i,j)$. The clusterings are assigned sequence numbers $0,1,\ldots,n-1$ and $L(k)$ is the level of the $k$-th clustering. A cluster with sequence number $m$ is denoted ($m$) and the proximity between clusters ($r$) and ($s$) is denoted $d[(r),(s)]$.

The complete linkage clustering algorithm consists of the following steps:

1. Begin with the disjoint clustering having level $L(0) = 0$ and sequence number $m=0$.
2. Find the most similar pair of clusters in the current clustering, say pair $(r), (s)$, according to $d[(r),(s)] = \min d[(i),(j)]$where the minimum is over all pairs of clusters in the current clustering.
3. Increment the sequence number: $m = m + 1$. Merge clusters $(r)$ and $(s)$ into a single cluster to form the next clustering $m$. Set the level of this clustering to $L(m) = d[(r),(s)]$
4. Update the proximity matrix, $D$, by deleting the rows and columns corresponding to clusters $(r)$ and $(s)$ and adding a row and column corresponding to the newly formed cluster. The proximity between the new cluster, denoted $(r,s)$ and old cluster $(k)$ is defined as $d[(r), (s)] = \max \{d[(k),(r)], d[(k),(s)] \}$.
5. If all objects are in one cluster, stop. Else, go to step 2.

### Working example

The working example is based on a JC69 genetic distance matrix computed from the 5S ribosomal RNA sequence alignment of five bacteria: Bacillus subtilis ($a$), Bacillus stearothermophilus ($b$), Lactobacillus viridescens ($c$), Acholeplasma modicum ($d$), and Micrococcus luteus ($e$).

#### First step

First clustering

Let us assume that we have five elements $(a,b,c,d,e)$ and the following matrix $D_1$ of pairwise distances between them:

a b c d e
a 0 17 21 31 23
b 17 0 30 34 21
c 21 30 0 28 39
d 31 34 28 0 43
e 23 21 39 43 0

In this example, $D_1 (a,b)=17$ is the smallest value of $D_1$, so we join elements $a$ and $b$.

First branch length estimation

Let $u$ denote the node to which $a$ and $b$ are now connected. Setting $\delta(a,u)=\delta(b,u)=D_1(a,b)/2$ ensures that elements $a$ and $b$ are equidistant from $u$. This corresponds to the expectation of the ultrametricity hypothesis. The branches joining $a$ and $b$ to $u$ then have lengths $\delta(a,u)=\delta(b,u)=17/2=8.5$

First distance matrix update

We then proceed to update the initial proximity matrix $D_1$ into a new proximity matrix $D_2$ (see below), reduced in size by one row and one column because of the clustering of $a$ with $b$. Bold values in $D_2$ correspond to the new distances, calculated by retaining the maximum distance between each element of the first cluster $(a,b)$ and each of the remaining elements:

$D_2((a,b),c)=max(D_1(a,c),D_1(b,c))=max(21,30)=30$

$D_2((a,b),d)=max(D_1(a,d),D_1(b,d))=max(31,34)=34$

$D_2((a,b),e)=max(D_1(a,e),D_1(b,e))=max(23,21)=23$

Italicized values in $D_2$ are not affected by the matrix update as they correspond to distances between elements not involved in the first cluster.

#### Second step

Second clustering

We now reiterate the three previous steps, starting from the new distance matrix $D_2$ :

(a,b) c d e
(a,b) 0 30 34 23
c 30 0 28 39
d 34 28 0 43
e 23 39 43 0

Here, $D_2 ((a,b),e)=23$ is the lowest value of $D_2$, so we join cluster $(a,b)$ with element $e$.

• Second branch length estimation

Let $v$ denote the node to which $(a,b)$ and $e$ are now connected. Because of the ultrametricity constraint, the branches joining $a$ or $b$ to $v$, and $e$ to $v$, are equal and have the following total length: $\delta(a,v)=\delta(b,v)=\delta(e,v)=23/2=11.5$

We deduce the missing branch length: $\delta(u,v)=\delta(e,v)-\delta(a,u)=\delta(e,v)-\delta(b,u)=11.5-8.5=3$

Second distance matrix update

We then proceed to update the $D_2$ matrix into a new distance matrix $D_3$ (see below), reduced in size by one row and one column because of the clustering of $(a,b)$ with $e$ :

$D_3(((a,b),e),c)=max(D_2((a,b),c),D_2(e,c))=max(30,39)=39$

$D_3(((a,b),e),d)=max(D_2((a,b),d),D_2(e,d))=max(34,43)=43$

#### Third step

Third clustering

We again reiterate the three previous steps, starting from the updated distance matrix $D_3$.

((a,b),e) c d
((a,b),e) 0 39 43
c 39 0 28
d 43 28 0

Here, $D_3 (c,d)=28$ is the smallest value of $D_3$, so we join elements $c$ and $d$.

Third branch length estimation

Let $w$ denote the node to which $c$ and $d$ are now connected. The branches joining $c$ and $d$ to $w$ then have lengths $\delta(c,w)=\delta(d,w)=28/2=14$ (see the final dendrogram)

Third distance matrix update

There is a single entry to update: $D_4((c,d),((a,b),e))=max(D_3(c,((a,b),e)), D_3(d,((a,b),e)))=max(39, 43)=43$

#### Final step

The final $D_4$ matrix is:

((a,b),e) (c,d)
((a,b),e) 0 43
(c,d) 43 0

So we join clusters $((a,b),e)$ and $(c,d)$. Let $r$ denote the (root) node to which $((a,b),e)$ and $(c,d)$ are now connected. The branches joining $((a,b),e)$ and $(c,d)$ to $r$ then have lengths:

$\delta(((a,b),e),r)=\delta((c,d),r)=43/2=21.5$

We deduce the two remaining branch lengths:

$\delta(v,r)=\delta(((a,b),e),r)-\delta(e,v)=21.5-11.5=10$

$\delta(w,r)=\delta((c,d),r)-\delta(c,w)=21.5-14=7.5$

The dendrogram is now complete. It is ultrametric because all tips ($a$ to $e$) are equidistant from $r$ :

$\delta(a,r)=\delta(b,r)=\delta(e,r)=\delta(c,r)=\delta(d,r)=21.5$

The dendrogram is therefore rooted by $r$, its deepest node.

In statistics, single-linkage clustering is one of several methods of hierarchical clustering. It is based on grouping clusters in bottom-up fashion (agglomerative clustering), at each step combining two clusters that contain the closest pair of elements not yet belonging to the same cluster as each other.

A drawback of this method is that it tends to produce long thin clusters in which nearby elements of the same cluster have small distances, but elements at opposite ends of a cluster may be much farther from each other than two elements of other clusters. This may lead to difficulties in defining classes that could usefully subdivide the data.

### Overview of agglomerative clustering methods

In the beginning of the agglomerative clustering process, each element is in a cluster of its own. The clusters are then sequentially combined into larger clusters, until all elements end up being in the same cluster. At each step, the two clusters separated by the shortest distance are combined. The function used to determine the distance between two clusters, known as the linkage function, is what differentiates the agglomerative clustering methods.

In single-linkage clustering, the distance between two clusters is determined by a single pair of elements: those two elements (one in each cluster) that are closest to each other. The shortest of these pairwise distances that remain at any step causes the two clusters whose elements are involved to be merged. The method is also known as nearest neighbour clustering. The result of the clustering can be visualized as a dendrogram, which shows the sequence in which clusters were merged and the distance at which each merge took place.

Mathematically, the linkage function – the distance $D(X,Y)$ between clusters $X$ and $Y$– is described by the expression

[$]D(X,Y)=\min_{x\in X, y\in Y} d(x,y),[$]

where $X$ and $Y$ are any two sets of elements considered as clusters, and $d(x,y)$ denotes the distance between the two elements $x$ and $y$.

### Naive algorithm

The following algorithm is an agglomerative scheme that erases rows and columns in a proximity matrix as old clusters are merged into new ones. The $N \times N$ proximity matrix $D$ contains all distances $d(i,j)$. The clusterings are assigned sequence numbers $0,1, \ldots, n-1$ and $L(k)$ is the level of the $k$-th clustering. A cluster with sequence number $m$ is denoted ($m$) and the proximity between clusters $(r)$ and $(s)$ is denoted $d[(r),(s)]$.

The single linkage algorithm is composed of the following steps:

1. Begin with the disjoint clustering having level $L(0) = 0$ and sequence number $m=0$.
2. Find the most similar pair of clusters in the current clustering, say pair $(r), (s)$, according to $d[(r),(s)] = \min d[(i),(j)]$where the minimum is over all pairs of clusters in the current clustering.
3. Increment the sequence number: $m = m + 1$. Merge clusters $(r)$ and $(s)$ into a single cluster to form the next clustering $m$. Set the level of this clustering to $L(m) = d[(r),(s)]$
4. Update the proximity matrix, $D$, by deleting the rows and columns corresponding to clusters $(r)$ and $(s)$ and adding a row and column corresponding to the newly formed cluster. The proximity between the new cluster, denoted $(r,s)$ and old cluster $(k)$ is defined as $d[(r,s),(k)] = \min \{d[(k),(r)], d[(k),(s)] \}$.
5. If all objects are in one cluster, stop. Else, go to step 2.

### Working example

This working example is based on a JC69 genetic distance matrix computed from the 5S ribosomal RNA sequence alignment of five bacteria: Bacillus subtilis ($a$), Bacillus stearothermophilus ($b$), Lactobacillus viridescens ($c$), Acholeplasma modicum ($d$), and Micrococcus luteus ($e$).

#### First step

First clustering

Let us assume that we have five elements $(a,b,c,d,e)$ and the following matrix $D_1$ of pairwise distances between them:

a b c d e
a 0 17 21 31 23
b 17 0 30 34 21
c 21 30 0 28 39
d 31 34 28 0 43
e 23 21 39 43 0

In this example, $D_1 (a,b)=17$ is the lowest value of $D_1$, so we cluster elements a and b.

First branch length estimation

Let u denote the node to which a and b are now connected. Setting $\delta(a,u)=\delta(b,u)=D_1(a,b)/2$ ensures that elements a and b are equidistant from u. This corresponds to the expectation of the ultrametricity hypothesis. The branches joining a and b to u then have lengths $\delta(a,u)=\delta(b,u)=17/2=8.5$ (see the final dendrogram)

First distance matrix update

We then proceed to update the initial proximity matrix $D_1$ into a new proximity matrix $D_2$ (see below), reduced in size by one row and one column because of the clustering of a with b. Bold values in $D_2$ correspond to the new distances, calculated by retaining the minimum distance between each element of the first cluster $(a,b)$ and each of the remaining elements:

[$]\begin{array}{lllllll} D_2((a,b),c)&=&\min(D_1(a,c),D_1(b,c))&=&\min(21,30)&=&21 \\ D_2((a,b),d)&=&\min(D_1(a,d),D_1(b,d))&=&\min(31,34)&=&31 \\ D_2((a,b),e)&=&\min(D_1(a,e),D_1(b,e))&=&\min(23,21)&=&21 \end{array}[$]

Italicized values in $D_2$ are not affected by the matrix update as they correspond to distances between elements not involved in the first cluster.

#### Second step

Second clustering

We now reiterate the three previous actions, starting from the new distance matrix $D_2$ :

(a,b) c d e
(a,b) 0 21 31 21
c 21 0 28 39
d 31 28 0 43
e 21 39 43 0

Here, $D_2 ((a,b),c)=21$ and $D_2 ((a,b),e)=21$ are the lowest values of $D_2$, so we join cluster $(a,b)$ with element c and with element e.

Second branch length estimation

Let v denote the node to which $(a,b)$, c and e are now connected. Because of the ultrametricity constraint, the branches joining a or b to v, and c to v, and also e to v are equal and have the following total length:

[$]\delta(a,v)=\delta(b,v)=\delta(c,v)=\delta(e,v)=21/2=10.5[$]

We deduce the missing branch length:

[$]\delta(u,v)=\delta(c,v)-\delta(a,u)=\delta(c,v)-\delta(b,u)=10.5-8.5=2[$]

Second distance matrix update

We then proceed to update the $D_2$ matrix into a new distance matrix $D_3$ (see below), reduced in size by two rows and two columns because of the clustering of $(a,b)$ with c and with e :

[$]D_3(((a,b),c,e),d)=\min(D_2((a,b),d),D_2(c,d),D_2(e,d))=\min(31,28,43)=28[$]

#### Final step

The final $D_3$ matrix is:

((a,b),c,e) d
((a,b),c,e) 0 28
d 28 0

So we join clusters $((a,b),c,e)$ and $d$. Let $r$ denote the (root) node to which $((a,b),c,e)$ and $d$ are now connected. The branches joining $((a,b),c,e)$ and $d$ to $r$ then have lengths:

$\delta(((a,b),c,e),r)=\delta(d,r)=28/2=14$

We deduce the remaining branch length:

$\delta(v,r)=\delta(a,r)-\delta(a,v)=\delta(b,r)-\delta(b,v)=\delta(c,r)-\delta(c,v)=\delta(e,r)-\delta(e,v)=14-10.5=3.5$

The dendrogram is now complete. It is ultrametric because all tips ($a$, $b$, $c$, $e$, and $d$) are equidistant from $r$ :
$\delta(a,r)=\delta(b,r)=\delta(c,r)=\delta(e,r)=\delta(d,r)=14$
The dendrogram is therefore rooted by $r$, its deepest node.