CUPED variance reduction

90 min5 partsCoreFundamentalsChecked Nov 2026
Start the lessonShort steps, each with something to do
What you'll learn
  • Why a metric's variance sets both the width of the interval and the number of users a test needs
  • How CUPED subtracts the part of the metric that the pre-period predicts, and why the best θ, cov(x, y) / var(x), is the regression slope
  • Why the variance falls to exactly 1 - ρ² of the raw, and why the estimate stays unbiased when x is measured before assignment
  • Center x, use one ddof, and spot four quiet bugs: the wrong variance, mixed ddof, an uncentered x and a flipped sign
  • Run it on a real test: one pooled θ, the per-arm trap, and the link to regression adjustment (Lin 2013)
  • Users with no history, and CUPAC, which uses a model's prediction as the covariate
Part 1 of 5about 19 min
Less variance

CUPED, from a pre-period to a smaller test

The sample-size lesson ended on a cost: detecting 10% going to 12% takes 3,839 users per arm, and the metric's variance sets that number. CUPED cuts it with data the test already has, each user's history from before it started. The 2013 paper that introduced it, by a team at Microsoft, reports for Bing: "we can reduce variance by about 50%, effectively achieving the same statistical power with only half of the users, or half the duration." Our version of the practice reads like this:

Each user has y, their metric during the test, and x, the same metric over the weeks before it. Write cuped(y, x): return the adjusted metric, which keeps the mean of y and has the least variance any adjustment y - theta * (x - mean(x)) can give, and the theta it used. Then read a test with it.

The practice comes in four levels. Level 1 writes cuped. Level 2 writes cuped_effect(y, x, t): the difference in adjusted means and its standard error, from one call on both arms. Level 3 keeps the users with no history, whose x is missing. Level 4 writes lin_effect, with one theta per arm.

You don't write it yet. The lesson starts with what less variance buys, finds the best θ, shows why the estimate stays unbiased, then runs CUPED on a test and answers the follow-ups. At the end you solve the whole problem, level by level, in an interview simulation with a 40-minute timer.

This lesson builds on the A/B test lesson's comparison of two means and on the sample-size formula, where the variance sits on top. The cluster-randomized lesson comes next, for tests where the unit you randomize isn't the unit you measure.

Less variance buys a narrower interval and fewer users

Start with what a smaller variance buys, in interval width and in users.

Predict

Cut a metric's variance in half. How wide is the test's interval now?

Pick an answer above to see the rest

Each user also brings a number from before the test

The variance CUPED removes is visible in a second number each user already has.

Predict

Which covariate usually cuts the most variance?

Pick an answer above to see the rest

Last week's sessions predict this week's

Here are eight users, the same eight as in the worked example. xx is each user's sessions in the week before the test, and yy their sessions in the test's first week.

import numpy as np

# sessions, the week before
x = np.array([2.0, 4, 6, 8, 4, 6, 8, 10])
# sessions, the test week
y = np.array([2.0, 4, 5, 7, 6, 8, 7, 9])
print(np.corrcoef(x, y)[0, 1].round(3))
Predict

How strongly do the eight users' x and y move together?

Pick one to see what it prints.
Pick an answer above to see the rest
Predict

The line expects 6 sessions from both users at x = 6. The treatment user did 8 and the control user 5. What does CUPED keep for each?

Pick an answer above to see the rest

The adjusted metric keeps each user's distance from the line, plus the mean

Take 0.75, the line's slope, times each user's distance from the average xx, and subtract it from yy. adjust(y, x, theta) does that, and you write it at the end of this step:

y_cv = adjust(y, x, 0.75)
print(y_cv)
print(y.mean(), y_cv.mean())
print(y.var(), y_cv.var())
Predict

Subtract 0.75 × (x - mean of x) from each y. What happens to the mean and the variance?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 1 done
Part 2 of 5about 15 min
The best θ

Search θ for the least variance

Try a range of θ\theta on the eight users and print the variance each one leaves, with a bar to show its size:

for theta in [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5]:
    v = (y - theta * (x - x.mean())).var()
    print(f"{theta:.2f}  {'█' * round(8 * v):<36}  {v:.3f}")
Predict

Try θ from 0 to 1.5 on the eight users. Where is the variance lowest?

Pick one to see what it prints.
Pick an answer above to see the rest

How much variance the best θ leaves

At the bottom of the U, the variance left depends on one number.

r = np.corrcoef(x, y)[0, 1]
print(round(r ** 2, 4), y_cv.var() / y.var())
Predict

r² is 0.75 on the eight users. What share of the variance does CUPED leave?

Pick one to see what it prints.
Pick an answer above to see the rest

Every wrong θ is a multiple of the right one

A wrong θ still sits somewhere on the same U.

Predict

Besides θ = 0, which θ leaves the variance exactly where it started?

