Grouping assets by how they move together
A distance says how far apart two things are. For assets, close should mean that they move together, so we turn a correlation into a distance: . Perfect correlation gives distance , no correlation gives , and perfect opposite movement gives . Unlike , this formula behaves like a real distance, so it can be fed to clustering tools. scipy.spatial.distance.pdist computes all pairwise distances at once, and squareform converts between a full table and the compact list that the clustering functions expect. For finding the nearest neighbours of a point among many, scipy.spatial.cKDTree is much faster than comparing with every point.
Hierarchical clustering starts with every asset alone and repeatedly merges the two closest groups. How to measure the distance between two groups is the linkage rule. single uses the closest pair across the groups, complete uses the farthest pair and average uses the mean of all cross distances. hierarchy.linkage(distances, method) returns the whole merge history, with the height of each merge. hierarchy.fcluster(Z, t, 'distance') then cuts the tree at height to give group labels, or with 'maxclust' into a chosen number of groups. In finance the groups become a map of diversification: assets in one cluster carry mostly the same risk, so holding five of them is not five bets.
- Make correlation a distance: is for twins and for opposites.
- Linkage is a choice: single, complete and average linkage can give different merge heights for the same data.
- Clusters map diversification: pick across clusters, because assets within one cluster share most of their risk.
Lab · what's graded
- From correlation to distance10 pts
- From distance to correlation10 pts
- An average-linkage height15 pts
- A single-linkage height10 pts
- Groups after a cut10 pts
- What each routine does15 pts
- Clusters and diversification30 pts