Foundations of Data Science slides 📂 Introduction · 9 of 10 32 min read

Covariance Explained: What It Measures, Formula & Examples

The number behind correlation. Covariance answers one question — do two variables move together? — by multiplying how far each strays from its own mean and averaging. This tutorial covers the sign mechanic (the four-quadrant logic), population vs sample formulas, a worked example (Cov = 57.5), the units problem, how standardizing gives correlation, the covariance matrix, and code — with animated diagrams.

↗️

Covariance Explained

The number behind correlation. Covariance answers one question — do these two variables move together? — by multiplying how far each strays from its own mean, and averaging the result.
Joint Variability Sign = Direction Unbounded Covariance Matrix

Press Next → or use ← → arrow keys

Section 01

The Intuition — Do They Move Together?

When one goes up, what does the other do?
As study hours rise, do exam scores rise with them? As a product's price climbs, does demand fall? Every time you ask whether two measurements track each other, you're reaching for covariance.

Covariance measures how much two variables change together relative to their own means. It takes each point's distance above or below its mean, multiplies the pair, and averages — producing a single number whose sign reveals the direction of the relationship.
💡
The Foundation Under Correlation & PCA

Covariance is the raw material for correlation, the covariance matrix, and PCA. Nail this one idea — joint variability around the means — and a whole layer of multivariate statistics opens up.

Section 02 · Three Outcomes

What The Sign Tells You

↗️
Cov > 0
move together
When one is above its mean, the other tends to be too. Both rise and fall in sync — like height and weight.
↘️
Cov < 0
move opposite
When one is above its mean, the other tends to be below. They pull in opposite directions — like price and demand.
➡️
Cov ≈ 0
no linear link
No consistent joint movement — though a curved (non-linear) relationship could still hide underneath.
🚦
Sign, Not Size

Covariance's sign is trustworthy, but its magnitude isn't directly meaningful — a covariance of 500 isn't "bigger" than one of 5 in any comparable way, because it depends entirely on the units. Read the sign for direction; wait for correlation to judge strength.

Section 02 · Diagram

Why The Sign Works — Four Quadrants

x = x̄ y = ȳ (+)(+) = + (−)(−) = + (−)(+) = − (+)(−) = −
✳️
Every Point Casts A Vote

Split the cloud by the two mean lines. A point above both means, or below both, gives a positive product (+·+ or −·−). A point above one and below the other gives a negative product. Covariance sums these votes: if the green quadrants dominate, Cov > 0; if red dominates, Cov < 0; if they cancel, Cov ≈ 0.

Section 03 · Formulas

Population vs Sample Covariance

Population covariance
Cov(X,Y) = Σ(xᵢ − μₓ)(yᵢ − μᵧ) / N
Average product of paired deviations across the whole population.
Sample covariance
Cov(X,Y) = Σ(xᵢ − x̄)(yᵢ − ȳ) / (n − 1)
Divide by (n − 1) — Bessel's correction — for an unbiased estimate from a sample.
🔤
Read It Like Variance With Two Variables

Variance multiplies a variable's deviation by itself: (x−x̄)². Covariance multiplies X's deviation by Y's: (x−x̄)(y−ȳ). In fact Cov(X, X) = Var(X) — variance is just covariance of a variable with itself. Same (n−1) logic, same Bessel correction.

Section 04 · Worked Example

Five Students, Step By Step

Study hours X = [2, 4, 6, 8, 10] (mean 6) and scores Y = [50, 60, 70, 85, 95] (mean 72). Multiply each pair of deviations:

Studentx − x̄y − ȳproduct
A (2, 50)−4−22+88
B (4, 60)−2−12+24
C (6, 70)0−20
D (8, 85)+2+13+26
E (10, 95)+4+23+92
🧮
Sum = 230 · Cov = 230 / 4 = 57.5

Every product is positive (or zero), so the sum is a solid +230; divide by n−1 = 4 and Cov = 57.5. The positive sign confirms study hours and scores move together. But "57.5 hour·marks" is an awkward unit — which is exactly the problem correlation will fix.

Section 05 · The Catch

The Units Problem — Magnitude Is Slippery

Cov = 57.5 units: hours · marks measure time in minutes (×60) Cov = 3450 units: minutes · marks
📏
Same Relationship, 60× The Number

Switch study time from hours to minutes and the covariance jumps from 57.5 to 3450 — even though the relationship is identical. Covariance has no fixed range and rides on the units of X and Y, so you can never compare raw covariances across datasets. That single flaw is why correlation exists.

