Algorithm Search⌘K

Algorithmadvanced

Benjamini-Yekutieli procedure

Also called BY procedure.

Run Benjamini-Hochberg at level q / (1 + 1/2 + ... + 1/m). This controls the FDR at (m_0/m) q under any dependence, at a level about ln m times smaller.

Time
Θ(m log m) worst · best
Space
Θ(m) extra
Where
m = the number of hypotheses
Properties
deterministicnot exactofflineiterative
Origin
Yoav Benjamini, Daniel Yekutieli, 2001

The Benjamini-Yekutieli procedure (BY) is the Benjamini-Hochberg procedure run at the smaller level q/cmq/c_m, where cm=∑i=1m1/ic_m = \sum_{i=1}^m 1/i is the mm-th harmonic number. It sorts the p-values, finds the largest kk with p(k)≤kq/(mcm)p_{(k)} \le kq/(mc_m), and rejects H(1),…,H(k)H_{(1)}, \dots, H_{(k)}. Its false discovery rate is at most (m0/m)q(m_0/m)q under any dependence among the p-values, with only the validity of each null p-value assumed. That is the reason to use it. The price is power: cmc_m is about ln⁡m+0.577\ln m + 0.577, so the thresholds are 2.93 times smaller than those of BH for m=10m = 10 and 7.49 times smaller for m=1000m = 1000. The factor cannot be removed in general: there are joint laws of valid p-values under which plain BH has FDR exactly qcmqc_m.

In plain words

Benjamini-Hochberg keeps the share of false discoveries low when the tests behave independently, or when they tend to move together in the same direction. If the tests can depend on each other in strange ways, false discoveries can pile up and the guarantee fails. This method repairs that by making the bar stricter by a fixed factor that grows slowly with the number of tests: about 3 for ten tests, about 5 for a hundred, about 7.5 for a thousand. After that change the method works for any pattern of dependence. You pay for this safety with fewer discoveries, so use it only when you cannot argue that the dependence is harmless.

The problem

Input. P-values p1,…,pmp_1, \dots, p_m for hypotheses H1,…,HmH_1, \dots, H_m, and a target FDR level q∈(0,1]q \in (0, 1].

Output. The rejected set, and the adjusted p-values p~i\tilde p_i, with HiH_i rejected exactly when p~i≤q\tilde p_i \le q.

Error rate controlled. The false discovery rate E[V/max⁡(R,1)]E[V/\max(R, 1)], strongly, over every configuration of true and false nulls.

Dependence and level. Arbitrary. If each null p-value is valid, P(pi≤u)≤uP(p_i \le u) \le u, then FDR≤(m0/m)q\text{FDR} \le (m_0/m)q, in finite samples. No condition is put on the joint law. The bound is attained when m0=mm_0 = m, by the construction in the proof. Under independence BY is just BH at q/cmq/c_m, so its FDR is exactly (m0/m)q/cm(m_0/m)q/c_m.

The algorithm

  1. BY(p₁, …, p_m, q) ▷ p-values in [0, 1], FDR level q
  2. 1c ← 1 + 1/2 + … + 1/m
  3. 2sort the indices so that p_(1) ≤ … ≤ p_(m)
  4. 3k ← m; while k ≥ 1 and m · c · p_(k) > k · q do
  5. 4k ← k − 1
  6. 5reject H_(1), …, H_(k)
  7. 6run ← 1; for r ← m down to 1 do ▷ adjusted p-values
  8. 7run ← min(run, m · c · p_(r) / r); p̃_(r) ← run
  9. 8return (rejected set, p̃)

Lines L2 to L8 are the BH algorithm with qq replaced by q/cmq/c_m. The implementation computes cmc_m as an exact fraction when the inputs are fractions, and with a correctly rounded sum of floats otherwise. For very large mm, cm=ln⁡m+γ+1/(2m)+O(m−2)c_m = \ln m + \gamma + 1/(2m) + O(m^{-2}) with γ=0.5772…\gamma = 0.5772\ldots, see the harmonic series.

Example

Four p-values. p=(0.004,0.011,0.02,0.6)p = (0.004, 0.011, 0.02, 0.6) at q=0.05q = 0.05. Here c4=25/12=2.0833c_4 = 25/12 = 2.0833.

kk p(k)p_{(k)} BH threshold kq/mkq/m BY threshold kq/(mc4)kq/(mc_4)
1 0.004 0.0125 0.006
2 0.011 0.025 0.012
3 0.02 0.0375 0.018
4 0.6 0.05 0.024

