Covariance, Correlation and Independence
Features rarely vary on their own: taller people weigh more, prices move together. Covariance measures how two variables move together, correlation is its unit-free version, and the covariance matrix collects all pairs. It is the central object of PCA, Gaussian models and whitening.
Covariance and correlation
The bound comes from Cauchy-Schwarz (Inner Products). means an exact linear relation.
The covariance matrix
For a random vector with mean :
It is symmetric and positive semidefinite, with the variances on the diagonal. For data with centred rows, the sample covariance is . Under a linear map, covariances transform like a sandwich:
import numpy as np
rng = np.random.default_rng(0)
height = rng.normal(170, 8, size=5000)
weight = 0.9 * height - 85 + rng.normal(0, 6, size=5000)
X = np.column_stack([height, weight])
Xc = X - X.mean(axis=0)
S = Xc.T @ Xc / (len(X) - 1)
print("covariance:\n", np.round(S, 2))
print("np.cov agrees:", np.allclose(S, np.cov(X.T)))
print("correlation:", round(S[0, 1] / np.sqrt(S[0, 0] * S[1, 1]), 4), " np.corrcoef:", round(np.corrcoef(X.T)[0, 1], 4))
A = np.array([[1.0, 1.0], [1.0, -1.0]])
print("Cov[Ax] = A S A^T:", np.allclose(np.cov((X @ A.T).T), A @ S @ A.T))covariance:
[[63.42 57.76]
[57.76 88.64]]
np.cov agrees: True
correlation: 0.7703 np.corrcoef: 0.7703
Cov[Ax] = A S A^T: True
Independence versus uncorrelatedness
and are independent if . Independence implies zero covariance, but zero covariance does not imply independence: covariance only sees linear relations.
import numpy as np
rng = np.random.default_rng(1)
x = rng.uniform(-1, 1, size=200000)
y = x**2 # completely determined by x!
print("corr(x, y):", round(np.corrcoef(x, y)[0, 1], 3))
print("E[y | |x| > 0.9]:", round(y[np.abs(x) > 0.9].mean(), 3), " E[y | |x| < 0.1]:", round(y[np.abs(x) < 0.1].mean(), 3))corr(x, y): 0.002
E[y | |x| > 0.9]: 0.903 E[y | |x| < 0.1]: 0.003
The correlation is essentially zero, yet knowing tells you exactly. For jointly Gaussian variables (and only special cases like that), uncorrelated does mean independent.
Variance of a sum
Negative covariance reduces the variance of a sum. That is the logic of diversification in portfolios and of ensembles in ML: averaging models whose errors are not perfectly correlated lowers the error variance (Bagging).
In software
np.cov(X.T)(variables in rows!) ornp.cov(X, rowvar=False);np.corrcoeffor correlations.- Correlation heatmaps are a standard first look at a dataset; strongly correlated features hint at rank problems.
- Mutual information (in Information Theory) detects the nonlinear dependence that correlation misses.
Exercises
Exercise 1: From moments to covariance
, , . Find .
Answer
Solution
.
Exercise 2: Variance of a difference
, , . Find .
Answer
Solution
, and .
Exercise 3: Sandwich rule (code)
has covariance . Find the variance of .
Answer
Solution
With : .
import numpy as np
S = np.array([[2.0, 1.0], [1.0, 3.0]]); a = np.array([1.0, -2.0])
print(a @ S @ a)10.0
Test yourself
Zero covariance between and implies…
- independence
- no linear relationship, but possibly a nonlinear one
- both have zero mean
If , then is…
A covariance matrix is always…
- diagonal
- symmetric positive semidefinite
- invertible
- orthogonal