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)?

\(1.96 \times 10 / \sqrt{100} = 1.96\). The interval is 48.04 to 51.96. To halve the margin you'd need four times the data.

Which statement about a 95% interval [48.0, 52.0] is right?

Subtle but it matters. (A Bayesian credible interval, from Probability 3's posterior, does mean "95% probability the value is in here", given the prior. With lots of data the two usually agree in practice.)

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.

n351030100∞
95% t value4.302.782.262.051.981.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?

And "significant" isn't "important": with a million test examples, a 0.1-point gap is significant and probably irrelevant. Always report the size of the difference with its confidence interval, not just p.

4. Two ways to be wrong, and power

no real differencea real difference
you say "B is better"Type I error (false positive), probability \(\alpha\)correct: probability = power
you say "can't tell"correctType 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?
  1. Each version's conversion rate wobbles like a coin flipped \(n\) times: variance \(p(1 - p)/n\).
  2. The difference of two independent groups adds their variances, so its standard error is \(\sqrt{2p(1 - p)/n}\).
  3. To be significant at \(\alpha = 0.05\), the measured gap must land beyond 1.96 standard errors.
  4. 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.
  5. 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.
  6. 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?

\(16 \times 0.1 \times 0.9 / 0.01^2 = 14{,}400\) per version. Small effects on rare events need big samples, which is why A/B tests run for weeks, and why "we tried it on 200 users and it looked better" means little.

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?

That's why chapter 11 keeps a validation set for choosing and touches the test set once. With many comparisons, either hold out fresh data for the final check or correct the threshold (Bonferroni: use \(\alpha / k\) for \(k\) comparisons).

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:

  1. Decide first: the metric, the smallest effect worth shipping, \(\alpha\), power, and so \(n\) and the duration. Write it down.
  2. Randomize by user, not by request, so one person sees one version.
  3. 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").
  4. Run full weeks: behavior differs on weekends; novelty wears off.
  5. Don't peek to stop (or use methods designed for it, like sequential tests).
  6. 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?

The same questions are easy or hard for both models, and pairing cancels that shared difficulty, as the section 3 picture showed. Chapter 8 (stage 5) will compare Blip's models exactly this way.

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

7. What to carry forward

All lessons: AI from scratch.

Last updated