BH rejects three, since 0.02 is below 0.0375. BY rejects two: 0.011 is below 0.012, but 0.02 is above 0.018 and 0.6 is above 0.024.

The 15 p-values of Benjamini and Hochberg (1995). At q=0.05q = 0.05, c15=3.3182c_{15} = 3.3182, and the BY thresholds are k⋅0.0010046k \cdot 0.0010046. The first three p-values, 0.0001, 0.0004 and 0.0019, pass, and none of 0.0095, 0.0201, ..., 0.3240 passes its threshold, so BY rejects three, against four for BH. The adjusted p-values start 0.005, 0.01, 0.0315, 0.1182, 0.2001.

The worst case for BH, by hand. Take m=2m = 2, both nulls true, and this joint law. With probability qq, pick one of the two indices at random, set its p-value to q/2q/2 and the other to 1. With probability q/2q/2, set both to qq. Otherwise set both to 1. Each p-value then has P(pi≤q/2)=q/2P(p_i \le q/2) = q/2 and P(pi≤q)=qP(p_i \le q) = q, and it is valid. BH at qq rejects one hypothesis in the first case and two in the second, all of them false, so its FDR is q+q/2=qc2q + q/2 = qc_2. BY at qq uses q/c2q/c_2 in place of qq, and the same construction at q/c2q/c_2 shows that its FDR qq can be reached.

Correctness

The proof shows FDR≤(m0/m)q\text{FDR} \le (m_0/m)q under arbitrary dependence. Write tk=kq′/mt_k = kq'/m for the thresholds at level q′=q/cmq' = q/c_m, fix a true null HiH_i, and let AkA_k be the event that HiH_i is rejected and R=kR = k. The events AkA_k are disjoint, and Ak⊆{pi≤tk}A_k \subseteq \{p_i \le t_k\} because a rejected hypothesis has pi≤tRp_i \le t_R. Writing 1/k1/k as a telescoping sum gives

∑k=1mP(Ak)k=∑j=1m(1j−1j+1)P(⋃k≤jAk)+P(⋃kAk)m+1≤∑j=1mtjj(j+1)+tmm+1=q′m(∑j=2m+11j+mm+1)=q′mcm=qm,\sum_{k=1}^m \frac{P(A_k)}{k} = \sum_{j=1}^m \Big(\frac1j - \frac1{j+1}\Big)P\Big(\bigcup_{k \le j}A_k\Big) + \frac{P(\bigcup_k A_k)}{m+1} \le \sum_{j=1}^m \frac{t_j}{j(j+1)} + \frac{t_m}{m+1} = \frac{q'}{m}\Big(\sum_{j=2}^{m+1}\frac1j + \frac{m}{m+1}\Big) = \frac{q'}{m}c_m = \frac qm,

using only P(⋃k≤jAk)≤P(pi≤tj)≤tjP(\bigcup_{k \le j}A_k) \le P(p_i \le t_j) \le t_j. Summing over the m0m_0 true nulls gives the bound. The same argument with qq in place of q′q' shows that plain BH has FDR≤(m0/m)qcm\text{FDR} \le (m_0/m)qc_m, and the construction in the proof attains this when m0=mm_0 = m and qcm≤1qc_m \le 1.

The tests check the rejections against BH at q/cmq/c_m in exact rational arithmetic, and the bound under arbitrary dependence exactly. In the second check the null p-values are uniform on {1/24,…,1}\{1/24, \dots, 1\} and are coupled through random permutations of one shared uniform index, which gives many different dependence structures with valid marginals; the FDR is summed over the 24 outcomes as a fraction. The worst-case law is built for m≤5m \le 5 and gives FDR exactly qcmqc_m for BH and exactly qq for BY.

Proof: The Benjamini-Yekutieli procedure controls the FDR at (m_0/m) q under any dependence, and the harmonic factor is sharpfull

We prove that any step-up procedure with thresholds t1≤⋯≤tmt_1 \le \dots \le t_m has false discovery rate at most m0(∑j=1mtj/(j(j+1))+tm/(m+1))m_0\big(\sum_{j=1}^m t_j/(j(j+1)) + t_m/(m+1)\big) under any dependence (Theorem 1). For the Benjamini-Yekutieli thresholds this is (m0/m)q(m_0/m)q (Corollary 2). Then we build a joint law of valid null p-values under which Benjamini-Hochberg at qq has FDR exactly qcmqc_m and Benjamini-Yekutieli at qq has FDR exactly qq (Theorem 3). Everything here is proved; the tests check Corollary 2 exactly on random dependence structures and Theorem 3 exactly for m≤5m \le 5.

