Multiple Testing, Peeking & P-Hacking
Part 1, Power & Sample Size, taught you to decide the experiment before you run it: power is the chance of catching a real effect, and the sample size comes from the distance between two bells, about 14,752 users per arm to see a lift from 10% to 11%. That plan assumes you look once, at the end, and ask one question. This lesson is about what happens when you do not: peeking every day, testing many metrics, and trying many analyses.
A reminder of the notation from Part 1. A proportion such as a conversion rate is written p1, p2 or pbar, and a p-value is always written out as "p-value". In the correction formulas below, p_i and p_(i) are p-values (the i-th one and the i-th smallest). In code, proportions keep names like p1 and p2 and p-values are called pvals or pv, except that a lone p is a conversion rate in the peeking cell and an array of p-values in the corrections cell.
After this lesson, you will be able to:
- Explain why checking an experiment every day and stopping at the first p < 0.05 inflates false positives, and measure by how much
- Control the false positive rate across many tests with Bonferroni, Holm and Benjamini-Hochberg, and apply them to ML model and hyperparameter comparisons
- Name p-hacking and the garden of forking paths, and list the defences that work: pre-registration, one primary metric and replication
Before you start
#Peeking and Optional Stopping
Why 22% and not higher? If each of the 14 looks used brand-new users, the looks would be independent tests, and the chance of at least one false win would be 1 - 0.95^14 = 51.2% (the extra cell lines show 0.50 when simulated). The daily looks are not independent. Day 14 contains all of day 13's users plus 500 more, so the z-scores of neighbouring days are almost the same number: the correlation between day 13 and day 14 is about 0.96, and between day 1 and day 14 about 0.27. When one look has already crossed the line, the next one usually has too, so the chances overlap instead of adding up, and the damage lands well below the independent-tests bound. It is still more than four times the 5% you thought you had.
The 22% is not a quirk of the simulation. Open the derivation to see why repeated looks must push the rate above 5%, and how to compute the exact value.
Slide the number of looks and switch between "stop at first crossing" and "only look at the end", then watch how many paths cross the 1.96 lines and how the false-win rate climbs from 5% to about 22% at 14 looks and about 28% at 30 looks.
A Bayesian analysis (the next lesson) does not make this problem vanish. The posterior itself stays a valid summary of the data you have, but a decision rule such as "ship the first day the probability that B is better exceeds 95%" still has an error rate that grows with the number of looks, and it depends on the prior. Whatever method you choose, simulate the stopping rule on data where nothing is going on.
You planned 30,000 users and the p-value was 0.07 at 20,000. A teammate says 'we are close, run it to 40,000 and see.' What is the sound response?
#The Multiple Comparisons Problem
Why exactly 5%? When the null is true, the p-value is uniformly distributed between 0 and 1: it is as likely to land in 0 to 0.05 as in 0.50 to 0.55. So P(p-value below 0.05) = 5% for every test whose null is true, whatever the data look like.
The simulation matches the formula to two decimals. Each simulated p-value is a draw from the uniform distribution, which is exactly what a true null produces. By 50 tests, a false "win" is almost certain; by 100, it is virtually guaranteed.
#Bonferroni, Holm and Benjamini-Hochberg
There are two different promises you can ask a correction to make, and the choice matters.
- Control the FWER: keep the probability of any false positive at or below alpha. Right when a single false claim is costly (a medical claim, a launch decision).
- Control the FDR (false discovery rate): keep the expected fraction of your discoveries that are false at or below q. Right when you are screening many candidates and can tolerate a small share of duds, as in feature or gene screening.
Three short derivations follow, one per method: why Bonferroni's guarantee holds, why Holm keeps it while rejecting at least as much, and what Benjamini-Hochberg controls.
Drag the share of true nulls to 100 percent, then shrink the effect size, and watch which rank Benjamini-Hochberg stops at.
#A hand-worked example with 8 p-values
You tested 8 metrics. The raw p-values, sorted, are 0.001, 0.0065, 0.012, 0.021, 0.031, 0.044, 0.30 and 0.74. Take alpha = q = 0.05 and m = 8.
| Rank i | p-value | Bonferroni cutoff (0.05/8) | Holm cutoff 0.05/(8-i+1) | BH cutoff (i/8) x 0.05 |
|---|---|---|---|---|
| 1 | 0.001 | 0.00625 | 0.00625 | 0.00625 |
| 2 | 0.0065 | 0.00625 | 0.00714 | 0.01250 |
| 3 | 0.012 | 0.00625 | 0.00833 | 0.01875 |
| 4 | 0.021 | 0.00625 | 0.01000 | 0.02500 |
| 5 | 0.031 | 0.00625 | 0.01250 | 0.03125 |
| 6 | 0.044 | 0.00625 | 0.01667 | 0.03750 |
| 7 | 0.30 | 0.00625 | 0.02500 | 0.04375 |
| 8 | 0.74 | 0.00625 | 0.05000 | 0.05000 |
- Uncorrected: six of the eight (p below 0.05) look like discoveries.
- Bonferroni: only p = 0.001 passes 0.00625. One discovery.
- Holm: 0.001 passes 0.00625; 0.0065 passes 0.00714; then 0.012 fails 0.00833 and you stop. Two discoveries. Holm kept the same FWER guarantee and found one more.
- BH: walk from the bottom. Rank 8 fails (0.74 > 0.05), rank 7 fails, rank 6 fails (0.044 > 0.0375), rank 5 passes (0.031 is at most 0.03125, barely). So the largest passing rank is 5, and BH rejects ranks 1 to 5. Five discoveries. The 5% promise is an average over many repeated experiments, not a claim about these five: it means about 0.05 x 5 = 0.25 of a false discovery per batch of five, on average, at most.
Try four tests with q = 0.05. The cutoffs (i / 4) x 0.05 are 0.0125, 0.025, 0.0375 and 0.05. The sorted p-values are 0.001, 0.026, 0.030 and 0.60. Rank 2 misses its own cutoff, since 0.026 is above 0.025. Rank 3 passes, since 0.030 is at most 0.0375. The last passing rank is 3, so ranks 1, 2 and 3 are all discoveries.
Now code all three and run them on this example, then in a simulation with known truth.
Correcting alpha has a price in data, and the explorer from Power & Sample Size shows it. Set the "Metrics tested" slider to 20 and read the readout: Bonferroni's corrected alpha is 0.0025 (0.05 divided by 20), the uncorrected chance of at least one false positive is 0.6415, and the required n at the corrected alpha is 28,076 per arm, about 90% more than the 14,752 for one metric.
#P-Hacking and the Garden of Forking Paths
Warning signs:
- A significant result in a subgroup (new users in one country, on one device) that was never in the plan.
- A "primary metric" that changed between the plan and the report.
- Outlier removal or transformations chosen after looking at their effect on p.
- A p-value just under 0.05 reported with no mention of how many things were tried.
#Connection to Machine Learning
#Many models, one test set (the winner's curse)
#Leaderboard overfitting
A public leaderboard that many teams probe repeatedly is a test set being reused thousands of times. Each submission is another look at the same data, so the top scores drift upward from fitting the test set's noise, not from better generalization. Practical defences are a private held-out set revealed only at the end, rounding or limiting how much feedback each submission returns, and limiting the number of submissions.
#Experiments in production
Online evaluation of a new model is an A/B test with all of the above: a power calculation for the primary metric, a fixed stopping rule, a guardrail set (secondary metrics, such as page load time or error rate, that must not get worse) corrected with Holm or BH, and a count of how many variants you ran in parallel. A "multi-armed" test with 6 variants against one control is 6 comparisons.
The 2 points are not an accident of the seed. Open the derivation to see why the best of K noisy scores sits above the truth, and how large the bias can be.
#Try It Yourself
Build the second half of the "Experiment Planner" from Power & Sample Size. You will count how many chances for a false alarm a plan gives noise, correct alpha with Bonferroni and Benjamini-Hochberg, and pay for the correction in sample size. The sample size formula from Part 1 is reused as a function.
Tests: fwer at alpha 0.05 is 0.050, 0.226, 0.642 and 0.923 for m = 1, 5, 20 and 50, with Bonferroni alpha per test 0.0500, 0.0100, 0.0025 and 0.0010; n_per_arm at 10% to 11% is 14,752 (15 days) for 1 metric, 21,951 (22 days) for 5 metrics and 28,076 (29 days) for 20 metrics; on the 8 p-values the counts are 6 uncorrected, 1 with Bonferroni and 5 with BH; in the simulation the smallest p-value is below 0.05 about 0.642 of the time and at most 0.0025 about 0.048 of the time.
When you run the solution you should see the chance of at least one false positive climb from 0.050 to 0.226, 0.642 and 0.923 as the tests grow from 1 to 5, 20 and 50, while the Bonferroni alpha per test falls from 0.0500 to 0.0010. Paying for that correction costs data: 14,752 per arm and 15 days for one metric, 21,951 and 22 days for five, and 28,076 and 29 days for twenty. Twenty metrics cost about 1.9 times the sample of one, because the smaller alpha raises the critical value z from 1.960 to 3.023. On the 8 p-values you get 6, 1 and 5 discoveries, and the simulation confirms the correction: with 20 true nulls, the smallest p-value falls below 0.05 in 0.642 of experiments, but at or below 0.05 / 20 in only 0.048.
#Key Takeaways
- Peeking breaks alpha — checking 14 times and stopping at the first p below 0.05 turned a nominal 5% false positive rate into about 22% in a simulation with no real effect (below the 51% bound for 14 independent tests, because the looks share data); fix the sample size or use a sequential method
- Many tests means many chances — when the null is true a p-value is uniform between 0 and 1, so with 20 independent tests at alpha 0.05 the chance of at least one false positive is 1 - 0.95^20 = 0.64; Bonferroni (alpha / m) and Holm control that chance, Benjamini-Hochberg controls the false discovery fraction and finds more
- BH on 8 p-values finds 5, Holm finds 2, Bonferroni finds 1 — the thresholds are (i / m) x q for BH, alpha / (m - i + 1) for Holm, and alpha / m for Bonferroni
- In ML, the best of many is biased — the best of 50 identical 80%-accuracy models scored about 82% on one test set; select on validation and report on fresh data
#Quick Check
Two arms have identical conversion rates. You test once a day for 14 days and ship the first time p < 0.05. In a simulation, about what fraction of such experiments would ship a variant that does nothing?