Multiple Testing Corrections for A/B Tests in Python: Bonferroni, Holm and Benjamini-Hochberg

Correct the p-values of an A/B test with many metrics using statsmodels multipletests, compare Bonferroni, Holm and Benjamini-Hochberg, and read the result on a forest plot.

When an A/B test reports many metrics, some will look significant by chance. In Python, statsmodels.stats.multitest.multipletests(p_values, alpha=0.05, method="fdr_bh") adjusts for this. Use method="holm" or "bonferroni" when any false positive is costly, and "fdr_bh" (Benjamini-Hochberg) when you can accept a small share of false discoveries in exchange for more power. The code targets statsmodels 0.14 and SciPy 1.18.

Quick Example

This simulates one experiment with 20 metrics, 3 of which have a real effect, runs a z-test on each and applies three corrections. It runs on its own.

import numpy as np
import pandas as pd
from scipy import stats
from statsmodels.stats.multitest import multipletests

rng = np.random.default_rng(54)
n_per_arm = 4000
metrics = [f"metric_{i:02d}" for i in range(1, 21)]
true_effect = np.zeros(20)
true_effect[[2, 8, 14]] = [0.12, 0.09, 0.07]       # only 3 metrics really move (in SD units)

se = np.sqrt(2 / n_per_arm)
estimate = rng.normal(true_effect, se)
p_raw = 2 * stats.norm.sf(np.abs(estimate / se))

res = pd.DataFrame({"metric": metrics, "effect": estimate, "se": se, "p_raw": p_raw,
                    "truth": true_effect > 0})
for method in ["bonferroni", "holm", "fdr_bh"]:
    res[method] = multipletests(res["p_raw"], alpha=0.05, method=method)[0]

print("raw p < 0.05:", (res["p_raw"] < 0.05).sum())
print(res[["bonferroni", "holm", "fdr_bh"]].sum())

Five metrics pass at raw p < 0.05, but only three are real. Two of the five (metric_11 and metric_12) are false positives. Bonferroni and Holm keep 2 metrics, Benjamini-Hochberg keeps 3, and all 3 are the real effects.

The seed was picked so this single run shows the contrast. That draw has two false positives out of 17 null metrics, which is a plausible but not typical outcome. The simulation further down uses 4,000 repeated experiments and gives the typical picture.

Why Do Many Metrics Cause False Positives?

Each metric has a 5% chance of crossing p < 0.05 when nothing is going on. With 20 independent null metrics, the chance that at least one does is 1 - 0.95^20, about 64%. Looking at 20 metrics and reporting the ones that "won" is how experiments end up crediting noise.

Read the Result on a Forest Plot

A forest plot shows every metric's estimate and confidence interval on one axis, and is the quickest way to see which results stand out. Here, color shows which test each metric passes.

import matplotlib.pyplot as plt

order = res.sort_values("effect").reset_index(drop=True)
colors = np.where(order["fdr_bh"], "#d1495b", np.where(order["p_raw"] < 0.05, "#edae49", "#9aa5b1"))
y = np.arange(len(order))

fig, ax = plt.subplots(figsize=(7.5, 6.5))
ax.errorbar(order["effect"], y, xerr=1.96 * order["se"], fmt="none", ecolor="#c3c9d0", lw=1.5, zorder=1)
ax.scatter(order["effect"], y, c=colors, s=55, zorder=2)
ax.axvline(0, color="#333", lw=1, ls="--")
ax.set_yticks(y, order["metric"], fontsize=8)
ax.set_xlabel("Estimated effect (standard deviations), 95% CI")
fig.tight_layout()
plt.show()

Forest plot of 20 metric effects with 95 percent intervals; three metrics pass the Benjamini-Hochberg correction, two more pass only the raw p-value threshold

The two orange points have intervals that barely clear zero, which is the usual look of a result that is significant only because there were many chances. The intervals shown are uncorrected 95% intervals. Adjusting the p-values does not widen them unless you also correct the intervals.

Bonferroni, Holm and Benjamini-Hochberg Compared

Method method= Controls Behavior
Bonferroni "bonferroni" Family-wise error rate (any false positive) Divides alpha by the number of tests. Simple, conservative
Holm "holm" Family-wise error rate Step-down version of Bonferroni. Never rejects fewer tests, so prefer it
Benjamini-Hochberg "fdr_bh" False discovery rate (share of rejections that are false) More power, tolerates a few false discoveries
Benjamini-Yekutieli "fdr_by" False discovery rate BH adjusted for dependence between tests, more conservative

Bonferroni rejects only if a raw p-value is below 0.05 / 20 = 0.0025. Benjamini-Hochberg sorts the p-values and compares the k-th smallest with 0.05 ยท k / 20. It rejects every p-value up to the last one that sits under that rising line.

Sorted p-values plotted against the rising Benjamini-Hochberg line, the flat Bonferroni cutoff and the raw 0.05 cutoff

multipletests returns four values: a boolean reject array, the adjusted p-values, and two adjusted alphas. Use the adjusted p-values ([1]) when you want to report them next to each metric.

reject, p_adj, _, _ = multipletests(res["p_raw"], alpha=0.05, method="fdr_bh")
print(res.assign(p_bh=p_adj).loc[res["p_raw"] < 0.05, ["metric", "p_raw", "p_bh", "truth"]].round(4))

How Much Power Do You Give Up?

One experiment cannot show the trade-off, so the same design was simulated 4,000 times (20 metrics, 3 real effects, 4,000 users per arm).

Method P(at least one false positive) Real effects found (of 3) Share of rejections that are false
No correction 58.1% 2.86 18.9%
Bonferroni 4.4% 2.37 1.4%
Holm 5.0% 2.39 1.6%
Benjamini-Hochberg 14.8% 2.58 4.3%

Uncorrected testing finds the most real effects and produces a false positive in more than half of experiments. Bonferroni and Holm hold the family-wise error rate at about 5% and give up roughly half a real effect per experiment. Benjamini-Hochberg sits in between: it finds more real effects, and about 4% of what it flags is false on average. Its chance of at least one false positive is higher, because that is not what it controls.

Two bar charts comparing no correction, Bonferroni, Holm and Benjamini-Hochberg: probability of at least one false positive and number of real effects detected

Which Correction Should I Use?

Situation Use
One primary metric, several secondary Test only the primary at 0.05. Correct the rest, or label them exploratory
A ship decision hinges on any one of several metrics Holm
A dashboard of dozens of metrics, to prioritize follow-ups Benjamini-Hochberg
Slicing one metric by many segments Holm or BH, with the segment list fixed in advance

Practical Tips

  • Decide the primary metric and the list of metrics before the test starts. Corrections only work if the number of tests is honest.
  • Counting every segment, variant and time window as a test is what makes the number honest. A hidden slice cut is still a test.
  • Correct the p-values, but also look at the effect size and interval. A metric that survives correction with a tiny effect may not matter.
  • Several arms against one control is the same problem. Apply the correction across the arm comparisons.
  • Guardrail metrics, where you want to detect harm, often deserve a looser threshold than success metrics, since missing a regression is the costlier mistake.

Similar Topics