Pick an answer above to see the rest
Part 2 done
Part 3 of 5about 17 min
Unbiased

The estimate stays unbiased when x comes from before assignment

Less variance is only worth having if the estimate still centers on the truth.

Predict

Treatment's users were already 2 sessions heavier before the test, and θ = 0.75. How much of the raw 3-session gap does CUPED take back?

Pick an answer above to see the rest

Check CUPED in 10,000 simulated tests

Check the claim the way you'd check any function: simulate it.

rng = np.random.default_rng(0)
raw, adj = [], []
for _ in range(10_000):
    # before the test
    x = rng.normal(10, 1, 1_000)
    y = 5 + 0.7 * (x - 10) + rng.normal(0, np.sqrt(0.51), 1_000)
    # random assignment
    t = rng.random(1_000) < 0.5
    # true effect: 0.1
    y = y + 0.1 * t
    # one call, both arms
    y_adj, _ = cuped(y, x)
    raw.append(y[t].mean() - y[~t].mean())
    adj.append(y_adj[t].mean() - y_adj[~t].mean())

print(f"average, raw and CUPED: {np.mean(raw):.2f} {np.mean(adj):.2f}")
print(f"variance, CUPED over raw: {np.var(adj) / np.var(raw):.2f}")
Predict

10,000 simulated tests at ρ = 0.7, true effect 0.10. Do the raw and CUPED estimates average the same?

Pick one to see what it prints.
Pick an answer above to see the rest

Center x, so the mean stays where it was

Two details decide whether the numbers on a dashboard still mean anything. The first is centering.

Each arm's mean of the adjusted metric, on the eight users
centered 5.25 and 6.75: lift +28.6%
uncentered 0.75 and 2.25: lift +200%
01234567
controltreatment

X−XˉX - \bar{X} averages to zero, so subtracting θ(X−Xˉ)\theta(X - \bar{X}) leaves the mean of YY exactly where it was. The adjusted metric stays in the original units at the original level: sessions per user still read as sessions per user.

Skip the centering and every value drops by an extra θXˉ\theta\bar{X} compared with the centered version. On the eight users that's 0.75×6=4.50.75 \times 6 = 4.5. Both arms drop by the same amount, so the difference between them survives: 1.5 in the figure.

Everything that uses a level breaks: the arm means on a dashboard, and the relative lift, which divides by the control mean. On the eight users the lift jumps from +28.6% to +200%.

In the editor, write arm_readout(y_adj, t): each arm's level and the lift, on both versions of the eight users.

Divide θ's top and bottom by the same count, or by none

The second detail is the divisor inside θ. theta_numpy is this step's task.

import statistics

x = np.array([2.0, 4, 6, 8, 4, 6, 8, 10])
y = np.array([2.0, 4, 5, 7, 6, 8, 7, 9])
c = np.cov(x, y)
print(theta_numpy(y, x))
print(statistics.linear_regression(x, y).slope)
print(c[0, 1] / np.var(x))
0.75
0.75
0.8571428571428571

Three ways to compute θ\theta on the eight users. The first two give the slope, 0.75. The third comes out 0.857, which is 8/78/7 times too big. np.cov(x, y) returns a 2 by 2 grid: the variance of x at [0, 0] and the covariance at [0, 1].

The reason is the divisor. θ\theta is a ratio of two averages over the same users. Write Sxy=∑i(xi−xˉ)(yi−yˉ)S_{xy} = \sum_i (x_i - \bar{x})(y_i - \bar{y}) for the sum of products and Sxx=∑i(xi−xˉ)2S_{xx} = \sum_i (x_i - \bar{x})^2 for the sum of squares. Divide both by the same count and the count cancels:

θ=Sxy/(n−d)Sxx/(n−d)=SxySxx\theta = \frac{S_{xy} / (n - d)}{S_{xx} / (n - d)} = \frac{S_{xy}}{S_{xx}}

Here dd is the ddof, short for delta degrees of freedom: the number taken off nn in the divisor, 0 or 1. Use different ones on top and bottom and the count doesn't cancel. The defaults make that easy to do by accident:

CallDivides by
np.cov(x, y)n−1n - 1
np.var(x)nn
statistics.covariance(x, y)n−1n - 1
statistics.variance(x)n−1n - 1
statistics.pvariance(x)nn

The safest version never divides by a count at all: a sum of products over a sum of squares, as cuped does. On the practice data, that, np.cov for both, and statistics.linear_regression(x, y).slope all match np.polyfit(x, y, 1)[0] to within 10−1510^{-15}. The names aren't the point. Any one convention, used on both sides, works.

In the editor, write theta_numpy(y, x) with np.cov for both numbers.

Part 3 done
Part 4 of 5about 12 min
The worked example

A worked example

