EVERYTHING AIAI engineering, made visual
0/23 complete
LESSON 15 · MATHEMATICS × AI · BUILD

Real improvement,
or just noise?

0.87 versus 0.89 on 500 test examples is about one standard error — the kind of gap pure luck produces a third of the time. This lesson is the toolkit that tells the difference: standard error, confidence intervals, p-values, and effect size.

60 MIN · 8 CHAPTERSPREREQ · LESSONS 06 & 07
FIG. 15 / CHANCE, MADE VISIBLE
MEAN 33.0 · MEDIAN 32.0σ/√n · n = 4 → SE 6.25
LESSON 15TYPE · BUILD~60 MINPREREQ · LESSONS 06 & 07ORIGINAL LESSON ↗
THE 60-SECOND VERSIONThen show me the middle ↓
01 / CENTRE AND SPREAD

Where is the middle? How wide is the spread?

The mean is the balance point, the median the halfway mark when sorted, the mode the most common value. Variance is the average squared distance from the mean; its square root, the standard deviation, is back in the data's units. When mean and median diverge, the data is skewed.

mean 28.6 · median 23 · s ≈ 24.2
02 / A SAMPLE IS AN ESTIMATE

Averages wobble, and the wobble shrinks like 1/√n.

Every test-set number is one sample from a noisy process. Its standard error is σ/√n: quadruple the data and the error halves. The central limit theorem says sample means become bell-shaped as n grows, which is why we can put an error bar on almost anything.

SE = σ / √n · 4× data → ½ error
03 / SIGNIFICANT ≠ WORTH IT

A p-value says “surprising”, an effect size says “big”.

The p-value is P(data this extreme | the null is true) — it is not the probability the null is true. With enough data, a 0.03% gain becomes significant. Cohen's d measures the size of the difference independently of n, and Bonferroni keeps multiple tests honest.

p < 0.05 but d = 0.015 → ship nothing
MENTAL MODEL IN ONE SENTENCE

Every number you measure on a test set is a sample from a noisy process, so statistics asks one question over and over: could this difference have happened by chance? Standard errors, confidence intervals, p-values and the bootstrap are all different ways of measuring how big “chance” is.

By the end you will be able to describe a dataset with the right summary, read a p-value and a confidence interval without misquoting them, correct for multiple comparisons, and decide whether a model difference is real, large, or neither.

DESCRIBING DATA

Where is the middle?
How wide is the spread?

Before you model anything, compress the data into a few numbers that capture its shape. The two questions are always the same: where is the centre, and how far does the data wander from it?

The mean adds every value and divides by the count; it is the point where the data balances. The median sorts the data and takes the middle value. The mode is the most frequent value, which is useful for categories and rarely informative for continuous measurements. Watch them disagree on [1, 2, 3, 4, 1000]: the mean is (1+2+3+4+1000)/5 = 202, but the median is 3. The one rich value dragged the mean; the median did not move.

Spread answers the second question. Variance is the average squared distance from the mean, s² = Σ(xᵢ − x̄)² / (n − 1); the standard deviation is its square root, which returns the number to the units of the data. Percentiles divide sorted data into 100 parts: P50 is the median, P95 and P99 are the tail. For a training loss, the mean is dragged around by a few hard examples, while percentile monitoring shows whether the tail is growing.

StatisticFormulaPlain EnglishRobust to outliers?
Meanx̄ = (1/n) · Σ xᵢThe balance point of the data.No — one huge value drags it
Medianmiddle value when sortedThe 50th percentile: half the data on each side.Yes — it only counts positions
Variances² = (1/(n−1)) · Σ (xᵢ − x̄)²The average squared distance from the mean.No — the square magnifies extremes
Standard deviations = √(s²)The typical distance from the mean, in the data's units.No — same reason as variance
QuartilesQ1, Q3The 25th and 75th percentiles: the middle half lives between them.Mostly — quartiles move slowly
IQRQ3 − Q1The width of the middle 50%, used by box plots and outlier rules.Yes — built for it

Second worked example, a left-skewed loss. Five training examples lose [0.2, 3.0, 3.0, 3.2, 3.4]. The mean is 12.8 / 5 = 2.56, below the median 3.0, because one unusually easy example pulls the mean down. Incomes do the opposite: a few billionaires push the mean above the median. The direction of the gap tells you which way the data is skewed.

