Adjust P-Values for Multiple Testing Corrections
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests implementation of Bonferroni and Holm-Bonferroni procedures to control family-wise error rate when conducting multiple hypothesis tests.
When conducting multiple hypothesis tests simultaneously, the likelihood of observing at least one false positive grows with the number of tests. Multiple testing corrections adjust raw p-values upward to maintain proper error control.
Implement a function that accepts a list of raw p-values and a method name, returning the corrected p-values in their original order.
Correction Methods
Bonferroni correction: Multiply each raw p-value by n (the total count of tests). If the result exceeds 1.0, return 1.0 instead.
Holm correction (sequential Bonferroni): A step-down procedure that maintains the same family-wise error rate control as Bonferroni but with greater statistical power.
Algorithm:
- Sort p-values in ascending order while tracking their original indices
- Process in sorted order: multiply the i-th smallest p-value (using 0-based indexing) by (n - i)
- Apply the ceiling of 1.0 to each multiplication result
- Enforce monotonicity: traverse the sorted adjusted values and ensure each is at least as large as all previous values in sorted order (take the running maximum)
- Place each adjusted value back at its original input position
Constraints
- 0 ≤ len(p_values) ≤ 1000
- 0.0 ≤ each p-value ≤ 1.0
- method is either "bonferroni" or "holm"
- If the input list is empty, return an empty list
Examples
Example 1
Input: adjust_p_values([0.01, 0.04, 0.2], 'bonferroni')
Output: [0.03, 0.12, 0.6000000000000001]
Example 2
Input: adjust_p_values([0.01, 0.04, 0.2], 'holm')
Output: [0.03, 0.08, 0.2]
Example 3
Input: adjust_p_values([0.001, 0.001, 0.05, 0.4], 'bonferroni')
Output: [0.004, 0.004, 0.2, 1]
Example 4
Input: adjust_p_values([0.001, 0.001, 0.05, 0.4], 'holm')
Output: [0.004, 0.004, 0.1, 0.4]
Hints
Hint 1
Bonferroni is a direct transformation: scale each value by n and cap at 1.0.
Hint 2
For Holm, pair each p-value with its original index before sorting to enable mapping results back to input order.
Hint 3
The monotonicity step in Holm means maintaining a running maximum as you process the sorted sequence—each adjusted value must be at least as large as the one before it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Multiple testing corrections compensate for the inflation of Type I error (false positive) probability when testing many hypotheses. Without correction, testing 20 independent hypotheses at α = 0.05 yields roughly a 64% chance of at least one false rejection.
Bonferroni applies a uniform penalty to all tests by multiplying each p-value by n. This is conservative but guarantees family-wise error rate control. Conceptually, we're testing each hypothesis at a threshold of α/n rather than α.
Holm refines Bonferroni with a sequential approach. It recognizes that once the most significant findings are validated, less stringent corrections can apply to subsequent tests. By processing p-values from smallest to largest and applying decreasing multipliers (n, n-1, n-2, ..., 1), we gain power while preserving error control. The monotonicity requirement prevents a logical inconsistency where a larger raw p-value ends up with a smaller adjusted p-value than a smaller raw p-value.
Implementation Strategy
For Bonferroni: iterate the list, multiplying each element by n and capping at 1.0.
For Holm:
- Create pairs of (index, p-value) and sort by p-value
- Traverse in sorted order, applying multipliers n, n-1, ..., 1
- Maintain a running maximum to satisfy monotonicity: if the current calculation yields a value smaller than any prior adjusted value, use the prior maximum instead
- Write each adjusted value to its original position in the output array
The running maximum handles cases where raw p-values are close together—once we've set a floor based on an earlier calculation, subsequent values cannot dip below it.
Complexity
Time complexity: O(n) for Bonferroni (single pass). O(n log n) for Holm (dominated by sorting).
Space complexity: O(n) for the output array in both cases. Holm requires additional O(n) space for the index array used in sorting.
Solution
from typing import List
def adjust_p_values(p_values: List[float], method: str) -> List[float]:
n = len(p_values)
if n == 0:
return []
if method == "bonferroni":
return [min(1.0, p * n) for p in p_values]
if method == "holm":
ordered = sorted(range(n), key=lambda i: p_values[i])
adjusted = [0.0] * n
running_max = 0.0
for rank, index in enumerate(ordered):
value = min(1.0, (n - rank) * p_values[index])
running_max = max(running_max, value)
adjusted[index] = running_max
return adjusted
raise ValueError("method must be 'bonferroni' or 'holm'")