Quant techniques · portfolio construction

Why your covariance matrix is lying, and how shrinkage fixes it

A portfolio optimiser takes your covariance matrix at its word. Give it the sample covariance of 100 stocks estimated on 250 days, and it will find combinations that look nearly riskless in the data, because noise has made some directions look much quieter than they are. Then it bets heavily on exactly those directions. The fix is to stop trusting the sample matrix so much.

1q=N/Tq = N/T Assets over observations. The noise in the sample covariance is driven by this ratio, not by N or T alone.
2λ±=σ2(1±q)2\lambda_\pm = \sigma^2(1 \pm \sqrt q)^2 Marchenko–Pastur: with no true structure, sample eigenvalues still spread over this range instead of all being σ².
3Σ^=α μI+(1−α) S\hat\Sigma = \alpha\,\mu I + (1 - \alpha)\,S Shrink the sample matrix S towards a simple target (here, the average variance μ times the identity).
4α∗=min⁡(1, b2/d2)\alpha^* = \min\left(1,\ b^2 / d^2\right) Ledoit–Wolf: b² = how noisy S is (the spread of the daily outer products xtxt⊤x_t x_t^\top around S, over T²), d² = how far S is from the target.
The noise

Pure noise still has a spread-out spectrum

Take 100 assets that are truly independent with variance 1, so the true covariance is the identity and every eigenvalue is exactly 1. Estimate the covariance from T = 250 days and compute the eigenvalues of the sample matrix. They don’t cluster around 1. They spread from about 0.14 to 2.7.

That is the Marchenko–Pastur law. As N and T grow with q=N/Tq = N/T fixed, the histogram of sample eigenvalues converges to a known density supported on [(1−q)2, (1+q)2][(1 - \sqrt q)^2,\ (1 + \sqrt q)^2]. With q = 0.4, that is [0.135, 2.665]. Even with four times as many days as assets, the smallest sample eigenvalue understates the true variance by a factor of 7.

Why does this matter? The eigenvectors with small eigenvalues are portfolios the data says are very low risk. The optimiser loves them. But their low variance is mostly luck in this particular sample.

100 assets, days of data T =

Eigenvalues of a sample covariance when the truth is the identity

Min-variance portfolio risk vs shrinkage α (× best possible)

sample eigenvalues / true riskMarchenko–Pastur law / Ledoit–Wolf αrisk the portfolio thinks it has
–eigenvalue range (truth: all exactly 1)
–true risk, no shrinkage
–true risk at Ledoit–Wolf α
–Ledoit–Wolf α

In the left panel, compare T = 125, 250 and 1,000. The spread shrinks as T grows, but slowly: even with 10 times more data than assets (q = 0.1), the eigenvalues still range from about 0.47 to 1.73.

The damage

The optimiser underestimates risk and then takes too much

The minimum-variance portfolio, w∝Σ^−11w \propto \hat\Sigma^{-1}\mathbf 1, uses only the covariance matrix, so it isolates the problem. When the true covariance is the identity and returns are normal, there are clean results for large N and T:

risk it thinks it hasbest possible≈1−q,risk it really hasbest possible≈11−q\frac{\text{risk it thinks it has}}{\text{best possible}} \approx \sqrt{1 - q}, \qquad \frac{\text{risk it really has}}{\text{best possible}} \approx \frac{1}{\sqrt{1 - q}}

With q = 0.4, that’s 0.77× and 1.29×. The portfolio believes it is 23% safer than the best possible portfolio, and is in fact 29% riskier. As q approaches 1 (as many assets as days) both go badly wrong, and at q ≥ 1 the sample matrix can’t be inverted at all.

The right panel uses a more realistic truth: volatilities ranging from 0.7 to 1.4 and a correlation of 0.1 between every pair. The blue curve is the true risk of the min-variance portfolio built with shrinkage α. With α = 0 it is about 1.29× the best possible. Shrinking brings it down to about 1.12× around α = 0.4, after which the target’s own bias takes over and the risk climbs again. The dashed grey curve is what each portfolio thinks its risk is. It sits below the truth everywhere.

The fix

Shrinkage: a weighted average of noisy and wrong

The sample covariance S is unbiased but noisy. A simple target F, such as the average variance times the identity, is very stable but biased: it ignores all correlations and differences in volatility. A weighted average,

Σ^=αF+(1−α)S,\hat\Sigma = \alpha F + (1 - \alpha) S,

trades some bias for a lot less variance. It also pulls the extreme eigenvalues towards the middle: every eigenvalue becomes αμ+(1−α)λi\alpha\mu + (1 - \alpha)\lambda_i, so the near-zero directions the optimiser loved are no longer near zero.

The question is how much to shrink. Ledoit and Wolf (2004) found the α that minimises the expected squared (Frobenius) error between Σ^\hat\Sigma and the true Σ, and showed it can be estimated from the data:

  • Numerator: how noisy S is. Each day’s outer product xtxt⊤x_t x_t^\top is a one-observation estimate of Σ; their average spread around S, divided by T, estimates the variance of S.
  • Denominator: how far S is from the target. If S is already close to F, there’s little to lose by shrinking.

