Compute a CUPED-adjusted metric using a pooled theta and the global covariate mean

Quick Overview

Implement the CUPED variance-reduction transform in pandas and NumPy by estimating one covariate coefficient on all experiment rows, centering the pre-period covariate, and returning the adjusted metric in row order. It tests the CUPED formula, consistent ddof handling, and why the adjustment leaves the treatment effect unbiased.

Compute a CUPED-adjusted metric using a pooled theta and the global covariate mean

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

CUPED lowers the variance of an experiment metric by subtracting a scaled, centered pre-experiment covariate from it. Implement `cuped_adjusted_metric(df)` in `solution.py`. The DataFrame `df` has the columns `group` (`'control'` or `'treatment'`), `y` (the metric) and `x` (the pre-experiment covariate). Return the CUPED-adjusted metric as a NumPy float array in the original row order. ```python def cuped_adjusted_metric(df: pd.DataFrame) -> np.ndarray: ... ``` Requirements: - Estimate a pooled theta on all rows: `theta = np.cov(df['y'].values, df['x'].values, ddof=1)[0, 1] / np.var(df['x'].values, ddof=1)`. - Center on the global mean `x_bar = df['x'].mean()`, taken over the rows of both arms together. - Return `df['y'].values - theta * (df['x'].values - x_bar)` without sorting or regrouping the rows. - Computing the treatment effect is out of scope. Return the transformed array only. Example: ```python import numpy as np import pandas as pd df = pd.DataFrame({ "group": ["control"] * 8 + ["treatment"] * 8, "y": [12, 15, 9, 20, 14, 11, 18, 16, 19, 22, 14, 25, 18, 16, 23, 21], "x": [10, 13, 8, 17, 12, 10, 15, 14, 11, 14, 9, 16, 12, 10, 15, 14], }) np.round(cuped_adjusted_metric(df), 2) # array([15.41, 14.32, 15.13, 13.87, 14.68, 14.41, 14.59, 13.96, # 21.04, 19.96, 18.77, 20.23, 18.68, 19.41, 19.59, 18.96]) ``` On these rows theta is 144.5 / 106 = 1.3632075471698113 and `x_bar` is 12.5. The first row has `y = 12` and `x = 10`, so its adjusted value is `12 - 1.3632 * (10 - 12.5) = 15.408`. ```hint Two NumPy defaults `np.cov` returns a matrix and normalizes differently from `np.var` by default. Check which entry you need and pass the same ddof to both. ``` ### Constraints & Assumptions - `df` has the columns `group`, `y` and `x`, and `group` holds only `'control'` and `'treatment'`. - `y` and `x` are numeric, there are at least two rows, and `x` is not constant. ### What a Strong Answer Covers - One theta estimated on all rows, with matching `ddof=1` and the off-diagonal covariance entry - Centering on the mean of `x` over all rows - A float array with one value per input row, in the input's row order - Linear time, and the inputs that make theta undefined or the output misleading (constant `x`, missing values) - Why the adjustment leaves the difference in arm means unbiased ### Follow-up Questions - Why does CUPED reduce variance without biasing the treatment effect? - What breaks if you estimate theta separately in each arm, or center `x` on each arm's own mean? - How would you compute the treatment effect and its standard error from the adjusted metric?

Overview: Implement the CUPED variance-reduction transform in pandas and NumPy by estimating one covariate coefficient on all experiment rows, centering the pre-period covariate, and returning the adjusted metric in row order. It tests the CUPED formula, consistent ddof handling, and why the adjustment leaves the treatment effect unbiased.

Nov 24, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

CUPED lowers the variance of an experiment metric by subtracting a scaled, centered pre-experiment covariate from it. Implement cuped_adjusted_metric(df) in solution.py. The DataFrame df has the columns group ('control' or 'treatment'), y (the metric) and x (the pre-experiment covariate). Return the CUPED-adjusted metric as a NumPy float array in the original row order.

def cuped_adjusted_metric(df: pd.DataFrame) -> np.ndarray:
    ...

Requirements:

  • Estimate a pooled theta on all rows: theta = np.cov(df['y'].values, df['x'].values, ddof=1)[0, 1] / np.var(df['x'].values, ddof=1) .
  • Center on the global mean x_bar = df['x'].mean() , taken over the rows of both arms together.
  • Return df['y'].values - theta * (df['x'].values - x_bar) without sorting or regrouping the rows.
  • Computing the treatment effect is out of scope. Return the transformed array only.

Example:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    "group": ["control"] * 8 + ["treatment"] * 8,
    "y": [12, 15, 9, 20, 14, 11, 18, 16, 19, 22, 14, 25, 18, 16, 23, 21],
    "x": [10, 13, 8, 17, 12, 10, 15, 14, 11, 14, 9, 16, 12, 10, 15, 14],
})

np.round(cuped_adjusted_metric(df), 2)
# array([15.41, 14.32, 15.13, 13.87, 14.68, 14.41, 14.59, 13.96,
#        21.04, 19.96, 18.77, 20.23, 18.68, 19.41, 19.59, 18.96])

On these rows theta is 144.5 / 106 = 1.3632075471698113 and x_bar is 12.5. The first row has y = 12 and x = 10, so its adjusted value is 12 - 1.3632 * (10 - 12.5) = 15.408.

Constraints & Assumptions

  • df has the columns group , y and x , and group holds only 'control' and 'treatment' .
  • y and x are numeric, there are at least two rows, and x is not constant.

What a Strong Answer Covers Guidance

  • One theta estimated on all rows, with matching ddof=1 and the off-diagonal covariance entry
  • Centering on the mean of x over all rows
  • A float array with one value per input row, in the input's row order
  • Linear time, and the inputs that make theta undefined or the output misleading (constant x , missing values)
  • Why the adjustment leaves the difference in arm means unbiased

Follow-up Questions Guidance

  • Why does CUPED reduce variance without biasing the treatment effect?
  • What breaks if you estimate theta separately in each arm, or center x on each arm's own mean?
  • How would you compute the treatment effect and its standard error from the adjusted metric?