The five-number summary, live

Drag a dot to the right and watch the mean chase it while the median barely moves. The box is the middle 50% (Q1 to Q3), and the 1.5 × IQR rule marks outliers.

mean 37.0 median 36.0 sd 12.5 Q1 27.0 Q3 47.0 IQR 20.0 outlier rule: outside [-3.0, 77.0] no outliers right now since the start: mean moved +0.0, median moved +0.0

The mean is the balance point, so one far-away weight tips it. The median only counts positions, so it stays where the middle is. That is why latency dashboards quote P50 and P99, not the mean.

Derivation: Bessel's correction, or why sample variance divides by n − 1

Population variance uses the true mean μ. You only have the sample mean x̄, which sits closer to your particular samples than μ does, so the deviations from x̄ come out systematically a little too small. How much too small?

  1. Add and subtract μ inside each deviation: xᵢ − x̄ = (xᵢ − μ) − (x̄ − μ).
  2. Square and sum. The cross term is −2(x̄ − μ)Σ(xᵢ − μ) = −2(x̄ − μ)·n(x̄ − μ), because Σ(xᵢ − μ) = n x̄ − nμ. So Σ(xᵢ − x̄)² = Σ(xᵢ − μ)² − n(x̄ − μ)².
  3. Take expectations. E[Σ(xᵢ − μ)²] = nσ², and the variance of the sample mean is σ²/n, so the second term contributes n·σ²/n = σ².
  4. Therefore E[Σ(xᵢ − x̄)²] = nσ² − σ² = (n − 1)σ². Dividing by n − 1 makes the estimate unbiased.
Numeric check — data {2, 4, 4, 4, 6}: x̄ = 20/5 = 4 squared deviations: (−2)², 0², 0², 0², 2² → Σ = 8 population variance: 8/5 = 1.60 sd = 1.265 sample variance: 8/4 = 2.00 sd = 1.414 with n = 5 the correction is 25% — invisible with thousands of samples, decisive with five cross-validation folds.
Quick check

For [1, 2, 3, 4, 1000], which pair of statistics best tells you what a typical value looks like?

COVARIANCE & CORRELATION

Do they move together?
Careful what you conclude.

Covariance says two variables rise and fall together; correlation rescales it so that every pair is comparable on a −1 to +1 ruler. Both are about linear co-movement — and neither one is causation.

The covariance between X and Y is Cov(X,Y) = (1/(n−1)) Σ (xᵢ − x̄)(yᵢ − ȳ): multiply how far each point sits from its own mean, and average. Positive means the variables rise together, negative means one rises as the other falls, zero means no linear co-movement. For features x = [1, 2, 3] and y = [2, 4, 6], the centred products are 2, 0, 2, so the sample covariance is 4/2 = 2. The covariance matrix of the two features is [[1, 2], [2, 4]] — variances on the diagonal, covariance off it. Its determinant is 4 − 4 = 0, exactly because the two features are the same information rescaled.

Covariance has units, so it cannot tell you whether 2 is strong or weak. Dividing by both standard deviations gives Pearson’s r = Cov(X,Y) / (sₓ·s_y), which lives in [−1, 1]: +1 is a perfect increasing line, −1 a perfect decreasing line, 0 no line. Spearman’s ρ is the same computation after replacing every value with its rank, so it catches any monotonic relationship and shrugs off outliers. Use Pearson for continuous, roughly normal data with no extreme points; use Spearman for rankings, skew, or outliers.

linear: r ≈ 1.00curved: r ≈ 0.00flipped: r ≈ -1.00
Pearson measures the straight-line part only. The middle panel has a perfect relationship shaped like a U — and a correlation of exactly 0. Zero correlation rules out a line, not a relationship.
Derivation: Pearson's r is the cosine of the centred data vectors

Centre both variables: uᵢ = xᵢ − x̄ and vᵢ = yᵢ − ȳ. Then the correlation is a dot product divided by two lengths:

r = Σ uᵢvᵢ / (√(Σuᵢ²) · √(Σvᵢ²)) = (u · v) / (‖u‖ ‖v‖) = cos θ A cosine always lives in [−1, 1] — that is why r does. r = 1: the centred vectors point the same way (y increases with x). r = −1: opposite directions. r = 0: perpendicular. Numeric check — x = [1, 2, 3], y = [1, 2, 2]: means 2 and 5/3, so u = [−1, 0, 1], v = [−2/3, 1/3, 1/3] u · v = 2/3 + 0 + 1/3 = 1 ‖u‖ = √2 ≈ 1.414 ‖v‖ = √(6/9) ≈ 0.816 r = 1 / 1.1547 ≈ 0.866 (not 1: y is not perfectly linear in x)

One extreme point can swing the direction of u or v and therefore r, which is exactly why Pearson is fragile and Spearman replaces values with ranks first.

SAMPLING & STANDARD ERROR

Every number is a sample.
How much does it wobble?

Model A scores 0.87 and Model B scores 0.89 — but on a different test set the order could flip. The sampling distribution tells you how much a statistic would move if you ran the whole experiment again.

Your test set is one sample from a noisy process. If you had drawn a different 500 examples, you would have measured a different accuracy. The sampling distribution of a statistic is the distribution of the values it takes across all those hypothetical repetitions. Its standard deviation has a special name: the standard error. For a sample mean, SE = σ/√n — the population spread divided by the square root of the sample size.

The square root is the part everyone underestimates. If σ = 15 and you test 100 examples, SE = 1.5. Test 400 examples and SE = 0.75. To get SE down to 0.3 you need n = (15/0.3)² = 2,500 examples. Ten times the precision costs a hundred times the data.

The central limit theorem (CLT) is why this works even when the data is not bell-shaped: as n grows, the distribution of the sample mean approaches a normal distribution centered on the true mean, whatever shape the population had. A rule of thumb is n ≥ 30 for mildly skewed data, n ≥ 100 for heavily skewed data. What the CLT does not promise: it makes the mean normal, not the data; it fails for infinite-variance tails (Cauchy) and for dependent observations such as sentences from one document or images from one patient.

The sampling distribution shrinks

Each of the 500 experiments draws n values from the same skewed population and records its mean. The histogram is the sampling distribution; the orange curve is the normal prediction centered on the true mean with width σ/√n.

population: mean μ = 57.09 sd σ = 14.99 sample size n = 16 predicted SE = σ/√n = 3.748 observed sd of the 500 means = 3.592 ratio observed / predicted = 0.96 (should hover near 1) increase n: 4× the data → SE ÷ 2 to halve the error again: n 16 → 64

The individual values are skewed, but the means are not — that is the central limit theorem. Watch the blue histogram: as n grows it becomes a narrower, taller bell around the true mean.

Derivation: where σ/√n comes from
  1. The sample mean is an average: X̄ = (1/n)(X₁ + X₂ + … + Xₙ).
  2. Scaling a random variable scales its variance by the square: Var(aX) = a²Var(X), so Var(X̄) = (1/n²)·Var(ΣXᵢ).
  3. Independent observations have variances that add: Var(ΣXᵢ) = nσ².
  4. Therefore Var(X̄) = nσ²/n² = σ²/n, and the standard error is its square root: SE = σ/√n.
Numeric check — population σ = 15: n = 100: Var(X̄) = 225/100 = 2.25 SE = 1.50 n = 400: Var(X̄) = 225/400 = 0.5625 SE = 0.75 (4× data, ½ error) n = 900: SE = 15/30 = 0.50 to hit SE = 0.30: n = (15/0.30)² = 2,500 same arithmetic for an accuracy near 0.88: σ = √(0.88 × 0.12) ≈ 0.325, n = 500 → SE ≈ 0.0145
CONFIDENCE INTERVALS

One number is a guess.
Add an error bar.

“Accuracy 0.88” hides how much data it came from. A confidence interval turns the guess into a range, and the width of that range is the honesty — or the lack of it — in the row.

For a mean, the 95% confidence interval is x̄ ± 1.96 · s/√n. The piece s/√n is the standard error; the 1.96 is how many standard errors you must step out to cover the middle 95% of a normal distribution. For the opening story — accuracy 0.88 on 500 examples — SE = √(0.88 × 0.12 / 500) ≈ 0.0145, so the interval is about 0.88 ± 0.028, or [0.852, 0.908].

