liveBook · Manning

Hierarchical clustering

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

Hierarchical clustering is a clustering technique that organizes objects into progressively combined or subdivided groups according to a distance measure.

Hierarchical clustering groups portfolio assets from their correlations, producing a tree of progressively merged clusters. In hierarchical risk parity (HRP), that tree supplies the structure for reordering assets and later splitting portfolio weight across clusters. section 11.2.4

Start with correlation matrix R and convert each pair to distance: Dᵢⱼ = √(½(1 − Rᵢⱼ)). Highly correlated assets have smaller distances. Then compute D̄, the Euclidean distance between columns of D; a small D̄ᵢⱼ means two assets have similar correlation relationships to all other assets. section 11.2.4

Eight-asset clustering

For VV, VB, AGG, VEA, BNDX, VWO, EMB, and DJP, calculate returns, covariance, correlations, D, and D̄. Iteratively merge the closest clusters into a link matrix; VV and VB merge first at distance 0.2951. The resulting ordering places related assets together for a reordered correlation heatmap. section 11.2.4

Listing 11.5 Calculating the distance matrix
import itertools

tickers = ['VV', 'VB', 'AGG', 'VEA', 'BNDX', 'VWO', 'EMB', 'DJP']           
rets = yf.download(tickers, start='2014-01-01', end='2021-12-31')['Adj Close']
rets = rets.pct_change().dropna(axis=0, how='any')[tickers]
cov, corr = rets.cov() * 252, rets.corr()

dist = ((1 - corr) / 2.) ** .5

d_bar = dist * 0
for i, j in itertools.permutations(dist.columns, 2):
    d_bar[i][j] = np.linalg.norm(dist[i] - dist[j])

Computes correlation-derived distances and the second distance matrix used to compare each asset’s relationships with the full asset set. section 11.2.4

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

How deep it goes
Derives two distance matrices and implements iterative linkage, while deferring weight calculation to recursive bisection.
Examples
Works through eight ETF tickers, a distance matrix, cluster merges, asset ordering, and reordered correlation heatmaps.
What it assumes
Assumes asset returns and correlation matrices, then teaches the distance transformations and cluster-link construction.
What you can do after
Build it from scratch by transforming correlations into distances and merging the nearest asset clusters into a link matrix.
How it connects
Connects correlation matrices, covariance matrices, linkage matrices, recursive bisection, and hierarchical risk parity.

Also explained in

Compare how 5 books explain hierarchical clustering →

Related concepts

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.