Foundations to Frontier
Tests
A. Mathematical Foundations › A5 Statistics › Causal inference

Randomized Experiments and A/B Testing

Randomization is the gold standard of causal inference: it breaks every arrow into the treatment, measured or not. You will design experiments end to end: power and sample size, blocking and stratification, two-proportion tests and confidence intervals, variance reduction with pre-experiment covariates (CUPED), sanity checks such as sample-ratio mismatch, and the pitfalls of peeking, multiple metrics and interference, applied to robot fleets and flight software.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Let a coin decide

To find out whether a new gripper really picks better, don’t let engineers choose which robots get it; they might pick the best-maintained ones. Flip a coin for each robot. The coin knows nothing about the robots, so the two groups are alike in every way on average, whether or not we measured it. Any difference afterwards is caused by the gripper, or by luck. Statistics tells us how much luck to expect.

An A/B test is the same idea for software. Half the fleet runs version A and half runs version B, chosen at random.

Run an A/B test, and see what peeking does to it
+1.96−1.96
final z -0.48 (not significant)
Over 300 experiments, rejection rate: test once at the end 4.7% · stop at first |z| > 1.96 26.3% (with no true lift both should be 5%)
With lift = 0 this is an A/A test: any "win" is a false positive. The running z wanders like a random walk and eventually crosses ±1.96 if you look often enough, so peeking every day inflates the false-positive rate far above 5%. Fixes: commit to a sample size from a power calculation, or use sequential methods built for continuous monitoring (alpha spending, always-valid p-values).
Why engineers care

Fleet operators A/B-test planners, perception models and firmware. Flight-software teams randomize test cases and Monte Carlo seeds. Good experiment design (enough samples, no peeking, sanity checks) separates real improvements from noise and prevents shipping regressions.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Why assign treatment by coin flip?
  2. 2. An A/B test compares…
  3. 3. Checking results every day and stopping when it looks good…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Analysing a success-rate experiment

With sA/nAs_A/n_A and sB/nBs_B/n_B successes, the two-proportion z-test uses the pooled p^\hat p:

z=p^B−p^Ap^(1−p^)(1/nA+1/nB)z = \frac{\hat p_B - \hat p_A}{\sqrt{\hat p(1 - \hat p)(1/n_A + 1/n_B)}}

The 95% CI for the lift is (p^B−p^A)±1.96p^A(1−p^A)/nA+p^B(1−p^B)/nB(\hat p_B - \hat p_A)\pm1.96\sqrt{\hat p_A(1 - \hat p_A)/n_A + \hat p_B(1 - \hat p_B)/n_B}. For 82% vs 86% with 1000 each, z≈2.44z\approx2.44 and p≈0.015p\approx0.015.

Sample size before you start

To detect an absolute lift δ\delta with significance α\alpha and power 1−β1 - \beta, use per arm

n=(z1−α/2+z1−β)2 [p0(1−p0)+p1(1−p1)]δ2n = \frac{(z_{1-\alpha/2} + z_{1-\beta})^2\,[p_0(1 - p_0) + p_1(1 - p_1)]}{\delta^2}

Detecting 80% → 83% takes about 2600 robot runs per arm. Halving δ\delta quadruples nn.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Halving the minimum detectable effect multiplies the required n by…
  2. 2. At α = 0.05 and 80% power, z₁₋α/₂ + z₁₋β ≈
  3. 3. The sample size should be fixed…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Design choices

  • Unit of randomization: robot, mission, day or site. Analyse at that level. Interference between units (shared maps, traffic) calls for cluster or switchback designs.
  • Blocking / stratification: randomize separately within terrain types or hardware revisions. This guarantees balance on them and reduces variance.
  • Variance reduction with CUPED (Deng et al., 2013): use a pre-experiment covariate XX and analyse Y−θ(X−Xˉ)Y - \theta(X - \bar X) with θ=Cov⁡(Y,X)/Var⁡X\theta = \operatorname{Cov}(Y, X)/\operatorname{Var}X. The variance drops by 1−ρ21 - \rho^2, so the same precision needs fewer units. Since XX is measured before treatment, the estimate stays unbiased.
  • Sanity checks: a sample-ratio mismatch (SRM) test on the A/B counts, A/A tests, and guardrail metrics.

Pitfalls

  • Peeking: repeatedly testing and stopping at significance inflates the type I error. With 10 looks it’s about 20%. Use group-sequential designs (Pocock, O’Brien–Fleming), alpha spending, or always-valid inference (Johari et al., 2017).
  • Many metrics or segments: correct for multiplicity (A5.1-10) or pre-specify a primary metric.
  • Novelty effects and non-stationarity: run full cycles.
🐍 Exercise 1: z-test, CI and sample size
Implement the two-proportion z-test with a CI and the sample-size formula. Verify by simulation that the planned n gives about 80% power.
📝 Quick check (Level 2)
0/3 answered
  1. 1. CUPED reduces variance by the factor…
  2. 2. A sample-ratio mismatch indicates…
  3. 3. Blocking randomizes…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Deriving the sample-size formula