Compare two models on the same 500 examples. Each estimate has SE ≈ 0.0145, and the difference has SE_diff = √(0.0145² + 0.0145²) ≈ 0.021. A 0.02 gap is about one standard error, so the 95% interval for the difference runs from roughly 0.02 − 0.04 = −0.02 to 0.02 + 0.04 = +0.06 — it contains zero. “One standard error” happens by luck about a third of the time, which is exactly why the less accurate model can win a small test set.

What the 95% means. If you repeated the experiment forever, 95% of the intervals you built this way would contain the true value. It is a property of the recipe, not of any single interval. The width measures precision; it says nothing about accuracy. A biased sample gives a tight interval around the wrong number — precise and wrong.

Does the 95% interval actually cover?

Each row is one whole experiment: draw n samples, compute the confidence interval. Green bars contain the true mean, red bars miss. The guarantee is about the procedure — not about any single bar.

covered 98 / 100 · expected 95 / 100 missed 2 intervals z = 1.960 for 95% t = 2.042 with df = n − 1 = 29 (fat tails for small n, approaching z) run this whole page again and again: the long-run coverage is 95%, but any single batch wobbles around it.

A common misreading: “this specific interval has a 95% chance of containing μ”. Wrong — this interval either does or does not. The 95% describes the recipe: repeat it forever, and 95% of the intervals come out right.

Derivation: where 1.96 comes from, and how to read any z
  1. The CLT says the sample mean is approximately normal: X̄ ~ Normal(μ, σ²/n).
  2. Standardize: Z = (X̄ − μ)/(σ/√n) has a standard normal distribution.
  3. For a standard normal, 95% of the mass lies within 1.96 standard deviations of zero: P(−1.96 ≤ Z ≤ 1.96) = 0.95.
  4. Rearranging the inequality gives P(X̄ − 1.96·SE ≤ μ ≤ X̄ + 1.96·SE) = 0.95 — an interval that covers μ with probability 0.95 over repetitions.
Common multipliers (two-sided): 90% → 1.645 95% → 1.96 99% → 2.576 Numeric check — accuracy 0.88, n = 500: s = √(0.88 · 0.12) ≈ 0.325 SE = 0.325/√500 ≈ 0.0145 half-width = 1.96 × 0.0145 ≈ 0.0285 95% CI = [0.8515, 0.9085] four times the data → half the width; to halve it again, 4× again. For small n, s is itself uncertain, so use Student's t (slightly fatter tails) instead of 1.96 — the coverage lab shows the fix.
Derivation: the bootstrap — an error bar with no formula

The t-test needs a formula for the standard error and a normality assumption. The bootstrap needs neither. Your sample is your best estimate of the population, so resampling from it with replacement imitates drawing fresh data from the world.

  1. Draw n values from your n data points with replacement (some appear twice, some not at all).
  2. Compute the statistic on this resample.
  3. Repeat B ≈ 1,000–10,000 times. The spread of those B values is the sampling distribution.
  4. A 95% percentile interval runs from the 2.5th to the 97.5th percentile of the B values.
Why resampling works: each point is left out with probability (1 − 1/n)ⁿ → 1/e ≈ 0.368, so a bootstrap sample keeps about 63% of the distinct points and duplicates the rest — the same kind of variation a fresh sample would show. Model comparison: resample test indices, compute metric_A and metric_B, store B − A. If the 95% interval for the difference excludes 0, the difference is significant — for AUC, F1, the median, anything, with no distributional assumptions.

The price: the bootstrap trusts your sample. If the sample is biased, every resample inherits the bias — a narrow interval around the wrong answer, with no warning.

Quick check

A model’s 95% confidence interval for accuracy is [0.83, 0.89]. Which statement is correct?

HYPOTHESIS TESTING

How surprising is the data
if nothing is going on?

A hypothesis test is a what-if machine: assume there is no effect, then ask how weird your measurement would look. The p-value is the answer to exactly that question — and to no other.

Start with the null hypothesis H₀ — the boring default, usually “no difference”. For two models that is “A and B have the same accuracy”. The alternative H₁ is what you are trying to find evidence for, such as “B is better”. Then measure how far the data landed from the null world.

p-value = P(observing data at least as extreme as ours | H₀ is true) It is the probability of the DATA, given the null — not the probability of the null, given the data.

