Write a large-sample CUPED readout: effect, standard error, z, p-value and 95% confidence interval
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def cuped_readout(df: pd.DataFrame) -> dict`. The DataFrame `df` has columns `group` (`'control'` or `'treatment'`), `y` and the covariate `x`. Fit `theta = cov(y, x) / var(x)` on pooled data (the covariance is the off-diagonal entry `[0, 1]` of `np.cov`), form `Y_cuped = y - theta * (x - global_mean_x)` with the pooled mean of `x`, and return a dict with exactly the keys `'effect'`, `'se'`, `'z'`, `'p_value'`, `'ci_low'` and `'ci_high'`, and no others.
Requirements:
- Use `ddof=1` for every variance and covariance.
- `effect` is the treatment mean of `Y_cuped` minus the control mean. `se` is the square root of the summed per-arm variances, each divided by its arm's `n` (the unpooled form).
- `z = effect / se`, against a normal reference. Do not use a t reference.
- `p_value = 2 * scipy.stats.norm.sf(abs(z))`. Use `norm.sf`, and do not compute it as `1 - norm.cdf`.
- `ci_low` and `ci_high` are `effect - z_crit * se` and `effect + z_crit * se`, with `z_crit = scipy.stats.norm.ppf(0.975)`, which is `1.959963984540054`.
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_readout(df) -> {
'effect': 1.761905, (exactly 37/21)
'se': 0.533107,
'z': 3.30498,
'p_value': 0.00095,
'ci_low': 0.71704,
'ci_high': 2.80677,
}
```
Values are rounded for display. The function returns them unrounded. An eight-row frame is far too small for a normal reference in practice. It is here so the arithmetic can be checked by hand.
```hint Compute two numbers first
Every output after `effect` and `se` is a function of those two numbers and fixed normal quantiles. Get `Y_cuped`, the effect and the unpooled SE right, and derive the rest from them.
```
```hint A consistency check
The 95% interval and the two-sided p-value use the same `se` and the same normal distribution. Work out which value of `abs(z)` puts one end of the interval exactly at zero, and use it to check your outputs agree.
```
### Constraints & Assumptions
- `numpy`, `pandas` and `scipy.stats` are available.
- The `group` values are exactly `'control'` and `'treatment'`.
- Both arms have enough rows for `ddof=1` variances. Input validation is out of scope.
### What a Strong Answer Covers
- A pooled theta with `ddof=1`, a single global centering mean, and the effect and unpooled SE computed on `Y_cuped`.
- The z statistic against a normal reference, the two-sided p-value from the survival function, and the 95% interval from the 0.975 quantile.
- Why `norm.sf` keeps precision far in the tail where `1 - norm.cdf` fails.
- When a normal reference is justified, and what to use when it is not.
- Exactly the six keys, each a float.
### Follow-up Questions
- Why use `norm.sf(abs(z))` instead of `1 - norm.cdf(abs(z))`?
- How does the choice of `theta` affect the validity of the effect estimate?
- When would a normal reference be inappropriate here?
Overview: Write a pandas and SciPy function that turns an A/B test with a pre-experiment covariate into a full CUPED readout: effect, standard error, z statistic, two-sided p-value and 95% confidence interval. It tests variance reduction, large-sample inference and numerically stable tail probabilities.