1. Controlled Experimentation at Scale
In modern tech organizations (Netflix, Meta, Google, Uber), product changes, UI designs, and backend algorithms are not deployed based on intuition. They are validated via Online Controlled Experiments (A/B Testing).
A randomized hashing algorithm assigns incoming users into two isolated cohorts:
- Control ($A$): Current baseline system.
- Treatment ($B$): New feature, model, or UI.
text
2. Sample Size Determination & Power Planning
Before launching an experiment, engineers must calculate the minimum required sample size per variant ($n$). Launching an underpowered experiment risks discarding genuine multi-million dollar improvements (Type II error).
2.1 The Two-Sample Sizing Formula
For significance level $\alpha$ (typically $0.05 \implies z_{\alpha/2} = 1.960$) and power $1 - \beta$ (typically $80% \implies z_\beta = 0.842$):
$$n = \frac{2 (z_{\alpha/2} + z_\beta)^2 \sigma^2}{\delta^2}$$
where:
- $\delta = |\mu_B - \mu_A|$ is the Minimum Detectable Effect (MDE).
- $\sigma^2$ is the metric variance.
2.2 Proportions Sizing (Conversion Rates)
For conversion rates where baseline conversion is $p$, variance is $\sigma^2 = p(1 - p)$:
$$n = \frac{2 (z_{\alpha/2} + z_\beta)^2 p(1 - p)}{\delta^2}$$
Concrete Production Calculation:
A fintech checkout page has a baseline conversion rate $p = 5%$ ($0.05$). The product team wants to detect an absolute lift of $\delta = 0.25%$ (i.e. $5.00% \to 5.25%$, a $5%$ relative lift):
$$(z_{0.025} + z_{0.20})^2 = (1.960 + 0.842)^2 = (2.802)^2 \approx 7.851$$
$$n = \frac{2 \times 7.851 \times (0.05 \times 0.95)}{(0.0025)^2} = \frac{15.702 \times 0.0475}{0.00000625} \approx 119,335 \text{ users per variant}$$
To detect this lift with $80%$ statistical power, you must run the experiment until at least $239,000$ total users have participated.
3. The Peeking Problem (Repeated Significance Testing)
A catastrophic mistake in software engineering is dashboard peeking: checking the experiment daily and stopping the test the instant $p < 0.05$.
Why Peeking Destroys Validity:
When you peek continuously over $N$ days, the probability of reaching a false positive at some point during the experiment is no longer $\alpha = 0.05$. It inflates dramatically:
$$\alpha_{\text{peeking}} = P\left( \exists t \le T : p_t \le 0.05 ;\middle|; H_0 \right) \approx 30% \text{ to } 40% !$$
text
Stopping Rule Solutions:
- Fixed-Horizon Testing: Commit to sample size $n$ ahead of time; evaluate $p$-values exactly once when $n$ is reached.
- Sequential Testing (O'Brien-Fleming / Pocock Boundaries): Uses dynamically adjusted critical thresholds that start very conservative ($\alpha \approx 0.001$) and widen as sample size increases, guaranteeing total false positives remain bounded by $\alpha = 0.05$.
4. CUPED (Controlled-experiment Using Pre-Experiment Data)
Invented by Microsoft Research (Deng et al., 2013), CUPED is the gold standard for variance reduction in production experimentation.
Users exhibit wide behavioral heterogeneity: some users naturally spend $$500$/week, while others spend $$5$. This variance dilutes statistical power.
4.1 The CUPED Mathematical Transformation
Let $Y$ be the metric of interest observed during the experiment. Let $X$ be the same metric observed for that user during the pre-experiment period (before assignment).
Define the adjusted metric:
$$Y_{\text{CUPED}} = Y - \theta (X - E[X])$$
where $\theta$ is an optimal scaling constant.
4.2 Optimal Choice of $\theta$
To minimize the variance of $Y_{\text{CUPED}}$:
$$\text{Var}(Y_{\text{CUPED}}) = \text{Var}(Y) + \theta^2 \text{Var}(X) - 2\theta \text{Cov}(Y, X)$$
Differentiating with respect to $\theta$ and setting to zero:
$$2\theta \text{Var}(X) - 2\text{Cov}(Y, X) = 0 \implies \theta^* = \frac{\text{Cov}(Y, X)}{\text{Var}(X)}$$
4.3 Variance Reduction Factor
Substituting $\theta^*$ back into the variance formula:
$$\text{Var}(Y_{\text{CUPED}}) = \text{Var}(Y) \left( 1 - \rho_{X, Y}^2 \right)$$
where $\rho_{X, Y}$ is the correlation between pre-experiment and post-experiment metrics.
text
CUPED removes user-specific baseline variance while preserving an identical, unbiased estimate of the treatment effect:
$$E[Y_{\text{CUPED}}] = E[Y] - \theta (E[X] - E[X]) = E[Y]$$
5. Multi-Armed Bandits vs. Fixed-Horizon A/B Testing
In scenarios where the cost of showing a suboptimal variant is high (e.g. ad revenue or emergency disaster response):
text
6. Python Implementation: Sample Sizing & CUPED
python
7. Key Takeaways
- Size Before Launching: Calculate required $n$ using target MDE, $\alpha$, and $1 - \beta$ to avoid underpowered tests.
- Avoid Peeking: Continuously evaluating $p$-values inflates false positive rates from $5%$ up to $40%$.
- Always Use CUPED: Using pre-experiment metrics removes between-user variance, cutting experiment duration by $30\text{--}60%$.