KatInterview

Adjust P-Values for Multiple Testing Corrections

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashingstatisticshypothesis-testingmultiple-testing

Last 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:

  1. Sort p-values in ascending order while tracking their original indices
  2. Process in sorted order: multiply the i-th smallest p-value (using 0-based indexing) by (n - i)
  3. Apply the ceiling of 1.0 to each multiplication result
  4. 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)
  5. 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.