δ^≈N(δ,σ2/n)\hat\delta\approx\mathcal N(\delta, \sigma^2/n) with σ2=p0(1−p0)+p1(1−p1)\sigma^2 = p_0(1 - p_0) + p_1(1 - p_1). We reject when δ^>z1−α/2σ/n\hat\delta > z_{1-\alpha/2}\sigma/\sqrt n (one side dominates). Power is P(δ^>z1−α/2σ/n)=Φ(δn/σ−z1−α/2)P(\hat\delta > z_{1-\alpha/2}\sigma/\sqrt n) = \Phi(\delta\sqrt n/\sigma - z_{1-\alpha/2}). Setting this to 1−β1 - \beta gives δn/σ=z1−α/2+z1−β\delta\sqrt n/\sigma = z_{1-\alpha/2} + z_{1-\beta}. ∎

Why CUPED is unbiased and optimal

E[X]\mathbb E[X] is the same in both arms (it’s pre-treatment and randomization balances it), so subtracting θ(X−Xˉ)\theta(X - \bar X) doesn’t shift the difference in means. Var⁡(Y−θX)=Var⁡Y−2θCov⁡(X,Y)+θ2Var⁡X\operatorname{Var}(Y - \theta X) = \operatorname{Var}Y - 2\theta\operatorname{Cov}(X, Y) + \theta^2\operatorname{Var}X is minimized at θ∗=Cov⁡/Var⁡X\theta^* = \operatorname{Cov}/\operatorname{Var}X, giving Var⁡Y(1−ρ2)\operatorname{Var}Y(1 - \rho^2). ∎ This is the control-variates idea from Monte Carlo (A5.2-6).

Why peeking inflates error

Under the null, the running z-statistic behaves like a scaled random walk zk=Sk/kz_k = S_k/\sqrt k. By the law of the iterated logarithm it eventually exceeds any fixed threshold with probability 1, so “test until significant” always rejects eventually. Group-sequential boundaries spend α\alpha across looks so the total stays at α\alpha.

Exercise 3.1: Per-arm n for a continuous metric with σ = 1 and δ = 0.5

n=2(1.96+0.84)2/0.25≈62.8n = 2(1.96 + 0.84)^2/0.25\approx62.8, so 63 per arm.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The optimal CUPED θ is…
  2. 2. Testing until significant under the null…
  3. 3. O'Brien–Fleming boundaries are…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Fleet experimentation done right

A delivery-robot operator tests a new path planner. Plan: randomize per robot, blocked by depot. Primary metric: on-time rate. CUPED uses the previous month’s on-time rate. Fix n from a power calculation, run an SRM check daily (but don’t peek at the metric), and analyse once. Exercise 2 shows CUPED’s variance cut and the false-positive inflation from peeking, using simulation.

🐍 Exercise 2 (research): CUPED and the peeking problem
Implement CUPED and show its variance ratio equals 1 − ρ². Then run 2000 A/A experiments with 10 interim looks and compare the false-positive rate with and without peeking.

Experimentation in AI, robotics and aerospace

  • Online controlled experiments at scale: Kohavi, Tang and Xu (2020) is the field manual, covering SRM, guardrails, variance reduction and interference.
  • Bandits vs A/B tests: adaptive allocation trades estimation precision for lower regret (A4.1 and A5.2), as in Thompson sampling (Russo et al., 2018).
  • Sim-to-real evaluation: randomized seeds and paired designs (common random numbers) compare controllers in simulation with much less variance.
  • Clinical-trial-style flight tests: pre-registered success criteria and sequential monitoring for expensive, safety-critical trials (Jennison and Turnbull, 2000).

References

  1. R. A. Fisher (1935). The Design of Experiments. Oliver & Boyd.
  2. R. Kohavi, D. Tang, Y. Xu (2020). Trustworthy Online Controlled Experiments. Cambridge University Press.
  3. A. Deng, Y. Xu, R. Kohavi, T. Walker (2013). “Improving the sensitivity of online controlled experiments by utilizing pre-experiment data.” WSDM.
  4. R. Johari, P. Koomen, L. Pekelis, D. Walsh (2017). “Peeking at A/B tests: why it matters, and what to do about it.” KDD.
  5. S. Pocock (1977). “Group sequential methods in the design and analysis of clinical trials.” Biometrika, 64(2).
  6. P. O’Brien, T. Fleming (1979). “A multiple testing procedure for clinical trials.” Biometrics, 35(3).
  7. C. Jennison, B. Turnbull (2000). Group Sequential Methods with Applications to Clinical Trials. Chapman & Hall/CRC.
  8. A. Fabijan et al. (2019). “Diagnosing sample ratio mismatch in online controlled experiments.” KDD.
  9. D. Russo, B. Van Roy, A. Kazerouni, I. Osband, Z. Wen (2018). “A tutorial on Thompson sampling.” Foundations and Trends in Machine Learning, 11(1).
  10. I. Bojinov, D. Simchi-Levi, J. Zhao (2023). “Design and analysis of switchback experiments.” Management Science, 69(7).