Winsorize an experiment metric with one pooled cap and report per-variant variance reduction
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def winsorize_shared_threshold_by_variant(df: pd.DataFrame, upper_pct: float) -> pd.DataFrame`. `df` has one row per user with columns `variant` (object, `'control'` or `'treatment'`), `user_id` (object) and `value` (float64). Winsorize with one pooled cap and report the per-variant capped mean and variance reduction.
Requirements:
- Compute one shared cap on the pooled values: `cap = np.quantile(df['value'], upper_pct)` (default linear interpolation, no per-variant thresholds).
- Apply `min(value, cap)` to every row regardless of variant.
- Per variant, `capped_mean` is the mean of the capped values and `var_reduction = 1 - np.var(capped_within_variant, ddof=1) / np.var(raw_within_variant, ddof=1)`, where raw means the uncapped `value`.
- Return columns `['variant', 'capped_mean', 'var_reduction']`, one row per variant, sorted by `variant` ascending, with the index reset.
- Both metric columns are float64.
For example:
```python
df = pd.DataFrame({
"variant": ["control"] * 4 + ["treatment"] * 4,
"user_id": ["c1", "c2", "c3", "c4", "t1", "t2", "t3", "t4"],
"value": [1.0, 2.0, 3.0, 10.0, 2.0, 4.0, 6.0, 20.0],
})
winsorize_shared_threshold_by_variant(df, upper_pct=0.75)
```
| variant | capped_mean | var_reduction |
|---|---|---|
| control | 3.25 | 0.585 |
| treatment | 4.75 | 0.92625 |
The pooled 75th percentile of the eight values is 7.0, so 10.0 and 20.0 are both capped at 7.0.
```hint Keep both versions
The variance ratio needs the raw and the capped values for the same rows. Make sure capping does not overwrite the column you still need.
```
```hint Matching variances
`np.var` defaults to `ddof=0` and pandas `.var()` defaults to `ddof=1`. Check that both variances in the ratio are computed the same way.
```
### Constraints & Assumptions
- One row per user, with columns `variant`, `user_id` and `value` (float64).
- `variant` takes the values `'control'` and `'treatment'`.
- `upper_pct` is a quantile level between 0 and 1.
### What a Strong Answer Covers
- Computing a single cap on the pooled values with the stated quantile rule, and why per-arm caps would bias the comparison.
- Applying that cap to every row and computing both metrics within each variant from the same rows.
- Using the same sample variance (`ddof=1`) for the raw and capped values.
- Meeting the output contract: column order, one row per variant sorted by name, reset index, float64 metrics.
- Degenerate inputs such as a variant with one row, a variant with constant values, or missing values.
### Follow-up Questions
- How would you also winsorize the lower tail symmetrically?
- Does the difference in capped means estimate the same thing as the difference in raw means? When does capping hide a real effect?
- How would you choose `upper_pct`, and why should it be fixed before you look at the results?
Overview: Implement a pandas function that winsorizes a per-user experiment metric at one quantile cap computed on both arms together, then reports each variant's capped mean and variance reduction. It tests why a shared threshold keeps the comparison fair, consistent sample variances, and how degenerate groups behave.