Quant 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)]

In words: "the covariance of X and Y equals the expected value of (X minus its mean) times (Y minus its mean)." μX\mu_X reads "mu sub X" — the mean of X; μY\mu_Y is the mean of Y. So: take each day's deviation of X from its mean, multiply it by that same day's deviation of Y from its mean, and average the products.

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 — 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). In words: the covariance of X with itself is the average squared deviation of X — which is exactly the variance from A1-04. Variance is covariance with yourself.

Correlation: covariance you can interpret

Covariance has awkward units (return × return). Its magnitude also scales with each asset's volatility, so "Cov = 0.0002" is uninterpretable on its own. The fix: normalize — divide out both assets' scales so what's left is a pure number:

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

In words: correlation — written ρ\rho, the Greek letter "rho" — equals the covariance divided by the product of the two standard deviations. And rho always lands between minus 1 and plus 1, inclusive.

Concrete: if Cov = 0.0002 and both assets have daily volatility 0.02 (2%), then ρ=0.0002/(0.02×0.02)=0.5\rho = 0.0002 / (0.02 \times 0.02) = 0.5 — a strong positive relationship, now visible.

The bounds are a theorem (Cauchy–Schwarz, a classical inequality of mathematics), not a convention. ρ=+1\rho = +1 means an exact increasing straight-line relationship; 1-1 an exact decreasing one; 00 means 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 plain averages over your data, using the same N1N-1 Bessel correction as A2-02. One gotcha: numpy's np.cov uses ddof=1 by default, unlike np.std, which defaults to ddof=0:

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 (ordinary fixed numbers — the fractions of your money in each asset):

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)

In words: the variance of the portfolio "a of X plus b of Y" is: a-squared times X's variance, plus b-squared times Y's variance, plus a cross term — 2 times a times b times the covariance of X and Y.

Everything about diversification — spreading money across assets to reduce risk — hides in that last term. Work the pretest example properly. Take σX=σY=20%\sigma_X = \sigma_Y = 20\% and a=b=0.5a = b = 0.5 (half your money in each). 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. The covariance is Cov=ρ×0.2×0.2=0.04ρ\text{Cov} = \rho \times 0.2 \times 0.2 = 0.04\rho, so the cross term depends entirely on the correlation:

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. That'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. The first half is A1-05's conditional-is-not-causal trap, in linear clothing. The second half: financial correlations are estimated from a window of data, and they drift across regimes — market environments — 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 — sells positions to pay down borrowed money — at once. (One technical caveat: correlation measured only during 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 — one of the rare, extreme days out in the far end of the distribution.

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}}). In words: beta equals the covariance of the stock's return with the market's return, divided by the variance of the market's return — covariance rescaled by the market's variance alone, 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