Users the pre-period never saw
Some users arrive in the test with no history at all.
Some users have no pre-period at all. What do you do with them?
| User | x, sessions before | Has history | x as filled in |
|---|---|---|---|
| U1 | 12 | 1 | 12 |
| U2 | 7 | 1 | 7 |
| U3 | none: a new user | 0 | 0, or any constant |
New users, and users whose cookie changed, have no pre-period, so x is missing. Don't drop them: they were randomized like everyone else, and the effect on them counts.
The paper's fix: "One way to address this is to define another covariate that indicates whether or not a user appeared in the pre-experiment period. With this additional binary covariate, we can set the missing covariate values to be any constant we like." That makes CUPED a regression on two covariates, the last two columns in the table. In NumPy it takes five moves:
np.isnan(x)is True wherexis missing, and~flips it, sohas = ~np.isnan(x)is the has-history flag.np.where(has, x, 0.0)keepsxwhere the flag is True and puts the constant, 0, everywhere else.- Center both covariates, the filled
xand the flag, by subtracting each one's mean.np.column_stack([a, b])makes them the two columns of a matrixC. np.linalg.lstsq(C, y - y.mean(), rcond=None)[0]gives the least-squares coefficients for predictingy's distance from its mean from those columns, one per column. The first is the slope onx.C @ coefsis each user's predicted part. Subtract it fromy.
The flag gives users without history their own level, like a separate stratum, and the slope on x is fitted on the users who have one. The constant really doesn't matter: fill with 0 or with 123 and the adjusted values agree to within 10−12.
Without the flag it does matter. In a simulation with 30% new users, the flag version cut the estimate's variance to 0.43 of raw, against 0.46 for zeros alone and 0.80 for filling in the returning users' mean.
In the editor, write cuped_new_users(y, x): the flag version, on eight users, three of them new.
- 1
Write
cuped_new_users(y, x): fill a missingxwith 0, add a has-history flag, center both columns, and fit both coefficients withnp.linalg.lstsq. Return the adjustedyand the coefficient onx. - 2
np.isnan(x)is True wherexis missing. The step lists the five moves.
Run your code to check it. Output shows here.