Implement the Benjamini-Hochberg step-up reject rule

Quick Overview

Implement the Benjamini-Hochberg step-up procedure in NumPy: sort the p-values, find the largest rank that passes its threshold, and return a rejection mask in input order. It tests multiple-testing correction, false discovery rate control and careful index handling.

Implement the Benjamini-Hochberg step-up reject rule

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `bh_reject_stepup` from scratch, applying the Benjamini-Hochberg step-up procedure. Sort the p-values ascending, find the largest 1-indexed rank `k` with `p_(k) <= (k / m) * alpha`, and reject every hypothesis whose raw p-value is `<= p_(k)`. ```python def bh_reject_stepup(pvals: list[float], alpha: float = 0.05) -> np.ndarray: ... ``` Requirements: - Reject all p-values `<= p_(k)`, including those that fail their own `(i/m)*alpha` threshold. - Return a 1-D `bool` numpy array of length `m` in the original input order. - If no `k` qualifies, return all `False`. - Do not call any FDR helper from `statsmodels`. Examples: ```text Input: pvals = [0.001, 0.008, 0.039, 0.041, 0.042, 0.06, 0.074, 0.205, 0.212, 0.216], alpha = 0.05 Output: [True, True, False, False, False, False, False, False, False, False] ``` The largest passing rank is `k = 2`, so the cutoff is `p_(2) = 0.008` and only the first two hypotheses are rejected. ```text Input: pvals = [0.035, 0.9, 0.02, 0.03], alpha = 0.05 Output: [True, False, True, True] ``` Sorted, the p-values are 0.02, 0.03, 0.035 and 0.9 against thresholds 0.0125, 0.025, 0.0375 and 0.05. Only rank 3 passes, so the cutoff is 0.035, and 0.02 and 0.03 are rejected even though they miss their own thresholds. ```hint Which rank to use Check every rank before you choose `k`. A small rank can fail its threshold while a larger rank passes. ``` ```hint Keeping the input order Once you have the cutoff, ask whether you need to undo the sort at all. ``` ### Constraints & Assumptions - `pvals` is non-empty, and each entry is in `[0, 1]`. - `m = len(pvals)` is the number of hypotheses tested. - Repeated p-values are allowed. ### What a Strong Answer Covers - Picks the largest rank whose sorted p-value passes `(k/m)*alpha`, even when smaller ranks fail. - Rejects by comparing raw p-values with the single cutoff `p_(k)`, so hypotheses below rank `k` that miss their own line are still rejected. - Returns the mask in the original input order and gives tied p-values the same decision. - Handles the case where no rank passes, and states the `O(m log m)` time and `O(m)` space cost. ### Follow-up Questions - How would you return BH-adjusted q-values instead of a boolean mask? - How does the procedure change under arbitrary dependence between tests? - When would you choose Bonferroni or Holm over BH for a set of experiment metrics?

Overview: Implement the Benjamini-Hochberg step-up procedure in NumPy: sort the p-values, find the largest rank that passes its threshold, and return a rejection mask in input order. It tests multiple-testing correction, false discovery rate control and careful index handling.

General logo
General
Nov 25, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement bh_reject_stepup from scratch, applying the Benjamini-Hochberg step-up procedure. Sort the p-values ascending, find the largest 1-indexed rank k with p_(k) <= (k / m) * alpha, and reject every hypothesis whose raw p-value is <= p_(k).

def bh_reject_stepup(pvals: list[float], alpha: float = 0.05) -> np.ndarray:
    ...

Requirements:

  • Reject all p-values <= p_(k) , including those that fail their own (i/m)*alpha threshold.
  • Return a 1-D bool numpy array of length m in the original input order.
  • If no k qualifies, return all False .
  • Do not call any FDR helper from statsmodels .

Examples:

Input: pvals = [0.001, 0.008, 0.039, 0.041, 0.042, 0.06, 0.074, 0.205, 0.212, 0.216], alpha = 0.05
Output: [True, True, False, False, False, False, False, False, False, False]

The largest passing rank is k = 2, so the cutoff is p_(2) = 0.008 and only the first two hypotheses are rejected.

Input: pvals = [0.035, 0.9, 0.02, 0.03], alpha = 0.05
Output: [True, False, True, True]

Sorted, the p-values are 0.02, 0.03, 0.035 and 0.9 against thresholds 0.0125, 0.025, 0.0375 and 0.05. Only rank 3 passes, so the cutoff is 0.035, and 0.02 and 0.03 are rejected even though they miss their own thresholds.

Constraints & Assumptions

  • pvals is non-empty, and each entry is in [0, 1] .
  • m = len(pvals) is the number of hypotheses tested.
  • Repeated p-values are allowed.

What a Strong Answer Covers Guidance

  • Picks the largest rank whose sorted p-value passes (k/m)*alpha , even when smaller ranks fail.
  • Rejects by comparing raw p-values with the single cutoff p_(k) , so hypotheses below rank k that miss their own line are still rejected.
  • Returns the mask in the original input order and gives tied p-values the same decision.
  • Handles the case where no rank passes, and states the O(m log m) time and O(m) space cost.

Follow-up Questions Guidance

  • How would you return BH-adjusted q-values instead of a boolean mask?
  • How does the procedure change under arbitrary dependence between tests?
  • When would you choose Bonferroni or Holm over BH for a set of experiment metrics?