Algorithm Search⌘K

Algorithmadvanced

Storey's q-value method

Also called q-value, adaptive FDR estimation.

Estimate the share of true nulls as #{p > lambda} / (m (1 - lambda)) and run BH at q / pi0. The q-value of a test is the smallest estimated FDR at which it is rejected.

Time
Θ(m log m) worst · best
Space
Θ(m) extra
Where
m = the number of hypotheses
Properties
deterministicnot exactofflineiterative
Origin
John D. Storey, Robert Tibshirani, Jonathan E. Taylor, David Siegmund, 2002

Storey's method makes the Benjamini-Hochberg procedure adaptive. BH controls the false discovery rate at (m0/m)q(m_0/m)q, not at qq, so when many nulls are false it is more conservative than it needs to be. Storey estimates the proportion of true nulls π0=m0/m\pi_0 = m_0/m from the p-values above a cutoff λ\lambda, which come mostly from true nulls, as π^0=#{i:pi>λ}/(m(1−λ))\hat\pi_0 = \#\{i : p_i > \lambda\}/(m(1 - \lambda)). Then he runs BH at q/π^0q/\hat\pi_0. The q-value of a hypothesis is the smallest estimated FDR among the rejection rules "reject pj≤tp_j \le t" that reject it; it is to the FDR what an adjusted p-value is to the FWER. With a small change to the estimator, the procedure controls the FDR at qq in finite samples when the null p-values are independent (Storey, Taylor and Siegmund 2004), and the plain version controls it asymptotically under weak dependence.

In plain words

When a study has many real effects, the usual false discovery method acts as if every test could be a false alarm, and so it is too strict. This method first estimates how many tests are really null. It looks at the tests with large p-values, which are mostly nulls, and scales their count up, because a null p-value is equally likely to land anywhere between 0 and 1. If only half the tests look null, the method allows itself about twice the usual leniency. Each test then gets a q-value: the smallest estimated share of false discoveries you must accept to call that test a discovery. Report the tests whose q-value is below the share you can live with.

The problem

Input. P-values p1,…,pmp_1, \dots, p_m, a tuning constant λ∈[0,1)\lambda \in [0, 1), often 1/21/2, and, for a rejection rule, a target level qq.

Output. The estimate π^0\hat\pi_0, the q-values q^1,…,q^m\hat q_1, \dots, \hat q_m, and the rejected set {i:q^i≤q}\{i : \hat q_i \le q\}.

Error rate controlled. The false discovery rate E[V/max⁡(R,1)]E[V/\max(R, 1)].

