Algorithmcore
Benjamini-Hochberg procedure
Also called BH procedure, linear step-up procedure.
Sort the p-values and reject the k smallest, for the largest k with p_(k) <= k q / m. With independent null p-values the FDR is exactly (m_0/m) q, so at most q.
- Time
- Θ(m log m) worst · best
- Space
- Θ(m) extra
- Where
- m = the number of hypotheses
- Model
- Real RAM
- Properties
- deterministicnot exactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Yoav Benjamini, Yosef Hochberg, 1995
The Benjamini-Hochberg procedure (BH) tests hypotheses and controls the false discovery rate, the expected proportion of false rejections among the rejections. It sorts the p-values, , finds the largest with , and rejects . When the null p-values are independent of each other and of the non-null ones, its FDR is at most , and exactly when the null p-values are uniform, whatever the false nulls are. Here is the number of true nulls. The thresholds grow linearly from , the Bonferroni level, to , so BH rejects far more than the FWER methods when many effects are real. It is the most used multiple testing method in genomics and other large-scale screening.
In plain words
You have many tests and expect some real effects among them. Instead of asking that no false alarm ever happens, you ask that false alarms make up only a small share of what you report, on average. Sort the results from the most convincing to the least. The most convincing one must pass a very strict bar, the second a bar twice as easy, the third three times as easy, and so on. Then find the last result in the list that passes its own bar, and report it and everything above it, even the ones above it that missed their own bar. The more results look convincing, the more lenient the method gets. This is what makes it powerful when there are many real effects.
The problem
Input. P-values for hypotheses , and a target FDR level . The set of true nulls is , with elements.
Output. The rejected set for the above, and the adjusted p-values , with rejected exactly when .
Error rate controlled. The false discovery rate , where is the number of rejected true nulls and the number of rejections. Control is strong, over every configuration of true and false nulls.
Dependence and level.
- Independent null p-values, independent of the non-null p-values: in finite samples, with equality when each null p-value is exactly uniform. This is proved below.
- Positive regression dependence on the subset of true nulls (PRDS), as for one-sided tests on normal vectors with nonnegative correlations: (Benjamini and Yekutieli 2001). This is cited, not proved here.
- Arbitrary dependence: , and this bound can be attained when and . The Benjamini-Yekutieli procedure runs BH at for this case.
FWER. Under the global null BH is the Simes test, so it controls the FWER weakly at for independent p-values. It does not control the FWER strongly.
The algorithm
BH(p₁, …, p_m, q) ▷ p-values in [0, 1], FDR level q- 1
sort the indices so that p_(1) ≤ … ≤ p_(m) - 2
k ← m; while k ≥ 1 and m · p_(k) > k · q do - 3
k ← k − 1 ▷ step up: from the largest p-value down - 4
reject H_(1), …, H_(k) - 5
run ← 1; for r ← m down to 1 do ▷ adjusted p-values - 6
run ← min(run, m · p_(r) / r) - 7
p̃_(r) ← run - 8
return (rejected set, p̃)
Lines L2 and L3 look for the last p-value below its line, starting from the largest one. All hypotheses before it are rejected at line L4, even those whose own p-value lies above its own threshold. That is the step-up rule. The test at line L2 multiplies, , instead of dividing, so that the decisions do not change with rounding and agree with the adjusted p-values. Ties do not matter: the rejected set is , which does not depend on how the sort orders tied values.
Equivalent descriptions.
- Graph. Plot against , draw the line through the origin with slope , and reject everything up to the last point on or below the line.
- Counting. With , the number of rejections is . So is the largest threshold with , where is the estimate of the FDR of the rule "reject " when all nulls are counted as true.
- Self-consistency. The rejected set is the largest set such that for every .
Example
The 15 p-values of Benjamini and Hochberg (1995). The p-values are 0.0001, 0.0004, 0.0019, 0.0095, 0.0201, 0.0278, 0.0298, 0.0344, 0.0459, 0.3240, 0.4262, 0.5719, 0.6528, 0.7590 and 1.000, at , so the thresholds are .
| adjusted | ||||
|---|---|---|---|---|
| 1 | 0.0001 | 0.0033 | yes | 0.0015 |
| 2 | 0.0004 | 0.0067 | yes | 0.003 |
| 3 | 0.0019 | 0.01 | yes | 0.0095 |
| 4 | 0.0095 | 0.0133 | yes | 0.0356 |
| 5 | 0.0201 | 0.0167 | no | 0.0603 |
| 6 | 0.0278 | 0.02 | no | 0.0639 |
| 7 | 0.0298 | 0.0233 | no | 0.0639 |
| 8 | 0.0344 | 0.0267 | no | 0.0645 |
| 9 | 0.0459 | 0.03 | no | 0.0765 |
| 10 | 0.3240 | 0.0333 | no | 0.486 |
The last row that passes is , so BH rejects four hypotheses. Bonferroni, Holm and Hochberg at reject three. The adjusted value at is a running minimum: is lowered to by line L6.
Step-up at work. For at , the thresholds are 0.025, 0.05, 0.075 and 0.1, and every sorted p-value passes, so all four are rejected. For at , the row passes, , so both 0.03 values are rejected although each is above the first threshold .
The level by hand. Take , false with , and true with uniform. BH rejects always, since , and rejects exactly when , with . So with probability and 0 otherwise, and .
Correctness
The proof shows that for independent uniform null p-values. It is a leave-one-out argument. Fix a true null and set to 0. Let be the event that BH then rejects exactly hypotheses. This event depends only on the other p-values. A short lemma shows that is rejected with exactly when and holds. So
by independence and uniformity, since the events partition the sample space. Summing over the true nulls gives . A valid but not uniform null p-value, , turns the second equality into .
The tests check this identity exactly. With and , every threshold is a point of the grid , so a null p-value uniform on that grid satisfies exactly. Summing over all values of the null p-values in rational arithmetic gives an FDR of exactly for each and each choice of the non-null p-values. The tests also compare BH with a brute force over all subsets, using the self-consistency description, and measure the FDR by Monte Carlo for independent and for equicorrelated normal statistics. The equicorrelated case is PRDS, so the measured FDR is checked against there too, but that bound is only measured, not proved.
Proof: The Benjamini-Hochberg procedure has FDR exactly (m_0/m) q for independent uniform null p-valuesfull
We prove that BH rejects the set described on the page (Lemma 1), that this set is the largest self-consistent set (Proposition 2), and that its false discovery rate is at most when every null p-value is valid and independent of all the other p-values, with equality when the null p-values are uniform on the thresholds (Theorem 4). As a corollary, the Simes global test has size exactly . Control under positive dependence (PRDS) is due to Benjamini and Yekutieli (2001) and is only cited here; the tests measure it by Monte Carlo in one PRDS model.
Setting. P-values and a level . For let , and write . is the set of true nulls, , is the number of rejected true nulls and the number of rejections.
Lemma 1 (what the lines compute). Let , or if there is no such . Then lines L2 and L3 end with , line L4 rejects exactly the set , and this set has exactly elements. (For the set is empty.)
Proof. For any and , if and only if at least of the p-values are at most , that is, . So the test at line L2 fails, , exactly when . The loop starts at and goes down while the test holds, so it stops at the largest with , or at 0. Next, : if , then and , because , which contradicts the choice of . So exactly p-values are at most . They are the smallest, so they are the hypotheses of ranks for any order of ties, which is what line L4 rejects.
Proposition 2 (self-consistency). Call a set self-consistent if for all . The BH set is self-consistent, and it contains every self-consistent set. So it is the largest one.
Proof. by Lemma 1, so is self-consistent. If is self-consistent with , then , so and . Then .
The test file uses Proposition 2 as a brute force: it searches all subsets for the largest self-consistent one, for , and compares the result with BH.
Lemma 3 (leave one out). Fix . Let be the vector with replaced by 0, and let be the number of rejections of BH on . Then for each ,
Proof. because the replaced value 0 is at most . Write for the count on , so , and let .
Case . For every , , so . Hence , and for . By Lemma 1, , and is rejected since .
Case . Since for all , every with also has , so . Then , and is not rejected.
So is rejected exactly when , and then . For a fixed , the event " rejected and " is therefore the event " and ".
The point of Lemma 3 is that is a function of the other p-values only.
Theorem 4 (FDR of BH). Suppose that for each , is independent of and for . Then
If moreover for all and all , as for continuous uniform null p-values, then exactly, for every law of the non-null p-values.
Proof. Pointwise, . When is rejected, , so by Lemma 3
Take expectations. Since depends only on , which is independent of ,
The last step holds because takes a value in . With the inequality is an equality. Summing over the true nulls gives the claim.
The proof needs each null p-value to be independent of all the other p-values, null and non-null, but it puts no condition on the joint law of the non-null p-values among themselves. It uses validity only at the points . This is why the exact test in the code works: on the grid with and , every is a grid point, so a null p-value uniform on the grid has , and Theorem 4 gives exactly. The test computes this sum over all null configurations in rational arithmetic and finds equality for .
Corollary 5 (Simes). If all nulls are true and the p-values are independent and uniform, then .
Proof. The event is by Lemma 1. Under the global null , so , and Theorem 4 with gives .
Proposition 6 (monotone). If for all , then BH on rejects every hypothesis that BH on rejects.
Proof. for all , so by Lemma 1, and . If then .
Adjusted p-values. Let , which lines L5 to L7 compute (the running value starts at 1). For , if and only if some has , that is, if and only if , which by Lemma 1 is the rejection of rank . So is rejected at level exactly when , and the adjusted p-values give the decisions at every level at once.
What is cited, not proved. Benjamini and Yekutieli (2001) show that when the p-values are PRDS on the subset of true nulls, and that under any dependence. The second bound is proved on the Benjamini-Yekutieli page. The first is checked only by simulation in the tests, with equicorrelated one-sided normal statistics.
Complexity
On a real RAM, sorting at line L1 takes time, which is the sorting problem. Lines L2 to L7 take . So the time is , and the extra space is .
The rejections alone need no sort. Put each into the bucket , the first with , with bucket for . Then is the prefix sum of the bucket counts up to , and is the largest with . This is the idea of counting sort and takes time. The implementation gives both versions and checks that they agree, including on ties and values that fall exactly on a threshold.
Properties
- FDR at for independent tests. Exact, in finite samples, for every configuration of the false nulls. The bound is conservative by the factor , which Storey's method estimates and removes.
- Weak FWER control. Under the global null, , the FDR is the FWER, and BH is the Simes test of size .
- More powerful than FWER methods. The -th threshold is at least the Holm and Hochberg threshold , because for . So at the same level BH rejects a superset of what Hochberg's procedure rejects, and so of Holm and Bonferroni.
- Step-up. It rejects for the largest passing , so a hypothesis is rejected whenever enough other p-values are small.
- Monotone. Lowering any p-value never removes a rejection: only grows, so only grows, and the thresholds with it.
- Adjusted p-values. . They are often called BH q-values, but they are not the same as Storey's q-values, which also estimate .
Variants and improvements
- Benjamini-Yekutieli. BH at controls the FDR under any dependence. See Benjamini-Yekutieli procedure.
- Adaptive BH. Estimate and run BH at . With a suitable estimator this still controls the FDR at under independence; see Storey's q-value.
- Weighted BH. Use with weights that average 1, to give prior favourites a larger threshold. Under independence and fixed weights the FDR is still at most .
- e-BH. For e-values, reject the largest for the largest with . This controls the FDR at under any dependence (Wang and Ramdas 2022).
- Knockoffs. In variable selection for regression, the knockoff filter uses a BH-like stopping rule on signed statistics, with an estimate of built from fake copies of the variables.
- Local fdr. The Bayesian view of BH estimates a tail-area false discovery rate; the local false discovery rate gives the posterior probability for each case instead.
When to use it
Use BH when is large, when you expect a fair number of real effects, and when a small proportion of false discoveries is acceptable, as in screening for genes, proteins, brain voxels or features for follow-up. The tests should be independent or positively dependent; for one-sided tests on positively correlated normal or statistics this is the case. For arbitrary dependence use Benjamini-Yekutieli, at a large cost in power. When many nulls are false, Storey's method gains power by estimating . When every single rejection must be right, as for the primary end points of a clinical trial, control the family-wise error rate instead.
Pitfalls
- Reading the level as a bound for one study. FDR control is a bound on the mean proportion. In one study the proportion of false discoveries can be much larger than .
- Rejecting only the hypotheses below their own threshold. That is a step-down reading of the rule, and it rejects less than BH. In the 15 p-value example both readings give 4, but for the step-down reading rejects nothing.
- Two-sided p-values under positive correlation. PRDS holds for one-sided tests on positively correlated normal statistics. It need not hold for two-sided tests, although simulations usually show that BH still works there.
- Discrete p-values. For discrete tests BH is conservative, because is below .
- Choosing or the family after seeing the result. As with any error rate, the guarantee holds only when the level and the set of hypotheses are fixed in advance.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/benjamini-hochberg-procedure/. The authors are listed in prov/MA7Z7G.jsonl.
"""The Benjamini-Hochberg linear step-up procedure.
Sort the p-values, p_(1) <= ... <= p_(m), find the largest k with
p_(k) <= k q / m, and reject H_(1), ..., H_(k). For independent null p-values
(independent of each other and of the non-null ones) the false discovery rate is
at most (m_0 / m) q, with equality when the null p-values are uniform. Comparisons
are done as m p <= k q, so that floats and fractions.Fraction give the same
decisions and the adjusted p-values agree with the rejections.
Two implementations: bh sorts (Theta(m log m)); bh_linear counts p-values in m
buckets and needs no sort (Theta(m)).
"""
import math
from collections.abc import Sequence
def _check(p: Sequence, q) -> None:
if any(not 0 <= v <= 1 for v in p):
raise ValueError("p-values must lie in [0, 1]")
if not 0 < q <= 1:
raise ValueError("q must lie in (0, 1]")
def bh(p: Sequence, q) -> tuple[list[bool], list]:
"""Return (reject, adjusted) in the original order of p."""
m = len(p)
if m == 0:
return [], []
_check(p, q)
order = sorted(range(m), key=lambda i: p[i]) # @line L1
k = m
while k >= 1 and m * p[order[k - 1]] > k * q: # @line L2
k -= 1 # @line L3
reject = [False] * m
for r in range(k): # @line L4
reject[order[r]] = True
adjusted = [0] * m
running = 1
for r in range(m - 1, -1, -1): # @line L5
running = min(running, m * p[order[r]] / (r + 1)) # @line L6
adjusted[order[r]] = running # @line L7
return reject, adjusted # @line L8
def bh_linear(p: Sequence, q) -> list[bool]:
"""The same rejections as bh, found by counting in Theta(m) time without sorting.
Bucket b holds the p-values whose smallest k with m p <= k q is b. Then
#{i : p_i <= k q / m} is the prefix sum up to k, and BH's k is the largest k
whose prefix sum is at least k.
"""
m = len(p)
if m == 0:
return []
_check(p, q)
count = [0] * (m + 2)
bucket = [0] * m
for i, v in enumerate(p):
b = max(1, math.ceil(m * v / q)) # first guess, corrected below for rounding
while b > 1 and m * v <= (b - 1) * q:
b -= 1
while b <= m and m * v > b * q:
b += 1
bucket[i] = min(b, m + 1) # m + 1 means "never at most k q / m"
count[bucket[i]] += 1
k, cum = 0, 0
for j in range(1, m + 1):
cum += count[j]
if cum >= j:
k = j
return [bucket[i] <= k for i in range(m)]
test_benjamini_hochberg_procedure.py 149 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/benjamini-hochberg-procedure/. The authors are listed in prov/MA7Z7G.jsonl.
import itertools
import math
import random
import statistics
from fractions import Fraction
import pytest
from benjamini_hochberg_procedure import bh, bh_linear
BH95 = [0.0001, 0.0004, 0.0019, 0.0095, 0.0201, 0.0278, 0.0298, 0.0344, 0.0459, 0.3240, 0.4262, 0.5719, 0.6528, 0.7590, 1.000]
def bh_brute_force(p: list, q) -> list[bool]:
"""Brute force from the self-consistency characterization: the largest set S with m p_i <= |S| q for all i in S.
For each size s from m down to 1, S must be {i : m p_i <= s q}; it works when it has at least s elements.
The answer is checked against every subset for small m in a separate test.
"""
m = len(p)
for s in range(m, 0, -1):
chosen = [m * v <= s * q for v in p]
if sum(chosen) >= s:
return chosen
return [False] * m
def test_worked_example_by_hand() -> None:
reject, adjusted = bh(BH95, 0.05)
# k q / m = k / 300: 0.0095 <= 4/300 = 0.01333, but 0.0201 > 5/300, 0.0278 > 6/300, ..., 0.0459 > 9/300.
assert [i for i, r in enumerate(reject) if r] == [0, 1, 2, 3]
assert adjusted[:6] == pytest.approx([0.0015, 0.003, 0.0095, 0.035625, 0.0603, 15 * 0.0298 / 7], abs=1e-9)
# The sixth adjusted value is a running minimum: 15 * 0.0278 / 6 = 0.0695 is larger than 15 * 0.0298 / 7.
assert bh_linear(BH95, 0.05) == reject
def test_step_up_rejects_p_values_above_their_own_threshold() -> None:
p = [Fraction(1, 100), Fraction(3, 100), Fraction(28, 1000), Fraction(4, 100)]
reject, _ = bh(p, Fraction(1, 10))
# Thresholds k/40: 0.025, 0.05, 0.075, 0.1. Sorted 0.01, 0.028, 0.03, 0.04: k = 4 passes.
assert reject == [True, True, True, True]
reject, _ = bh([Fraction(3, 100), Fraction(3, 100), Fraction(9, 10)], Fraction(1, 20))
# 0.9 > 0.05; 0.03 <= 2 * 0.05 / 3 = 0.0333, so both 0.03 are rejected, though 0.03 > 0.05/3.
assert reject == [True, True, False]
def test_self_consistency_by_subset_enumeration() -> None:
rng = random.Random(95)
for _ in range(200):
m = rng.randint(1, 8)
p = [Fraction(rng.randint(0, 40), 40) for _ in range(m)]
q = Fraction(rng.randint(1, 10), 20)
best: tuple = ()
for size in range(m, 0, -1):
for s in itertools.combinations(range(m), size):
if all(m * p[i] <= size * q for i in s):
best = s
break
if best:
break
expected = [i in best for i in range(m)]
assert bh(p, q)[0] == expected == bh_brute_force(p, q) == bh_linear(p, q)
def test_linear_version_matches_sorting_version_on_floats() -> None:
rng = random.Random(6)
for _ in range(3000):
m = rng.randint(1, 40)
p = [rng.random() ** rng.choice([1, 3, 6]) for _ in range(m)]
if rng.random() < 0.3: # exact ties and values on the thresholds
p = [round(v * 20) / 20 for v in p]
q = rng.choice([0.05, 0.1, 0.2])
assert bh_linear(p, q) == bh(p, q)[0] == bh_brute_force(p, q)
def test_adjusted_p_values_match_rejections() -> None:
rng = random.Random(7)
for _ in range(300):
m = rng.randint(1, 10)
p = [Fraction(rng.randint(0, 100), 100) for _ in range(m)]
_, adjusted = bh(p, Fraction(1, 10))
for q in [Fraction(k, 100) for k in range(1, 100, 4)]:
assert bh(p, q)[0] == [a <= q for a in adjusted]
def test_invalid_input() -> None:
assert bh([], 0.1) == ([], [])
assert bh_linear([], 0.1) == []
with pytest.raises(ValueError):
bh([0.5], 0)
with pytest.raises(ValueError):
bh_linear([1.5], 0.1)
def exact_fdr_on_grid(m: int, m0: int, q: Fraction, false_p: list) -> Fraction:
"""FDR by summing over all 10^m0 values of independent null p-values, each uniform on {1/10, ..., 1}."""
grid = [Fraction(k, 10) for k in range(1, 11)]
total = Fraction(0)
for nulls in itertools.product(grid, repeat=m0):
p = list(nulls) + false_p
reject, _ = bh(p, q)
r = sum(reject)
v = sum(reject[:m0])
if r:
total += Fraction(v, r)
return total / 10**m0
def test_fdr_equals_m0_over_m_times_q_exactly() -> None:
"""m = 4 and q = 2/5, so every threshold k q / m = k / 10 is a grid point and P(p_i <= k q / m) = k q / m exactly."""
m, q = 4, Fraction(2, 5)
rng = random.Random(2001)
for m0 in range(1, m + 1):
for _ in range(2 if m0 < m else 1):
false_p = [Fraction(rng.randint(0, 1000), 1000) for _ in range(m - m0)]
assert exact_fdr_on_grid(m, m0, q, false_p) == Fraction(m0, m) * q
def test_monte_carlo_fdr_independent_normals() -> None:
rng = random.Random(1995)
nd = statistics.NormalDist()
m, m0, q, reps = 20, 15, 0.1, 6000
total = 0.0
for _ in range(reps):
z = [rng.gauss(0, 1) + (2.5 if i >= m0 else 0.0) for i in range(m)]
reject = bh_linear([nd.cdf(-v) for v in z], q)
r = sum(reject)
total += sum(reject[:m0]) / r if r else 0.0
# Exact value (m0/m) q = 0.075. The standard deviation of the false discovery proportion is below 0.25,
# so four standard errors are below 0.013.
assert abs(total / reps - m0 / m * q) < 0.013
def test_monte_carlo_fdr_under_positive_dependence() -> None:
"""One-sided tests on equicorrelated normals (rho = 0.5) are PRDS: the FDR is at most (m0/m) q."""
rng = random.Random(29)
nd = statistics.NormalDist()
m, m0, q, reps = 20, 15, 0.1, 6000
total = 0.0
for _ in range(reps):
shared = rng.gauss(0, 1)
z = [math.sqrt(0.5) * shared + math.sqrt(0.5) * rng.gauss(0, 1) + (2.5 if i >= m0 else 0.0) for i in range(m)]
reject, _ = bh([nd.cdf(-v) for v in z], q)
r = sum(reject)
total += sum(reject[:m0]) / r if r else 0.0
assert total / reps <= m0 / m * q + 0.013
History
Simes (1986) proposed the rule "reject the global null if for some " as a global test, and noted that it does not control the FWER for single hypotheses. Benjamini and Hochberg (1995) introduced the false discovery rate and showed that the same rule, applied to single hypotheses, controls this new error rate for independent test statistics. Benjamini and Yekutieli (2001) proved control under positive dependence and gave the correction for arbitrary dependence. Storey (2002) and Storey, Taylor and Siegmund (2004) gave the adaptive version, the q-value and a martingale proof of the control.
References
- Yoav Benjamini and Yosef Hochberg, Controlling the false discovery rate, a practical and powerful approach to multiple testing, Journal of the Royal Statistical Society, Series B 57(1) (1995), pages 289 to 300, the linear step-up procedure, the proof that it controls the FDR at (m_0/m) q for independent test statistics, and the example with 15 p-values
- Yoav Benjamini and Daniel Yekutieli, The control of the false discovery rate in multiple testing under dependency, The Annals of Statistics 29(4) (2001), pages 1165 to 1188, FDR control at (m_0/m) q under positive regression dependence on the subset of true nulls (PRDS), and the bound for arbitrary dependence
- R. J. Simes, An improved Bonferroni procedure for multiple tests of significance, Biometrika 73(3) (1986), pages 751 to 754, the global test that rejects when p_(j) <= j alpha / n for some j, which is the Benjamini-Hochberg rule under the global null
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag MA7Z7G Edit this page History Report an error Markdown