If p < α (usually 0.05), the data is unusual enough under H₀ that we reject it and call the result statistically significant. Otherwise we fail to reject H₀ — which is not the same as proving it; it often means the experiment was too small. The test statistic turns the comparison into a ruler: t = (x̄ − μ₀)/(s/√n) asks “how many standard errors away from the null is this?”, and Welch’s two-sample version t = (x̄₁ − x̄₂)/√(s₁²/n₁ + s₂²/n₂) does the same for two groups without assuming equal variances. When both models are evaluated on the same folds, subtract fold by fold and run a one-sample test on the differences — pairing removes the fold-to-fold difficulty and buys a lot of power. With only 5–10 folds the normality assumption is shaky, so the rank-based Wilcoxon signed-rank test is often the safer paired choice (and the bootstrap works there too).

Where does the p-value come from?

Group B is shifted by the effect you choose. The null distribution is built by shuffling the 16 labels 2,000 times — that is what the world looks like when H₀ is true and there is no difference. The p-value is the share of shuffles at least as extreme as what we observed.

group A: 8 values, mean 71.59 group B: 8 values, mean 73.93 observed difference B − A = 2.34 shuffles at least this extreme: 165 / 2000 p ≈ 0.083 p ≥ 0.05: not unusual if H₀ were true — no evidence here. p = P(data this extreme | H₀ true), not P(H₀ true).

Slide the effect to 0: both groups come from the same world, and the observed difference lands in the middle of the null distribution, so p is large. That is the whole logic of a significance test.

Derivation: the t statistic, with two numeric checks
One sample: t = (x̄ − μ₀) / (s/√n) df = n − 1 Two samples: t = (x̄₁ − x̄₂) / √(s₁²/n₁ + s₂²/n₂) Welch, df ≈ messy Paired: t = d̄ / (s_d/√n) on the differences dᵢ = xᵢ − yᵢ Counts: χ² = Σ (observed − expected)² / expected

Check 1 — not significant. A sensor claims mean 100. You measure x̄ = 103 on n = 16 readings with s = 8:

t = (103 − 100) / (8/√16) = 3 / 2 = 1.50 |t| = 1.50 is inside the |t| ≈ 2 “p < 0.05” zone, so not significant. Two-sided p ≈ 0.15 — data this far off happens 15% of the time by luck alone.

Check 2 — same gap, different noise. Two models on 100 examples each, both with s = 0.05, means 0.87 and 0.89:

t = (0.89 − 0.87) / √(0.05²/100 + 0.05²/100) = 0.02 / 0.00707 ≈ 2.83 → p ≈ 0.005 significant The 0.02 gap from the opening story is not inherently “noise” or “real” — it depends on the noise in the measurement. Smaller variance or more examples can turn the same gap significant.

Counts, for completeness. A model produces 120 positive and 80 negative outputs where 100/100 was expected: χ² = 20²/100 + 20²/100 = 8 with 1 degree of freedom, so p < 0.005 — the output distribution is not what was expected.

The permutation flavour (what the lab runs) skips the formula: pool the two groups, shuffle the labels 2,000 times, and count how often the difference is at least as extreme.

observed difference = 4.2 37 of 2,000 shuffles are at least that extreme p ≈ (37 + 1)/(2,000 + 1) ≈ 0.019 “If there were no effect, a gap this large would appear about 2% of the time.” That is the p-value, said in plain English.
Quick check

A model comparison reports p = 0.2. What does that mean?

MANY COMPARISONS

Test enough things,
and one will look brilliant.

A 5% false-positive rate sounds safe until you run twenty experiments. This is the trap behind hyperparameter sweeps, multi-metric evaluations and leaderboard wins.

Under H₀, each test falsely rejects with probability α = 0.05. Run m = 20 independent tests and the chance that at least one of them fires is 1 − (1 − 0.05)²⁰ = 1 − 0.358 ≈ 0.64 — a 64% chance of a bogus “significant” result, and an expected one false positive per batch even though nothing is real. The more configurations you sweep, the more of your “wins” are arithmetic rather than signal.

