Quant Memo
Advanced

Nested Clustered Optimization

Instead of feeding a noisy covariance matrix straight into an optimizer and hoping for the best, first group similar assets into clusters, optimize inside each cluster, then optimize across clusters. Splitting the problem in two makes it far less sensitive to estimation error.

Prerequisites: Hierarchical Risk Parity (López de Prado), K-Means Clustering, The Sample Covariance Matrix and Eigenvalue Bias

Feed a standard mean-variance optimizer a covariance matrix estimated from noisy historical data and it will happily bet huge, offsetting long-short positions on the two assets whose estimated correlation happens to look most favorable, purely because a matrix inversion is extremely sensitive to small errors in near-collinear inputs. The optimizer is not wrong given its inputs; the inputs are the problem. Nested clustered optimization (NCO) restructures the problem so a bad correlation estimate between two similar assets can no longer wreck the whole portfolio.

The analogy before any symbols

Imagine allocating a company's budget by asking every one of two hundred employees, individually, to negotiate directly with each other over who gets what, all at once. Small misunderstandings between any two people can cascade unpredictably through the whole negotiation. A functioning company instead groups people into departments, lets each department sort out its own internal split, and then has department heads negotiate the budget split across departments. Errors inside one department's internal negotiation stay contained there; they do not corrupt the cross-department allocation. NCO applies exactly that two-level structure to a covariance matrix instead of a budget.

The mechanics

NCO runs in three steps. First, cluster. Convert the correlation matrix ρij\rho_{ij} into a distance, commonly dij=12(1ρij)d_{ij} = \sqrt{\tfrac12(1-\rho_{ij})}, so highly correlated assets sit close together, and run hierarchical or k-means clustering to partition NN assets into KK clusters of similar assets. Second, optimize within each cluster. Inside each cluster, solve the usual minimum-variance (or mean-variance) problem using only the assets in that cluster, producing intra-cluster weights. Third, optimize across clusters. Treat each cluster as if it were a single synthetic asset, with return and variance built from its intra-cluster weights, and solve a small KK-asset optimization across clusters. The final weight on any individual asset is:

wi=wi(intra-cluster)×wc(i)(inter-cluster),w_i = w_i^{\text{(intra-cluster)}} \times w_{c(i)}^{\text{(inter-cluster)}},

where c(i)c(i) is the cluster asset ii belongs to. In words: how much weight an asset gets inside its own cluster, times how much weight that whole cluster gets in the overall portfolio.

The key benefit: the matrix that has to be inverted at each step is small, either the handful of assets inside one cluster, or the handful of clusters themselves, not the full N×NN \times N matrix at once. Inversion error scales badly with matrix size when the matrix is close to singular (which correlated financial assets usually make it), so two small, better-conditioned inversions beat one large, ill-conditioned one.

Correlation explorer
X →Y ↑
ρ = 0.60r² = 0.36relationship: moderate positive

This scatter is what NCO's first step is built to exploit: assets with high ρ\rho cluster tightly together in return space, and clustering groups exactly those tight scatters before any optimization touches them, rather than letting the optimizer discover (and over-trust) that relationship on its own.

step 1 & 2: cluster, then optimize inside each cluster A cluster B cluster C step 3: treat each cluster as one synthetic asset, optimize across just three
No single matrix inversion ever spans all twelve assets at once. Estimation noise inside cluster A cannot leak into the weight given to cluster B.

Worked example: twelve assets, three clusters

Twelve equity names split into three clusters of four by sector similarity: tech, energy, and staples. A naive full-matrix minimum-variance optimization on all twelve, with a covariance matrix estimated from only 100 days of data, produces a handful of weights above 40% and several negative weights below −20%, driven by spurious cross-sector correlation estimates. Under NCO: within tech, the four names split roughly 30/20/35/15%; within energy, 40/25/20/15%; within staples, 20/30/35/15%. The cross-cluster step then allocates, say, 45% tech, 20% energy, 35% staples based on each cluster's aggregate risk. No individual final weight exceeds 45% × 35% ≈ 16%, and nothing goes negative, because neither optimization step ever saw all twelve assets' noisy pairwise correlations simultaneously.

Worked example: out-of-sample stability

Backtest both methods on 250 days in-sample, then measure realized volatility on the next 250 days out-of-sample. The naive full-matrix optimizer's in-sample "optimal" variance might be 8% annualized, but realized out-of-sample variance often lands near 14 to 16%, because the extreme long-short weights were fit to noise that does not repeat. NCO's in-sample variance is typically higher, closer to 10 to 11% (it never chased the noise as hard), but its out-of-sample realized variance tends to land close to that same 10 to 11%, since the clustering step removed the main channel through which noise could distort the weights. The comparison de Prado popularized shows NCO giving up a little in-sample "optimality" for a lot of out-of-sample reliability.

What this means in practice

NCO is a practical middle ground between full mean-variance optimization, which is fast and elegant but fragile with noisy inputs, and equal-weighting, which is robust but ignores real risk differences entirely. It is closely related to Hierarchical Risk Parity (López de Prado), which uses a similar clustering idea but skips the cross-cluster optimization step in favor of a simpler risk-based split. Both are common defaults exactly when NN is large relative to the amount of clean history available, the regime where a full covariance matrix is least trustworthy.

Splitting one large, ill-conditioned optimization into many small, well-conditioned ones (cluster, optimize inside, then optimize across clusters) trades a small amount of in-sample optimality for a large improvement in out-of-sample stability, because estimation error can no longer propagate through a single giant matrix inversion.

NCO is only as good as the clustering step. If the number of clusters KK is chosen badly, too few clusters lump genuinely different assets together and the intra-cluster step inherits the same noise problem it was meant to avoid; too many clusters shrink each cluster toward a single asset and the method degenerates back toward the fragile full-matrix case. Clustering choices (distance metric, linkage method, number of clusters) are themselves estimated from the same noisy data, so treat KK and the cluster assignments as tunable, not as ground truth, and check sensitivity before trusting the output.

Related concepts

Practice in interviews

Further reading

  • de Prado (2019), A Robust Estimator of the Efficient Frontier
  • de Prado, Advances in Financial Machine Learning (Ch. 16)
ShareTwitterLinkedIn