Choose the CUPED covariate with the largest variance reduction
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `choose_best_covariate(y: np.ndarray, covariates: pd.DataFrame) -> tuple`. `y` is the experiment metric per user, and `covariates` holds candidate pre-experiment covariates, one column per candidate, with rows aligned to `y`. For each column compute `np.corrcoef(y, covariates[col].values)[0, 1] ** 2` and return `(best_col_name, best_reduction)` for the column with the highest $\rho^2$.
Requirements:
- Rank by $\rho^2$, not by signed $\rho$. A strong negative correlation counts.
- `best_reduction` is $\rho^2$ itself, not ${1 - \rho^2}$.
- Ties go to the first column in DataFrame order.
- Return exactly `(str, float)`.
Example:
```python
y = np.array([1, 1, 2, 3, 3])
covariates = pd.DataFrame({
"sessions": [1, 2, 2, 2, 3], # rho = 0.707..., rho^2 about 0.5
"days_idle": [3, 3, 2, 1, 1], # rho = -1.0, rho^2 = 1.0
"prev_spend": [2, 2, 4, 6, 6], # rho = 1.0, rho^2 = 1.0
})
choose_best_covariate(y, covariates)
# -> ('days_idle', 1.0)
```
`days_idle` moves exactly opposite to `y`, so it beats `sessions` even though its signed correlation is lower. It ties with `prev_spend` and wins because it comes first in column order.
```hint Tie-breaking
Pick the comparison in your running-maximum update so that a later column with an equal value cannot replace the current best.
```
```hint Degenerate columns
Check what `np.corrcoef` returns when one of the two series has zero variance, and how that value behaves in a comparison.
```
### Constraints & Assumptions
- `y` has length `n >= 2`, and `covariates` has `n` rows in the same order.
- `covariates` has at least one column, and every value is numeric with no missing entries.
- Every covariate is measured before the experiment starts, so treatment cannot affect it.
### What a Strong Answer Covers
- Why CUPED's variance reduction with a single covariate equals $\rho^2$, which makes it the right quantity to rank on.
- Ranking on the squared correlation so strongly negative covariates are kept.
- A single pass with a strict comparison so ties keep the first column, and the exact `(str, float)` return types.
- Time and space complexity, and the behavior for degenerate input such as a constant column.
- Why the covariate must come from before treatment assignment.
### Follow-up Questions
- How does $\rho^2$ translate into the actual variance reduction from CUPED, and into the sample size you need?
- How would you extend this to combine multiple covariates?
- Pre-period spend is missing for users who signed up recently. How would you still use it as a covariate?
- What goes wrong if the chosen covariate is measured after users are exposed to the treatment?
Overview: Write a function that picks the pre-experiment covariate giving CUPED the largest variance reduction by ranking columns on their squared correlation with the metric. It tests how CUPED reduces variance, handling of negative correlations, tie-breaking and degenerate columns.