The Bonferroni correction is the blunt fix: test each hypothesis at α/m instead of α. For 20 tests that is 0.05/20 = 0.0025, and the family-wise false-positive rate returns to 1 − (1 − 0.0025)²⁰ ≈ 0.049. It is conservative — real effects also have to clear the higher bar — but it is simple and safe when tests are independent. With hundreds of tests, the Benjamini–Hochberg false-discovery-rate procedure is a less punishing alternative. What no correction can fix: leakage, a biased split, or an experiment designed to find something.

Twenty tests, one truth: nothing is going on

Each card is one independent A/B test of a change that does not work in the real world; its p-value is uniform noise. At α = 0.05 you expect one “winner” per batch. Bonferroni demands p < 0.05/20 = 0.0025, and the winners almost vanish.

T01
0.9153
T02
0.4876
T03
0.9048
T04
0.0945
T05
0.0231
α only
T06
0.5973
T07
0.9442
T08
0.1896
T09
0.1388
T10
0.7156
T11
0.5808
T12
0.9891
T13
0.6571
T14
0.9285
T15
0.3043
T16
0.4526
T17
0.3775
T18
0.4210
T19
0.5943
T20
0.9611

orange = p < 0.05 · green = p < 0.0025 (survives Bonferroni) · ★ = the one test with a real effect (when enabled)

this batch: 1 of 20 have p < 0.05 under Bonferroni: 0 of 20 have p < 0.0025 P(at least one false positive) = 1 − (1 − α)^m = 1 − 0.95^20 ≈ 0.642 expected false positives per batch = m·α = 1.0 over 1 batch (20 tests): false positives at α = 0.05 1 false positives at 0.05/20 = 0.0025 0

Turn on the real effect: it survives Bonferroni because its p-value is genuinely tiny. Bonferroni does not kill real effects — it kills small ones that cannot clear the higher bar. Run batches until a false positive shows up under the correction, and notice how rare that is.

Derivation: the family-wise false-positive rate
  1. Under H₀ a single test correctly does nothing with probability 1 − α.
  2. Independent tests multiply: the chance that none of m tests fires is (1 − α)ᵐ.
  3. So the chance of at least one false positive is 1 − (1 − α)ᵐ.
  4. Bonferroni asks each test to clear α/m, which makes the family-wise rate approximately α again.
m = 20, α = 0.05: expected false positives = m·α = 1 P(at least one) = 1 − 0.95²⁰ ≈ 0.641 Bonferroni: 1 − (1 − 0.0025)²⁰ ≈ 0.049 m = 50 hyperparameter configs, α = 0.05: P(at least one) = 1 − 0.95⁵⁰ ≈ 0.923 Bonferroni threshold: 0.05/50 = 0.001 Winner's curse: the best of m noisy numbers is biased upward, so the “best config” will regress when you re-test it on a fresh validation set.
REAL VS. NOISE

Significant is not the same
as worth shipping.

The p-value answers “is it real?”. The effect size answers “is it big enough to matter?”. A model comparison needs both answers, plus a test set neither model has ever seen.

With a million test samples, a 0.03% accuracy gap gets p = 0.001: statistically significant and rarely worth the engineering cost of a deployment. Effect size measures the difference in units of spread, independent of sample size. Cohen’s d = (mean₁ − mean₂)/pooled standard deviation, with familiar landmarks: d = 0.2 small, 0.5 medium, 0.8 large. Two worked examples show why the units matter:

latency: 250 ms vs 230 ms, pooled sd 40 ms d = 20/40 = 0.50 → medium. Worth investigating. accuracy: 0.9234 vs 0.9237, pooled sd 0.02 d = 0.0003/0.02 = 0.015 → negligible. Not worth shipping, however tiny the p-value.

Model A/B testing is not web A/B testing. Both models must be scored on the same test set; accuracy alone is never enough (add precision, recall, F1, latency, fairness); variance comes from cross-validation or bootstrap rather than a single number; and any test set used during model selection is contaminated, so keep a final holdout. The procedure: pick the metric and α; run both models on the same k folds; collect paired differences; run a paired t-test (or bootstrap the difference); report the confidence interval and the effect size; then decide against a minimum useful gap that you wrote down beforehand.

Underpowered experiments are their own trap. To detect a gap δ with 80% power at α = 0.05 you need roughly n ≈ 15.68 · σ²/δ² examples per group. With σ = 0.05 and δ = 0.02 that is 15.68 × 0.0025/0.0004 ≈ 98 per group — unpowered comparisons produce “no significant difference” that only means “not enough data”.

