Implement the CUPED adjusted-metric transform in NumPy

Quick Overview

Implement the CUPED variance reduction transform in NumPy, adjusting an experiment metric with a pre-experiment covariate, a given theta and a pooled covariate mean. It tests vectorized array code and whether you understand how the centering choice affects the adjusted metric and the treatment effect estimate.

Implement the CUPED adjusted-metric transform in NumPy

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `cuped_transform(y, x, theta, x_mean)`. Apply the CUPED adjustment to metric `y` using the pre-experiment covariate `x`, returning `Y_cuped[i] = y[i] - theta * (x[i] - x_mean)`. Requirements: - Use the provided `theta` and `x_mean` (computed on pooled data). Do not recompute them. - Center every `x[i]` on the single global `x_mean`. Do not use per-arm means. - Do not skip centering (subtracting `theta * x` alone shifts the scale away from `y`). - Return an unrounded NumPy float array the same length as `y`. Example: ```text y = [12, 15, 9, 20, 14, 11, 18, 16, 8, 22] x = [10, 13, 8, 17, 12, 10, 15, 14, 7, 19] theta = 1.1933085501858736 x_mean = 12.5 Output: [14.9832713755, 14.4033457249, 14.3698884758, 14.6301115242, 14.5966542751, 13.9832713755, 15.0167286245, 14.2100371747, 14.5631970260, 14.2434944238] ``` The output is printed to 10 decimal places. The function itself returns the values unrounded. ```hint Let NumPy do the work Convert both inputs to float arrays once, then write the formula on whole arrays. The scalars `theta` and `x_mean` broadcast across every element, so no loop is needed. ``` ### Constraints & Assumptions - `y` and `x` are numeric sequences (lists or NumPy arrays) of the same length, one entry per user, covering both arms. - `theta` and `x_mean` are floats already estimated on the pooled data of both arms. ### What a Strong Answer Covers - A vectorized implementation of the formula that uses the provided `theta` and `x_mean` as given. - Why every value is centered on the single pooled mean, and what per-arm centering would do to the treatment effect estimate. - What skipping the centering does to the level of the adjusted metric and to the arm comparison. - Output type and edge cases: an unrounded float array of the same length, list or integer inputs, mismatched lengths, missing pre-period values. - Time and space complexity. ### Follow-up Questions - How is `theta` typically computed, and on which data? - Why does CUPED reduce variance without biasing the treatment effect? - What would you do for users who have no pre-experiment value of `x`?

Overview: Implement the CUPED variance reduction transform in NumPy, adjusting an experiment metric with a pre-experiment covariate, a given theta and a pooled covariate mean. It tests vectorized array code and whether you understand how the centering choice affects the adjusted metric and the treatment effect estimate.

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

Implement cuped_transform(y, x, theta, x_mean). Apply the CUPED adjustment to metric y using the pre-experiment covariate x, returning Y_cuped[i] = y[i] - theta * (x[i] - x_mean).

Requirements:

  • Use the provided theta and x_mean (computed on pooled data). Do not recompute them.
  • Center every x[i] on the single global x_mean . Do not use per-arm means.
  • Do not skip centering (subtracting theta * x alone shifts the scale away from y ).
  • Return an unrounded NumPy float array the same length as y .

Example:

y      = [12, 15, 9, 20, 14, 11, 18, 16, 8, 22]
x      = [10, 13, 8, 17, 12, 10, 15, 14, 7, 19]
theta  = 1.1933085501858736
x_mean = 12.5
Output: [14.9832713755, 14.4033457249, 14.3698884758, 14.6301115242,
         14.5966542751, 13.9832713755, 15.0167286245, 14.2100371747,
         14.5631970260, 14.2434944238]

The output is printed to 10 decimal places. The function itself returns the values unrounded.

Constraints & Assumptions

  • y and x are numeric sequences (lists or NumPy arrays) of the same length, one entry per user, covering both arms.
  • theta and x_mean are floats already estimated on the pooled data of both arms.

What a Strong Answer Covers Guidance

  • A vectorized implementation of the formula that uses the provided theta and x_mean as given.
  • Why every value is centered on the single pooled mean, and what per-arm centering would do to the treatment effect estimate.
  • What skipping the centering does to the level of the adjusted metric and to the arm comparison.
  • Output type and edge cases: an unrounded float array of the same length, list or integer inputs, mismatched lengths, missing pre-period values.
  • Time and space complexity.

Follow-up Questions Guidance

  • How is theta typically computed, and on which data?
  • Why does CUPED reduce variance without biasing the treatment effect?
  • What would you do for users who have no pre-experiment value of x ?