TExcelTAD
Home › Probability Theory › Covariance and Correlation

Covariance and Correlation

Variance measures how one variable spreads. Covariance measures how two variables move together: whether large values of one tend to come with large (or small) values of the other. It is also the missing term in the variance of a sum of dependent variables.

Definition

Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−E[X]E[Y]\operatorname{Cov}(X, Y) = E\left[(X - \mu_X)(Y - \mu_Y)\right] = E[XY] - E[X]E[Y]

Positive covariance: the variables tend to be above or below their means together. Negative: one tends to be above when the other is below. Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X).

The variance of a sum picks up covariance terms:

Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y)

Correlation

Covariance depends on units. The correlation coefficient removes them:

ρ(X,Y)=Cov⁡(X,Y)σXσY,−1≤ρ≤1\rho(X, Y) = \frac{\operatorname{Cov}(X, Y)}{\sigma_X \sigma_Y}, \qquad -1 \le \rho \le 1

ρ=±1\rho = \pm 1 exactly when Y=aX+bY = aX + b for some a≠0a \ne 0. Correlation measures linear association only.

python · try it yourself
from fractions import Fraction
from itertools import product

# two dice: X = first die, S = sum, M = max
omega = list(product(range(1, 7), repeat=2))
E = lambda g: Fraction(sum(g(a, b) for a, b in omega), 36)

def cov(f, g):
    return E(lambda a, b: f(a, b) * g(a, b)) - E(f) * E(g)

X = lambda a, b: a
Y = lambda a, b: b
S = lambda a, b: a + b
D = lambda a, b: a - b
print("Cov(X, Y) =", cov(X, Y))
print("Cov(X, S) =", cov(X, S), "  (= Var(X) = 35/12)")
print("Cov(S, D) =", cov(S, D), "  but S and D are not independent")
print("P(S = 12 and D = 0) =", E(lambda a, b: S(a, b) == 12 and D(a, b) == 0), " P(S = 12) P(D = 0) =", E(lambda a, b: S(a, b) == 12) * E(lambda a, b: D(a, b) == 0))
Cov(X, Y) = 0
Cov(X, S) = 35/12   (= Var(X) = 35/12)
Cov(S, D) = 0   but S and D are not independent
P(S = 12 and D = 0) = 1/36  P(S = 12) P(D = 0) = 1/216

Uncorrelated is not independent

Independent variables have zero covariance, but zero covariance does not imply independence. The sum and difference of two dice are uncorrelated, yet knowing the sum is 12 forces the difference to be 0. An extreme case: XX uniform on [−1,1][-1, 1] and Y=X2Y = X^2 are completely dependent but uncorrelated, because the relationship is not linear.

python · try it yourself
import numpy as np

rng = np.random.default_rng(2)
x = rng.uniform(-1, 1, 200_000)
y = x ** 2
print("corr(X, X^2) on [-1, 1]:", round(float(np.corrcoef(x, y)[0, 1]), 4))
x2 = rng.uniform(0, 1, 200_000)
print("corr(X, X^2) on [0, 1]: ", round(float(np.corrcoef(x2, x2 ** 2)[0, 1]), 4))
corr(X, X^2) on [-1, 1]: 0.0004
corr(X, X^2) on [0, 1]:  0.9682

Diversification

If nn assets each have return variance σ2\sigma^2 and pairwise correlation ρ\rho, an equal-weight portfolio has variance

Var⁡(1n∑Xi)=σ2n+n−1nρσ2  ⟶  ρσ2\operatorname{Var}\left(\frac{1}{n}\sum X_i\right) = \frac{\sigma^2}{n} + \frac{n-1}{n}\rho\sigma^2 \;\longrightarrow\; \rho\sigma^2

With independent assets (ρ=0\rho = 0) risk vanishes as nn grows; with correlated assets it levels off at ρσ2\rho\sigma^2. The same formula explains why averaging many correlated models (an ensemble) helps less than averaging independent ones, and why replicas in the same data centre are less reliable than they look.

python · try it yourself
import numpy as np