The A/B verdict calculator

Two models, one shared test set. The p-value asks whether the gap is real; Cohen’s d and the interval ask whether it is big enough to care about. Slide the test set down and watch a “winner” evaporate.

model A accuracy0.880
0.500.751.00
model B accuracy0.890
0.500.751.00
95% interval for the gap B − A
−6%0+6%
✗ not significant✗ not worth shipping
SE of the gap = √(pA(1−pA)/n + pB(1−pB)/n) = √(0.1056/500 + 0.0979/500) = 0.0202 z = gap/SE = 0.50 two-sided p ≈ 0.6201 95% CI = [-0.0295, 0.0495] Cohen's d = 0.031 → negligible effect n needed for 80% power at this gap: 15,955 per model ⚠ underpowered: you are below that, so a real gap can hide here

Statistical significance is about whether the interval crosses zero. Practical significance is about whether the whole interval clears the minimum useful gap. With 1,000,000 samples a 0.03% gain passes the first test easily — and fails the second. This is a design calculator, so it treats the true gap as the observed gap: the interval shows the luck of sampling around a known effect.

Derivation: effect size, power, and the two errors
  1. Type I error (false alarm): reject a true H₀. Rate = α, the significance level.
  2. Type II error (miss): fail to reject a false H₀. Rate = β. Power = 1 − β is the chance of catching a real effect.
  3. For a two-sided test at α = 0.05 and 80% power, the sample size per group for a difference δ is n ≈ 2(z₀.₉₇₅ + z₀.₈)²σ²/δ² ≈ 15.68 σ²/δ².
  4. Cohen’s d = δ / pooled sd removes the units and the sample size, so it is comparable across studies.
Check — σ = 0.05 (per-example accuracy sd), δ = 0.02: n ≈ 15.68 × 0.05² / 0.02² = 15.68 × 0.0025 / 0.0004 ≈ 98 per group (Chapter 05 saw 100 per group clear p ≈ 0.005 — consistent.) Shrink the gap to δ = 0.005: n ≈ 15.68 × 0.0025 / 0.000025 ≈ 1,568 per group A gap 4× smaller costs 16× the data — the square root again.
MistakeWhy it bites
Testing on the training setGuarantees an optimistic number.
No confidence intervalsA single accuracy is unverifiable.
Ignoring multiple comparisonsThe best of 50 configs is noise-inflated.
Accuracy on imbalanced data99% negatives means a do-nothing model scores 99%.
Leakage across splitsNormalizing before splitting, or using the future to predict the past.
Assuming independenceCorrelated observations shrink the effective n.
p-hackingTrying subsets and tests until p < 0.05.
Quick check

Model B beats Model A by 0.01% accuracy with p = 0.001 on five million examples. What is the right conclusion?

CHECK YOURSELF

Five questions.
Then the terms worth keeping.

Answer before you look. The p-value and multiple-comparison questions are exactly the ones that decide real model comparisons.

0 / 5 answered · 0 correct

01What does a p-value of 0.03 mean in a hypothesis test?

02Why do you divide by (n − 1) instead of n when computing sample variance?

03You test 20 different model configurations at alpha = 0.05. What is the approximate probability of at least one false positive?

04Model A scores 0.9234 and Model B scores 0.9237 on 1 million test samples with p-value = 0.001. What should you conclude?

05What advantage does bootstrap have over the paired t-test for comparing two ML models?

Key terms, demystified

Click a card to swap the lazy description for what it actually means.

Exercises from the lesson

