Compute CUPED theta on pooled, control and treatment rows

Quick Overview

Write a pandas and NumPy function that computes the CUPED theta coefficient on pooled experiment data and on the control and treatment arms separately. It tests the covariance-over-variance formula, consistent ddof handling, and whether per-arm coefficients are safe to use in the variance-reduction adjustment.

Compute CUPED theta on pooled, control and treatment rows

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

CUPED adjusts an experiment metric `y` using a pre-experiment covariate `x`, and theta is the coefficient that scales the covariate in that adjustment. Implement `cuped_theta_by_scope(df)` in `solution.py`. The DataFrame `df` has a column `group` with the values `'control'` and `'treatment'`, an outcome column `y`, and a covariate column `x`. Compute theta three times: on all rows pooled, on the control rows only, and on the treatment rows only. ```python def cuped_theta_by_scope(df: pd.DataFrame) -> dict: ... ``` Requirements: - Return a dict with exactly the keys `'pooled'`, `'control'` and `'treatment'`, each mapped to a float. - `'pooled'` uses all rows. `'control'` and `'treatment'` use only that arm's rows. - On each subset, `theta = np.cov(y_sub, x_sub, ddof=1)[0, 1] / np.var(x_sub, ddof=1)`. Example: ```python import pandas as pd df = pd.DataFrame({ "group": ["control"] * 4 + ["treatment"] * 4, "x": [1, 2, 3, 4, 1, 2, 3, 4], "y": [2, 4, 5, 7, 4, 5, 7, 8], }) result = cuped_theta_by_scope(df) {k: round(v, 4) for k, v in result.items()} # {'pooled': 1.5, 'control': 1.6, 'treatment': 1.4} ``` Both arms have the same `x` values, so the two per-arm thetas differ only because `y` moves with `x` at a different rate in each arm. ```hint One helper, three calls The same ratio is computed on three row sets. Write it once and pass it the full arrays, then each arm's masked arrays. ``` ```hint Defaults differ Check which normalization `np.cov` and `np.var` use when you do not pass `ddof`. ``` ### Constraints & Assumptions - `df` has the columns `group`, `y` and `x`, and `group` holds only `'control'` and `'treatment'`. - `y` and `x` are numeric. - Each arm has at least two rows and `x` varies within each arm, so every variance in the formula is positive. ### What a Strong Answer Covers - One covariance-over-variance helper applied to all rows and to each arm's rows - The same `ddof=1` in the covariance and the variance, and the correct entry of the 2 by 2 covariance matrix - Exactly the three required keys, with float values - Linear time in the number of rows, and the cases where theta is undefined (an arm with fewer than two rows, or a constant `x`) - Which of the three thetas belongs in the CUPED adjustment, and why ### Follow-up Questions - Why does fitting a separate theta in each arm risk biasing the CUPED estimate of the treatment effect? - How does CUPED reduce variance, and how much reduction can you expect for a given correlation between `y` and `x`? - In a real test the two per-arm thetas differ by a wide margin. What could cause that, and what would you do about it?

Overview: Write a pandas and NumPy function that computes the CUPED theta coefficient on pooled experiment data and on the control and treatment arms separately. It tests the covariance-over-variance formula, consistent ddof handling, and whether per-arm coefficients are safe to use in the variance-reduction adjustment.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

CUPED adjusts an experiment metric y using a pre-experiment covariate x, and theta is the coefficient that scales the covariate in that adjustment. Implement cuped_theta_by_scope(df) in solution.py. The DataFrame df has a column group with the values 'control' and 'treatment', an outcome column y, and a covariate column x. Compute theta three times: on all rows pooled, on the control rows only, and on the treatment rows only.

def cuped_theta_by_scope(df: pd.DataFrame) -> dict:
    ...

Requirements:

  • Return a dict with exactly the keys 'pooled' , 'control' and 'treatment' , each mapped to a float.
  • 'pooled' uses all rows. 'control' and 'treatment' use only that arm's rows.
  • On each subset, theta = np.cov(y_sub, x_sub, ddof=1)[0, 1] / np.var(x_sub, ddof=1) .

Example:

import pandas as pd

df = pd.DataFrame({
    "group": ["control"] * 4 + ["treatment"] * 4,
    "x": [1, 2, 3, 4, 1, 2, 3, 4],
    "y": [2, 4, 5, 7, 4, 5, 7, 8],
})

result = cuped_theta_by_scope(df)
{k: round(v, 4) for k, v in result.items()}
# {'pooled': 1.5, 'control': 1.6, 'treatment': 1.4}

Both arms have the same x values, so the two per-arm thetas differ only because y moves with x at a different rate in each arm.

Constraints & Assumptions

  • df has the columns group , y and x , and group holds only 'control' and 'treatment' .
  • y and x are numeric.
  • Each arm has at least two rows and x varies within each arm, so every variance in the formula is positive.

What a Strong Answer Covers Guidance

  • One covariance-over-variance helper applied to all rows and to each arm's rows
  • The same ddof=1 in the covariance and the variance, and the correct entry of the 2 by 2 covariance matrix
  • Exactly the three required keys, with float values
  • Linear time in the number of rows, and the cases where theta is undefined (an arm with fewer than two rows, or a constant x )
  • Which of the three thetas belongs in the CUPED adjustment, and why

Follow-up Questions Guidance

  • Why does fitting a separate theta in each arm risk biasing the CUPED estimate of the treatment effect?
  • How does CUPED reduce variance, and how much reduction can you expect for a given correlation between y and x ?
  • In a real test the two per-arm thetas differ by a wide margin. What could cause that, and what would you do about it?