Setting. P-values p1,…,pmp_1, \dots, p_m with any joint law. I0I_0 is the set of true nulls, m0=∣I0∣m_0 = |I_0|, and each null p-value is valid: P(pi≤u)≤uP(p_i \le u) \le u for u∈[0,1]u \in [0, 1]. cm=∑i=1m1/ic_m = \sum_{i=1}^m 1/i. A step-up procedure with thresholds 0≤t1≤⋯≤tm≤10 \le t_1 \le \dots \le t_m \le 1 sets R=max⁡{k:N(tk)≥k}R = \max\{k : N(t_k) \ge k\}, with N(t)=#{j:pj≤t}N(t) = \#\{j : p_j \le t\} and R=0R = 0 if there is no such kk, and rejects {i:pi≤tR}\{i : p_i \le t_R\}. BH at level qq is the case tk=kq/mt_k = kq/m, and BY at level qq is the case tk=kq/(mcm)t_k = kq/(mc_m), which is what lines L1 to L5 compute (Lemma 1 in the correctness proof of the Benjamini-Hochberg procedure applies with q/cmq/c_m in place of qq).

Theorem 1. For every step-up procedure with thresholds t1≤⋯≤tmt_1 \le \dots \le t_m and every joint law with valid null p-values,

FDR≤m0(∑j=1mtjj(j+1)+tmm+1).\text{FDR} \le m_0\Big(\sum_{j=1}^m \frac{t_j}{j(j+1)} + \frac{t_m}{m+1}\Big).

Proof. Pointwise, V/max⁡(R,1)=∑i∈I0∑k=1m1Ak(i)/kV/\max(R, 1) = \sum_{i \in I_0}\sum_{k=1}^m \mathbf 1_{A_k^{(i)}}/k, where Ak(i)A_k^{(i)} is the event that HiH_i is rejected and R=kR = k. Fix i∈I0i \in I_0 and write Ak=Ak(i)A_k = A_k^{(i)}. The events A1,…,AmA_1, \dots, A_m are disjoint, since RR takes one value. On AkA_k, HiH_i is rejected, so pi≤tR=tkp_i \le t_R = t_k. Hence for each jj, ⋃k≤jAk⊆{pi≤tj}\bigcup_{k \le j}A_k \subseteq \{p_i \le t_j\}, because tk≤tjt_k \le t_j for k≤jk \le j, and so

P(⋃k≤jAk)≤P(pi≤tj)≤tj.P\Big(\bigcup_{k \le j}A_k\Big) \le P(p_i \le t_j) \le t_j.

For 1≤k≤m1 \le k \le m the telescoping sum gives 1k=∑j=km(1j−1j+1)+1m+1\frac1k = \sum_{j=k}^m\big(\frac1j - \frac1{j+1}\big) + \frac1{m+1}. Therefore, exchanging the order of summation and using disjointness,

∑k=1mP(Ak)k=∑j=1m(1j−1j+1)∑k≤jP(Ak)+1m+1∑k=1mP(Ak)=∑j=1mP(⋃k≤jAk)j(j+1)+P(⋃k≤mAk)m+1.\sum_{k=1}^m \frac{P(A_k)}{k} = \sum_{j=1}^m\Big(\frac1j - \frac1{j+1}\Big)\sum_{k \le j}P(A_k) + \frac1{m+1}\sum_{k=1}^m P(A_k) = \sum_{j=1}^m \frac{P(\bigcup_{k \le j}A_k)}{j(j+1)} + \frac{P(\bigcup_{k \le m}A_k)}{m+1}.

Each probability is at most the matching tjt_j, and the coefficients are positive, so the sum is at most ∑jtj/(j(j+1))+tm/(m+1)\sum_j t_j/(j(j+1)) + t_m/(m+1). Summing over the m0m_0 true nulls gives the theorem. □\square

No independence or positive dependence was used: only validity at the thresholds and the fact that a step-up rejection of HiH_i with R=kR = k needs pi≤tkp_i \le t_k.