Four short problems with fully worked answers. Try first; the solution is one click away.

  1. Take ten numbers: 12, 15, 15, 18, 22, 24, 24, 30, 31, 95. Compute the mean, the median, the sample standard deviation (divide by n − 1), Q1, Q3, the IQR, and apply the 1.5 × IQR rule. Which value is an outlier, and how much does removing it move the mean versus the median?
    Show one worked answer

    Sum = 286, so mean = 286/10 = 28.6. Sorted, the middle two are 22 and 24, so median = 23. Squared deviations sum to 5260.4; sample variance = 5260.4/9 ≈ 584.5, so s ≈ 24.2. Q1 = 15 (middle of the lower half), Q3 = 30 (middle of the upper half), IQR = 15. The fences are 15 − 22.5 = −7.5 and 30 + 22.5 = 52.5, so 95 is an outlier. Remove it: the mean becomes 191/9 ≈ 21.2, a drop of 7.4, while the median becomes 22, a move of just 1.

  2. Use x = 1, 2, 3, 4 and y = 1, 8, 27, 64 (that is y = x³). Compute Pearson's r and Spearman's rho. Explain the gap between them.
    Show one worked answer

    Means: x̄ = 2.5, ȳ = 25. Centred vectors u = [−1.5, −0.5, 0.5, 1.5] and v = [−24, −17, 2, 39]. Σuv = 36 + 8.5 + 1 + 58.5 = 104, Σu² = 5, Σv² = 2390, so r = 104 / √(5 × 2390) = 104 / 109.3 ≈ 0.951. The ranks of x are 1, 2, 3, 4 and the ranks of y are also 1, 2, 3, 4, so Spearman is Pearson on identical rank lists: rho = 1 exactly. The curve is perfectly monotonic; Pearson only credits the linear part, which is why it reads 0.95 instead of 1.

  3. Simulate 20 A/B tests where the truth is “no difference” at α = 0.05. What is the probability of at least one false positive, and what does the Bonferroni correction change? Then explain what a p-value of 0.03 does and does not mean.
    Show one worked answer

    P(at least one false positive) = 1 − (1 − 0.05)^20 = 1 − 0.95^20 ≈ 0.64, so about a 64% chance — and an expected one false positive per batch of 20. Bonferroni tests each hypothesis at 0.05/20 = 0.0025, which brings the family-wise rate back to 1 − 0.9975^20 ≈ 0.049 ≈ 5%. The cost: a real effect needs p < 0.0025 to survive, so small true effects are missed. A p-value of 0.03 means that if H₀ were true, data at least this extreme would appear 3% of the time. It is NOT the probability that H₀ is true (3%), NOT the probability the result is a fluke, and NOT evidence that the effect is large or worth deploying.

  4. Bootstrap a 95% interval for the median of the tiny dataset [2, 4, 9] by enumerating all 27 resamples with replacement. Why is the interval so wide?
    Show one worked answer

    There are 3³ = 27 ordered resamples, which collapse into 10 multisets. Counting weights: median 2 in 1 + 3 + 3 = 7 samples (25.9%), median 4 in 1 + 3 + 3 + 6 = 13 samples (48.1%), median 9 in 1 + 3 + 3 = 7 samples (25.9%). The 2.5th and 97.5th percentiles both fall inside the extreme categories, so the 95% interval is [2, 9] — nearly the whole range of the data. The bootstrap is not broken; with n = 3 the sample genuinely carries almost no information about the median, and the interval says so honestly instead of pretending otherwise.

Terms this lesson borrows from later lessons (or outside)

You do not need to master these here. Each one gets a proper treatment in its own lesson; the one-line meaning is enough to keep reading. Orange dotted underlines in the prose point back to this list.

  • outlierA data point far from the rest. The median and the IQR are built to survive one; the mean and the standard deviation are not.
  • hyperparameterA setting you choose before training (learning rate, batch size, β) rather than a weight the model learns. (Lesson 08)
  • eigenvalue / eigenvectorThe directions a matrix only stretches, and by how much. PCA is the eigendecomposition of the covariance matrix. (Lesson 10)
  • mutual informationA measure of any statistical dependence, curved or not. It catches relationships where correlation is exactly 0. (Lesson 09)
  • credible intervalThe Bayesian cousin of the confidence interval: a range with a 95% posterior probability of holding the parameter. A confidence interval is NOT that. (Lesson 07)
  • AUC / F1Classification metrics that accuracy alone hides (especially on imbalanced data). Bootstrap works on both without a formula. (outside these lessons)
KEEP GOING

A picture is a start.
Practice is the rest.

This lesson is a port of an open course. Everything here traces back to it — and the next step is running the code yourself.

Lesson text adapted from AI Engineering from Scratch (Phase 01, Lesson 15) and the Math Foundations Notebook reference build. Interactive figures, the second worked examples, the correlation gallery, the coverage and permutation labs, and the multiple-comparison and A/B calculators are original to this page. Every lab runs in your browser.