Skip to content
Finance Lessons

Portfolio Optimization

The Covariance Matrix

How the covariance and correlation matrices are built, why a covariance matrix must be symmetric and positive semidefinite, how its size explodes with the number of assets, and what ill-conditioning means for the optimizer.

12 min Updated Jun 7, 2026

The covariance matrix Σ is the single object every risk calculation in this course leans on — portfolio variance, the optimizer’s solution, the Sharpe ratio, all of it routes through Σ. So before we can optimize, we have to really understand this table: how it’s assembled, the structural rules it must obey (symmetry, positive semidefiniteness), how monstrously large it grows as you add assets, and the property — conditioning — that quietly determines whether your optimizer produces sane weights or noise-driven nonsense. Get Σ right and the rest is arithmetic; get it wrong and no amount of clever optimization can rescue you.

Before you read — take a guess

You're optimizing a portfolio of 100 stocks. Roughly how many distinct numbers must you estimate to fill in the covariance matrix?

Building Σ: variances on the diagonal, covariances off it

Analogy. Picture a spreadsheet with the same list of assets down the rows and across the columns. Each cell answers: “how do the row asset and the column asset move together?” The cells on the main diagonal compare an asset with itself — that’s just its own variance — and every off-diagonal cell measures a genuine pair.

Definition. For NN assets with returns r1,,rNr_1, \dots, r_N, the covariance matrix Σ\Sigma is the N×NN \times N table with entries Σij=Cov(ri,rj)=E[(riμi)(rjμj)].\Sigma_{ij} = \mathrm{Cov}(r_i, r_j) = \mathbb{E}\big[(r_i - \mu_i)(r_j - \mu_j)\big]. On the diagonal (i=ji = j) this is the variance Σii=σi2\Sigma_{ii} = \sigma_i^2. Off the diagonal it factors into volatilities and correlation: Σij=σiσjρij,\Sigma_{ij} = \sigma_i\,\sigma_j\,\rho_{ij}, where ρij[1,1]\rho_{ij} \in [-1, 1] is the correlation. The correlation matrix is the same table with the volatilities stripped out — all ones on the diagonal — so it’s the pure co-movement skeleton, and Σ=DRD\Sigma = D R D where DD is the diagonal matrix of volatilities and RR the correlation matrix.

Worked example (3 assets). Suppose σ=(20%, 10%, 15%)\sigma = (20\%,\ 10\%,\ 15\%) and the correlations are ρ12=0.3\rho_{12} = 0.3, ρ13=0.2\rho_{13} = -0.2, ρ23=0.5\rho_{23} = 0.5. Then each covariance is volatility × volatility × correlation:

  • Σ12=0.20×0.10×0.3=0.006\Sigma_{12} = 0.20 \times 0.10 \times 0.3 = 0.006.
  • Σ13=0.20×0.15×(0.2)=0.006\Sigma_{13} = 0.20 \times 0.15 \times (-0.2) = -0.006.
  • Σ23=0.10×0.15×0.5=0.0075\Sigma_{23} = 0.10 \times 0.15 \times 0.5 = 0.0075.

So the full matrix is Σ=(0.04000.00600.00600.00600.01000.00750.00600.00750.0225).\Sigma = \begin{pmatrix} 0.0400 & 0.0060 & -0.0060 \\ 0.0060 & 0.0100 & 0.0075 \\ -0.0060 & 0.0075 & 0.0225 \end{pmatrix}. Read it: asset 1 is the most volatile (diagonal 0.04 = 0.20²), assets 1 and 3 are a diversifying pair (negative covariance), and assets 2 and 3 move together (positive).

The covariance matrix, lit upρ̄ 0.30
ABCDABCD1.000.150.170.380.151.000.310.200.170.311.000.120.380.200.121.00

The diagonal is each asset's own variance; everything off it is co-movement, blue for positive and warm for negative. Crank the average correlation and the whole grid floods blue — that is diversification evaporating, because correlated assets fall together. The matrix is always symmetric: the cell at row i, column j equals the cell at row j, column i.

Info:

Symmetry isn't optional

Because Cov(rᵢ, rⱼ) = Cov(rⱼ, rᵢ), every covariance matrix is symmetric: the cell above the diagonal mirrors the one below it. That’s why you only estimate N(N−1)/2 distinct off-diagonal numbers, not N(N−1). Any “covariance matrix” that isn’t symmetric has a bug.

Two assets have volatilities 25% and 40% and a correlation of −0.5. What is their covariance?

Positive semidefiniteness: the rule Σ can’t break

Analogy. A portfolio’s variance is a real, physical quantity — the spread of its returns. It can be zero (a perfectly hedged book) but it can never be negative: nothing is “less risky than perfectly certain.” A valid covariance matrix is exactly one that guarantees this for every possible portfolio you could build from the assets.