Eight users: θ = 0.75, and a quarter of the variance left

Eight users: C1 to C4 in control and T1 to T4 in treatment. xx is each user's sessions in the week before the test, yy their sessions in the test's first week, and xc=x−xˉx_c = x - \bar{x}.

Userxxyyxcx_cθxc\theta x_cycvy_{cv}
C122-4-3.05.0
C244-2-1.55.5
C36500.05.0
C48721.55.5
T146-2-1.57.5
T26800.08.0
T38721.55.5
T410943.06.0
  1. Means. xˉ=48/8=6\bar{x} = 48/8 = 6 and yˉ=48/8=6\bar{y} = 48/8 = 6.
  2. θ. The distances of yy from 6 are -4, -2, -1, 1, 0, 2, 1, 3. Multiply each by xcx_c and add: Sxy=16+4+0+2+0+0+2+12=36S_{xy} = 16 + 4 + 0 + 2 + 0 + 0 + 2 + 12 = 36. The squares of xcx_c add up to Sxx=48S_{xx} = 48. So θ=36/48=0.75\theta = 36/48 = 0.75.
  3. Adjust. ycv=y−0.75 xcy_{cv} = y - 0.75\,x_c, the last column.
  4. Check. The ycvy_{cv} column still averages 6. Its squared distances from 6 add up to 9, against 36 for yy, so a quarter is left. The squared correlation is 362/(48×36)=0.7536^2/(48 \times 36) = 0.75, and 1−0.75=0.251 - 0.75 = 0.25. In the sample the identity is exact.

By arm: the head start takes 1.5 of the raw 3 sessions

xˉ\bar{x}yˉ\bar{y}yˉcv\bar{y}_{cv}
Control54.55.25
Treatment77.56.75
Difference23.01.5

This is the head start from the chapter on the unbiased estimate, worth 1.5 of the raw 3 sessions. Eight users are far too few for a real test. The point here is the arithmetic.

Each bug shows up in these eight users

Run the broken versions on the same eight users:

  • Dividing by var(y): θ=36/36=1.0\theta = 36/36 = 1.0. The difference comes out 1.0, and 12 of the 36 is left instead of 9.
  • Flipped sign: the difference comes out 4.5, the head start added instead of removed. The squares add up to 117, which is 36 (1+3×0.75)36\,(1 + 3 \times 0.75).
  • No centering: the difference stays 1.5, but the arm means become 0.75 and 2.25, a lift of +200% where the centered means give +28.6%.
  • Mixed ddof: θ=0.857\theta = 0.857, which is 8/78/7 times too big. On 8 users the n/(n−1)n/(n-1) factor is easy to see.

In the editor, write sums(y, x): the worked example's three sums, which give θ and r².

Running it on an A/B test

On a real test you adjust every user, then compare the arms.

Predict

Why call the function once on all users, rather than once per arm?

Pick an answer above to see the rest

Called once per arm, it keeps the raw difference but shrinks the interval

The tempting shortcut is one call per arm, and it fails quietly.

Control's four usersMean
x24685
from the pooled mean, 6-4-20+2-1
from control's own mean, 5-3-1+1+30

CUPED subtracts θ\theta times these distances. From the pooled center, control's distances average -1, so its mean rises by 0.75×10.75 \times 1, from 4.5 to 5.25. From its own center they average 0, and the mean stays at 4.5. Treatment mirrors it: 7.5 falls to 6.75 with the pooled center and stays at 7.5 with its own.

That's what one call per arm does. Each call centers its arm at that arm's own mean of xx, so the adjustment sums to zero inside each arm. Each arm's adjusted mean equals its raw mean, and the difference doesn't move at all: 3.0 on the eight users.

The variances still shrink, though, and the standard error with them. So you report the raw estimate with a CUPED-sized interval. In 10,000 simulated A/A tests at ρ=0.7\rho = 0.7, where both arms got the same experience, that called 15.9% of them significant at a nominal 5%.

In the editor, adjust and best_theta are ready. Write cuped_readout(y, x, t): the difference and the lift from one call on all users.

CheckpointRead the test on the adjusted metric

Finish cuped_readout(y, x, t), where t is True for treated users. Adjust every user with one θ\theta fitted on all of them, then return the difference in adjusted means, treatment minus control, and the relative lift, that difference over the control's adjusted mean.

Part 4 done
Part 5 of 5about 27 min
Follow-ups

What interviewers ask next

These are the questions that follow the practice problem: why the covariate must come from before the test, the link to regression, one θ\theta or two, new users, CUPAC, and why not just subtract last month. Levels 3 and 4 of the practice code two of them.

A covariate measured after assignment can carry part of the effect away

Predict

You use week 1 of a two-week test as the covariate for week 2. What happens?

Pick an answer above to see the rest

With one θ, CUPED is regression adjustment, up to how θ is fitted

