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