Corollary 2 (Benjamini-Yekutieli). For every q′∈(0,1]q' \in (0, 1], the step-up procedure with tk=kq′/mt_k = kq'/m has FDR≤(m0/m)q′cm\text{FDR} \le (m_0/m)q'c_m. In particular BH at qq has FDR≤(m0/m)qcm\text{FDR} \le (m_0/m)qc_m, and BY at qq, which is the case q′=q/cmq' = q/c_m, has FDR≤(m0/m)q\text{FDR} \le (m_0/m)q under any dependence.

Proof. With tj=jq′/mt_j = jq'/m, tj/(j(j+1))=q′/(m(j+1))t_j/(j(j+1)) = q'/(m(j + 1)) and tm/(m+1)=(q′/m)⋅m/(m+1)t_m/(m + 1) = (q'/m)\cdot m/(m + 1). So the bound of Theorem 1 is

m0q′m(∑j=2m+11j+mm+1)=m0q′m(cm−1+1m+1+mm+1)=m0mq′cm.□m_0\frac{q'}{m}\Big(\sum_{j=2}^{m+1}\frac1j + \frac{m}{m+1}\Big) = m_0\frac{q'}{m}\Big(c_m - 1 + \frac1{m+1} + \frac{m}{m+1}\Big) = \frac{m_0}{m}q'c_m. \qquad\square

Theorem 3 (the factor cmc_m is sharp). Let m≥1m \ge 1 and q>0q > 0 with qcm≤1qc_m \le 1, and q<1q < 1 if m≥2m \ge 2. Define the joint law LqL_q of (p1,…,pm)(p_1, \dots, p_m) as follows. For each k=1,…,mk = 1, \dots, m, with probability q/kq/k, choose a set SS of kk indices uniformly at random, set pi=kq/mp_i = kq/m for i∈Si \in S and pi=1p_i = 1 for i∉Si \notin S. With the remaining probability 1−qcm1 - qc_m, set every pi=1p_i = 1. Then every pip_i is a valid p-value with P(pi≤kq/m)=kq/mP(p_i \le kq/m) = kq/m for each kk, and if all mm nulls are true, BH at level qq has FDR=qcm\text{FDR} = qc_m.

Proof. The probabilities q/kq/k and 1−qcm1 - qc_m are nonnegative and sum to 1. Fix ii. In case kk, index ii is in SS with probability k/mk/m, so P(pi=kq/m)=(q/k)(k/m)=q/mP(p_i = kq/m) = (q/k)(k/m) = q/m for each kk, and P(pi=1)=1−qP(p_i = 1) = 1 - q. So P(pi≤kq/m)=kq/mP(p_i \le kq/m) = kq/m. For u∈[0,1)u \in [0, 1), let jj be the largest kk with kq/m≤ukq/m \le u (or j=0j = 0); then P(pi≤u)=jq/m≤uP(p_i \le u) = jq/m \le u. For u=1u = 1 the probability is 1. So pip_i is valid.

In case kk, exactly kk p-values equal tk=kq/mt_k = kq/m and the others equal 1>q≥tj1 > q \ge t_j for all jj (for m=1m = 1 there are no others). So N(tj)=0<jN(t_j) = 0 < j for j<kj < k, N(tk)=kN(t_k) = k, and N(tj)=k<jN(t_j) = k < j for j>kj > k. Hence R=kR = k. In the last case R=0R = 0. Under the global null every rejection is false, so V/max⁡(R,1)=1{R≥1}V/\max(R, 1) = \mathbf 1\{R \ge 1\} and FDR=P(R≥1)=∑k=1mq/k=qcm\text{FDR} = P(R \ge 1) = \sum_{k=1}^m q/k = qc_m. □\square

Corollary 4. Under the law Lq/cmL_{q/c_m} with all nulls true, BY at level q≤1q \le 1 has FDR exactly qq. So the bound of Corollary 2 is attained when m0=mm_0 = m.

Proof. BY at qq is BH at q/cmq/c_m, and (q/cm)cm=q≤1(q/c_m)c_m = q \le 1, so Theorem 3 applies with level q/cmq/c_m and gives FDR=(q/cm)cm=q\text{FDR} = (q/c_m)c_m = q. (For m≥2m \ge 2, q/cm<1q/c_m < 1 since cm>1c_m > 1.) □\square

For m=2m = 2 the law LqL_q is the example on the page: with probability qq one p-value is q/2q/2 and the other is 1, with probability q/2q/2 both are qq, and BH has FDR 3q/23q/2.

Adjusted p-values. Lines L6 and L7 compute p~(r)=min⁡j≥rmin⁡(1,mcmp(j)/j)\tilde p_{(r)} = \min_{j \ge r}\min(1, mc_mp_{(j)}/j). For q≤1q \le 1, p~(r)≤q\tilde p_{(r)} \le q if and only if mcmp(j)≤jqmc_mp_{(j)} \le jq for some j≥rj \ge r, which is R≥rR \ge r, the rejection of rank rr. The tests check this equivalence on random vectors in exact arithmetic.

Complexity

The harmonic number takes Θ(m)\Theta(m) additions at line L1, the sort at line L2 takes Θ(mlog⁡m)\Theta(m\log m) on a real RAM, and the rest takes Θ(m)\Theta(m). So the time is Θ(mlog⁡m)\Theta(m\log m) and the extra space Θ(m)\Theta(m). As for BH, the rejections alone can be found in Θ(m)\Theta(m) time by counting the p-values in buckets instead of sorting.

Properties

  • FDR at (m0/m)q(m_0/m)q under any dependence. Proved, in finite samples, with only valid null p-values.
  • Sharp. No constant smaller than cmc_m works for every dependence with BH's linear thresholds: the construction gives FDR qcmqc_m for BH at qq.
  • Conservative in common cases. Under independence or PRDS, BY has FDR (m0/m)q/cm(m_0/m)q/c_m or less, far below qq.
  • Between BH and Bonferroni. Its first threshold is q/(mcm)q/(mc_m), below the Bonferroni level q/mq/m. Its last threshold is q/cmq/c_m. For small kk it is stricter than Bonferroni at the same level, and for k>cmk > c_m it is more lenient, so neither rejection set contains the other in general.
  • Monotone and step-up. As for BH: lowering a p-value never removes a rejection.

Variants and improvements

  • Other shapes. Blanchard and Roquain showed that BH with the thresholds qβ(k)/mq\beta(k)/m controls the FDR under any dependence when β(k)=∑j≤kjν({j})\beta(k) = \sum_{j \le k}j\nu(\{j\}) for a probability distribution ν\nu on {1,…,m}\{1, \dots, m\}. BY is the case ν({j})∝1/j\nu(\{j\}) \propto 1/j. Other choices of ν\nu give more power where many rejections are expected.
  • e-BH. For e-values, the BH rule e(k)≥m/(kq)e_{(k)} \ge m/(kq) controls the FDR at (m0/m)q(m_0/m)q under any dependence without a harmonic factor. Turning p-values into e-values loses power, so this helps when the evidence comes as e-values in the first place.
  • Use the dependence. When the joint law is known or can be resampled, permutation and bootstrap methods, as in the Westfall-Young procedure for the FWER, use the actual dependence and lose less.
  • FWER instead. Bonferroni and Holm also work under any dependence, and for small mm they can reject as much as BY.

When to use it

Use BY when the FDR is the right error rate, the dependence between the tests is unknown and could be negative, and a conservative result is acceptable. Examples are two-sided tests on correlated statistics with signs of both kinds, or tests that share data in complex ways. When the tests are independent, or positively dependent as for one-sided tests on positively correlated normal statistics, plain BH is valid and much more powerful. Many analysts use BH even with mild unknown dependence, because simulations rarely show its FDR above qq; BY is the choice when a proof is required.

Pitfalls

  • Using it by default. Under independence BY controls the FDR at about q/ln⁡mq/\ln m, not qq. Reporting "FDR at 5%" then hides a much stricter error rate and a large loss of power.
  • Writing ln⁡m\ln m for cmc_m. ln⁡m<cm\ln m < c_m, so dividing by ln⁡m\ln m gives a level that is too large and voids the proof. Use the exact sum, or ln⁡m+1\ln m + 1 as an upper bound.
  • Applying the factor twice. Software that reports BY adjusted p-values already includes cmc_m; compare them with qq, not with q/cmq/c_m.

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.

benjamini_yekutieli_procedure.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/benjamini-yekutieli-procedure/. The authors are listed in prov/HW5E27.jsonl.
"""The Benjamini-Yekutieli procedure.

It is the Benjamini-Hochberg step-up procedure run at level q / c_m, where
c_m = 1 + 1/2 + ... + 1/m. Find the largest k with p_(k) <= k q / (m c_m) and
reject H_(1), ..., H_(k). Under any dependence among the p-values, with valid
null p-values, the false discovery rate is at most (m_0 / m) q.

Comparisons are done as m c_m p <= k q. With fractions.Fraction inputs, c_m is
computed exactly as a Fraction, so the decisions are exact.
"""

import math
from collections.abc import Sequence
from fractions import Fraction


def harmonic(m: int, exact: bool = False):
    """c_m = 1 + 1/2 + ... + 1/m, as a Fraction if exact, else as a correctly summed float."""
    if exact:
        return sum((Fraction(1, i) for i in range(1, m + 1)), Fraction(0))
    return math.fsum(1 / i for i in range(1, m + 1))


def by(p: Sequence, q) -> tuple[list[bool], list]:
    """Return (reject, adjusted) in the original order of p."""
    m = len(p)
    if m == 0:
        return [], []
    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]")
    exact = isinstance(q, Fraction) or any(isinstance(v, Fraction) for v in p)
    c = harmonic(m, exact)  # @line L1
    order = sorted(range(m), key=lambda i: p[i])  # @line L2
    k = m
    while k >= 1 and m * c * p[order[k - 1]] > k * q:  # @line L3
        k -= 1  # @line L4
    reject = [False] * m
    for r in range(k):  # @line L5
        reject[order[r]] = True
    adjusted = [0] * m
    running = 1
    for r in range(m - 1, -1, -1):  # @line L6
        running = min(running, m * c * p[order[r]] / (r + 1))  # @line L7
        adjusted[order[r]] = running
    return reject, adjusted  # @line L8
