Skip to content
VibeFormer
Intermediate24 min

Covariance and Correlation

Measuring joint variation, Pearson correlation, and why zero correlation does not imply independence.

Covariance and Correlation

Intuition first

Variance measures how one variable spreads. Covariance measures whether two variables spread together.

The idea is simple: for each observation, look at whether XX is above or below its mean, and whether YY is above or below its mean. Multiply the two deviations. If they usually share a sign — both high or both low — the products are positive and the average is positive. If one is typically high when the other is low, the products are negative.

The awkwardness is units. Covariance between height in centimetres and weight in kilograms is in centimetre-kilograms, which means nothing to anyone and changes if you switch to inches. Correlation fixes that by dividing out both standard deviations, producing a unitless number always between 1-1 and +1+1.

And the essential caveat, which the last solved problem makes concrete: correlation measures linear association only. Two variables can be perfectly determined by one another and have correlation exactly zero.

Definitions

Cov(X,Y)=E[(XμX)(YμY)]\Cov(X, Y) = \E\big[(X - \mu_X)(Y - \mu_Y)\big]

with the computational form, derived exactly as for variance:

Cov(X,Y)=E[XY]E[X]E[Y]\Cov(X, Y) = \E[XY] - \E[X]\,\E[Y]

Correlation standardises it:

ρXY=Cov(X,Y)σXσY\rho_{XY} = \frac{\Cov(X, Y)}{\sigma_X \sigma_Y}
Notation used in this lesson
SymbolMeaning
Cov(X,Y)Covariance — in units of X times units of Y
ρ or ρₓᵥPearson correlation coefficient, between −1 and 1
σₓ, σᵥStandard deviations of X and Y
ΣCovariance matrix for a random vector

Properties

Cov(X,Y)=Cov(Y,X)(symmetric)\Cov(X, Y) = \Cov(Y, X) \qquad \text{(symmetric)} Cov(aX+b,  cY+d)=acCov(X,Y)(constants shift, multipliers scale)\Cov(aX + b,\; cY + d) = ac\,\Cov(X, Y) \qquad \text{(constants shift, multipliers scale)} Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\Var(X + Y) = \Var(X) + \Var(Y) + 2\Cov(X, Y)

The last identity is why covariance matters practically: it is the correction term that makes variances add or fail to add.

Why correlation is bounded by 1

The Cauchy–Schwarz argumentAdvanced

For any real tt, the variance of XtYX - tY is non-negative because it is the expectation of a square:

0Var(XtY)=Var(X)2tCov(X,Y)+t2Var(Y)0 \leq \Var(X - tY) = \Var(X) - 2t\,\Cov(X,Y) + t^2 \Var(Y)

Read the right-hand side as a quadratic in tt:

q(t)=Var(Y)t22Cov(X,Y)t+Var(X)q(t) = \Var(Y)\,t^2 - 2\Cov(X,Y)\,t + \Var(X)

A quadratic that is never negative has a non-positive discriminant:

(2Cov(X,Y))24Var(Y)Var(X)0\big(2\Cov(X,Y)\big)^2 - 4\,\Var(Y)\Var(X) \leq 0Cov(X,Y)2Var(X)Var(Y)\Cov(X,Y)^2 \leq \Var(X)\Var(Y)

Take square roots:

Cov(X,Y)σXσYρ1\lvert \Cov(X,Y) \rvert \leq \sigma_X \sigma_Y \quad\Longrightarrow\quad \lvert \rho \rvert \leq 1

When is ρ=±1\rho = \pm 1? Equality in the discriminant means q(t)q(t) has a repeated root tt^* with q(t)=0q(t^*) = 0, so Var(XtY)=0\Var(X - t^*Y) = 0, so XtYX - t^*Y is constant. That is X=tY+cX = t^*Y + c — an exact linear relationship.

So ρ=1\lvert \rho \rvert = 1 if and only if XX and YY are exactly linearly related, with the sign of ρ\rho giving the sign of the slope.

Independence implies zero correlation — not the converse

If X ⁣ ⁣ ⁣YX \ind Y then E[XY]=E[X]E[Y]\E[XY] = \E[X]\E[Y], so Cov(X,Y)=0\Cov(X,Y) = 0 and ρ=0\rho = 0.

The reverse fails, and this is the most important caveat in the lesson.

Solved problem 1 · Zero correlation with perfect dependence

Let XX take values 1,0,1-1, 0, 1 each with probability 1/31/3, and set Y=X2Y = X^2. Show ρ=0\rho = 0 despite YY being a deterministic function of XX.

Step 1 — the joint distribution