In the right panel’s setting the Ledoit–Wolf α averages about 0.27 and brings the risk to 1.13× the best possible, close to the 1.12× of the best α in hindsight. It isn’t exactly at the minimum because it optimises the error of the matrix, not the risk of the portfolio, but it gets most of the benefit with no tuning.

import numpy as np

def ledoit_wolf(X):
    """Ledoit–Wolf shrinkage towards mu * I. X is T x N (rows = observations)."""
    T, N = X.shape
    Xc = X - X.mean(axis=0)
    S = Xc.T @ Xc / T
    mu = np.trace(S) / N
    F = mu * np.eye(N)
    d2 = np.sum((S - F) ** 2)                                   # distance from target
    b2 = sum(np.sum((np.outer(x, x) - S) ** 2) for x in Xc) / T**2   # noise in S
    alpha = min(b2, d2) / d2
    return alpha * F + (1 - alpha) * S, alpha

# sklearn.covariance.LedoitWolf gives the same estimator.

Other ways to clean a covariance matrix

  • Better targets. The constant-correlation matrix, or a single-factor (market) model, keep more structure than μI and usually work better for stocks. When one strong market factor dominates, μI is a poor target and the Ledoit–Wolf α comes out small.
  • Eigenvalue clipping. Replace every eigenvalue inside the Marchenko–Pastur band by their average, and keep the ones above it as real signal.
  • Factor models. Impose structure directly: Σ = BΩBᵀ + D with a few factors. Far fewer parameters to estimate.
  • Nonlinear shrinkage (Ledoit–Wolf 2017) shrinks each eigenvalue by its own amount, and does better than linear shrinkage when N is large.

Where people slip in interviews

  • “More assets is fine as long as T > N.” The noise depends on N/T. At N/T = 0.5 the sample matrix is badly distorted even though it’s invertible.
  • Trusting in-sample risk. An optimised portfolio’s in-sample risk is biased down, because the optimiser picked the directions that looked quietest.
  • Thinking shrinkage biases the answer, so it’s worse. It does add bias, but it removes much more variance. The total error goes down.
  • Shrinking correlations but not volatilities, or vice versa, without thinking. Shrinking towards μI also pulls volatilities together. Shrinking the correlation matrix towards its average leaves volatilities alone.
Practice

Try these before you open the answer

Warm-up500 stocks, 2 years of daily data (504 days). Can you invert the sample covariance matrix?
Show answer

Barely. N = 500 < T = 504, so the matrix is full rank, but q ≈ 0.99 puts the smallest eigenvalues near (1−0.99)2≈0.000025(1 - \sqrt{0.99})^2 \approx 0.000025 times the true scale. The inverse is dominated by noise.

MPWith N = 50 and T = 200, where does Marchenko–Pastur put the sample eigenvalues when the truth is the identity?
Show answer
q=0.25,(1±0.5)2=[0.25, 2.25]q = 0.25, \quad (1 \pm 0.5)^2 = [0.25,\ 2.25]
RiskTruth is the identity, N = 100, T = 400. By what factors are the min-variance portfolio's in-sample and true risk off from the best possible?
Show answer

q = 0.25. In-sample 0.75≈0.87×\sqrt{0.75} \approx 0.87\times, true 1/0.75≈1.15×1/\sqrt{0.75} \approx 1.15\times.

EigenvaluesS has eigenvalues 0.1 and 3.0, the average variance is μ = 1.55, and you shrink with α = 0.3. What are the eigenvalues of the shrunk matrix?
Show answer

The target is a multiple of I, so eigenvectors don’t change: 0.3×1.55+0.7×0.1=0.5350.3 \times 1.55 + 0.7 \times 0.1 = 0.535, and 0.3×1.55+0.7×3.0=2.5650.3 \times 1.55 + 0.7 \times 3.0 = 2.565. The ratio of largest to smallest falls from 30 to about 4.8.

LimitsWhat does the Ledoit–Wolf α do as T grows with N fixed? Why?
Show answer

It goes to 0. The noise term (numerator) shrinks like 1/T, while the distance from S to the target converges to the distance from Σ to the target, which stays fixed. With lots of data, trust the data.

StretchWhy does the min-variance optimiser make in-sample risk too low, even though S is an unbiased estimate of Σ?
Show answer

For any fixed w, w⊤Sww^\top S w is unbiased for w⊤Σww^\top \Sigma w. But the optimiser picks the w that minimises w⊤Sww^\top S w, and the minimum of noisy estimates is biased down. It’s the same selection effect as picking the best of many backtests.

The risk ratios were checked over 100 simulated samples for each setting. The figure computes the eigenvalues and the Ledoit–Wolf α in your browser on a fresh sample each time.