CUPED Expected Variance Reduction
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
CUPED lowers the variance of an experiment metric `y` by adjusting it with a pre-experiment covariate `x`. With the optimal adjustment, the fraction of variance it removes is the squared Pearson correlation between the two.
Implement `expected_variance_reduction(y: np.ndarray, x: np.ndarray) -> float` in Python. It returns the theoretical fraction of variance CUPED removes from `y` using `x`: compute `rho = np.corrcoef(y, x)[0, 1]` and return `rho ** 2`.
- Return the fraction removed. Do not return `1 - rho ** 2`, which is the fraction that remains, `var(Y_cuped) / var(Y)`.
- Return a plain, unrounded `float` in `[0, 1]`.
```python
y = [12, 15, 9, 20, 14, 11, 18, 16, 8, 22]
x = [10, 13, 8, 17, 12, 10, 15, 14, 7, 19]
expected_variance_reduction(y, x)
# rho = 0.9974669797088627
# -> 0.9949403756095206
```
```hint Watch the inputs
The example passes Python lists, and `np.corrcoef` returns a 2 by 2 matrix. Think about which entry you need, and what NumPy gives back when one of the series never changes.
```
### Constraints & Assumptions
- `y` and `x` are one-dimensional, have the same length and hold at least two values. `x[i]` is the pre-experiment value for the same unit as `y[i]`.
- Either input may be a Python list or a NumPy array of numbers.
### What a Strong Answer Covers
- Computes the Pearson correlation of `y` and `x`, takes the off-diagonal entry, and returns its square as a Python `float`.
- Derives from the variance of the adjusted metric why `rho^2` is the fraction removed and `1 - rho^2` the fraction remaining.
- Handles inputs that break the correlation: mismatched lengths, fewer than two points, a constant series, missing values.
- Explains why the sample `rho^2` can differ from the reduction a live experiment actually gets.
### Follow-up Questions
- Why does the actual observed reduction differ from `rho ** 2`?
- Where should `x` come from, and what goes wrong if it is measured after the experiment starts?
- Before launching, how would you check that a covariate delivers the reduction you expect?
Overview: Write a Python function that computes the share of metric variance CUPED removes with a pre-experiment covariate, using the Pearson correlation between the two. It tests the CUPED variance identity, handling of degenerate inputs, and why a sample estimate differs from the reduction a live experiment gets.