Y=X2Y = X^2, so:

XXYYProbability
1-111/3
001/3
111/3

Step 2 — the means

E[X]=13(1)+13(0)+13(1)=0\E[X] = \tfrac13(-1) + \tfrac13(0) + \tfrac13(1) = 0E[Y]=13(1)+13(0)+13(1)=23\E[Y] = \tfrac13(1) + \tfrac13(0) + \tfrac13(1) = \tfrac23

Step 3 — E[XY]

XY=XX2=X3XY = X \cdot X^2 = X^3E[XY]=E[X3]=13(1)+13(0)+13(1)=0\E[XY] = \E[X^3] = \tfrac13(-1) + \tfrac13(0) + \tfrac13(1) = 0

Step 4 — covariance

Cov(X,Y)=E[XY]E[X]E[Y]=0(0)(23)=0\Cov(X,Y) = \E[XY] - \E[X]\E[Y] = 0 - (0)\left(\tfrac23\right) = 0

Hence ρ=0\rho = 0.

Step 5 — confirm the dependence is total

YY is completely determined by XX: observing X=0X = 0 tells you Y=0Y = 0 with certainty. Check independence formally:

P(X=0,Y=0)=13\Prob(X = 0, Y = 0) = \tfrac13P(X=0)P(Y=0)=13×13=19\Prob(X = 0)\,\Prob(Y = 0) = \tfrac13 \times \tfrac13 = \tfrac19

1319\tfrac13 \neq \tfrac19, so they are emphatically not independent.

Step 6 — why correlation missed it

The relationship is symmetric about zero: increasing X\lvert X \rvert increases YY, but XX going up from 1-1 to 00 decreases YY while XX going up from 00 to 11 increases it. The positive and negative contributions cancel exactly, leaving zero linear association.

Answer

ρ=0\rho = 0 with YY a deterministic function of XX. Zero correlation means no linear association; it does not mean independence. The implication runs one way only.

ρ ≈ 0.95ρ ≈ 0.45ρ ≈ 0.00ρ ≈ 0.00
Four datasets. The first three have progressively weaker linear association. The fourth has correlation near zero and an obvious, strong, non-linear relationship — which is what correlation cannot see.

Solved problem 2 · Covariance from a joint table

XX and YY have this joint distribution:

Y=0Y = 0Y=1Y = 1P(X)\Prob(X)
X=0X = 00.300.200.50
X=1X = 10.100.400.50
P(Y)\Prob(Y)0.400.601.00

Find Cov(X,Y)\Cov(X,Y) and ρ\rho.

Step 1 — marginal means

Both are Bernoulli, so the mean is the probability of 1:

E[X]=0.50,E[Y]=0.60\E[X] = 0.50, \qquad \E[Y] = 0.60

Step 2 — E[XY]

XY=1XY = 1 only when both are 1, which has probability 0.40:

E[XY]=1×0.40=0.40\E[XY] = 1 \times 0.40 = 0.40

Step 3 — covariance

Cov(X,Y)=0.40(0.50)(0.60)=0.400.30=0.10\Cov(X,Y) = 0.40 - (0.50)(0.60) = 0.40 - 0.30 = 0.10

Positive: XX and YY tend to be high together.

Step 4 — standard deviations

For Bernoulli(pp), Var=p(1p)\Var = p(1-p):

Var(X)=0.5×0.5=0.25σX=0.50\Var(X) = 0.5 \times 0.5 = 0.25 \quad\Longrightarrow\quad \sigma_X = 0.50Var(Y)=0.6×0.4=0.24σY=0.240.4899\Var(Y) = 0.6 \times 0.4 = 0.24 \quad\Longrightarrow\quad \sigma_Y = \sqrt{0.24} \approx 0.4899

Step 5 — correlation

ρ=0.100.50×0.4899=0.100.244950.4082\rho = \frac{0.10}{0.50 \times 0.4899} = \frac{0.10}{0.24495} \approx 0.4082

Step 6 — cross-check with conditional probabilities

P(Y=1X=1)=0.400.50=0.80,P(Y=1X=0)=0.200.50=0.40\Prob(Y = 1 \mid X = 1) = \frac{0.40}{0.50} = 0.80, \qquad \Prob(Y = 1 \mid X = 0) = \frac{0.20}{0.50} = 0.40

Knowing X=1X = 1 doubles the chance that Y=1Y = 1, consistent with a moderate positive correlation.

Answer

Cov(X,Y)=0.10\Cov(X,Y) = 0.10 and ρ0.408\rho \approx 0.408 — a moderate positive linear association.

The covariance matrix

