Benjamini-Hochberg correct a pandas Series of p-values keyed by metric name
Quick Overview
Apply the Benjamini-Hochberg correction to a pandas Series of p-values keyed by metric name using statsmodels, and return corrected values with the original labels and order. It tests multiple-testing correction in practice and careful index alignment in pandas.
Benjamini-Hochberg correct a pandas Series of p-values keyed by metric name
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `bh_adjust_series`, where `pvals` holds raw p-values indexed by metric name. Apply Benjamini-Hochberg via `multipletests(pvals, alpha=alpha, method='fdr_bh')` from `statsmodels` and return a new Series of corrected p-values.
```python
def bh_adjust_series(pvals: pd.Series, alpha: float = 0.05) -> pd.Series:
...
```
Requirements:
- Values are element `[1]` of the returned 4-tuple.
- Preserve index labels and their original order: do not sort or reset the index.
- Result dtype is `float64`.
Example:
```python
pvals = pd.Series({'ctr': 0.001, 'revenue': 0.20, 'latency': 0.04, 'retention': 0.30})
bh_adjust_series(pvals)
# ctr 0.00400000
# revenue 0.26666667
# latency 0.08000000
# retention 0.30000000
# dtype: float64
```
```hint What comes back
Check what type `multipletests` returns when you hand it a Series, and what that means for the metric names.
```
### What a Strong Answer Covers
- Takes element `[1]` of the `multipletests` tuple as the corrected p-values.
- Rebuilds the result with the original index and order, and explains why positional alignment is safe here.
- Returns `float64` values without sorting or resetting the index.
- Knows which outputs the `alpha` argument affects and which it does not.
### Follow-up Questions
- How would you also return the boolean reject flags at `alpha`?
- How should NaN p-values be handled?
- If the metrics come from several experiments, would you correct them in one call or per experiment?
Overview: Apply the Benjamini-Hochberg correction to a pandas Series of p-values keyed by metric name using statsmodels, and return corrected values with the original labels and order. It tests multiple-testing correction in practice and careful index alignment in pandas.
Implement bh_adjust_series, where pvals holds raw p-values indexed by metric name. Apply Benjamini-Hochberg via multipletests(pvals, alpha=alpha, method='fdr_bh') from statsmodels and return a new Series of corrected p-values.