0bps

Learn / Track I / lesson 4 of 4

Linear algebra for portfolios

Vectors of weights, covariance matrices and eigenvectors — the compact language every portfolio and risk model is written in.

12 min read

Once you hold more than two assets, writing out every variance and covariance by hand stops working. Linear algebra packs it all into a few symbols.

Weights, returns and the covariance matrix

Stack nn asset returns into a vector rr with mean μ=E[r]\mu = \mathbb{E}[r]. Portfolio weights are a vector ww. The covariance matrix Σ\Sigma has variances on the diagonal and covariances off it:

Σij=Cov⁡(ri,rj)\Sigma_{ij} = \operatorname{Cov}(r_i, r_j)

Then the portfolio’s expected return and variance are

μp=w⊤μ,σp2=w⊤Σ w\mu_p = w^\top \mu, \qquad \sigma_p^2 = w^\top \Sigma\, w

That second formula is the matrix version of the variance rule for sums. Every cross term 2wiwjΣij2 w_i w_j \Sigma_{ij} is in there.

Correlation

Correlation rescales covariance to lie between −1-1 and 11:

ρij=Σijσiσj\rho_{ij} = \frac{\Sigma_{ij}}{\sigma_i \sigma_j}

If DD is the diagonal matrix of volatilities and CC the correlation matrix, then Σ=D C D\Sigma = D\,C\,D. Separating volatility from correlation is useful because they behave differently: volatility is fairly predictable, correlation is noisy and jumps towards 1 in a crisis.

Positive semi-definiteness

A valid covariance matrix must satisfy w⊤Σw≥0w^\top \Sigma w \ge 0 for every ww, since variance cannot be negative. When you build Σ\Sigma from pieces (different data windows, hand-edited correlations), this can break, and an optimiser will happily “find” a portfolio with negative risk. Always check the smallest eigenvalue.

Eigenvectors and principal components

Because Σ\Sigma is symmetric, it can be decomposed as

Σ=QΛQ⊤\Sigma = Q \Lambda Q^\top

where the columns of QQ are orthogonal eigenvectors and Λ\Lambda is the diagonal matrix of eigenvalues. Each eigenvector is a portfolio; its eigenvalue is that portfolio’s variance; and these portfolios are uncorrelated with one another. This is principal component analysis (PCA).

What PCA finds in real markets
  • In a basket of stocks, the first component is close to equal-weighted — “the market” — and often explains 30–50% of total variance.
  • In a yield curve, the first three components are level, slope and curvature, and together explain well over 95% of moves.
  • In crypto, the first component is essentially “BTC beta”; most altcoins load heavily on it.

Why this matters for strategies

  • Risk decomposition. The contribution of asset ii to portfolio variance is wi(Σw)iw_i (\Sigma w)_i. These contributions sum to σp2\sigma_p^2, which is how risk-parity portfolios are built.
  • Hidden concentration. Ten strategies that each look different may all load on the same first principal component. PCA tells you how many independent bets you really have.
  • Stat-arb. Residuals after removing the top components are often more mean-reverting than raw prices — the basis of PCA-based pairs and basket trading.
Common mistake

Inverting a sample covariance matrix estimated from too little data. With nn assets you need far more than nn observations; otherwise the smallest eigenvalues are pure noise, and Σ−1\Sigma^{-1} (used by mean-variance optimisation) amplifies exactly that noise. Use shrinkage (Ledoit–Wolf) or factor models.

esc

Try , , or .

↑↓ move↵ open