Skip to content
BIASED

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 button or the noise?

The topic Confidence intervals and the bootstrap taught us to put an honest error bar around an estimate. A product team usually asks a sharper question, one with a yes-or-no answer: is the effect real, or could chance alone have produced it?

Here is the example we will carry through the whole topic. A team tests a new "Buy" button. Users are split at random into two groups of n=20 000n = 20\,000 each: group A sees the old button, group B the new one. The conversion rates come out as

p^A=0.020,p^B=0.023.\hat p_A = 0.020, \qquad \hat p_B = 0.023.

Group B converted better by 0.3 percentage points. But even two identical buttons would never give exactly equal rates. Each rate is a sample proportion, and by the central limit theorem it wobbles around its true value pAp_A or pBp_B.

The random split matters as much as the arithmetic. Because a coin decides who sees which button, the groups differ in only two ways: the button and chance. The test will account for chance, so whatever is left is the button's doing. Show the new button only to this week's sign-ups instead, and the groups differ in a hundred other ways that no calculation can separate.

A hypothesis test turns the question into a calculation. Assume there is no effect, compute how surprising the data would be under that assumption, and act only if they are surprising enough.

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.

  1. The null hypothesis H0H_0 says there is no effect: the coin is fair, the button changes nothing. The alternative H1H_1 is what we conclude otherwise: the coin is biased, the button changes the rate.
  2. The test statistic TT is a number computed from the data whose distribution under H0H_0 we know. For the coin it is the number of heads in 100 flips; under H0H_0 it has the distribution Bin⁡(100,0.5)\operatorname{Bin}(100, 0.5).
  3. The p-value is the probability, computed under H0H_0, of a statistic at least as extreme as the one observed.
  4. The decision: reject H0H_0 if p≤αp \le \alpha. The significance level α\alpha is fixed before anyone looks at the data, usually at 0.050.05.

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

p=P(T≥60)+P(T≤40)=0.057.p = P(T \ge 60) + P(T \le 40) = 0.057.

That is above 0.050.05, so the test does not reject H0H_0. 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 H0 ⁣:pA=pBH_0\colon p_A = p_B. For the statistic we take the observed difference p^B−p^A\hat p_B - \hat p_A and measure it in units of its own standard error.

A sample proportion has variance p(1−p)/np(1-p)/n, 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

se⁡^=p^A(1−p^A)n+p^B(1−p^B)n=0.00145.\widehat{\operatorname{se}} = \sqrt{\frac{\hat p_A(1-\hat p_A)}{n} + \frac{\hat p_B(1-\hat p_B)}{n}} = 0.00145.

By the central limit theorem the difference is approximately normal. So under H0H_0 the statistic

z=p^B−p^Ase⁡^=0.0030.00145=2.07z = \frac{\hat p_B - \hat p_A}{\widehat{\operatorname{se}}} = \frac{0.003}{0.00145} = 2.07

has approximately the distribution N(0,1)\mathcal N(0, 1). Large values of ∣z∣|z| are surprising under H0H_0. Let Φ\Phi be the CDF of N(0,1)\mathcal N(0, 1). The two-sided p-value is

p=P(∣Z∣≥2.07)=2(1−Φ(2.07))=0.039.p = P(|Z| \ge 2.07) = 2\bigl(1 - \Phi(2.07)\bigr) = 0.039.

Since 0.039≤0.050.039 \le 0.05, the test rejects H0H_0: at the level 0.050.05 the difference is statistically significant. Equivalently, the test rejects whenever ∣z∣>1.96|z| > 1.96. These values of zz form the rejection region, and under H0H_0 it has probability 0.050.05.

The test and the interval from the previous topic are two views of one calculation. The 95% interval for the difference is

0.003±1.96⋅0.00145=[0.0002, 0.0058].0.003 \pm 1.96 \cdot 0.00145 = [0.0002,\ 0.0058].

