Implement Benjamini-Yekutieli FDR-adjusted p-values by hand
Quick Overview
Implement the Benjamini-Yekutieli false discovery rate correction from scratch in NumPy and return adjusted p-values in input order. It tests the harmonic penalty for arbitrary dependence, the monotone cumulative minimum and mapping sorted results back to their positions.
Implement Benjamini-Yekutieli FDR-adjusted p-values by hand
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `by_adjust_manual`, the Benjamini-Yekutieli FDR correction, from scratch. BY is BH scaled by `c(m) = sum(1/i for i in 1..m)`.
```python
def by_adjust_manual(pvals: list[float]) -> np.ndarray:
...
```
Requirements:
- With `m = len(pvals)` and p-values sorted ascending, the value at 1-indexed rank `i` is `p_(i) * m * c(m) / i`.
- Enforce the cumulative minimum scanning from the largest p-value down to the smallest.
- Clip results at `1.0`.
- Return a 1-D `float64` array in original input order.
- Output must match `multipletests(pvals, method='fdr_by')[1]` from `statsmodels` to floating-point tolerance.
Examples (values rounded to 6 decimals):
```text
Input: pvals = [0.03, 0.0012, 0.0096, 0.6]
Output: [0.083333, 0.01, 0.04, 1.0]
```
Here `m = 4` and `c(4) = 25/12`, so each sorted p-value is multiplied by `(25/3) / i`. The largest one becomes 1.25 and is clipped to 1.0.
```text
Input: pvals = [0.02, 0.01, 0.012]
Output: [0.036667, 0.033, 0.033]
```
Here `m * c(m) = 5.5`. The smallest p-value scales to 0.055, but the running minimum from the top lowers it to 0.033, the value at rank 2.
```hint Undoing the sort
If `order` is the permutation that sorts the input, think about which side of the assignment it belongs on when you write the results back.
```
```hint A way to test it
`c(m)` is the same for every rank. Work out how BY values relate to BH-adjusted values before clipping, and use that as a check.
```
### Constraints & Assumptions
- `pvals` is a 1-D list of values in `[0, 1]`, and values may repeat.
- `statsmodels` may be used only to check the result, never to compute it.
### What a Strong Answer Covers
- Computes the harmonic factor `c(m)` once and applies `m * c(m) / i` with 1-indexed ranks.
- Takes the running minimum from the largest p-value down and explains why that keeps adjusted values monotone in rank.
- Clips at 1 and writes the results back to the original positions with the inverse of the sort order.
- Explains why BY is more conservative than BH and which dependence assumption it removes.
- Checks the output against `multipletests` with a tolerance and notes how ties behave.
### Follow-up Questions
- How would you turn the adjusted values into reject decisions at a given `alpha`, and how do they compare with BH on the same p-values?
- When is the extra conservatism of BY worth it compared with BH, for example with correlated metrics such as clicks and CTR?
- How large is `c(m)` for 100 or 10,000 tests, and what does that do to power?
Overview: Implement the Benjamini-Yekutieli false discovery rate correction from scratch in NumPy and return adjusted p-values in input order. It tests the harmonic penalty for arbitrary dependence, the monotone cumulative minimum and mapping sorted results back to their positions.