Section 06 · The Fix

Standardize It → Correlation

Cov = 57.5 unit-dependent · unbounded ÷ (σₓ · σᵧ) r ≈ 0.997 unitless · always −1 … +1
🔗
Correlation Is Just Covariance, Normalized

Divide the covariance by the product of the two standard deviations — r = Cov(X,Y) / (σₓ·σᵧ) — and the units cancel out. The result is the correlation coefficient: the same directional information, now squeezed into a comparable −1 to +1 scale. Covariance is the engine; correlation is the readable dial.

Section 06 · Comparison

Covariance vs Correlation

PropertyCovarianceCorrelation (r)
Range−∞ to +∞−1 to +1
Depends on units?YesNo (unitless)
Magnitude interpretable?HardEasy
Tells direction?YesYes
Symmetric?Yes — Cov(X,Y)=Cov(Y,X)Yes
Best forMatrices, PCA, theoryReporting & comparing strength
🤝
Two Views Of Joint Movement

Both answer "which direction do they move together?" and both are symmetric. Use covariance inside the maths — covariance matrices, PCA's eigen-decomposition, portfolio variance. Use correlation whenever a human needs to judge how strong the link is.

Section 07 · Many Variables

The Covariance Matrix

X (hours) Y (score) X Y 10.0Var(X) 57.5Cov(X,Y) 57.5Cov(Y,X) 332.5Var(Y)
🔲
Variances On The Diagonal, Covariances Off It

Stack every pairwise covariance into a grid and you get the covariance matrix: each variable's variance sits on the diagonal, and the covariances fill the off-diagonal — always symmetric (top-right = bottom-left). This matrix is exactly what PCA eigen-decomposes to find the directions of greatest variance.

Section 08 · Applications

Why Covariance Matters

🔦
PCA
PCA finds the eigenvectors of the covariance matrix — the axes along which the data varies most. No covariance, no PCA.
💹
Portfolio Risk
A portfolio's total variance depends on the covariances between assets. Low/negative covariance is how diversification cuts risk.
🔗
Correlation
Correlation is literally standardized covariance — every r value you compute starts life as a covariance.
🧠
The Same Warning As Correlation

Because covariance underlies correlation, it inherits the same caveats: a non-zero covariance shows variables move together, not that one causes the other, and a zero covariance only rules out a linear link — a curved relationship can still hide beneath it.

Section 09 · Code

Covariance In Python

import numpy as np

X = [2, 4, 6, 8, 10]
Y = [50, 60, 70, 85, 95]

np.cov(X, Y)          # 2×2 matrix; np.cov defaults to ddof=1 (sample) ✓
# [[ 10.   57.5 ]
#  [ 57.5 332.5 ]]   → Cov(X,Y) = 57.5

# ── pandas ──
df.cov()                        # full covariance matrix
df['hours'].cov(df['score'])   # 57.5 — one pair
A Rare Case Where The Default Is Right

Unlike np.var() and np.std() (which default to the population divisor), np.cov() already uses ddof=1 — the sample formula. So the matrix it returns is the sample covariance out of the box. The diagonal holds each variable's variance; the off-diagonal is the covariance you want.

Section 10 · Golden Rules

Six Rules For Covariance

🏅 Covariance, Distilled
1Read the sign, not the size. + means together, − means opposite; the magnitude is unit-bound.
2Use (n−1) for samples. Bessel's correction keeps the estimate unbiased.
3Never compare raw covariances across datasets or units — standardize to correlation first.
4Zero covariance ≠ independence. It only rules out a linear relationship, not a curved one.
5It's symmetric, and Cov(X,X) = Var(X) — variance is covariance with itself.
6Covariance ≠ causation. Moving together never proves one drives the other.
Wrap-Up

You Now Understand Covariance

Σ(Δx·Δy)Paired deviations
÷(n−1)Sample formula
sign= direction
−∞…+∞Unbounded
÷σₓσᵧ→ correlation
matrixPowers PCA
🎯
The Through-Line

Covariance multiplies how far two variables stray from their means and averages it — the sign reveals whether they move together or apart. Its magnitude is unit-bound and unbounded, so we standardize it into correlation for comparison. As a matrix, it's the raw material of PCA and portfolio risk.

📚
Where To Go Next

You've now seen the pair: covariance and its standardized twin, correlation. Next, watch the covariance matrix come alive inside PCA, and step up to regression, which models the exact line two variables trace out.

↗️ End of tutorial · Press to review, or click Restart