Foundations of Data Science slides 📂 Introduction · 3 of 10 35 min read

What Is Variance? Measuring the Spread Around the Mean

The average is only half the story. Variance measures how far values scatter from the mean — the number that separates "reliable" from "all over the place." This tutorial builds it step by step: deviations, squaring, and averaging (÷ n−1 for a sample), plus Bessel's correction, the units problem, variance vs standard deviation, properties, applications, and code — with animated diagrams.

📐

What Is Variance?

The average is only half the story. Variance measures how far values scatter from the mean — the single number that separates "reliable" from "all over the place."
Spread Squared Deviations n − 1 Risk

Press Next → or use ← → arrow keys

Section 01

The Intuition — Two Delivery Apps

Same average, wildly different experience
Two food-delivery apps both advertise a 30-minute average. App A ("Steady") always lands between 28 and 32 minutes — you can set your watch by it. App B ("Erratic") also averages 30, but any given order might arrive in 8 minutes or take a punishing 70. Same mean, opposite experience.

The average can't tell them apart — but variance can. Variance measures how much each value strays from the mean, capturing exactly the consistency that the average hides.
💡
Variance = Spread Around The Mean

Low variance means values cluster tightly around the mean (App A). High variance means they scatter far and wide (App B). It's the first and most important measure of how consistent a dataset is.

Section 01 · Diagram

The Average Hides This Difference

mean = 30 min (both) App A · Steady variance ≈ 1.33 min² App B · Erratic variance ≈ 667 min²
🎯
A 500× Gap The Mean Can't See

Both rows centre on the same purple mean line, yet App B's variance is about 500× larger than App A's. That single number is what tells you App A is dependable and App B is a gamble — the whole reason variance exists.

Section 02 · Diagram

How It's Built — Deviations, Then Squares

mean −2 −1 0 +1 +2 4 1 1 4 square each deviation → 4+1+0+1+4 = 10 · then average it
🧮
Three Steps

1. Measure each point's deviation from the mean (blue below, amber above). 2. Square each deviation — the purple squares, whose areas are 4, 1, 0, 1, 4. 3. Average those squared areas. That average is the variance.

Section 02 · Why Square?

Why Square The Deviations?

Cancels The Signs
Deviations above and below the mean always sum to zero. Squaring makes every term positive, so they stop cancelling out.
📈
Punishes Big Misses
A deviation of 4 contributes 16, but a deviation of 2 only contributes 4. Squaring weights large departures far more heavily.
🧠
Clean Algebra
Squared terms are smooth and differentiable, so variance slots neatly into calculus-based methods across statistics and ML.
🤔
Why Not Just Use Absolute Values?

