Compute a CUPED-adjusted treatment effect and its standard error in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def cuped_ate_with_se(df: pd.DataFrame) -> dict`. The DataFrame `df` has columns `group` (`'control'` or `'treatment'`), `y` (the outcome) and `x` (a pre-experiment covariate). Return a dict with exactly the keys `'effect'` and `'se'`, both floats.
Requirements:
- Use a pooled theta, estimated on all rows and not per arm: `np.cov(df['y'], df['x'], ddof=1)[0, 1] / np.var(df['x'], ddof=1)`.
- Compute `Y_cuped = y - theta * (x - x_bar)`, with `x_bar` the single global mean of `x`. Do not use per-arm means.
- `'effect'` is `mean(Y_cuped | treatment) - mean(Y_cuped | control)`.
- `'se'` is `sqrt(var(Y_cuped | treatment, ddof=1) / n_treatment + var(Y_cuped | control, ddof=1) / n_control)`. This is the unpooled two-sample form, computed on `Y_cuped`. Do not compute it on raw `y`, and do not use a paired SE.
- Use `ddof=1` consistently, in theta and in the SE variances.
Example:
```text
group x y
control 1 3
control 3 4
control 5 8
control 7 9
treatment 2 6
treatment 4 7
treatment 6 10
treatment 8 13
cuped_ate_with_se(df) -> {'effect': 1.7619047..., 'se': 0.5331065...}
```
The exact values are `effect = 37/21` and `se = sqrt(376/1323)`. For the same frame, the raw difference in means is 3 and its unpooled SE is about 2.160.
```hint Defaults differ
`np.var` divides by `n` by default, while `np.cov` and pandas `Series.var` divide by `n - 1`. Pass `ddof=1` everywhere it applies.
```
```hint A sanity check
Compute the unpooled SE of the raw difference on `y` as well. If the adjustment is working, the CUPED SE is smaller, and the ratio tells you how much `x` explains.
```
### Constraints & Assumptions
- pandas and numpy are available.
- `df` has one row per user. The `group` values are exactly `'control'` and `'treatment'`.
- Both arms are non-empty and `var(x) > 0`.
### What a Strong Answer Covers
- A pooled theta and a single global centering mean, with `ddof=1` in both the covariance and the variance.
- The effect as the treatment mean of the adjusted metric minus the control mean.
- The unpooled two-sample SE computed on the adjusted metric, with per-arm `ddof=1` variances.
- Why the SE is smaller than the raw SE, and what the formula assumes about theta.
- Edge cases: an arm with one row, missing values, unexpected group labels, return types.
### Follow-up Questions
- Why is theta estimated on pooled data, and what would separate per-arm thetas change?
- How much variance reduction should you expect from CUPED?
- Would you use a pooled or an unpooled (Welch) SE here, and why?
Overview: Write a pandas function that returns both the CUPED-adjusted treatment effect of an A/B test and its unpooled two-sample standard error. It tests pooled covariate adjustment, consistent ddof handling, and whether you know which metric the standard error must be computed on.