WRITE · TRACE · UNDERSTAND

Adjust a family of discoveries without losing original order

Implement bh_adjust(pvalues) returning BH adjusted values in original order, capped at 1.

Read lesson: Adjust a family of discoveries without losing original order →

Your experiment

Implement bh_adjust(pvalues) returning BH adjusted values in original order, capped at 1. Accept empty input. Reject nonfinite values or values outside [0,1].

Python runs in your browser. Your draft stays on this device.

What to look for

BH controls a false-discovery rate under conditions such as independent or suitable positively dependent tests. It does not establish a causal effect or replace a predeclared experiment and a meaningful effect-size threshold.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Adjust a family of discoveries without losing original order works

The idea

Testing many hypotheses at once guarantees some small p-values by chance. The Benjamini-Hochberg procedure controls the false discovery rate: the expected share of false positives among the results declared significant. It is less strict than Bonferroni, which guards against even one false positive.

The adjusted value for the p-value of rank j out of m, counting from the smallest, is p * m / j. Those raw values are not guaranteed to rise with rank, so bh_adjust walks from the largest p-value down and keeps a running minimum in best, which starts at 1.0 and so also caps the result. Sorting indices instead of values lets each result be written back to its original position.

How it works

  1. Reject any p-value that is not finite or lies outside 0 to 1; an empty list returns an empty list.
  2. Build order, the indices sorted by their p-value, leaving the input untouched.
  3. Start best at 1.0 and visit the ranks from the largest p-value down to the smallest.
  4. At position j, compute p * m / (j + 1) and lower best to it if it is smaller.
  5. Store best in result at the original index i.
  6. Return result, which lines up with the input order.

What it costs

Time
O(n log n)
Space
O(n)

Sorting the indices dominates; the backward pass that takes the running minimum is linear.

Where it is used

  • R's p.adjust(p, method = "BH") performs this exact computation with a cumulative minimum taken from the largest p-value.
  • statsmodels offers it as multipletests(pvals, method='fdr_bh').
  • DESeq2 reports a BH-adjusted padj column for every gene in a differential expression analysis.

Watch out for

  • Skipping the running minimum, which can give a smaller p-value a larger adjusted value than a bigger one.
  • Returning the values in sorted order instead of writing each one back to its original index.
  • Reading an adjusted value below 0.05 as a 5 percent chance that this one finding is false; the rate applies to the whole set of discoveries.