Regress yy on an intercept, the treatment flag and xx. That model is usually called ANCOVA. Its coefficient on the flag is exactly (yˉt−yˉc)−β^ (xˉt−xˉc)(\bar{y}_t - \bar{y}_c) - \hat\beta\,(\bar{x}_t - \bar{x}_c), where β^\hat\beta, the coefficient on xx, is the slope fitted within the arms.

That's CUPED with θ=β^\theta = \hat\beta. CUPED's pooled θ\theta is fitted on all users at once, so it also picks up the gap between the arms. In 200 simulated tests of 1,000 users the two estimates never differed by more than 0.004, about 3% of a standard error.

In the editor, write ancova_effect(y, x, t): that regression, to see it part ways with CUPED on the eight users.

Lin (2013): with interactions, adjustment can't hurt precision in large samples

Regression adjustment had a famous critic. In the words of Winston Lin's 2013 reply, Freedman "argued that adjustment can lead to worsened asymptotic precision, invalid measures of precision, and small-sample bias." Lin's answer comes in two parts:

  • Add the treatment-by-covariate interactions. "OLS adjustment cannot hurt asymptotic precision when a full set of treatment-covariate interactions is included."
  • Use robust standard errors. "Asymptotically valid confidence intervals can be constructed with the Huber-White sandwich standard error estimator."

A sandwich standard error estimates the variance from the residuals themselves, instead of assuming every user's noise has the same size. The cluster-randomized lesson builds one.

In CUPED terms, the interacted regression is one θ\theta per arm, with both arms centered at the same pooled mean xˉ\bar{x}. Its coefficient on the flag is exactly

(yˉt−θt(xˉt−xˉ))−(yˉc−θc(xˉc−xˉ))\left(\bar{y}_t - \theta_t(\bar{x}_t - \bar{x})\right) - \left(\bar{y}_c - \theta_c(\bar{x}_c - \bar{x})\right)

In words: each arm's mean, minus that arm's own slope (θt\theta_t or θc\theta_c) times its head start from the pooled mean xˉ\bar{x}.

One pooled θ, or one per arm

Lin's version raises a practical question: one θ or two?

Predict

With a 50/50 split, does one pooled θ do as well as one θ per arm?

Pick an answer above to see the rest

Users the pre-period never saw

Some users arrive in the test with no history at all.

Predict

Some users have no pre-period at all. What do you do with them?

Pick an answer above to see the rest

CUPAC: when there's no past value of the metric

Sometimes there's no past value of the metric at all.

Predict

There's no past value of the metric to use as x. What can serve instead?

Pick an answer above to see the rest

Subtracting last month's number is θ = 1, the best θ only when the slope is 1

The simplest adjustment of all is to subtract last month's number.

Predict

Subtracting last month's number is θ = 1. At ρ = 0.5, with x and y on the same scale, how much variance does it save?

Pick an answer above to see the rest

The practice problem, start to finish

That's every piece of the practice problem, and every follow-up it lists. The simulation asks for all three functions under a timer, so rehearse the order once.

Check yourself
5 questions
  1. 1.The pre-period metric has a correlation of 0.8 with the test-period metric. With CUPED, about what share of the original sample size does the test need for the same power?

  2. 2.On the practice data your adjusted metric comes out with 2.65 times the raw variance, while its mean and θ are both right. What did you write?

  3. 3.In a simulation, x is each user's sessions over the two weeks before the test and y their sessions in the test's one week, so var(x) is about 3 times var(y). You compute theta as cov(x, y) / var(y). What happens?

  4. 4.You compute theta = np.cov(x, y)[0, 1] / np.var(x) on the practice data's 1,000 users. Which test fails, and why?

  5. 5.Your function returns y - theta * x, with no centering, and you call it once on both arms. Which number is still right?

Practice

Sources (3)
  1. Improving the Sensitivity of Online Controlled Experiments by Utilizing Pre-Experiment Data · WSDM 2013, authors' version · Feb 4, 2013
    “We propose an approach (CUPED) that utilizes data from the pre-experiment period to reduce metric variability and hence achieve better sensitivity.”
  2. Agnostic notes on regression adjustments to experimental data: Reexamining Freedman's critique · arXiv 1208.2301, published in The Annals of Applied Statistics 7(1), 2013 · Aug 11, 2012
    “OLS adjustment cannot hurt asymptotic precision when a full set of treatment-covariate interactions is included.”
  3. Improving Experimental Power through Control Using Predictions as Covariate (CUPAC) · DoorDash engineering blog · Jun 8, 2020
    “CUPAC is inspired by the CUPED methodology pioneered at Microsoft (Deng, Xu, Kohavi, & Walker, 2013), extending it to leverage machine learning predictions built using inputs unaffected by experiment intervention.”