Dependence and level.

  • Finite samples, modified estimator. With π^0∗=(1+#{i:pi>λ})/(m(1−λ))\hat\pi_0^* = (1 + \#\{i : p_i > \lambda\})/(m(1 - \lambda)) and rejection thresholds limited to t≤λt \le \lambda, the FDR is at most (1−λm0)q≤q(1 - \lambda^{m_0})q \le q when the null p-values are valid and independent of each other and of the non-null p-values (Storey, Taylor and Siegmund 2004, Theorem 3). The proof page proves it, and the tests check it exactly on a grid and by Monte Carlo.
  • Asymptotic, plain estimator. For large mm with weakly dependent p-values whose empirical distribution functions converge, the plain rule controls the FDR at qq in the limit, and the q-values are simultaneously conservative (Theorems 4 to 7 of the same paper). In finite samples it can exceed qq; the tests measure 0.112 at q=0.1q = 0.1 in one model.
  • Arbitrary dependence. No guarantee. Strong positive correlation makes π^0\hat\pi_0 very noisy.

The algorithm

  1. STOREY-Q-VALUES(p₁, …, p_m, λ) ▷ p-values in [0, 1], 0 ≤ λ < 1
  2. 1W ← #{i : p_i > λ}
  3. 2π̂₀ ← min(1, W / (m(1 − λ)))
  4. 3sort the indices so that p_(1) ≤ … ≤ p_(m)
  5. 4run ← 1; for r ← m down to 1 do
  6. 5run ← min(run, π̂₀ · m · p_(r) / r)
  7. 6q̂_(r) ← run
  8. 7return q̂ ▷ reject H_i at level q when q̂_i ≤ q

The estimated FDR of the rule "reject pj≤tp_j \le t" is FDR^(t)=π^0mt/max⁡(#{j:pj≤t},1)\widehat{\text{FDR}}(t) = \hat\pi_0mt/\max(\#\{j : p_j \le t\}, 1). Over t≥p(r)t \ge p_{(r)}, between two data points it grows with tt, so its minimum is at a data point p(j)p_{(j)} with j≥rj \ge r, where it is π^0mp(j)/j\hat\pi_0mp_{(j)}/j. Lines L4 to L6 take that minimum as a running minimum from the right. With λ=0\lambda = 0, π^0=1\hat\pi_0 = 1 and the q-values are the BH adjusted p-values. Rejecting q^i≤q\hat q_i \le q is BH at level q/π^0q/\hat\pi_0 (Lemma 2 of Storey, Taylor and Siegmund).

The version with finite-sample control. Replace line L2 by π^0∗=(1+W)/(m(1−λ))\hat\pi_0^* = (1 + W)/(m(1 - \lambda)), without the cap, and reject the kk smallest p-values for the largest kk with p(k)≤λp_{(k)} \le \lambda and π^0∗mp(k)≤kq\hat\pi_0^*mp_{(k)} \le kq. This is the rule "reject pj≤tp_j \le t for the largest t≤λt \le \lambda with estimated FDR at most qq". The added 1 keeps π^0∗\hat\pi_0^* away from 0, and the limit t≤λt \le \lambda keeps the p-values used to estimate π0\pi_0 apart from the ones that are rejected.

Example

The 15 p-values of Benjamini and Hochberg (1995) (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), with λ=0.5\lambda = 0.5 and q=0.05q = 0.05. Four p-values exceed 0.5, so π^0=4/(15⋅0.5)=0.533\hat\pi_0 = 4/(15 \cdot 0.5) = 0.533 and π^0∗=5/7.5=0.667\hat\pi_0^* = 5/7.5 = 0.667.

kk p(k)p_{(k)} BH kq/mkq/m Storey kq/(mπ^0)kq/(m\hat\pi_0) STS kq/(mπ^0∗)kq/(m\hat\pi_0^*) q-value q^(k)\hat q_{(k)}
4 0.0095 0.0133 0.025 0.02 0.019
5 0.0201 0.0167 0.03125 0.025 0.0322
8 0.0344 0.0267 0.05 0.04 0.0344
9 0.0459 0.03 0.05625 0.045 0.0408
10 0.3240 0.0333 0.0625 0.05 0.2592

BH rejects 4. The plain Storey rule rejects 9, the last rank below its threshold; these are the nine q-values below 0.05. The STS version rejects 8, because 0.0459 is above 0.045. The q-values of all 15 are 0.0008, 0.0016, 0.0051, 0.019, 0.0322, 0.0341, 0.0341, 0.0344, 0.0408, 0.2592, 0.31, 0.3813, 0.4017, 0.4337 and 0.5333; the last one is π^0\hat\pi_0 itself.

Why the estimate is conservative. A uniform null p-value exceeds λ\lambda with probability 1−λ1 - \lambda, so E[W]≥m0(1−λ)E[W] \ge m_0(1 - \lambda), and the non-null p-values above λ\lambda only add to WW. Hence E[W/(m(1−λ))]≥m0/mE[W/(m(1 - \lambda))] \ge m_0/m: the uncapped estimate is biased upward, which errs on the safe side. The bias is the share of non-null p-values above λ\lambda; it shrinks as λ\lambda grows, while the variance grows, since fewer p-values are counted.

Correctness

The finite-sample guarantee for the modified procedure is a theorem of Storey, Taylor and Siegmund (2004), who prove it with a martingale argument. The proof page gives a leave-one-out proof instead. Fix a true null HiH_i. When pi≤λp_i \le \lambda, the count WW does not depend on pip_i, so given the other p-values the procedure is a step-up rule with fixed thresholds at most kq(1−λ)/(1+W−i)kq(1 - \lambda)/(1 + W_{-i}), where W−iW_{-i} counts the other p-values above λ\lambda. The argument used for BH then bounds the contribution of HiH_i to the FDR by q(1−λ)E[1/(1+W−i)]q(1 - \lambda)E[1/(1 + W_{-i})]. Finally W−iW_{-i} is at least a binomial count with m0−1m_0 - 1 trials and success probability 1−λ1 - \lambda, and E[1/(1+B)]=(1−λm0)/(m0(1−λ))E[1/(1 + B)] = (1 - \lambda^{m_0})/(m_0(1 - \lambda)) for such a count BB. Summing over the m0m_0 true nulls gives (1−λm0)q(1 - \lambda^{m_0})q. The proof page also shows that the lines compute the q-values of the definition, and that rejecting q^i≤q\hat q_i \le q is BH at level q/π^0q/\hat\pi_0. The asymptotic control of the plain version is only cited.

The tests check what can be checked:

  • the q-values against a brute force from the definition, a minimum over all thresholds, in exact rational arithmetic;
  • the STS rule against a brute force that finds the supremum threshold from its definition;
  • the rule q^i≤q\hat q_i \le q against BH at q/π^0q/\hat\pi_0;
  • the bound (1−λm0)q(1 - \lambda^{m_0})q exactly for 2≤m≤42 \le m \le 4, by summing over all values of independent null p-values uniform on {1/10,…,1}\{1/10, \dots, 1\}, with λ=1/2\lambda = 1/2 and q=1/5q = 1/5;
  • by Monte Carlo with m=40m = 40, m0=20m_0 = 20, independent normal statistics with mean 3 for the false nulls, q=0.1q = 0.1 and 2000 runs: the FDR is 0.049 for BH (the theory gives 0.05), 0.100 for STS and 0.112 for the plain q-value rule, and the average power is 0.911, 0.956 and 0.962.

The last line shows the trade-off: the adaptive methods gain power by spending the slack of BH, and the plain version slightly overspends in finite samples.

Proof: Storey's q-values are minimal estimated FDRs, and the Storey-Taylor-Siegmund procedure has FDR at most (1 - lambda^(m_0)) q under independencefull

We prove three things. First, the lines of the page compute the q-values of the definition, and rejecting q^i≤q\hat q_i \le q is BH at level q/π^0q/\hat\pi_0 (Proposition 1). Second, the uncapped estimate of π0\pi_0 is biased upward (Proposition 2). Third, the modified procedure of Storey, Taylor and Siegmund (2004) has FDR≤(1−λm0)q\text{FDR} \le (1 - \lambda^{m_0})q when the null p-values are independent of each other and of the non-null ones and valid (Theorem 4). The proof of Theorem 4 is a leave-one-out argument, not the martingale proof of the paper; it also covers discrete valid p-values, which is what the exact grid test in the code uses. The asymptotic control of the plain procedure under weak dependence is only cited.

Setting. P-values p1,…,pm∈[0,1]p_1, \dots, p_m \in [0, 1], 0≤λ<10 \le \lambda < 1, a level q∈(0,1]q \in (0, 1], N(t)=#{j:pj≤t}N(t) = \#\{j : p_j \le t\} and W=#{j:pj>λ}=m−N(λ)W = \#\{j : p_j > \lambda\} = m - N(\lambda). I0I_0 is the set of true nulls, m0=∣I0∣m_0 = |I_0|, and a null p-value is valid when P(pi≤u)≤uP(p_i \le u) \le u for all uu.

Proposition 1 (what the lines compute). Let π^0=min⁡(1,W/(m(1−λ)))\hat\pi_0 = \min(1, W/(m(1 - \lambda))) and FDR^(t)=π^0mt/max⁡(N(t),1)\widehat{\text{FDR}}(t) = \hat\pi_0mt/\max(N(t), 1). Lines L3 to L6 compute q^(r)=min⁡j≥rmin⁡(1,π^0mp(j)/j)\hat q_{(r)} = \min_{j \ge r}\min(1, \hat\pi_0mp_{(j)}/j), and this equals min⁡{min⁡(1,FDR^(t)):t≥p(r),t∈{p1,…,pm}}\min\{\min(1, \widehat{\text{FDR}}(t)) : t \ge p_{(r)}, t \in \{p_1, \dots, p_m\}\}, the definition that the tests use as a brute force. For q≤1q \le 1, {i:q^i≤q}\{i : \hat q_i \le q\} is the rejected set of BH at level q/π^0q/\hat\pi_0 when π^0>0\hat\pi_0 > 0.

Proof. The loop keeps a running minimum from the right, starting at 1, which gives the formula. For t=p(j)t = p_{(j)}, N(t)≥jN(t) \ge j, with equality when jj is the last rank among values equal to p(j)p_{(j)}. So FDR^(p(j))=π^0mp(j)/j′\widehat{\text{FDR}}(p_{(j)}) = \hat\pi_0mp_{(j)}/j' for the last such rank j′j', which is the smallest of the terms π^0mp(l)/l\hat\pi_0mp_{(l)}/l over the ranks ll tied with jj. Hence the minimum over the data points t≥p(r)t \ge p_{(r)} equals the minimum over ranks j≥rj \ge r. Finally q^(r)≤q\hat q_{(r)} \le q if and only if π^0mp(j)≤jq\hat\pi_0mp_{(j)} \le jq for some j≥rj \ge r, that is, p(j)≤j(q/π^0)/mp_{(j)} \le j(q/\hat\pi_0)/m for some j≥rj \ge r, which is the rejection of rank rr by BH at level q/π^0q/\hat\pi_0 (Lemma 1 in the proof for the Benjamini-Hochberg procedure). □\square

If π^0=0\hat\pi_0 = 0, every p-value is at most λ\lambda, every q-value is 0, and everything is rejected. This is one reason for the modified estimator below.

Proposition 2 (upward bias). If each null p-value is valid, then E[W/(m(1−λ))]≥m0/mE[W/(m(1 - \lambda))] \ge m_0/m.

Proof. For i∈I0i \in I_0, P(pi>λ)=1−P(pi≤λ)≥1−λP(p_i > \lambda) = 1 - P(p_i \le \lambda) \ge 1 - \lambda. So E[W]≥∑i∈I0P(pi>λ)≥m0(1−λ)E[W] \ge \sum_{i \in I_0}P(p_i > \lambda) \ge m_0(1 - \lambda). □\square

The modified procedure. Let π^0∗=(1+W)/(m(1−λ))\hat\pi_0^* = (1 + W)/(m(1 - \lambda)) and tk=min⁡(λ,kq/(mπ^0∗))=min⁡(λ,kq(1−λ)/(1+W))t_k = \min(\lambda, kq/(m\hat\pi_0^*)) = \min(\lambda, kq(1 - \lambda)/(1 + W)). The procedure rejects the kk smallest p-values for the largest kk with p(k)≤tkp_{(k)} \le t_k, or nothing. The thresholds are nondecreasing in kk, so, as in Lemma 1 of the BH proof, the number of rejections is R=max⁡{k:N(tk)≥k}R = \max\{k : N(t_k) \ge k\} and the rejected set is {i:pi≤tR}\{i : p_i \le t_R\}.

Lemma 3 (step-up with fixed thresholds, leave one out). Let 0≤s1≤⋯≤sm0 \le s_1 \le \dots \le s_m be fixed thresholds and consider the step-up procedure that rejects {j:pj≤sR}\{j : p_j \le s_R\} with R=max⁡{k:N(sk)≥k}R = \max\{k : N(s_k) \ge k\}. Fix ii, let R(i)R^{(i)} be its number of rejections when pip_i is replaced by 0, and suppose s1≥0s_1 \ge 0 so that R(i)≥1R^{(i)} \ge 1. Then HiH_i is rejected with R=kR = k exactly when pi≤skp_i \le s_k and R(i)=kR^{(i)} = k.

Proof. The proof of Lemma 3 in the proof for the Benjamini-Hochberg procedure uses only that the thresholds are nondecreasing, so it applies word for word with sks_k in place of kq/mkq/m. □\square

Theorem 4 (Storey, Taylor and Siegmund). Suppose that the null p-values are valid, that each null p-value is independent of all the other p-values, and that the null p-values are mutually independent. Then the modified procedure has

FDR≤(1−λm0)q≤q.\text{FDR} \le (1 - \lambda^{m_0})q \le q.

Proof. Fix i∈I0i \in I_0 and let W−i=#{j≠i:pj>λ}W_{-i} = \#\{j \ne i : p_j > \lambda\}, a function of the other p-values only. Define the fixed thresholds, given the other p-values, sk=min⁡(λ,kq(1−λ)/(1+W−i))s_k = \min(\lambda, kq(1 - \lambda)/(1 + W_{-i})).

Step 1: on the event that matters, the procedure is the fixed one. If pi≤λp_i \le \lambda, then W=W−iW = W_{-i}, so tk=skt_k = s_k for all kk and the two procedures make the same decisions. If pi>λp_i > \lambda, then HiH_i is rejected by neither, since both have thresholds at most λ\lambda. So in all cases 1{Hi rejected}/max⁡(R,1)=1{Hi rejected by s}/max⁡(Rs,1)\mathbf 1\{H_i \text{ rejected}\}/\max(R, 1) = \mathbf 1\{H_i \text{ rejected by } s\}/\max(R_s, 1), where RsR_s is the number of rejections with the thresholds ss.

Step 2: condition on the others. Given (pj)j≠i(p_j)_{j \ne i}, the thresholds sks_k and the count Rs(i)R_s^{(i)} of Lemma 3 are fixed, and pip_i keeps its law, by independence. By Lemma 3,

E[1{Hi rejected}max⁡(R,1) | (pj)j≠i]=∑k=1m1kP(pi≤sk)1{Rs(i)=k}≤∑k=1mskk1{Rs(i)=k}≤q(1−λ)1+W−i,E\left[\frac{\mathbf 1\{H_i \text{ rejected}\}}{\max(R, 1)} \,\middle|\, (p_j)_{j \ne i}\right] = \sum_{k=1}^m \frac1k P(p_i \le s_k)\mathbf 1\{R_s^{(i)} = k\} \le \sum_{k=1}^m \frac{s_k}{k}\mathbf 1\{R_s^{(i)} = k\} \le \frac{q(1 - \lambda)}{1 + W_{-i}},

because sk/k≤q(1−λ)/(1+W−i)s_k/k \le q(1 - \lambda)/(1 + W_{-i}) and exactly one indicator is 1.

Step 3: average over W−iW_{-i}. W−i≥BW_{-i} \ge B, where BB is the number of null j≠ij \ne i with pj>λp_j > \lambda. The m0−1m_0 - 1 events {pj>λ}\{p_j > \lambda\} are independent, each with probability at least 1−λ1 - \lambda, so BB is stochastically larger than a binomial variable B′B' with n=m0−1n = m_0 - 1 trials and success probability r=1−λr = 1 - \lambda. Since x↦1/(1+x)x \mapsto 1/(1 + x) is decreasing,

E[11+W−i]≤E[11+B′]=∑b=0n(nb)rb(1−r)n−bb+1=1(n+1)r∑b=0n(n+1b+1)rb+1(1−r)n−b=1−(1−r)n+1(n+1)r,E\left[\frac1{1 + W_{-i}}\right] \le E\left[\frac1{1 + B'}\right] = \sum_{b=0}^n \binom nb\frac{r^b(1 - r)^{n-b}}{b + 1} = \frac1{(n + 1)r}\sum_{b=0}^n \binom{n+1}{b+1}r^{b+1}(1 - r)^{n-b} = \frac{1 - (1 - r)^{n+1}}{(n + 1)r},

using (nb)/(b+1)=(n+1b+1)/(n+1)\binom nb/(b + 1) = \binom{n+1}{b+1}/(n + 1) and the binomial theorem with the missing term b+1=0b + 1 = 0. With n+1=m0n + 1 = m_0 and r=1−λr = 1 - \lambda this is (1−λm0)/(m0(1−λ))(1 - \lambda^{m_0})/(m_0(1 - \lambda)).

Step 4: sum. Taking expectations in Step 2 and using Step 3,

E[1{Hi rejected}max⁡(R,1)]≤q(1−λ)⋅1−λm0m0(1−λ)=(1−λm0)qm0.E\left[\frac{\mathbf 1\{H_i \text{ rejected}\}}{\max(R, 1)}\right] \le q(1 - \lambda)\cdot\frac{1 - \lambda^{m_0}}{m_0(1 - \lambda)} = \frac{(1 - \lambda^{m_0})q}{m_0}.

Summing over the m0m_0 true nulls gives FDR≤(1−λm0)q\text{FDR} \le (1 - \lambda^{m_0})q. If m0=0m_0 = 0 the FDR is 0. □\square

The added 1 in π^0∗\hat\pi_0^* is what makes Step 3 work: it turns 1/W1/W, whose mean is infinite when WW can be 0, into 1/(1+W)1/(1 + W), whose mean has the closed form above. The limit t≤λt \le \lambda is what makes Step 1 work: a rejected null p-value is never counted in WW.

Theorem 5 (equivalent threshold form). The modified procedure rejects {i:pi≤t^}\{i : p_i \le \hat t\}, where t^=sup⁡{t∈[0,λ]:π^0∗mt≤qmax⁡(N(t),1)}\hat t = \sup\{t \in [0, \lambda] : \hat\pi_0^*mt \le q\max(N(t), 1)\}, the definition of Storey, Taylor and Siegmund.

Proof. Write c=π^0∗mc = \hat\pi_0^*m and let A={t∈[0,λ]:ct≤qmax⁡(N(t),1)}A = \{t \in [0, \lambda] : ct \le q\max(N(t), 1)\}, the set of admissible tt. It contains 0.

AA contains its supremum. Take tn∈At_n \in A with tn→t^t_n \to \hat t and tn≤t^t_n \le \hat t. Since NN is nondecreasing, ctn≤qmax⁡(N(tn),1)≤qmax⁡(N(t^),1)ct_n \le q\max(N(t_n), 1) \le q\max(N(\hat t), 1), and letting n→∞n \to \infty gives ct^≤qmax⁡(N(t^),1)c\hat t \le q\max(N(\hat t), 1). Also t^≤λ\hat t \le \lambda. So t^∈A\hat t \in A.

Let j=N(t^)j = N(\hat t). If j≥1j \ge 1, then p(j)≤t^≤λp_{(j)} \le \hat t \le \lambda and cp(j)≤ct^≤qjcp_{(j)} \le c\hat t \le qj, so p(j)≤tjp_{(j)} \le t_j and j≤Rj \le R. If R≥1R \ge 1, then p(R)≤tR≤λp_{(R)} \le t_R \le \lambda and cp(R)≤qR≤qN(p(R))cp_{(R)} \le qR \le qN(p_{(R)}), so p(R)∈Ap_{(R)} \in A and t^≥p(R)\hat t \ge p_{(R)}, which gives j≥Rj \ge R. Hence N(t^)=RN(\hat t) = R in all cases. A set of the form {i:pi≤t}\{i : p_i \le t\} with exactly RR elements is the set of the RR smallest p-values, with no tie split, so {i:pi≤t^}={i:pi≤tR}\{i : p_i \le \hat t\} = \{i : p_i \le t_R\}. □\square

The test file checks Theorem 5 directly: it computes t^\hat t as the largest admissible point among 0, λ\lambda, the p-values and the points jq/(mπ^0∗)jq/(m\hat\pi_0^*), where the supremum must be attained, and compares the rejected sets in exact arithmetic.

What is cited, not proved. Storey, Taylor and Siegmund (2004, Theorems 4 to 7) show that, for large mm, under convergence conditions on the empirical distribution functions of the null and non-null p-values that allow weak dependence, the plain procedure with π^0\hat\pi_0 controls the FDR asymptotically and the q-values are simultaneously conservative. The tests measure the plain procedure in one model with m=40m = 40 and find an FDR of 0.112 at q=0.1q = 0.1, which shows that the plain version has no finite-sample guarantee.

Complexity

Counting WW takes Θ(m)\Theta(m), the sort at line L3 takes Θ(mlog⁡m)\Theta(m\log m) on a real RAM, and the running minimum Θ(m)\Theta(m). So the time is Θ(mlog⁡m)\Theta(m\log m) and the extra space Θ(m)\Theta(m). The rejections at one level need no sort, as for BH. Choosing λ\lambda by a smoother over a grid of gg values adds O(gm)O(gm), or O(mlog⁡m+g)O(m\log m + g) with the sorted p-values; choosing it by the bootstrap with BB resamples adds O(Bgm)O(Bgm).

Properties

  • Finite-sample FDR control for the modified procedure, under independent valid null p-values, at (1−λm0)q(1 - \lambda^{m_0})q. Proved.
  • Asymptotic FDR control for the plain procedure under weak dependence. Cited.
  • More powerful than BH whenever π^0<1\hat\pi_0 < 1, since its thresholds are 1/π^01/\hat\pi_0 times larger. When nearly all nulls are true, π^0\hat\pi_0 is near 1 and the methods agree.
  • q-values are monotone in the p-values, by the running minimum.
  • Bayesian meaning. Under the two-groups model with independent tests and nested rejection regions, the positive FDR of the region {p≤t}\{p \le t\} is P(H null∣p≤t)P(H \text{ null} \mid p \le t), so the q-value is a posterior probability of a tail, as the tail-area Fdr in the local false discovery rate page is (Storey 2003).

Variants and improvements

  • Choosing λ\lambda. Storey and Tibshirani (2003) fit a cubic spline to π^0(λ)\hat\pi_0(\lambda) over a grid of λ\lambda and take its value near λ=1\lambda = 1, where the bias is smallest. Storey (2002) chooses λ\lambda to minimize a bootstrap estimate of the mean squared error of π^0\hat\pi_0.
  • Other adaptive procedures. Benjamini, Krieger and Yekutieli (2006) estimate m0m_0 in a first BH pass and run a second pass with it. Their two-stage procedure also controls the FDR under independence.
  • pFDR q-values. Storey (2002) also defines q-values for the positive FDR, dividing by the estimated P(R>0)=1−(1−t)mP(R > 0) = 1 - (1 - t)^m; the version on this page, without that factor, is the common default.
  • Local fdr. The estimate of π0\pi_0 is the same idea as Efron's zero assumption for the local false discovery rate, which gives a posterior probability for each test instead of a tail.
  • Arbitrary dependence. No adaptive method on this page covers it. Under unknown dependence use Benjamini-Yekutieli, which gives up the factor m0/mm_0/m and more.

When to use it

Use q-values when many hypotheses are tested, a fair share of them are expected to be false, the tests are independent or weakly dependent, and the p-values are continuous and well calibrated. Genome-wide expression studies are the typical case, which is where the method became standard. When mm is small, use the modified STS version, which has the finite-sample guarantee. When the null p-values may not be uniform, for example when they are discrete or when the null distribution is misspecified, check the histogram of the p-values first: π^0\hat\pi_0 assumes a flat histogram for the nulls.

Pitfalls

  • A U-shaped or bumpy histogram near 1. Conservative tests give null p-values piled near 1 and inflate π^0\hat\pi_0; anti-conservative or misspecified tests deflate it and void the control.
  • Discrete p-values. Many p-values equal to 1, as with sparse count data, push π^0\hat\pi_0 up. It stays conservative but the gain over BH can vanish or reverse.
  • Reading the q-value as a probability for one test. The q-value is an FDR for the set of tests at least as significant, not the probability that this test is null. That is the local fdr, which is larger at the boundary.
  • Using π^0\hat\pi_0 capped at 1 in the finite-sample proof. The guarantee is for π^0∗\hat\pi_0^* with the added 1 and the limit t≤λt \le \lambda; the plain capped estimator is only asymptotically valid.
  • Strong dependence. With positively correlated tests, WW varies much more than under independence, and π^0\hat\pi_0 can be far too small in some data sets.

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.

storey_q_value.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/storey-q-value/. The authors are listed in prov/WKCJWX.jsonl.
"""Storey's estimate of the proportion of true nulls, q-values, and the
Storey-Taylor-Siegmund adaptive step-up procedure.

pi0_hat(lambda) = #{p_i > lambda} / (m (1 - lambda)) estimates pi_0 = m_0 / m.
The estimated FDR of the rule "reject p_i <= t" is pi0_hat m t / max(#{p_i <= t}, 1),
and the q-value of p_i is the smallest estimated FDR over the rules that reject it.

sts_reject is the version with finite-sample FDR control under independence
(Storey, Taylor and Siegmund 2004): it uses (1 + #{p_i > lambda}) in the
numerator and only thresholds t <= lambda.
"""

from collections.abc import Sequence
from fractions import Fraction


def _check(p: Sequence, lam) -> None:
    if any(not 0 <= v <= 1 for v in p):
        raise ValueError("p-values must lie in [0, 1]")
    if not 0 <= lam < 1:
        raise ValueError("lambda must lie in [0, 1)")


def pi0_estimate(p: Sequence, lam=0.5, plus_one: bool = False):
    """#{p_i > lam} / (m (1 - lam)), with 1 added to the count if plus_one; capped at 1 unless plus_one."""
    m = len(p)
    _check(p, lam)
    w = sum(1 for v in p if v > lam) + (1 if plus_one else 0)  # @line L1
    one = Fraction(1) if isinstance(lam, Fraction) else 1
    est = one * w / (m * (1 - lam))  # @line L2
    return est if plus_one else min(est, one)


def q_values(p: Sequence, lam=0.5) -> list:
    """Storey's q-values: q_(k) = min over j >= k of min(1, pi0_hat m p_(j) / j)."""
    m = len(p)
    if m == 0:
        return []
    pi0 = pi0_estimate(p, lam)
    order = sorted(range(m), key=lambda i: p[i])  # @line L3
    q = [0] * m
    running = 1
    for r in range(m - 1, -1, -1):  # @line L4
        running = min(running, pi0 * m * p[order[r]] / (r + 1))  # @line L5
        q[order[r]] = running  # @line L6
    return q  # @line L7


def sts_reject(p: Sequence, alpha, lam=0.5) -> list[bool]:
    """Reject p_i <= t, for the largest t <= lam with (1 + #{p_j > lam}) t / ((1 - lam) max(#{p_j <= t}, 1)) <= alpha.

    Equivalently, BH at level alpha / pi0_star on the p-values, keeping only ranks with p_(k) <= lam.
    """
    m = len(p)
    if m == 0:
        return []
    if not 0 < alpha <= 1:
        raise ValueError("alpha must lie in (0, 1]")
    pi0 = pi0_estimate(p, lam, plus_one=True)
    order = sorted(range(m), key=lambda i: p[i])
    k = m
    while k >= 1 and (p[order[k - 1]] > lam or pi0 * m * p[order[k - 1]] > k * alpha):
        k -= 1
    reject = [False] * m
    for r in range(k):
        reject[order[r]] = True
    return reject
test_storey_q_value.py 134 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/storey-q-value/. The authors are listed in prov/WKCJWX.jsonl.

import itertools
import random
import statistics
from fractions import Fraction

import pytest

from storey_q_value import pi0_estimate, q_values, sts_reject

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 q_values_brute_force(p: list, lam) -> list:
    """q_i = min over t in {p_j : p_j >= p_i} of min(1, pi0 m t / #{p_j <= t}), straight from the definition."""
    m = len(p)
    pi0 = min(Fraction(sum(1 for v in p if v > lam), 1) / (m * (1 - lam)), 1)
    return [min(min(Fraction(1), pi0 * m * t / sum(1 for v in p if v <= t)) for t in p if t >= pi) for pi in p]


def sts_brute_force(p: list, alpha, lam) -> list[bool]:
    """Storey-Taylor-Siegmund from the definition: t = sup{0 <= t <= lam : FDR*(t) <= alpha}, reject p_i <= t.

    FDR*(t) = pi0* m t / max(R(t), 1) increases between data points and drops at them, so the supremum is
    0, lam, a p-value, or a point j alpha / (pi0* m) where the ratio reaches alpha with R(t) = j.
    """
    m = len(p)
    pi0 = Fraction(1 + sum(1 for v in p if v > lam), 1) / (m * (1 - lam))
    candidates = {Fraction(0), lam} | {v for v in p if v <= lam}
    candidates |= {j * alpha / (pi0 * m) for j in range(1, m + 1) if j * alpha / (pi0 * m) <= lam}
    best = max(t for t in candidates if pi0 * m * t <= alpha * max(1, sum(1 for v in p if v <= t)))
    return [v <= best for v in p]


def test_worked_example_by_hand() -> None:
    # Four p-values are above 0.5, so pi0_hat = 4 / (15 * 0.5) = 0.5333 and pi0_star = 5 / 7.5 = 0.6667.
    assert pi0_estimate(BH95, 0.5) == pytest.approx(8 / 15)
    assert pi0_estimate(BH95, 0.5, plus_one=True) == pytest.approx(2 / 3)
    q = q_values(BH95, 0.5)
    # q_(9) = 0.5333 * 15 * 0.0459 / 9 = 0.0408, q_(10) = 0.5333 * 15 * 0.3240 / 10 = 0.2592.
    assert q[8] == pytest.approx(8 / 15 * 15 * 0.0459 / 9)
    assert sum(v <= 0.05 for v in q) == 9
    # The STS thresholds are k * 0.05 / (0.6667 * 15) = k * 0.005: 0.0344 <= 0.040 but 0.0459 > 0.045.
    assert sum(sts_reject(BH95, 0.05, 0.5)) == 8
    # With lambda = 0, pi0 = 1 and the q-values are the BH adjusted p-values: 4 below 0.05.
    assert sum(v <= 0.05 for v in q_values(BH95, 0)) == 4


def test_q_values_match_definition() -> None:
    rng = random.Random(2002)
    for _ in range(300):
        m = rng.randint(1, 12)
        p = [Fraction(rng.randint(0, 50), 50) for _ in range(m)]
        lam = Fraction(rng.choice([0, 1, 2, 3]), 4)
        assert q_values(p, lam) == q_values_brute_force(p, lam)


def test_sts_matches_definition() -> None:
    rng = random.Random(2004)
    for _ in range(500):
        m = rng.randint(1, 12)
        p = [Fraction(rng.randint(0, 40), 40) ** rng.choice([1, 2]) for _ in range(m)]
        lam = Fraction(rng.choice([1, 2, 3]), 4)
        alpha = Fraction(rng.randint(1, 8), 20)
        assert sts_reject(p, alpha, lam) == sts_brute_force(p, alpha, lam)


def test_q_value_rule_is_bh_at_alpha_over_pi0() -> None:
    """Rejecting q_i <= alpha is BH at level alpha / pi0_hat (Storey, Taylor and Siegmund 2004, Lemma 2)."""
    rng = random.Random(5)
    for _ in range(300):
        m = rng.randint(1, 12)
        p = [Fraction(rng.randint(0, 60), 60) for _ in range(m)]
        lam = Fraction(1, 2)
        pi0 = pi0_estimate(p, lam)
        alpha = Fraction(rng.randint(1, 6), 20)
        k = max([j for j in range(1, m + 1) if pi0 * m * sorted(p)[j - 1] <= j * alpha] or [0])
        threshold = sorted(p)[k - 1] if k else -1
        assert [v <= alpha for v in q_values(p, lam)] == [v <= threshold for v in p]


def test_invalid_input() -> None:
    assert q_values([], 0.5) == []
    assert sts_reject([], 0.1) == []
    with pytest.raises(ValueError):
        pi0_estimate([0.2], 1)
    with pytest.raises(ValueError):
        sts_reject([0.2], 0)


def test_exact_fdr_on_a_grid() -> None:
    """Null p-values uniform on {1/10, ..., 1}, independent; FDR summed exactly. STS has FDR <= (1 - lam^m0) alpha.

    The grid p-values are valid but not continuous; the leave-one-out proof on the page covers this case.
    """
    grid = [Fraction(k, 10) for k in range(1, 11)]
    lam, alpha = Fraction(1, 2), Fraction(1, 5)
    rng = random.Random(66)
    for m in (2, 3, 4):
        for m0 in range(1, m + 1):
            alt = [Fraction(rng.randint(0, 20), 100) for _ in range(m - m0)]
            total = Fraction(0)
            for nulls in itertools.product(grid, repeat=m0):
                reject = sts_reject(list(nulls) + alt, alpha, lam)
                r = sum(reject)
                if r:
                    total += Fraction(sum(reject[:m0]), r)
            assert total / 10**m0 <= (1 - lam**m0) * alpha


def test_monte_carlo_fdr_and_power() -> None:
    rng = random.Random(9440)
    nd = statistics.NormalDist()
    m, m0, alpha, reps = 40, 20, 0.1, 2000
    fdr = {"sts": 0.0, "q": 0.0, "bh": 0.0}
    power = dict.fromkeys(fdr, 0.0)
    for _ in range(reps):
        p = [nd.cdf(-(rng.gauss(0, 1) + (3.0 if i >= m0 else 0.0))) for i in range(m)]
        rules = {"sts": sts_reject(p, alpha), "q": [v <= alpha for v in q_values(p)], "bh": [v <= alpha for v in q_values(p, 0)]}
        for name, reject in rules.items():
            r = sum(reject)
            fdr[name] += sum(reject[:m0]) / r if r else 0.0
            power[name] += sum(reject[m0:]) / (m - m0)
    fdr = {k: v / reps for k, v in fdr.items()}
    power = {k: v / reps for k, v in power.items()}
    # BH has FDR exactly (m0/m) alpha = 0.05; STS is proved to be at most alpha = 0.1. Four standard errors < 0.02.
    assert abs(fdr["bh"] - 0.05) < 0.02
    assert fdr["sts"] <= 0.1 + 0.02
    # The plain q-value rule has no finite-sample guarantee. With this seed it measures 0.112, above alpha;
    # the standard error is 0.0018. It is only asymptotically valid.
    assert 0.1 < fdr["q"] < 0.13
    assert power["sts"] > power["bh"] and power["q"] > power["bh"]

History

Benjamini and Hochberg (1995) noted that their procedure is conservative by the factor m0/mm_0/m, and Benjamini and Hochberg (2000) proposed an adaptive version with an estimate of m0m_0, without a proof of control. Storey (2002) took the other view, estimating the FDR of a fixed rejection region with π^0(λ)\hat\pi_0(\lambda), and defined the pFDR and the q-value; Storey (2003) gave the Bayesian interpretation. Storey and Tibshirani (2003) made the q-value the standard summary for genome-wide studies. Storey, Taylor and Siegmund (2004) showed that thresholding the estimate is BH with mm replaced by π^0m\hat\pi_0m, and proved finite-sample control for the modified estimator and asymptotic control for the plain one.

References

  1. John D. Storey, A direct approach to false discovery rates, Journal of the Royal Statistical Society, Series B 64(3) (2002), pages 479 to 498, the estimate of pi_0 from the p-values above lambda, the estimated FDR and pFDR of a fixed rejection region, and the choice of lambda by the bootstrap
  2. John D. Storey, The positive false discovery rate, a Bayesian interpretation and the q-value, The Annals of Statistics 31(6) (2003), pages 2013 to 2035, the q-value as the minimum pFDR over the nested rejection regions that contain the statistic, and its posterior interpretation under the mixture model
  3. John D. Storey and Robert Tibshirani, Statistical significance for genomewide studies, Proceedings of the National Academy of Sciences 100(16) (2003), pages 9440 to 9445, the q-value for genome-wide studies, its estimate from the p-values, and the estimate of pi_0 from a smoother over lambda
  4. John D. Storey, Jonathan E. Taylor and David Siegmund, Strong control, conservative point estimation and simultaneous conservative consistency of false discovery rates, a unified approach, Journal of the Royal Statistical Society, Series B 66(1) (2004), pages 187 to 205, Theorem 1 (the estimated FDR is conservative), Lemma 2 (thresholding the estimate is BH with m replaced by pi0-hat m), Theorem 3 (FDR at most (1 - lambda^(m_0)) alpha for the modified procedure with independent null p-values), and Theorems 4 to 7 (asymptotic control and q-values under weak dependence)

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
  • a scene preset for the example