Compute the CUPED-adjusted average treatment effect from an experiment DataFrame

Quick Overview

Write a pandas function that returns the CUPED-adjusted average treatment effect of an A/B test, using a pooled theta and a pre-experiment covariate. It tests variance reduction for experiments, consistent variance estimates in NumPy, and how the centering choice changes the effect estimate.

Compute the CUPED-adjusted average treatment effect from an experiment DataFrame

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `def cuped_ate(df: pd.DataFrame) -> float`. The DataFrame `df` has columns `'group'` (`'control'` or `'treatment'`), `'y'` (the metric) and `'x'` (a pre-experiment covariate). Return the CUPED-adjusted average treatment effect: `mean(Y_cuped | treatment) - mean(Y_cuped | control)`. 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)`, where `x_bar` is the single global mean of `x`. Do not use per-arm means. - Use `ddof=1` throughout. - Return treatment minus control as an unrounded `float`. Example: ```text group x y control 2 5 control 4 6 control 6 10 treatment 3 8 treatment 5 10 treatment 7 12 cuped_ate(df) -> 1.7142857142857... (exactly 12/7) ``` The raw, unadjusted ATE for the same frame is 10 - 7 = 3. ```hint Watch the variance call `np.cov` and `np.var` do not share the same default `ddof`. Pass it explicitly to both, or `theta` comes out scaled by a factor that depends on the sample size. ``` ```hint A quick check Write the adjusted difference in terms of the raw difference in `y` and the difference between the arm means of `x`. It gives a second way to compute the result. ``` ### Constraints & Assumptions - `df` has one row per user with columns `group`, `y` and `x`. The `group` values are exactly `'control'` and `'treatment'`. - Both arms have at least one row, and `x` is measured before assignment. ### What a Strong Answer Covers - A pooled `theta` estimated once on all rows, with the same `ddof` in the covariance and the variance. - Centering on the single global mean of `x`, and what per-arm centering would do to the estimate. - The correct sign (treatment minus control) and an unrounded float. - Edge cases: an empty arm, a constant `x`, missing values, unexpected group labels. - How the adjusted estimate relates to the raw difference in means and why its variance is lower. ### Follow-up Questions - What happens to the estimate if you center `x` on per-arm means instead of the pooled mean? - How much variance reduction do you expect from CUPED, and what does it depend on? - How would you compute a standard error and a confidence interval for the CUPED-adjusted ATE?

Overview: Write a pandas function that returns the CUPED-adjusted average treatment effect of an A/B test, using a pooled theta and a pre-experiment covariate. It tests variance reduction for experiments, consistent variance estimates in NumPy, and how the centering choice changes the effect estimate.

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

Implement def cuped_ate(df: pd.DataFrame) -> float. The DataFrame df has columns 'group' ('control' or 'treatment'), 'y' (the metric) and 'x' (a pre-experiment covariate). Return the CUPED-adjusted average treatment effect: mean(Y_cuped | treatment) - mean(Y_cuped | control).

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) , where x_bar is the single global mean of x . Do not use per-arm means.
  • Use ddof=1 throughout.
  • Return treatment minus control as an unrounded float .

Example:

group      x   y
control    2   5
control    4   6
control    6  10
treatment  3   8
treatment  5  10
treatment  7  12

cuped_ate(df) -> 1.7142857142857...   (exactly 12/7)

The raw, unadjusted ATE for the same frame is 10 - 7 = 3.

Constraints & Assumptions

  • df has one row per user with columns group , y and x . The group values are exactly 'control' and 'treatment' .
  • Both arms have at least one row, and x is measured before assignment.

What a Strong Answer Covers Guidance

  • A pooled theta estimated once on all rows, with the same ddof in the covariance and the variance.
  • Centering on the single global mean of x , and what per-arm centering would do to the estimate.
  • The correct sign (treatment minus control) and an unrounded float.
  • Edge cases: an empty arm, a constant x , missing values, unexpected group labels.
  • How the adjusted estimate relates to the raw difference in means and why its variance is lower.

Follow-up Questions Guidance

  • What happens to the estimate if you center x on per-arm means instead of the pooled mean?
  • How much variance reduction do you expect from CUPED, and what does it depend on?
  • How would you compute a standard error and a confidence interval for the CUPED-adjusted ATE?