Measure the variance removed by winsorizing the upper tail of a metric
Quick Overview
Write a NumPy function that caps the upper tail of a heavy-tailed per-user metric at a quantile and reports the fraction of sample variance removed. It tests quantile interpolation, matching normalizations in a variance ratio, degenerate inputs, and how winsorizing affects experiment power.
Measure the variance removed by winsorizing the upper tail of a metric
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `winsorization_variance_reduction(x, upper_pct)`. `x` is a 1-D float64 array holding a heavy-tailed per-user metric. Return the fraction of variance removed by capping the upper tail.
Requirements:
- Cap with `np.minimum(x, cap)` where `cap = np.quantile(x, upper_pct)` (default linear interpolation).
- Use the sample variance `np.var(..., ddof=1)` for both `var_raw` and `var_capped`, not the default `ddof=0`.
- Return `1 - var_capped / var_raw` as a plain `float`.
```python
x = np.array([1.0, 2.0, 3.0, 4.0, 100.0])
winsorization_variance_reduction(x, upper_pct=0.9) # -> about 0.6322
```
The 90th percentile of these five values is 61.6, so only 100.0 is capped, and the sample variance falls from 1902.5 to about 699.81.
```hint Keep the raw values
The ratio needs the variance of the original array as well as the capped one. Make sure the capping step leaves `x` unchanged until you have computed `var_raw`.
```
### Constraints & Assumptions
- `x` is a 1-D float64 array of per-user values with a heavy upper tail.
- `upper_pct` is a quantile level between 0 and 1.
### What a Strong Answer Covers
- Computing the cap with the stated quantile rule and capping only the upper tail.
- Using `ddof=1` for both variances, and why the two normalizations must match.
- Returning a plain Python `float`.
- Behavior on degenerate inputs: constant values, a single element, missing values.
- Time and space cost.
### Follow-up Questions
- How would you also cap the lower tail symmetrically?
- What if `var_raw` is zero or the array is tiny?
- How does the variance reduction translate into the sample size an A/B test needs, and what can offset the gain?
Overview: Write a NumPy function that caps the upper tail of a heavy-tailed per-user metric at a quantile and reports the fraction of sample variance removed. It tests quantile interpolation, matching normalizations in a variance ratio, degenerate inputs, and how winsorizing affects experiment power.
Implement winsorization_variance_reduction(x, upper_pct). x is a 1-D float64 array holding a heavy-tailed per-user metric. Return the fraction of variance removed by capping the upper tail.
Requirements:
Cap with
np.minimum(x, cap)
where
cap = np.quantile(x, upper_pct)
(default linear interpolation).
Use the sample variance
np.var(..., ddof=1)
for both
var_raw
and
var_capped
, not the default
ddof=0
.
Return
1 - var_capped / var_raw
as a plain
float
.
x = np.array([1.0, 2.0, 3.0, 4.0, 100.0])
winsorization_variance_reduction(x, upper_pct=0.9) # -> about 0.6322
The 90th percentile of these five values is 61.6, so only 100.0 is capped, and the sample variance falls from 1902.5 to about 699.81.
Constraints & Assumptions
x
is a 1-D float64 array of per-user values with a heavy upper tail.
upper_pct
is a quantile level between 0 and 1.
What a Strong Answer Covers Guidance
Computing the cap with the stated quantile rule and capping only the upper tail.
Using
ddof=1
for both variances, and why the two normalizations must match.
Returning a plain Python
float
.
Behavior on degenerate inputs: constant values, a single element, missing values.
Time and space cost.
Follow-up Questions Guidance
How would you also cap the lower tail symmetrically?
What if
var_raw
is zero or the array is tiny?
How does the variance reduction translate into the sample size an A/B test needs, and what can offset the gain?