test_benjamini_yekutieli_procedure.py 149 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/benjamini-yekutieli-procedure/. The authors are listed in prov/HW5E27.jsonl.

import itertools
import math
import random
import statistics
from fractions import Fraction

import pytest

from benjamini_yekutieli_procedure import by, harmonic

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_reference(p: list, q) -> list[bool]:
    """Benjamini-Hochberg as the largest self-consistent set: S = {i : m p_i <= s q} with |S| >= s, s largest."""
    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_harmonic() -> None:
    assert harmonic(4, exact=True) == Fraction(25, 12)
    assert harmonic(15) == pytest.approx(3.318228993, abs=1e-9)


def test_worked_example_by_hand() -> None:
    reject, adjusted = by(BH95, 0.05)
    # Thresholds k * 0.05 / (15 * 3.3182) = k * 0.0010046: 0.0019 <= 0.0030 passes, and 0.0095 > 0.0040,
    # ..., 0.0459 > 0.0090, 0.3240 > 0.0100 all fail, so the last passing rank is 3.
    assert [i for i, r in enumerate(reject) if r] == [0, 1, 2]
    c = harmonic(15)
    assert adjusted[:4] == pytest.approx([15 * c * 0.0001, 15 * c * 0.0004 / 2, 15 * c * 0.0019 / 3, 15 * c * 0.0095 / 4])