Definition. A symmetric matrix Σ\Sigma is positive semidefinite (PSD) if for every weight vector ww, wΣw0.w^\top \Sigma\, w \ge 0. Since wΣww^\top \Sigma w is the portfolio variance, PSD is just the statement “no portfolio can have negative variance.” Equivalently, all of Σ\Sigma‘s eigenvalues are 0\ge 0. A matrix is positive definite (PD) if the inequality is strict (>0> 0) for all nonzero ww — i.e. no nonzero portfolio has exactly zero variance, which is what you need for Σ\Sigma to be invertible.

Why a matrix can fail to be PSD. If you estimate correlations carelessly — say from data where different pairs were measured over different time windows, or you hand-tweak a few cells — you can produce a symmetric matrix with a negative eigenvalue. That implies some portfolio has negative variance, which is nonsense, and the optimizer will exploit it ruthlessly: it’ll pour weight into that “free risk reduction” and hand you an absurd answer.

Worked example — spotting a broken matrix. Consider the correlation matrix claiming three assets are each strongly positively correlated except one pair is strongly negative: R=(10.90.90.910.90.90.91).R = \begin{pmatrix} 1 & 0.9 & 0.9 \\ 0.9 & 1 & -0.9 \\ 0.9 & -0.9 & 1 \end{pmatrix}. This is logically impossible: if asset 1 is 90% correlated with both 2 and 3, then 2 and 3 cannot be strongly negatively correlated — they’re both shadowing asset 1. Plug it in and you’d find a negative eigenvalue, flagging it as not PSD. The transitivity of correlation isn’t a suggestion; it’s enforced by the PSD constraint.

Match each property of the covariance matrix to its meaning.

Pick a term, then click its definition.

The dimensionality explosion

Analogy. Adding one more asset to your universe doesn’t add one more number to estimate — it adds a whole new row and column of co-movements with everything already there. The bookkeeping grows like the area of the matrix, not its length.

The count. An N×NN \times N covariance matrix has N2N^2 cells, but symmetry means the distinct numbers are Nvariances+N(N1)2covariances=N(N+1)2.\underbrace{N}_{\text{variances}} + \underbrace{\frac{N(N-1)}{2}}_{\text{covariances}} = \frac{N(N+1)}{2}.

Worked examples.

Assets NNDistinct parameters N(N+1)2\frac{N(N+1)}{2}
515
1055
501,275
1005,050
500125,250

For the S&P 500, you must pin down over 125,000 numbers. And here’s the killer: you typically have only a few years of daily data — maybe 750–1,250 observations per asset. You are estimating more parameters than you have independent observations. When the number of parameters approaches or exceeds the sample size, the estimated Σ becomes wildly noisy and, often, singular (non-invertible) — which torpedoes the optimizer. This curse of dimensionality is the deep reason shrinkage (lesson 5) exists.

Warning:

Parameters versus data — the core tension

With N = 500 assets and, say, 1,000 days of data, you have ~125,000 parameters and 500,000 data points — but those data points are far from independent, and per asset you have only 1,000 observations to nail down 500 covariances. The sample covariance matrix is then a noisy mess. More assets means exponentially more to estimate from the same thin history.

The size of the problem.

Pick the right option for each blank, then check.

A covariance matrix for N assets has distinct numbers because it is . As N grows, the count grows like , so you must estimate parameters than you have years of data — which makes the estimate noisy and sometimes non-invertible.

Conditioning: why a near-singular Σ wrecks the optimizer

Analogy. Imagine balancing a pencil on its tip versus laying it flat. Flat, a small nudge barely moves it (well-conditioned). On its tip, the tiniest breath sends it flying (ill-conditioned). A covariance matrix is “balanced on its tip” when some assets are nearly redundant — highly correlated — so the matrix is almost singular.

Definition. The condition number of Σ\Sigma is the ratio of its largest to smallest eigenvalue, κ(Σ)=λmaxλmin.\kappa(\Sigma) = \frac{\lambda_{\max}}{\lambda_{\min}}. A large κ\kappa means Σ\Sigma is ill-conditioned: it’s close to singular, λmin\lambda_{\min} is tiny, and the inverse Σ1\Sigma^{-1} (which the optimizer needs) has enormous entries scaled by 1/λmin1/\lambda_{\min}. Small errors in the inputs get magnified by roughly κ\kappa in the output weights.

Why correlated assets cause it. If two assets have correlation near 1, they’re nearly the same asset — the matrix has a direction with almost no variance, so λmin0\lambda_{\min} \approx 0 and κ\kappa blows up. The optimizer, seeing two near-identical assets with slightly different estimated returns, will take a giant long position in one and a giant short in the other to harvest the tiny apparent spread — a position that is pure noise and will reverse out of sample.

