TExcelTAD
Home › Math for Machine Learning › Covariance, Correlation and Independence

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

Cov⁡[X,Y]=E[(X−E[X])(Y−E[Y])]=E[XY]−E[X] E[Y]\operatorname{Cov}[X, Y] = \mathbb{E}\big[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])\big] = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y]
corr⁡[X,Y]=Cov⁡[X,Y]V[X] V[Y]∈[−1,1]\operatorname{corr}[X, Y] = \frac{\operatorname{Cov}[X, Y]}{\sqrt{\mathbb{V}[X]\,\mathbb{V}[Y]}} \in [-1, 1]

The bound comes from Cauchy-Schwarz (Inner Products). ±1\pm 1 means an exact linear relation.

The covariance matrix

For a random vector x∈RD\mathbf{x} \in \mathbb{R}^D with mean μ\boldsymbol{\mu}:

Σ=Cov⁡[x]=E[(x−μ)(x−μ)T],Σij=Cov⁡[xi,xj]\Sigma = \operatorname{Cov}[\mathbf{x}] = \mathbb{E}\big[(\mathbf{x} - \boldsymbol{\mu})(\mathbf{x} - \boldsymbol{\mu})^T\big], \qquad \Sigma_{ij} = \operatorname{Cov}[x_i, x_j]

It is symmetric and positive semidefinite, with the variances on the diagonal. For data X∈RN×DX \in \mathbb{R}^{N \times D} with centred rows, the sample covariance is 1N−1XcTXc\frac{1}{N-1}X_c^TX_c. Under a linear map, covariances transform like a sandwich:

Cov⁡[Ax+b]=A Σ AT\operatorname{Cov}[A\mathbf{x} + \mathbf{b}] = A\,\Sigma\,A^T
python · try it yourself
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

XX and YY are independent if p(x,y)=p(x) p(y)p(x, y) = p(x)\,p(y). Independence implies zero covariance, but zero covariance does not imply independence: covariance only sees linear relations.

python · try it yourself
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 xx tells you yy exactly. For jointly Gaussian variables (and only special cases like that), uncorrelated does mean independent.

Variance of a sum

V[X+Y]=V[X]+V[Y]+2Cov⁡[X,Y]\mathbb{V}[X + Y] = \mathbb{V}[X] + \mathbb{V}[Y] + 2\operatorname{Cov}[X, Y]

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!) or np.cov(X, rowvar=False); np.corrcoef for 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

E[X]=1\mathbb{E}[X] = 1, E[Y]=2\mathbb{E}[Y] = 2, E[XY]=5\mathbb{E}[XY] = 5. Find Cov⁡[X,Y]\operatorname{Cov}[X, Y].

Answer
Solution

Cov⁡[X,Y]=E[XY]−E[X]E[Y]=5−2=3\operatorname{Cov}[X, Y] = \mathbb{E}[XY] - \mathbb{E}[X]\mathbb{E}[Y] = 5 - 2 = 3.

Exercise 2: Variance of a difference

V[X]=4\mathbb{V}[X] = 4, V[Y]=9\mathbb{V}[Y] = 9, corr⁡[X,Y]=0.5\operatorname{corr}[X, Y] = 0.5. Find V[X−Y]\mathbb{V}[X - Y].

Answer
Solution

Cov⁡=0.5⋅2⋅3=3\operatorname{Cov} = 0.5 \cdot 2 \cdot 3 = 3, and V[X−Y]=4+9−2⋅3=7\mathbb{V}[X - Y] = 4 + 9 - 2 \cdot 3 = 7.

Exercise 3: Sandwich rule (code)

x\mathbf{x} has covariance Σ=[2113]\Sigma = \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix}. Find the variance of z=x1−2x2z = x_1 - 2x_2.

Answer
Solution

With a=(1,−2)\mathbf{a} = (1, -2): aTΣa=2−4⋅1+4⋅3=10\mathbf{a}^T\Sigma\mathbf{a} = 2 - 4 \cdot 1 + 4 \cdot 3 = 10.

python · try it yourself
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 XX and YY implies…

  1. independence
  2. no linear relationship, but possibly a nonlinear one
  3. X=YX = Y
  4. both have zero mean

If Cov⁡[x]=Σ\operatorname{Cov}[\mathbf{x}] = \Sigma, then Cov⁡[Ax]\operatorname{Cov}[A\mathbf{x}] is…

  1. AΣA\Sigma
  2. ΣAT\Sigma A^T
  3. AΣATA\Sigma A^T
  4. ATΣAA^T\Sigma A

A covariance matrix is always…

  1. diagonal
  2. symmetric positive semidefinite
  3. invertible
  4. orthogonal