rng = np.random.default_rng(3)
sigma, rho = 1.0, 0.3
for n in [1, 5, 25, 100]:
    common = rng.standard_normal((20_000, 1))
    own = rng.standard_normal((20_000, n))
    assets = sigma * (np.sqrt(rho) * common + np.sqrt(1 - rho) * own)   # pairwise correlation rho
    var_avg = assets.mean(axis=1).var()
    theory = sigma ** 2 / n + (n - 1) / n * rho * sigma ** 2
    print(f"n={n:3d}  variance of average: {var_avg:.4f}   formula: {theory:.4f}")
n=  1  variance of average: 0.9930   formula: 1.0000
n=  5  variance of average: 0.4380   formula: 0.4400
n= 25  variance of average: 0.3318   formula: 0.3280
n=100  variance of average: 0.3050   formula: 0.3070

In software

Covariance matrices are central to PCA (Linear Algebra), to Gaussian models, to Kalman filters for tracking and to portfolio optimisation. Feature correlation analysis catches redundant inputs to a model. Correlation is also the classic trap of data analysis: correlated metrics need not cause each other (Statistics: Correlation).

Exercises

Exercise 1: Variance of a difference

Var⁡(X)=4\operatorname{Var}(X) = 4, Var⁡(Y)=9\operatorname{Var}(Y) = 9, Cov⁡(X,Y)=3\operatorname{Cov}(X, Y) = 3. Find Var⁡(X−Y)\operatorname{Var}(X - Y) and ρ(X,Y)\rho(X, Y).

Answer

77; ρ=0.5\rho = 0.5.

Solution

Var⁡(X−Y)=4+9−2⋅3=7\operatorname{Var}(X - Y) = 4 + 9 - 2 \cdot 3 = 7. ρ=3/(2⋅3)=0.5\rho = 3/(2 \cdot 3) = 0.5.

Exercise 2: Indicator covariance

Draw two cards without replacement. Let I1,I2I_1, I_2 indicate that card 1 and card 2 are aces. Find Cov⁡(I1,I2)\operatorname{Cov}(I_1, I_2).

Answer

1221−1169≈−0.00140\frac{1}{221} - \frac{1}{169} \approx -0.00140.

Solution

E[I1I2]=452⋅351=1221E[I_1 I_2] = \frac{4}{52} \cdot \frac{3}{51} = \frac{1}{221}; E[I1]E[I2]=(113)2=1169E[I_1]E[I_2] = \left(\frac{1}{13}\right)^2 = \frac{1}{169}. Negative: an ace first makes an ace second less likely.

Exercise 3: Hypergeometric variance (code)

Use indicators and covariances to derive the variance of the number of aces in a 5-card hand, and check against the hypergeometric formula np(1−p)N−nN−1n p (1-p) \frac{N-n}{N-1}.

Answer

9402873≈0.3272\frac{940}{2873} \approx 0.3272.

Solution

Var⁡(∑Ii)=5Var⁡(I1)+5⋅4Cov⁡(I1,I2)\operatorname{Var}(\sum I_i) = 5\operatorname{Var}(I_1) + 5 \cdot 4 \operatorname{Cov}(I_1, I_2) by symmetry.

python · try it yourself
from fractions import Fraction as F

p = F(4, 52)
var_i = p * (1 - p)
cov = F(4, 52) * F(3, 51) - p * p
total = 5 * var_i + 20 * cov
formula = 5 * p * (1 - p) * F(52 - 5, 52 - 1)
print(total, formula, round(float(total), 4))
940/2873 940/2873 0.3272

Test yourself

If XX and YY are independent, Cov(X,Y)\mathrm{Cov}(X, Y) is…

  1. 1
  2. 0
  3. Var(X)Var(Y)\mathrm{Var}(X)\mathrm{Var}(Y)
  4. undefined

Does Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0 imply that XX and YY are independent?

  1. Yes
  2. No
  3. Only for positive variables
  4. Only if both are uniform

An average of nn assets with pairwise correlation ρ>0\rho > 0 has variance tending to…

  1. 0
  2. ρσ2\rho \sigma^2
  3. σ2\sigma^2
  4. σ2/n\sigma^2/n