It excludes zero exactly when ∣z∣>1.96|z| > 1.96. So the test at level 0.050.05 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 H0H_0: we ship a button that does nothing. Its probability is at most α\alpha by construction, because the rejection region has probability α\alpha under H0H_0.
  • A type II error keeps a false H0H_0: the button really helps, and we miss it. Its probability is written β\beta, and 1−β1 - \beta is the power: the probability of detecting an effect that is really there.

The two errors pull against each other. A smaller α\alpha shrinks the rejection region, so false alarms become rarer and real effects get missed more often.

We choose α\alpha, 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 pA=0.020p_A = 0.020 and pB=0.023p_B = 0.023. Then zz is no longer centred at zero. Approximately,

z∼N ⁣(pB−pAse⁡, 1)=N(2.07, 1).z \sim \mathcal N\!\left(\frac{p_B - p_A}{\operatorname{se}},\ 1\right) = \mathcal N(2.07,\ 1).

The test rejects when z>1.96z > 1.96; the other tail, z<−1.96z < -1.96, is now practically out of reach. The probability of rejecting is

1−β=P(z>1.96)=1−Φ(1.96−2.07)=Φ(0.11)≈0.54.1 - \beta = P(z > 1.96) = 1 - \Phi(1.96 - 2.07) = \Phi(0.11) \approx 0.54.

Picture two bell curves. The null curve is centred at 0, and its tails beyond ±1.96\pm 1.96 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 z=2.07z = 2.07; 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 0.80.8 at α=0.05\alpha = 0.05. How large must the experiment be?

Write δ=pB−pA\delta = p_B - p_A for the effect we want to detect and v=pA(1−pA)+pB(1−pB)v = p_A(1-p_A) + p_B(1-p_B), so that se⁡=v/n\operatorname{se} = \sqrt{v/n}. Let zqz_q be the quantile of N(0,1)\mathcal N(0, 1), defined by Φ(zq)=q\Phi(z_q) = q; the threshold 1.96 is z1−α/2z_{1-\alpha/2}. Under the alternative, zz is centred at δ/se⁡\delta/\operatorname{se}. The power equals 1−β1 - \beta exactly when that centre lies z1−βz_{1-\beta} above the threshold:

δv/n=z1−α/2+z1−β.\frac{\delta}{\sqrt{v/n}} = z_{1-\alpha/2} + z_{1-\beta}.

Solving for nn gives the number of users per group:

n=(z1−α/2+z1−β)2 vδ2.n = \frac{(z_{1-\alpha/2} + z_{1-\beta})^2\, v}{\delta^2}.

For our button z0.975=1.96z_{0.975} = 1.96, z0.8=0.84z_{0.8} = 0.84, v=0.0421v = 0.0421 and δ=0.003\delta = 0.003. That gives n=36 691n = 36\,691 users per group, almost twice what the team used.

The formula has a sobering shape: nn grows like 1/δ21/\delta^2. 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 nn 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 H0H_0 were true:

p=P(a statistic at least as extreme as observed∣H0).p = P(\text{a statistic at least as extreme as observed} \mid H_0).

It is not P(H0∣data)P(H_0 \mid \text{data}), 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 0.80.8, and α=0.05\alpha = 0.05. 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

4545+80=0.36\frac{45}{45 + 80} = 0.36

is false, although every one of them has p≤0.05p \le 0.05.

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. p=0.3p = 0.3 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 p=0.04p = 0.04 and the true effect equals the observed one. A repeat of the same size has its zz 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 α\alpha" 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 p<0.05p < 0.05. 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 ∣z∣>1.96|z| > 1.96 at some look, and the peeking analyst declares every one of those a win. The cures are to fix nn 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 α=0.05\alpha = 0.05. If nothing really changed and the metrics are independent, the probability of at least one false "effect" is

1−0.9520=0.64.1 - 0.95^{20} = 0.64.

The Bonferroni correction tests each of the mm hypotheses at level α/m\alpha/m. 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 m⋅α/m=αm \cdot \alpha/m = \alpha, 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.