liveBook · Manning

Linkage matrix

As explained in Build a Robo-Advisor with Python (From Scratch)

A linkage matrix is a tabular numerical representation of the successive cluster combinations, their intercluster distances, and the sizes of the resulting clusters.

A linkage matrix records agglomerative clustering as a sequence of merges. Each row identifies the two assets or prior clusters combined, the distance at which they join, and the size of the new cluster. Original assets use indices 0 through n−1; each merge creates a new cluster index for later rows. section 11.2.4

For hierarchical risk parity (HRP), the matrix turns pairwise asset distances into a cluster hierarchy. It is then read from the final merge backward: repeatedly expand cluster indices until only original asset indices remain, producing an ordering that places nearby assets together before recursive bisection assigns weights. section 11.2.4

Construct it incrementally:

  • Start every asset as a one-item cluster and find the smallest remaining intercluster distance. section 11.2.4
  • Merge that pair, record their indices, the minimum distance, and the combined cluster size. section 11.2.4
  • Update distances for the new cluster and repeat until one cluster contains all assets. section 11.2.4

Eight-asset merge history

The first row merges assets 0 and 1 at distance 0.2951 into a cluster of size 2. A later row merges asset 3 with that new cluster, while the final row joins clusters 11 and 13 into the eight-asset cluster. section 11.2.4

link_mat
array([[ 0.        ,  1.        ,  0.29511716,  2.        ],
       [ 3.        ,  8.        ,  0.36912864,  3.        ],
       [ 5.        ,  9.        ,  0.40906463,  4.        ],
       [ 2.        ,  4.        ,  0.60430531,  2.        ],
       [ 6.        , 10.        ,  0.70386939,  5.        ],
       [ 7.        , 12.        ,  0.83002921,  6.        ],
       [11.        , 13.        ,  0.90802712,  8.        ]])

Inspect the four-column numerical merge history used to reconstruct the asset ordering. section 11.2.4

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Works through iterative merge construction and backward cluster expansion, while noting a faster SciPy implementation.
Examples
Uses eight ETFs and assets, including VV, VB, AGG, and BNDX, in a seven-row merge history.
What you can do after
Build it from scratch or apply SciPy linkage to cluster larger asset sets for HRP.
How it connects
Connects agglomerative clustering, distance matrices, covariance-matrix ordering, seriation, and recursive bisection.

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.