Hypothesis tests and A/B testing
The null hypothesis and the p-value, the two kinds of error, power and sample size, a two-proportion A/B test, what a p-value is not, peeking and the Bonferroni correction.
The logic of a test
Every test has the same four parts. We take them on a coin first, where everything is easy to compute.
- The null hypothesis says there is no effect: the coin is fair, the button changes nothing. The alternative is what we conclude otherwise: the coin is biased, the button changes the rate.
- The test statistic is a number computed from the data whose distribution under we know. For the coin it is the number of heads in 100 flips; under it has the distribution .
- The p-value is the probability, computed under , of a statistic at least as extreme as the one observed.
- The decision: reject if . The significance level is fixed before anyone looks at the data, usually at .
The coin gave 60 heads. "At least as extreme" means at least as far from 50 in either direction: 60 heads or more, or 40 or fewer. We count both tails because a biased coin could lean either way; such a test is called two-sided. Summing the binomial pmf over both tails gives
That is above , so the test does not reject . Read this result carefully. The test has not shown that the coin is fair. It has shown that 100 flips are not enough to tell this coin from a fair one. A test either rejects the null hypothesis or fails to reject it; it never proves it.
The A/B test as a z-test
Back to the button. The null hypothesis is . For the statistic we take the observed difference and measure it in units of its own standard error.
A sample proportion has variance , and the two groups are independent, so the variances add (see the topic Joint distributions and independence). With the observed rates plugged in, the estimated standard error of the difference is
By the central limit theorem the difference is approximately normal. So under the statistic
has approximately the distribution . Large values of are surprising under . Let be the CDF of . The two-sided p-value is
Since , the test rejects : at the level the difference is statistically significant. Equivalently, the test rejects whenever . These values of form the rejection region, and under it has probability .
The test and the interval from the previous topic are two views of one calculation. The 95% interval for the difference is
It excludes zero exactly when . So the test at level rejects "no difference" exactly when the 95% interval for the difference does not contain zero. The interval tells more: not only whether zero is plausible, but which effect sizes are.
Two kinds of error
A test can go wrong in two ways.
- A type I error rejects a true : we ship a button that does nothing. Its probability is at most by construction, because the rejection region has probability under .
- A type II error keeps a false : the button really helps, and we miss it. Its probability is written , and is the power: the probability of detecting an effect that is really there.
The two errors pull against each other. A smaller shrinks the rejection region, so false alarms become rarer and real effects get missed more often.
We choose , but not the power: it depends on how large the true effect is and on how much data we have. Let us compute it for our button. Suppose the true rates really are and . Then is no longer centred at zero. Approximately,
The test rejects when ; the other tail, , is now practically out of reach. The probability of rejecting is
Picture two bell curves. The null curve is centred at 0, and its tails beyond hold 5% of its area. The alternative curve is centred at 2.07, and 54% of its area lies beyond 1.96. So an experiment of this size, run on a button that truly adds 0.3 points, detects the effect only about half the time. Our team was lucky to land on ; with slightly worse luck the same button would have looked like noise.
How many users are needed
Power 0.54 is a coin toss. The usual target is power at . How large must the experiment be?
Write for the effect we want to detect and , so that . Let be the quantile of , defined by ; the threshold 1.96 is . Under the alternative, is centred at . The power equals exactly when that centre lies above the threshold:
Solving for gives the number of users per group:
For our button , , and . That gives users per group, almost twice what the team used.
The formula has a sobering shape: grows like . Halving the effect you want to detect quadruples the experiment. Small improvements of small conversion rates are exactly what product teams hunt for, and they cost traffic in the hundreds of thousands. So is fixed before the experiment starts, from the smallest effect worth detecting. The next blocks show what goes wrong otherwise.
What a p-value is not
A p-value is a probability about the data, computed as if were true:
It is not , the probability that there is no effect. Mixing the two up is the base-rate fallacy from the topic Conditional probability and Bayes' formula, with ideas in place of patients.
Let us count. Suppose 10% of the ideas a team tests really work, every test has power , and . Of 1,000 ideas, 100 work, and 80 of them come out significant. The other 900 do nothing, and 5% of them, 45 ideas, come out significant by chance. Among the 125 significant results a share of
is false, although every one of them has .
Three more misreadings are common.
- A p-value is not the size of the effect. With enough users a negligible difference becomes significant; with too few a large one does not. Report the effect with its confidence interval, not only the p-value.
- A large p-value is not evidence of no effect. means the data are compatible with no effect, and usually with a sizeable effect too. The block on power showed how easily a real effect hides.
- A p-value is not a promise of replication. Suppose a result had and the true effect equals the observed one. A repeat of the same size has its centred near 2.05, just above 1.96, so it is significant only about half the time.
Peeking and many metrics
The promise "type I error at most " holds for one test, run once, on a sample size fixed in advance. Two everyday habits break it.
Peeking. An analyst checks the running experiment every day and stops as soon as . Each look is another chance for noise to cross the threshold. Simulate A/A tests, where both groups see the same button and convert at 2%: 1,000 users join each group between looks, 20 looks in all. At the final look 5% of the experiments are significant, as promised. But 25% of them cross at some look, and the peeking analyst declares every one of those a win. The cures are to fix in advance and look once, or to use sequential tests, whose thresholds are designed for repeated looks.
Many metrics. An experiment reports 20 metrics and tests each at . If nothing really changed and the metrics are independent, the probability of at least one false "effect" is
The Bonferroni correction tests each of the hypotheses at level . The probability of at least one false rejection is called the family-wise error rate. By the union bound from the topic Kolmogorov's axioms, it is now at most , however the metrics depend on each other. The price is power: each test needs a stronger signal. When many hypotheses are tested and a few false discoveries are acceptable, the Benjamini–Hochberg procedure controls the expected share of false discoveries instead and keeps more power.
The same trap hides in any analysis that tries several segments, metrics or time windows and reports the one that worked.
Beyond the z-test
The z-test leans on the central limit theorem. When that is doubtful — a dozen measurements, a median, the difference in AUC between two models — the permutation test leans only on the random split itself. If the button does nothing, the labels A and B are arbitrary: every reassignment of the labels among the same users is equally likely. Shuffle the labels many times and recompute the statistic each time. The p-value is the share of shuffles whose statistic is at least as extreme as the observed one. Under the null hypothesis of no effect at all, this test is exact for any distribution and any statistic.
Two limits apply to every test. Statistical significance is not practical significance: a real effect can be too small to be worth anything. And the p-value assumes independent units in a stable setting. Users who influence each other, the novelty of a new design wearing off, or traffic that drifts during the experiment all break that assumption.
This closes the statistics part of the course. We can now estimate a parameter, put an interval around the estimate and decide whether an effect is real. All of it rested on one assumption: the observations are independent, like users in an experiment or flips of a coin. But a sentence, a random walk over web pages and the successive states of a sampler have memory: the next state depends on the current one. The next topic, Markov chains, introduces the simplest model of such memory and its stationary distribution, with n-gram text and PageRank as examples.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.