Worked intuition. Two assets, both 20% vol, correlation ρ\rho. The eigenvalues of the correlation matrix are 1+ρ1+\rho and 1ρ1-\rho, so κ=1+ρ1ρ\kappa = \frac{1+\rho}{1-\rho}. At ρ=0\rho = 0, κ=1\kappa = 1 (perfectly balanced). At ρ=0.9\rho = 0.9, κ=1.90.1=19\kappa = \frac{1.9}{0.1} = 19. At ρ=0.99\rho = 0.99, κ=1.990.01=199\kappa = \frac{1.99}{0.01} = 199. As ρ1\rho \to 1, κ\kappa \to \infty and the matrix becomes singular — there’s no unique optimal split between two identical assets, so the optimizer’s answer is meaningless.

Correlation drives the conditioning19.6%
Naive weighted averagePortfolio volatilityDiversification benefit: 5.4%0%9%18%26%35%−1Correlation (ρ)+1Portfolio volatility
Portfolio volatility
19.6%
Naive weighted average (no diversification)
25.0%
Diversification benefit
5.4%

Slide the correlation between two assets toward 1 and watch them collapse into nearly the same series. At that point the covariance matrix is almost singular — its condition number explodes — and an optimizer can no longer tell the two apart, so it takes huge offsetting positions driven entirely by noise.

How does a high condition number turn into a literal bad trade?

Say assets X and Y are 99% correlated, and your estimates give X an expected return of 8.0% and Y 8.1% — a difference that’s pure noise at that sample size. The optimizer sees a near-arbitrage: two almost-identical assets, one a hair “better.” Because Σ⁻¹ has gigantic entries (κ ≈ 199), it recommends something like +900% in Y and −890% in X to scoop up the 0.1% edge with maximal leverage. Out of sample the 0.1% edge evaporates (or reverses), the two giant legs no longer cancel cleanly, and you take a catastrophic loss on what looked like a riskless spread. Nothing was wrong with the math — the closed form did exactly what you asked. The problem was feeding a near-singular, noisy Σ into an inversion that amplified a meaningless 0.1% into a 1,790%-gross position. Shrinkage and constraints exist precisely to stop this.

Two assets have correlation 0.95. Using κ = (1+ρ)/(1−ρ) for the correlation matrix, what is the condition number, and what does it imply?

Putting it together

The covariance matrix Σ is built from volatilities and correlations (Σij=σiσjρij\Sigma_{ij} = \sigma_i\sigma_j\rho_{ij}), with variances on the diagonal and co-movements off it. It must be symmetric (covariance ignores order) and positive semidefinite (wΣw0w^\top\Sigma w \ge 0, so no portfolio has negative variance — equivalently all eigenvalues 0\ge 0); a negative eigenvalue means the matrix is broken and the optimizer will abuse it. The number of distinct parameters grows like N2N^2 — over 125,000 for the S&P 500 — so you routinely estimate more numbers than you have data, producing a noisy, often singular sample estimate. And conditioning (the eigenvalue ratio κ\kappa) decides everything downstream: highly correlated, near-redundant assets make Σ nearly singular, so Σ1\Sigma^{-1} has huge entries that amplify tiny input errors into wild, unstable weights. That fragility is the bridge to the next lesson — and the reason shrinkage and constraints exist.

Big picture

The covariance matrix — the whole picture

  • The covariance matrix Σ
    • How it’s built
      • Diagonal = variances σᵢ²
      • Off-diagonal = σᵢσⱼρᵢⱼ
      • Σ = D R D (vols × correlation matrix)
    • Structural rules
      • Symmetric: cell (i,j) = cell (j,i)
      • PSD: wᵀΣw ≥ 0, no negative variance
      • All eigenvalues ≥ 0; negative one = broken
      • PD (> 0) needed for invertibility
    • Dimensionality
      • N(N+1)/2 distinct parameters
      • Grows like N² — 125k+ for the S&P 500
      • Often more parameters than data → noisy, singular
    • Conditioning
      • κ = λmax / λmin
      • High correlation → near-singular → huge κ
      • Σ⁻¹ amplifies errors ~κ× into weights
      • Motivates shrinkage + constraints
Σ stores risk and co-movement; it must be symmetric and PSD; it explodes in size with N; and its conditioning decides whether the optimizer is stable or fragile.

Recap: the covariance matrix

Question 1 of 40 correct

For 20 assets, how many distinct numbers does the covariance matrix contain?

Check your answer to continue.

Next — mean-variance mechanics: with Σ in hand, we actually solve for the weights. We’ll derive the minimum-variance and maximum-Sharpe (tangency) portfolios, see the Lagrangian and closed-form solution that invokes Σ1\Sigma^{-1}, and work a full numeric example by hand — turning the abstract optimization problem into concrete numbers.

Mark lesson as complete