You could average the absolute deviations (that's the "mean absolute deviation"), and it's also valid. But the absolute-value function has a sharp corner at zero that's awkward for calculus, while the squared version is smooth everywhere — which is exactly why variance became the standard the whole field is built on.

Section 03 · Formulas

Population vs Sample Variance

Population variance
σ² = Σ(xᵢ − μ)² / N
Every squared deviation from the population mean μ, averaged over all N members.
Sample variance
s² = Σ(xᵢ − x̄)² / (n − 1)
Same numerator, but divided by (n − 1) — Bessel's correction — for an unbiased estimate.
🔤
Only The Divisor Changes

The recipe is identical — deviations, squared, summed. What differs is the bottom of the fraction: divide by N when you have the whole population, and by n − 1 when you have a sample. Getting this one choice right is the most common variance decision in practice.

Section 03 · Diagram

Why n − 1? Bessel's Correction

true population (wide spread) sample looks narrower population is truly this wide ÷(n−1) nudges the estimate up ↑
🎯
A Sample Underestimates The Spread

A handful of sampled points almost always looks tighter than the full population — they rarely capture the true extremes. Dividing by n − 1 instead of n makes the fraction slightly bigger, correcting that downward bias so is an honest estimate of σ².

Section 04 · Worked Example

Both Apps, Computed In Full

App A (Steady) · mean 30
29,31,30,28,32,30,29,31,30,30
Deviations: −1,1,0,−2,2,0,−1,1,0,0 → squares sum to 12.
App A variance
12 / 9 = 1.33 min²
Tiny spread — every delivery lands within a couple of minutes of 30.
App B (Erratic) · mean 30
10,55,8,62,15,48,20,70,12,0
Deviations squared sum to 6006 across the 10 orders.
App B variance
6006 / 9 = 667.33 min²
Enormous spread — the average of 30 tells you almost nothing useful.
📊
Identical Means, 500× Apart On Variance

Both apps average exactly 30 minutes — but App A's variance is 1.33 and App B's is 667.33. That ~500× gap is the mathematical fingerprint of "dependable" vs "unpredictable," invisible to the mean alone.

Section 05 · The Units Problem

From Squared Units To Something Readable

Variance 1.33 min² (squared!) take the root Standard Deviation 1.15 min (real units)
⚠️
"Minutes Squared" Means Nothing To A Human

Because variance squares the deviations, its units are squared too — minutes², rupees², cm² — which nobody can picture. Taking the square root gives the standard deviation, back in the original units and instantly interpretable: "typically within ~1.15 minutes of 30."

Section 05 · Comparison

Variance vs Standard Deviation

AspectVariance (σ² / s²)Standard Deviation (σ / s)
DefinitionAverage squared deviationSquare root of variance
UnitsSquared (min², ₹²)Original (min, ₹)
Human-readable?NoYes
Best forMaths & ML operationsReporting & interpretation
Example1.33 min²1.15 min
🤝
Two Views Of One Idea

They're the same measurement in different clothes. Use variance inside formulas — it adds cleanly, powers PCA and ANOVA, and behaves well under calculus. Use standard deviation when you talk to humans, because it lives in the units they understand.

Section 06 · Properties

Four Properties Worth Memorizing

📌 How Variance Behaves
1Never negative. Squaring guarantees variance ≥ 0. It equals zero only when every value is identical.
2Sensitive to outliers. Because deviations are squared, one extreme value can inflate variance dramatically.
3Scales with the square of units. Multiply the data by 10 and the variance grows 100×.
4Adds for independent variables. Var(X + Y) = Var(X) + Var(Y) when X and Y are independent — the property that powers portfolio maths.
🚨
Outliers Hit Variance Hardest

A single mistaken data point — a delivery logged as 700 minutes instead of 70 — can dominate the entire variance because its deviation gets squared. Always screen for outliers before trusting a variance, and consider robust alternatives like the IQR when they're present.

Section 07 · Applications

Where Variance Runs The Show

💹
Finance & Risk
Variance of returns is volatility. Portfolio optimization minimizes variance for a target return — the core of modern portfolio theory.
🔦
PCA
Principal Component Analysis keeps the directions of maximum variance — variance is literally the signal it preserves.
🧪
ANOVA & A/B Tests
Analysis of Variance compares within-group vs between-group variance to decide whether groups genuinely differ.
⚖️
And The Bias–Variance Tradeoff

In machine learning, a model's error splits into bias (too simple) and variance (too sensitive to the training data). Managing that variance — through regularization, more data, or ensembling — is one of the central skills of building models that generalize.

Section 08 · Code

Computing Variance — Mind The ddof

import numpy as np
import statistics

data = [29, 31, 30, 28, 32, 30, 29, 31, 30, 30]

# ── NumPy ── mind the divisor!
np.var(data, ddof=1)   # 1.33 — SAMPLE variance (÷ n−1)  ← usually what you want
np.var(data, ddof=0)   # 1.20 — POPULATION variance (÷ n) ← NumPy's default!

# ── statistics module (clearer names) ──
statistics.variance(data)    # sample (÷ n−1)
statistics.pvariance(data)   # population (÷ n)
🛑
The #1 NumPy Trap

np.var() defaults to ddof=0 — the population variance. In almost all data-science work you're holding a sample, so you want ddof=1. Forgetting this silently gives you the wrong (too-small) number, and it's one of the most common statistics bugs in real code.

Section 09 · Golden Rules

Six Rules For Working With Variance

🏅 Variance, Distilled
1Report spread with the centre. A mean without a variance (or std) is only half the truth.
2Use sample variance (n−1) by default — only use n when you truly have the whole population.
3Set ddof=1 in NumPy. Its default of ddof=0 is the population formula.
4Report standard deviation to people, keep variance for the maths — mind those squared units.
5Screen for outliers first. Squaring means one extreme point can dominate the whole variance.
6Never report a negative variance. If you get one, it's a bug — variance is always ≥ 0.
Wrap-Up

You Now Understand Variance

Σ(x−μ)²Squared deviations
÷NPopulation
÷(n−1)Sample
≥ 0Never negative
→ std deviation
ddof=1NumPy sample
🎯
The Through-Line

Variance is the average squared distance from the mean — the number that turns "average 30 minutes" into "dependable" or "a gamble." Square the deviations, average them (÷ n−1 for a sample), and take the root for a human-readable standard deviation. It's the foundation of risk, PCA, ANOVA, and the bias–variance tradeoff.

📚
Where To Go Next

Pair variance with its readable twin standard deviation, then explore covariance and correlation (how two variables vary together), and see variance in action inside PCA and the bias–variance tradeoff.

📐 End of tutorial · Press to review, or click Restart