Covariance and Correlation
Measuring joint variation, Pearson correlation, and why zero correlation does not imply independence.
Assumes you know
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 is above or below its mean, and whether 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 and .
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
with the computational form, derived exactly as for variance:
Correlation standardises it:
| Symbol | Meaning | Read aloud |
|---|---|---|
| Cov(X,Y) | Covariance — in units of X times units of Y | covariance of X and Y |
| ρ or ρₓᵥ | Pearson correlation coefficient, between −1 and 1 | rho |
| σₓ, σᵥ | Standard deviations of X and Y | sigma X, sigma Y |
| Σ | Covariance matrix for a random vector | sigma (matrix) |
Properties
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 , the variance of is non-negative because it is the expectation of a square:
Read the right-hand side as a quadratic in :
A quadratic that is never negative has a non-positive discriminant:
Take square roots:
When is ? Equality in the discriminant means has a repeated root with , so , so is constant. That is — an exact linear relationship.
So if and only if and are exactly linearly related, with the sign of giving the sign of the slope.
Independence implies zero correlation — not the converse
If then , so and .
The reverse fails, and this is the most important caveat in the lesson.
Solved problem 1 · Zero correlation with perfect dependence
Let take values each with probability , and set . Show despite being a deterministic function of .
Step 1 — the joint distribution
, so:
| Probability | ||
|---|---|---|
| 1 | 1/3 | |
| 0 | 0 | 1/3 |
| 1 | 1 | 1/3 |
Step 2 — the means
Step 3 — E[XY]
Step 4 — covariance
Hence .
Step 5 — confirm the dependence is total
is completely determined by : observing tells you with certainty. Check independence formally:
, so they are emphatically not independent.
Step 6 — why correlation missed it
The relationship is symmetric about zero: increasing increases , but going up from to decreases while going up from to increases it. The positive and negative contributions cancel exactly, leaving zero linear association.
Answer
with a deterministic function of . Zero correlation means no linear association; it does not mean independence. The implication runs one way only.
Solved problem 2 · Covariance from a joint table
and have this joint distribution:
| 0.30 | 0.20 | 0.50 | |
| 0.10 | 0.40 | 0.50 | |
| 0.40 | 0.60 | 1.00 |
Find and .
Step 1 — marginal means
Both are Bernoulli, so the mean is the probability of 1:
Step 2 — E[XY]
only when both are 1, which has probability 0.40:
Step 3 — covariance
Positive: and tend to be high together.
Step 4 — standard deviations
For Bernoulli(), :
Step 5 — correlation
Step 6 — cross-check with conditional probabilities
Knowing doubles the chance that , consistent with a moderate positive correlation.
Answer
and — a moderate positive linear association.
The covariance matrix
For a random vector :
Variances sit on the diagonal, covariances off it. Two properties matter downstream:
- Symmetric, since .
- Positive semi-definite, because for any vector , .
Those two facts are what let PCA diagonalise by eigendecomposition, and what make the multivariate normal density well defined.
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
, , . Find and .
Show solutionHide solution
Worth noting the size of the correction. Ignoring covariance would give 41, understating the variance by 37%. And check the bound: , so the largest possible covariance here is 20, giving and .
Exercise 2
Prove .
Show solutionHide solution
Write and apply the variance-of-a-sum identity:
Two substitutions. The scaling rule gives
and the bilinearity of covariance gives
Substituting:
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.