def test_equals_bh_at_reduced_level() -> None:
    rng = random.Random(2001)
    for _ in range(400):
        m = rng.randint(1, 12)
        p = [Fraction(rng.randint(0, 200), 1000) for _ in range(m)]
        q = Fraction(rng.randint(1, 20), 20)
        reject, adjusted = by(p, q)
        assert reject == bh_reference(p, q / harmonic(m, exact=True))
        assert reject == [a <= q for a in adjusted]


def test_floats_agree_with_fractions() -> None:
    rng = random.Random(3)
    for _ in range(300):
        m = rng.randint(1, 30)
        p = [rng.random() ** 4 for _ in range(m)]
        fp = [Fraction(v) for v in p]
        # Random floats do not fall within rounding error of a threshold, so the decisions must agree.
        assert by(p, 0.1)[0] == by(fp, Fraction(1, 10))[0]


def test_invalid_input() -> None:
    assert by([], 0.1) == ([], [])
    with pytest.raises(ValueError):
        by([0.5], 1.5)
    with pytest.raises(ValueError):
        by([-0.1], 0.1)


def worst_case_law(m: int, q: Fraction) -> list[tuple[Fraction, list]]:
    """A joint law of m valid null p-values under which BH at level q has FDR exactly q (1 + 1/2 + ... + 1/m).

    With probability q / k, choose k indices at random, set them to k q / m, and set the rest to 1.
    With the remaining probability 1 - q c_m, set all to 1. Each marginal has P(p <= k q / m) = k q / m.
    """
    law = []
    for k in range(1, m + 1):
        subsets = list(itertools.combinations(range(m), k))
        for s in subsets:
            law.append((q / k / len(subsets), [k * q / m if i in s else Fraction(1) for i in range(m)]))
    rest = 1 - q * harmonic(m, exact=True)
    assert rest >= 0
    law.append((rest, [Fraction(1)] * m))
    return law


