What You'll Learn
- 1Compute required sample size and test duration from baseline rate, minimum detectable effect, and power
- 2Derive the smallest effect a fixed traffic budget can detect before launching
- 3Detect sample ratio mismatch with a chi-squared test before reading any result
- 4Cut required sample with CUPED and decide when a bandit beats a fixed split
Why This Matters
Deploying ML models without rigorous experimentation risks degraded user experience, revenue loss, or safety issues. A/B testing and statistical rigor ensure model improvements are real, not noise. Multi-armed bandits and shadow deployment enable faster, safer iteration.
The cost of guessing is measurable. A ranking change worth 1 percent of conversion on 100 million USD of annual GMV is a 1 million USD decision, and a test underpowered to detect a 1 percent lift returns a coin flip that reads like a result. Sample size arithmetic converts "how long do we run this" from an opinion into a date.
System Architecture Overview
Experiment Framework
An experimentation framework manages experiment definitions (variants, traffic splits, primary and secondary metrics), deterministic user-to-variant assignment using hash-based routing, and statistical analysis of results. Variant assignment hashes a concatenation of experiment name and user ID, then maps to a variant based on cumulative traffic percentages, so a user stays in the same arm for the life of the test and a page reload never reshuffles them.
Statistical analysis uses a two-sample test with confidence intervals and p-values, rejecting the null at alpha = 0.05. Required sample size per variant comes from power analysis:
For a 5 percent baseline conversion, a 10 percent relative lift means an absolute delta of 0.005, the variance of a proportion is p(1 - p) = 0.0475, and with alpha = 0.05 (Z = 1.96) and power 0.80 (Z = 0.84):
So a 10 percent relative improvement on a 5 percent metric needs about 30,000 users per arm, 60,000 total. The inverse question matters just as much, because traffic is fixed and the effect size is not:
With only 10,000 users per arm and the same 5 percent baseline, , which is 0.86 points or 17 percent relative. That test cannot see a 5 percent lift no matter how long it runs, and reporting a winner from it is reading noise.
| Relative MDE | n per variant | Days at 10,000 users per arm per day |
|---|---|---|
| 5% | 124,800 | 13 |
| 10% | 29,800 | 3 |
| 20% | 7,450 | 1 |
| 50% | 1,200 | 1 |
Sample Ratio Mismatch
Before reading any metric, check that the split you planned is the split you got. A chi-squared goodness-of-fit test against the planned allocation:
With 10,000 users planned 50/50 and observed 4,800 in A against 5,200 in B, the expected count is 5,000 each and , which is p = 6.3e-5 for one degree of freedom. That is far past the usual 0.001 alarm threshold, and it means the assignment or logging is broken, so every downstream metric is invalid. Common causes are a bot filter that fires asymmetrically, a cache serving stale assignments, and an exclusion that runs after the split.
Variance Reduction with CUPED
CUPED regresses out each user's pre-experiment metric from their in-experiment metric, using the covariate window from before the test launched:
Where is the correlation between the pre-period and in-period metrics. At the variance falls to 0.64 of its original value, and since required sample size scales with variance, the same test finishes with 36 percent fewer users: the 3-day test above drops to about 1.9 days. Anything in the 0.4 to 0.7 correlation band, which covers most engagement and revenue metrics, buys 16 to 51 percent of your runtime back for the cost of storing one extra week of pre-period data.
Multi-Armed Bandit
Multi-armed bandits adaptively allocate traffic based on observed performance. Thompson Sampling maintains Beta distribution posteriors for each arm and draws from each to pick the round's arm: after s successes in n pulls the posterior is Beta(1 + s, 1 + n - s), so an arm that has failed repeatedly is rarely drawn and its traffic share collapses on its own. Epsilon-greedy bandits exploit the best-known arm with probability and explore uniformly with probability , which is simpler to implement but keeps paying for obviously bad arms.
Regret is the yardstick. Over T rounds, pulling arm with mean reward when the best arm has mean :
At T = 100,000 rounds with epsilon = 0.10 and a suboptimal arm worth 2 percent less, exploration alone costs 10,000 x 0.02 = 200 reward units while the other 90,000 rounds earn the optimum. Thompson sampling cuts that exploration term by roughly half because it stops drawing arms whose posterior is clearly dominated. Bandits converge faster than A/B tests but hand back far less statistical confidence, so the common pattern is a two-week bandit to find a winner, followed by a fixed 50/50 test to prove it.
Shadow Deployment
Shadow deployment runs a new model alongside the production model without affecting users. Each request is processed by both models - the production result is returned to the user, while the shadow result is logged for comparison. A comparison report shows agreement rate and disagreements, enabling safe evaluation before A/B testing.
Common Mistakes
Launching without a power calculation
Fix: Compute n from baseline, MDE, and power first: a 5 percent baseline with a 10 percent relative lift needs about 30,000 users per arm.
Reading results before checking sample ratio mismatch
Fix: Run a chi-squared test on observed versus planned allocation; 4,800 against 5,200 gives chi-squared = 16 and invalidates every metric.
Peeking daily and stopping when p crosses 0.05
Fix: Fix the duration up front or use a sequential test with alpha spending, since daily peeking inflates the false positive rate past 20 percent.
Best Practices
- Pre-register the primary metric, MDE, and runtime so the analysis is decided before the data exists
- Store one week of pre-experiment metrics per user so CUPED can cut required sample by 16 to 51 percent
- Layer guardrail metrics on every experiment and block promotion when any of them degrades
Key Takeaways
- Sample size is 2 (Z_alpha + Z_beta)^2 sigma^2 / delta^2, which is about 29,800 per arm for a 5 percent baseline and a 10 percent relative lift
- With 10,000 users per arm the smallest detectable effect is 17 percent relative, so a 5 percent lift is invisible no matter how long you run
- Sample ratio mismatch is a chi-squared test: 4,800 against 5,200 of 10,000 gives 16, far past the 0.001 alarm threshold
- CUPED at correlation 0.6 removes 36 percent of variance, and required sample scales with variance, so the test finishes 36 percent sooner
Frequently Asked Questions
How long should I run an A/B test?
Run for whole weeks until the pre-computed sample size is reached, never for a convenient number of days. At 20,000 users per day split 50/50, a 10 percent relative lift on a 5 percent baseline needs 29,800 per arm, which is 3 days; the same test for a 5 percent lift needs 124,800 per arm, which is 13 days. Always cover at least two full weekly cycles, because a test started on a Tuesday and read on a Thursday compares different day-of-week mixes.
Why can't I just peek at the results every day?
Each peek is another chance to cross the 0.05 threshold by luck, and after 20 daily peeks the effective false positive rate is above 20 percent rather than 5 percent. The fix is either to fix the duration before launch, or to use a sequential procedure such as group sequential testing or always-valid p-values that spends alpha across looks so the overall rate stays at 0.05.
What is a guardrail metric?
A guardrail is a metric that must not degrade regardless of what the primary metric does: crash rate, p99 latency, refund rate, unsubscribe rate, and search zero-result rate are the usual set. The treatment can win on conversion and still be rolled back, so guardrails are evaluated with the same statistical test and wired into an automatic rollback rule rather than a dashboard nobody watches.
Interview Questions
Q1: How do you determine the minimum sample size for an A/B test?
From power analysis: n = 2 (Z_alpha + Z_beta)^2 sigma^2 / delta^2 with alpha 0.05 and power 0.80. For a 5 percent baseline and a 10 percent relative lift, sigma^2 = 0.0475 and delta = 0.005 give 29,792 users per arm. Compute it before launch and derive the end date from your traffic, not the other way round.
Q2: What is CUPED and why is it useful?
Controlled-experiment Using Pre-Experiment Data regresses out each user's pre-period metric, leaving variance of Var(Y) times (1 - rho^2). At rho = 0.6 that is 36 percent less variance and therefore 36 percent fewer users, which turns a 3-day test into 1.9 days at no statistical cost.
Q3: How do you handle network effects in A/B testing?
Social and marketplace features violate the assumption that one user's treatment does not affect another's outcome. Randomize by cluster instead: by geo cell, by team, or by household, or alternate treatment by week across the whole population. Cluster randomization inflates the required sample by the design effect of 1 + (cluster size - 1) times the intra-cluster correlation, which is often a 3 to 5x cost.