AI from scratch · Probability 4 · Stage 3: classic ML and judging models
Confidence and tests: A/B testing
Your new model scores 85% on the test set; the old one scored 82%. Ship it? Maybe the test set happened to suit it. Every number measured on a sample wobbles, and this lesson is about how much: confidence intervals that say how far off an estimate might be, tests that ask whether a difference is real or luck, and the sample sizes that make the answer trustworthy. These are the tools behind every A/B test in production and every "B beats A" claim in a paper.
1. A confidence interval, and what "95%" means
Estimate a mean from \(n\) data points and the result wobbles from sample to sample, with spread \(\sigma / \sqrt n\) (Probability 3). So report a range: the 95% confidence interval
\[ \bar x \pm t \cdot \frac{s}{\sqrt n} \]
where \(s\) is the sample standard deviation and \(t\) is about 2 (exactly 1.96 for big samples; section 2 says why it's bigger for small ones). The term after \(\pm\) is the margin of error. Below, the computer takes 100 samples from a population whose true mean is the dashed line, and draws each sample's interval. Green ones caught the truth, red ones missed.
That's the honest meaning of 95%: the method catches the truth 95% of the time. Any single interval either contains the true mean or doesn't; you just don't know which. Bigger \(n\) makes intervals narrower, but the share that miss stays the same.
An interval is a net cast by a method: 95% of the nets catch the truth, and any single net either did or didn't.
A sample of 100 has mean 50 and standard deviation 10. What's the 95% margin of error (use 1.96)?
Which statement about a 95% interval [48.0, 52.0] is right?
- There's a 95% chance the true mean is between 48 and 52
- If we repeated the sampling many times, 95% of the intervals built this way would contain the true mean
- 95% of the data lies between 48 and 52
2. Small samples need the t-distribution
Tick use 1.96 instead of t with \(n = 5\): about one interval in eight misses instead of one in twenty. With few points, \(s\) itself is a shaky estimate of \(\sigma\), and sometimes it comes out too small, so the intervals need to be wider. The t-distribution (found by a Guinness brewer, W. S. Gosset, testing small batches of barley) is the bell curve with fatter tails that accounts for that. It has degrees of freedom \(n - 1\); by 30 or so it's nearly normal, and its 95% cutoff is close to 1.96.
With few points the spread itself is a guess, sometimes too small, so the net has to be wider.
| n | 3 | 5 | 10 | 30 | 100 | ∞ |
|---|---|---|---|---|---|---|
| 95% t value | 4.30 | 2.78 | 2.26 | 2.05 | 1.98 | 1.96 |
3. A test: could it be luck?
A hypothesis test asks one specific question. Start from the boring explanation, the null hypothesis \(H_0\): "there's no difference; any gap is sampling noise". Then compute the p-value: if \(H_0\) were true, how often would noise alone produce a gap at least as big as the one you saw? A small p-value (by convention below 0.05) means the gap would be surprising under the null, so you reject it.
Model A gets 82% of a test set right, model B 85%. Both were scored on the same examples. Change the test set size:
A p-value asks how surprising your gap would be if nothing were going on, not how likely it is that nothing is going on.
The math, step by step
Unpaired (two separate test sets): the difference of two proportions has standard error \(\sqrt{p_A(1 - p_A)/n + p_B(1 - p_B)/n}\); divide the gap by it to get \(z\), and the p-value is the area of both bell tails beyond \(\pm z\).
Paired (one test set, the right way for models): examples both models get right, or both wrong, say nothing about which is better. Only the disagreements count. Let \(b\) = A right and B wrong, \(c\) = A wrong and B right. Under \(H_0\) each disagreement is a coin flip, so (McNemar's test)
\[ z = \frac{c - b}{\sqrt{b + c}} \]
With 500 examples, 90% agreement means 50 disagreements; B's 3-point edge is 15 more of them going B's way: \(c = 32.5,\ b = 17.5\), \(z = 15/\sqrt{50} \approx 2.12\), \(p \approx 0.034\). The unpaired test on the same numbers gives \(z \approx 1.28\), \(p \approx 0.20\): it can't see past the shared noise.
What does \(p = 0.034\) mean here?
- If the two models were truly equally good, a gap this big in B's favor (or A's) would show up only about 3.4% of the time
- There's a 3.4% chance the models are equally good
- B is definitely 3 points better
4. Two ways to be wrong, and power
| no real difference | a real difference | |
|---|---|---|
| you say "B is better" | Type I error (false positive), probability \(\alpha\) | correct: probability = power |
| you say "can't tell" | correct | Type II error (false negative), probability \(\beta\) |
Chapter 12's confusion matrix, for decisions about models. The threshold \(\alpha = 0.05\) caps false positives. Power \(= 1 - \beta\) is the chance of detecting a real effect, and it depends on the effect's size, the noise, and \(n\). The blue bell is what the measured difference looks like with no real effect; the yellow one with a real effect. Everything right of the red line is "significant".
Before a test, you pick \(\alpha\), the smallest effect worth detecting (call it \(\delta\), delta), and the power you want (80% is the custom), and that fixes \(n\). For comparing two proportions at \(\alpha = 0.05\) and 80% power, a handy rule:
\[ n \text{ per group} \approx \frac{16\, p(1 - p)}{\delta^2} \]
Power is the chance of seeing an effect that's really there; it grows with the effect's size and with √n, so the sample size is planned before the test starts.
Why the 16 in the sample-size rule?
- Each version's conversion rate wobbles like a coin flipped \(n\) times: variance \(p(1 - p)/n\).
- The difference of two independent groups adds their variances, so its standard error is \(\sqrt{2p(1 - p)/n}\).
- To be significant at \(\alpha = 0.05\), the measured gap must land beyond 1.96 standard errors.
- A real gap \(\delta\) gives measured gaps scattered around \(\delta\). For 80% of them to land past the line, \(\delta\) must sit 0.84 standard errors beyond it (0.84 is the bell curve's 80% point). So \(\delta = (1.96 + 0.84)\) standard errors \(= 2.80\) standard errors.
- Square both sides: \(\delta^2 = 2.80^2 \cdot 2p(1 - p)/n\), so \(n = 15.7\, p(1 - p)/\delta^2\). Round up to 16.
- The \(\delta^2\) is the lesson: half the effect needs four times the visitors.
A sign-up page converts 10% of visitors. To detect a lift to 11% (\(\delta = 0.01\)), about how many visitors does each version need?
5. Peeking ruins the test
The 5% false-positive promise holds for one look at a pre-planned sample size. Checking an A/B test every day and stopping the moment \(p < 0.05\) gives noise many chances to cross the line. This simulation runs 1000 A/A tests (both versions identical, so every "win" is false) and peeks after every 100 visitors:
Every extra look is another lottery ticket for noise; the 5% promise holds for one look, planned in advance.
The same trap shows up in ML. You try 20 hyperparameter settings and report the best one's test score. What's wrong?
- Nothing, as long as each run used the same test set
- You should have tried more settings
- Picking the best of 20 on the test set is 20 peeks: its score is optimistic, like a lucky A/B winner
6. A/B tests in production
Offline test sets only go so far: the real question is whether users are better off. An online A/B test randomly splits traffic between the current model (A) and the new one (B) and compares a business metric. The checklist:
- Decide first: the metric, the smallest effect worth shipping, \(\alpha\), power, and so \(n\) and the duration. Write it down.
- Randomize by user, not by request, so one person sees one version.
- Check the split: if you meant 50/50 and got 50.8/49.2 on a million users, something's broken (a "sample ratio mismatch").
- Run full weeks: behavior differs on weekends; novelty wears off.
- Don't peek to stop (or use methods designed for it, like sequential tests).
- Report the effect with its interval, plus guardrail metrics (latency, errors, complaints) that must not get worse.
Two models are scored on the same 500 test questions. Which test fits?
- A two-sample test of proportions
- A paired test on the disagreements (McNemar), or a paired bootstrap
Online, the only honest comparison is the one you designed before you looked at the numbers.
Where it breaks: when the p-value's assumptions don't hold
- Many metrics. Watch 20 metrics on an A/A test at \(\alpha = 0.05\) and the chance that at least one "wins" is \(1 - 0.95^{20} \approx 64\%\). Name one primary metric in advance; the rest are guardrails, or get the stricter \(\alpha / k\).
- Samples that aren't independent. The formulas assume every visitor or test example is its own coin. Randomize by request and a heavy user is counted hundreds of times; score 10 crops of one photo and you have about one example, not ten. The standard error comes out too small and so do the p-values. Randomize by user, count whole clusters, or bootstrap by user.
- Tiny counts. The bell curve is an approximation that needs enough disagreements. Five, all going B's way: \(z = 5/\sqrt 5 \approx 2.24\) and \(p \approx 0.025\), but the exact coin-flip answer is \(2 \times (1/2)^5 = 0.0625\), not significant. Below about 25 disagreements, use the exact test (the code box's
binomtest).
7. What to carry forward
- A confidence interval \(\bar x \pm t\, s/\sqrt n\) catches the truth in 95% of samples; the 95% describes the method, not one interval.
- Small samples need the t-distribution's wider cutoffs.
- A p-value is how often noise alone would produce a gap this big if there were no real difference. It's not the chance the null is true, and significant isn't the same as important.
- Type I = false alarm (\(\alpha\)); Type II = missed effect (\(\beta\)); power = \(1 - \beta\) grows with effect size and \(n\). Plan \(n\) before you start.
- Compare models on the same test set with a paired test. Don't peek and stop; many comparisons need a stricter threshold or fresh data.
All lessons: AI from scratch.
Last updated