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
Positive covariance: the variables tend to be above or below their means together. Negative: one tends to be above when the other is below. .
The variance of a sum picks up covariance terms:
Correlation
Covariance depends on units. The correlation coefficient removes them:
exactly when for some . Correlation measures linear association only.
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: uniform on and are completely dependent but uncorrelated, because the relationship is not linear.
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 assets each have return variance and pairwise correlation , an equal-weight portfolio has variance
With independent assets () risk vanishes as grows; with correlated assets it levels off at . 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.
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
, , . Find and .
Answer
; .
Solution
. .
Exercise 2: Indicator covariance
Draw two cards without replacement. Let indicate that card 1 and card 2 are aces. Find .
Answer
.
Solution
; . 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 .
Answer
.
Solution
by symmetry.
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 and are independent, is…
- 1
- 0
- undefined
Does imply that and are independent?
- Yes
- No
- Only for positive variables
- Only if both are uniform
An average of assets with pairwise correlation has variance tending to…
- 0