CUPED Theta Coefficient
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
CUPED adjusts an in-experiment metric `y` with a pre-experiment covariate `x`, aligned per unit, as `y - theta * (x - mean(x))`. The coefficient is `theta = cov(y, x) / var(x)`.
Implement `cuped_theta(y: np.ndarray, x: np.ndarray) -> float` in Python and return theta.
- Use `np.cov(y, x, ddof=1)[0, 1]` for the covariance (the off-diagonal entry, not `[0, 0]`) and `np.var(x, ddof=1)` for the variance.
- Use the same `ddof` in the numerator and the denominator. `ddof=0` on both gives the same answer.
- Do not compute `np.cov(y, x)[0, 1] / np.var(x)`. The two functions have different default `ddof` values, and on the example that expression gives `1.3258983890954150`.
- Return a plain, unrounded `float`.
```python
y = [12, 15, 9, 20, 14, 11, 18, 16, 8, 22]
x = [10, 13, 8, 17, 12, 10, 15, 14, 7, 19]
cuped_theta(y, x)
# -> 1.1933085501858736
```
```hint Think about the denominator
The ratio divides by the variance of `x`. Decide what the function should do when that variance is zero, or when there are too few points for `ddof=1`.
```
### Constraints & Assumptions
- `y` and `x` are one-dimensional, have the same length and hold at least two values. `x[i]` and `y[i]` belong to the same unit.
- Either input may be a Python list or a NumPy array of numbers.
### What a Strong Answer Covers
- Takes the off-diagonal entry of the covariance matrix, divides it by the variance of `x` computed with the same `ddof`, and returns a Python `float`.
- Explains why a matching `ddof` cancels in the ratio and by how much mixing the NumPy defaults distorts theta.
- Handles a constant covariate, too few points, mismatched lengths and missing values.
- Connects theta to the adjustment: why it minimizes the adjusted variance, and how it is estimated and applied across both arms.
### Follow-up Questions
- Why is `cov(y, x) / var(x)` the value of theta that minimizes the variance of the adjusted metric?
- Should theta be estimated on the pooled data from both arms or separately per arm? What changes?
- If theta is computed from the experiment data itself, does the adjustment bias the treatment effect estimate?
Overview: Implement a Python function that computes the CUPED adjustment coefficient theta from an in-experiment metric and a pre-experiment covariate. It tests the covariance over variance formula, consistent ddof handling in NumPy, and how theta is applied without biasing the treatment effect.