For a random vector X=(X1,,Xd)\mathbf{X} = (X_1, \dots, X_d):

Σij=Cov(Xi,Xj),Σ=E[(Xμ)(Xμ)T]\Sigma_{ij} = \Cov(X_i, X_j), \qquad \Sigma = \E\big[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^{\mathsf{T}}\big]

Variances sit on the diagonal, covariances off it. Two properties matter downstream:

  • Symmetric, since Cov(Xi,Xj)=Cov(Xj,Xi)\Cov(X_i, X_j) = \Cov(X_j, X_i).
  • Positive semi-definite, because for any vector aa, aTΣa=Var(aTX)0a^{\mathsf{T}}\Sigma a = \Var(a^{\mathsf{T}}\mathbf{X}) \geq 0.

Those two facts are what let PCA diagonalise Σ\Sigma by eigendecomposition, and what make the multivariate normal density well defined.

python
import numpy as np

rng = np.random.default_rng(0)

# The zero-correlation-yet-dependent example.
X = rng.choice([-1, 0, 1], size=300_000)
Y = X ** 2
print(f"corr(X, X²)     {np.corrcoef(X, Y)[0,1]:+.4f}   <- ~0, yet Y is determined by X")

# Correlation is invariant to linear rescaling; covariance is not.
A = rng.normal(0, 1, 100_000)
B = 2 * A + rng.normal(0, 1, 100_000)
print(f"\ncov(A,B)        {np.cov(A, B)[0,1]:.4f}")
print(f"cov(100A, B)    {np.cov(100*A, B)[0,1]:.4f}   <- scaled by 100")
print(f"corr(A,B)       {np.corrcoef(A, B)[0,1]:.4f}")
print(f"corr(100A, B)   {np.corrcoef(100*A, B)[0,1]:.4f}   <- unchanged")

# Variance of a sum needs the covariance term.
print(f"\nVar(A+B) empirical {np.var(A+B):.4f}")
print(f"Var(A)+Var(B)      {np.var(A)+np.var(B):.4f}  <- too small")
print(f"+ 2Cov(A,B)        {np.var(A)+np.var(B)+2*np.cov(A,B)[0,1]:.4f}  <- matches")

Exercise 1

Var(X)=16\Var(X) = 16, Var(Y)=25\Var(Y) = 25, Cov(X,Y)=12\Cov(X,Y) = 12. Find ρ\rho and Var(X+Y)\Var(X + Y).

Show solution
ρ=121625=124×5=1220=0.60\rho = \frac{12}{\sqrt{16}\sqrt{25}} = \frac{12}{4 \times 5} = \frac{12}{20} = 0.60Var(X+Y)=16+25+2(12)=41+24=65\Var(X+Y) = 16 + 25 + 2(12) = 41 + 24 = 65

Worth noting the size of the correction. Ignoring covariance would give 41, understating the variance by 37%. And check the bound: CovσXσY=20\lvert \Cov \rvert \leq \sigma_X\sigma_Y = 20, so the largest possible covariance here is 20, giving ρ=1\rho = 1 and Var(X+Y)=81\Var(X+Y) = 81.

Exercise 2

Prove Var(XY)=Var(X)+Var(Y)2Cov(X,Y)\Var(X - Y) = \Var(X) + \Var(Y) - 2\Cov(X,Y).

Show solution

Write XY=X+(1)YX - Y = X + (-1)Y and apply the variance-of-a-sum identity:

Var(X+(1)Y)=Var(X)+Var((1)Y)+2Cov(X,(1)Y)\Var\big(X + (-1)Y\big) = \Var(X) + \Var\big((-1)Y\big) + 2\Cov\big(X, (-1)Y\big)

Two substitutions. The scaling rule gives

Var((1)Y)=(1)2Var(Y)=Var(Y)\Var\big((-1)Y\big) = (-1)^2 \Var(Y) = \Var(Y)

and the bilinearity of covariance gives

Cov(X,(1)Y)=(1)Cov(X,Y)=Cov(X,Y)\Cov\big(X, (-1)Y\big) = (-1)\Cov(X, Y) = -\Cov(X,Y)

Substituting:

Var(XY)=Var(X)+Var(Y)2Cov(X,Y)\Var(X - Y) = \Var(X) + \Var(Y) - 2\Cov(X,Y)

The sign flips on the covariance term but not on the variance term, because variance carries a squared multiplier and covariance a linear one. This is why positively correlated variables have a less variable difference — the shared fluctuation cancels — which is the entire basis of paired experimental designs and of hedging.


Next: Bernoulli and Binomial Distributions, the first named distributions.