Seeded CUPED Variance-Reduction Simulation

Quick Overview

Write a seeded NumPy simulation that applies CUPED with a pre-experiment covariate and compares the raw and adjusted standard errors of the treatment effect. It tests reproducible random draws, the pooled theta estimate, and why the adjustment shrinks variance without biasing the estimate.

Seeded CUPED Variance-Reduction Simulation

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `def simulate_cuped_se_reduction(n: int, rho: float, effect: float, seed: int) -> dict`. Run one seeded simulation showing that CUPED reduces the standard error of the average treatment effect (ATE) while staying unbiased, using a pre-experiment covariate `x` correlated with the outcome `y`. Follow every step exactly so the results are reproducible. 1. `rng = np.random.default_rng(seed)`. 2. Draw `x = rng.normal(0, 1, 2*n)`, then `noise = rng.normal(0, sqrt(1 - rho**2), 2*n)`, in this order. 3. `y = rho*x + noise`. 4. The first `n` units are control (0) and the last `n` are treatment (1): `y[treatment] += effect`. 5. Estimate theta once on the pooled sample, not per arm: `np.cov(y, x, ddof=1)[0,1] / np.var(x, ddof=1)`. 6. `Y_cuped = y - theta*(x - x.mean())`, centered on the global mean of `x`. 7. Compute the raw and CUPED ATEs and the unpooled two-sample SEs (per-arm `ddof=1` variance divided by `n`). 8. Return a dict with exactly the keys `{'ate_cuped', 'se_raw', 'se_cuped'}`, all Python floats. Expected property: `ate_cuped` is near `effect`, and `se_cuped/se_raw` is near `sqrt(1 - rho**2)`. Example: ```python result = simulate_cuped_se_reduction(5000, 0.7, 0.1, 7) # result["ate_cuped"] is near 0.1 (the effect) # result["se_cuped"] / result["se_raw"] is near sqrt(1 - 0.49) = 0.714 ``` Only `numpy` is needed. There is no file I/O or plotting. ```hint Check against the generating process Work out the variance of `y` and of `y - rho*x` directly from how the data is generated. The SE ratio from your simulation should be close to the square root of their ratio. ``` ```hint Keep the arms in sync Build one boolean treatment mask and reuse it for adding the effect, for both ATEs and for both SEs. ``` ### Constraints & Assumptions - There are `2*n` units in total, with exactly `n` per arm, and `n` is at least 2 so the `ddof=1` variances are defined. - `rho` lies in `[-1, 1]`, so the noise standard deviation `sqrt(1 - rho**2)` is real. - `x` is drawn before treatment and does not depend on assignment. ### What a Strong Answer Covers - Reproducing the pinned RNG draw order and the control and treatment split exactly - Estimating theta once on the pooled sample with `ddof=1` in both the covariance and the variance - Centering the covariate on one shared mean so the adjustment leaves the ATE unbiased - Unpooled two-sample SEs from per-arm variances, returned as plain floats under the exact keys - Why the ATE stays unbiased and why the SE ratio approaches `sqrt(1 - rho**2)` ### Follow-up Questions - Can estimating theta on the pooled sample bias the ATE, and when is it safe? - Why does `se_cuped/se_raw` converge to `sqrt(1 - rho**2)`? - What goes wrong if you center `x` on each arm's own mean? - How would you apply CUPED when some users have no pre-experiment data?

Overview: Write a seeded NumPy simulation that applies CUPED with a pre-experiment covariate and compares the raw and adjusted standard errors of the treatment effect. It tests reproducible random draws, the pooled theta estimate, and why the adjustment shrinks variance without biasing the estimate.

General logo
General
Nov 25, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement def simulate_cuped_se_reduction(n: int, rho: float, effect: float, seed: int) -> dict. Run one seeded simulation showing that CUPED reduces the standard error of the average treatment effect (ATE) while staying unbiased, using a pre-experiment covariate x correlated with the outcome y. Follow every step exactly so the results are reproducible.

  1. rng = np.random.default_rng(seed) .
  2. Draw x = rng.normal(0, 1, 2*n) , then noise = rng.normal(0, sqrt(1 - rho**2), 2*n) , in this order.
  3. y = rho*x + noise .
  4. The first n units are control (0) and the last n are treatment (1): y[treatment] += effect .
  5. Estimate theta once on the pooled sample, not per arm: np.cov(y, x, ddof=1)[0,1] / np.var(x, ddof=1) .
  6. Y_cuped = y - theta*(x - x.mean()) , centered on the global mean of x .
  7. Compute the raw and CUPED ATEs and the unpooled two-sample SEs (per-arm ddof=1 variance divided by n ).
  8. Return a dict with exactly the keys {'ate_cuped', 'se_raw', 'se_cuped'} , all Python floats.

Expected property: ate_cuped is near effect, and se_cuped/se_raw is near sqrt(1 - rho**2).

Example:

result = simulate_cuped_se_reduction(5000, 0.7, 0.1, 7)
# result["ate_cuped"] is near 0.1 (the effect)
# result["se_cuped"] / result["se_raw"] is near sqrt(1 - 0.49) = 0.714

Only numpy is needed. There is no file I/O or plotting.

Constraints & Assumptions

  • There are 2*n units in total, with exactly n per arm, and n is at least 2 so the ddof=1 variances are defined.
  • rho lies in [-1, 1] , so the noise standard deviation sqrt(1 - rho**2) is real.
  • x is drawn before treatment and does not depend on assignment.

What a Strong Answer Covers Guidance

  • Reproducing the pinned RNG draw order and the control and treatment split exactly
  • Estimating theta once on the pooled sample with ddof=1 in both the covariance and the variance
  • Centering the covariate on one shared mean so the adjustment leaves the ATE unbiased
  • Unpooled two-sample SEs from per-arm variances, returned as plain floats under the exact keys
  • Why the ATE stays unbiased and why the SE ratio approaches sqrt(1 - rho**2)

Follow-up Questions Guidance

  • Can estimating theta on the pooled sample bias the ATE, and when is it safe?
  • Why does se_cuped/se_raw converge to sqrt(1 - rho**2) ?
  • What goes wrong if you center x on each arm's own mean?
  • How would you apply CUPED when some users have no pre-experiment data?