def exact_fdr(law, procedure, m0: int) -> Fraction:
    total = Fraction(0)
    for prob, p in law:
        reject = procedure(p)
        r = sum(reject)
        if r:
            total += prob * Fraction(sum(reject[:m0]), r)
    return total


def test_bh_bound_is_attained_and_by_level_is_exact() -> None:
    for m in range(1, 6):
        q = Fraction(1, 4)
        law = worst_case_law(m, q)
        assert sum(w for w, _ in law) == 1
        for i in range(m):  # the marginals are valid and exact at the thresholds
            for u in [Fraction(j, 40) for j in range(41)]:
                assert sum(w for w, p in law if p[i] <= u) <= u
            for k in range(1, m + 1):
                assert sum(w for w, p in law if p[i] <= k * q / m) == k * q / m
        c = harmonic(m, exact=True)
        assert exact_fdr(law, lambda p: bh_reference(p, q), m) == q * c
        # The same law built for level q / c makes BY at q reach FDR exactly q.
        law_by = worst_case_law(m, q / c)
        assert exact_fdr(law_by, lambda p: by(p, q)[0], m) == q


def test_bound_under_arbitrary_dependence_exactly() -> None:
    """Null p-values uniform on {1/n, ..., 1}, coupled through random permutations of one uniform index."""
    rng = random.Random(7)
    n = 24
    for _ in range(300):
        m = rng.randint(2, 6)
        m0 = rng.randint(1, m)
        q = Fraction(rng.randint(1, 6), 20)
        perms = [rng.sample(range(1, n + 1), n) for _ in range(m)]
        alt = [Fraction(rng.randint(0, n), 4 * n) for _ in range(m - m0)]
        law = []
        for u in range(n):
            nulls = [Fraction(perms[i][u], n) for i in range(m0)]
            noise = [min(Fraction(1), a + Fraction(perms[m0 + j][u], 8 * n)) for j, a in enumerate(alt)]
            law.append((Fraction(1, n), nulls + noise))
        c = harmonic(m, exact=True)
        assert exact_fdr(law, lambda p: by(p, q)[0], m0) <= Fraction(m0, m) * q
        assert exact_fdr(law, lambda p: bh_reference(p, q), m0) <= Fraction(m0, m) * q * c


def test_monte_carlo_independent() -> None:
    """Under independence BY is BH at q / c_m, so its FDR is exactly (m0/m) q / c_m."""
    rng = random.Random(1165)
    nd = statistics.NormalDist()
    m, m0, q, reps = 20, 10, 0.2, 6000
    total = 0.0
    for _ in range(reps):
        z = [rng.gauss(0, 1) + (3.0 if i >= m0 else 0.0) for i in range(m)]
        reject, _ = by([nd.cdf(-v) for v in z], q)
        r = sum(reject)
        total += sum(reject[:m0]) / r if r else 0.0
    target = m0 / m * q / harmonic(m)
    # target = 0.0278; the false discovery proportion is in [0, 1] with mean target, so its standard deviation is
    # below sqrt(target) = 0.167 and four standard errors are below 0.009.
    assert abs(total / reps - target) < 0.009
    assert math.isclose(target, 0.02779, abs_tol=1e-4)

History

Benjamini and Hochberg (1995) proved control of the FDR for independent test statistics. Benjamini and Yekutieli (2001) extended it to positive regression dependence on the subset of true nulls, which covers one-sided tests on positively correlated normal statistics, and showed that dividing the level by ∑i=1m1/i\sum_{i=1}^m 1/i gives control under any dependence. Later work showed that this factor is the best possible for the linear thresholds, and Blanchard and Roquain placed the method in a family of dependence-free step-up rules.

References

  1. 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, the step-up procedure with constants k q / (m sum_(i=1..m) 1/i) and its FDR control under arbitrary dependence, and the PRDS condition for the plain procedure
  2. 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 that the Benjamini-Yekutieli procedure rescales, and the example with 15 p-values

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example