QTQuant Terminal
A2-08A2·intro·~20 min

Covariance and correlation — the math of diversification

statisticscovariancecorrelationdiversificationportfolio

▸ Pretest — guess, even if you don't know

Two stocks each have 20% annual volatility. You put half your money in each. Their returns are uncorrelated. What's the portfolio's volatility?

Covariance: do they move together?

Volatility (A2-02) describes one return series in isolation. Portfolios live or die by a different question: when stock A has a bad day, what does stock B tend to do? Covariance is that question as a number:

Cov(X,Y)=E[(XμX)(YμY)]\text{Cov}(X, Y) = E[(X - \mu_X)(Y - \mu_Y)]

Read the inside term day by day for two stocks. On a day when both are above their means, the product of deviations is positive × positive. Both below: negative × negative — positive again. One up, one down: the product is negative. Covariance averages these products:

Note Cov(X,X)=E[(XμX)2]=Var(X)\text{Cov}(X, X) = E[(X-\mu_X)^2] = \text{Var}(X): variance is covariance with yourself.

Correlation: covariance you can interpret

Covariance has awkward units (return × return) and its magnitude scales with each asset's volatility, so "Cov = 0.0002" is uninterpretable on its own. Normalize away the scales:

ρ=Cov(X,Y)σXσY,1ρ+1\rho = \frac{\text{Cov}(X, Y)}{\sigma_X \, \sigma_Y}, \qquad -1 \le \rho \le +1

The bounds are a theorem (Cauchy–Schwarz), not a convention. ρ=+1\rho = +1 means an exact increasing linear relationship, 1-1 an exact decreasing one, 00 no linear relationship (a perfect U-shape can have ρ=0\rho = 0 — correlation only sees lines). Typical daily-return correlations: two large-cap US stocks 0.3–0.6; a stock with its own sector ETF 0.7–0.9.

The sample versions replace expectations with averages, with the same N1N-1 Bessel correction as A2-02 — numpy's np.cov uses ddof=1 by default (unlike np.std, which defaults to ddof=0; a classic gotcha):

cov_xy = np.cov(x, y)[0, 1]                       # 2x2 matrix; off-diagonal is Cov(x, y)
corr_xy = cov_xy / (x.std(ddof=1) * y.std(ddof=1))

The payoff: portfolio variance

From A1-04, for portfolio weights aa and bb:

Var(aX+bY)=a2Var(X)+b2Var(Y)+2abCov(X,Y)\text{Var}(aX + bY) = a^2\,\text{Var}(X) + b^2\,\text{Var}(Y) + 2ab\,\text{Cov}(X, Y)

Everything about diversification hides in that last term. Work the pretest example properly: σX=σY=20%\sigma_X = \sigma_Y = 20\%, a=b=0.5a = b = 0.5, so the first two terms give 0.25×0.04+0.25×0.04=0.020.25 \times 0.04 + 0.25 \times 0.04 = 0.02, and Cov=ρ×0.2×0.2=0.04ρ\text{Cov} = \rho \times 0.2 \times 0.2 = 0.04\rho:

Correlation ρ\rhoPortfolio variancePortfolio volatility
+1.0+1.00.02+0.02=0.040.02 + 0.02 = 0.0420.0%20.0\%
+0.3+0.30.02+0.006=0.0260.02 + 0.006 = 0.02616.1%16.1\%
0.00.00.020.0214.1%14.1\%
1.0-1.00.020.02=00.02 - 0.02 = 00%0\%

The lesson of the table: diversification is a correlation play, not a headcount play. At ρ=+1\rho = +1 you get nothing — you own one asset wearing two tickers. Real diversification benefit only appears as ρ\rho drops below 1, and it's why a portfolio of 30 highly correlated tech stocks is far less diversified than 5 genuinely unrelated return streams.

Two honest warnings

Correlation is not causation — and it isn't stable either. Beyond the classic inferential trap (A1-05's conditional-is-not-causal, in linear clothing), financial correlations are estimated from a window and drift across regimes, just like volatility in A2-02.

"Correlations go to 1 in a crash." The trading floor version is exaggerated but points at something real: measured correlations between risky assets rise sharply in crises — assets that looked pleasantly independent in calm markets fall together when everyone deleverages at once. (One technical caveat: correlation measured conditional on high-volatility periods is mechanically higher even if the true relationship never changed, so part of the effect is a measurement artifact — but the economic core is real.) The practical consequence is brutal: diversification measured in calm markets overstates the protection you'll have in the storm, which is precisely when you need it. Never size positions assuming calm-period correlations hold in a tail event.

Forward pointer: regression beta (A2-06) is Cov(rstock,rmkt)/Var(rmkt)\text{Cov}(r_{\text{stock}}, r_{\text{mkt}}) / \text{Var}(r_{\text{mkt}}) — covariance rescaled by the market's variance instead of by both standard deviations. Correlation and beta are the same object under two different normalizations.

Try it

▮ EXERCISE · a2-08-ex1

Implement corr(x, y): the sample correlation of two equal-length arrays — sample covariance (np.cov uses ddof=1 by default) divided by the product of the two sample standard deviations (std with ddof=1).

⧉ Review card
Define covariance and give the sign intuition.
Cov(X, Y) = E[(X − μx)(Y − μy)] — the average product of deviations. Positive: the two tend to be above/below their means together. Negative: opposite sides. Cov(X, X) = Var(X).
⧉ Review card
What is correlation and why use it instead of covariance?
ρ = Cov(X, Y) / (σx σy) — covariance with both scales divided out. Always in [−1, 1] (Cauchy–Schwarz), so it is comparable across asset pairs. It measures only LINEAR association.
⧉ Review card
What is Var(aX + bY), and where does diversification live in it?
a² Var(X) + b² Var(Y) + 2ab Cov(X, Y). Diversification lives entirely in the covariance term: at ρ = +1 a 50/50 mix of two 20%-vol assets is still 20% vol; at ρ = 0 it drops to 14.1%; at ρ = −1 it can reach 0%.
⧉ Review card
What happens to correlations between risky assets in a crisis?
Measured correlations rise sharply — assets that looked independent in calm markets fall together in a deleveraging. Calm-period correlation overstates crash protection. (Partly a conditioning artifact of high vol, but the economic effect is real.)
⧉ Review card
How does beta relate to covariance and correlation?
Beta = Cov(stock, market) / Var(market) — covariance normalized by the market's variance alone, versus correlation which normalizes by both stds. Same object, different scaling.

Map it

On paper, draw a concept map connecting: variance, covariance, correlation, Var(aX+bY), diversification, crisis correlation, and beta. Draw a labelled arrow for each relationship (e.g., which normalization turns covariance into correlation? into beta? which term of the portfolio-variance formula does diversification act through?).

◈ Calibration check

Could you compute a sample correlation from scratch and explain, with the formula, why diversification fails at correlation +1?

1 = guessing · 5 = could teach it

⏻ End of lesson

Mark it read to book its 5 review cards into your deck.

Sources & further reading