How do you determine the sample size needed for an A/B test?
Written by the mlprep team · Updated
Your PM asks: "How long do we need to run this A/B test?" Walk me through how you'd calculate the required sample size. What inputs do you need, and what happens if you get the inputs wrong?
Four parameters that determine sample size#
- α (significance level): acceptable false positive rate. Typically 0.05.
- β (Type II error rate): acceptable false negative rate. Power = 1-β. Typically target 80% or 90% power.
- MDE (Minimum Detectable Effect): smallest effect size you care about detecting. This is a business decision.
- Baseline metric and variance: current conversion rate (or metric mean and variance).
Formula for two-proportion z-test#
For comparing two proportions p_control and p_treatment = p_control + δ:
For α=0.05 (two-tailed): z_α/2 = 1.96 For 80% power (β=0.20): z_β = 0.84 For 90% power (β=0.10): z_β = 1.28
Worked example#
Baseline conversion rate: p_c = 0.10. You want to detect a 1 percentage point lift (MDE = δ = 0.01, so p_t = 0.11). α=0.05, 80% power.
n = (1.96 + 0.84)² · [0.10·0.90 + 0.11·0.89] / 0.01²
= 7.84 · [0.09 + 0.0979] / 0.0001
= 7.84 · 0.1879 / 0.0001
≈ 147,000 per variant
295k total users needed. At 50k users/day: ~6 days minimum.
Key levers: how to reduce sample size#
Increase MDE: only detect larger effects. Costs you: you'll miss small but real improvements.
Increase α: accept more false positives. Uncommon but appropriate for low-stakes exploratory tests.
Reduce variance: use CUPED (controlled experiment using pre-experiment data). Regress post-experiment metric on pre-experiment metric, use residuals. Variance reduction of 50-70% is common, cutting sample size by same factor.
One-tailed test: if you only care about improvement (not degradation). Reduces n by ~20%. Use with caution.
What goes wrong with bad inputs#
Overestimating the effect (optimism bias): the most common mistake. If you expect a 10% lift but actually get 2%, your test is wildly underpowered. You'll see p=0.3 and "fail to detect" an effect that's real.
Underestimating variance: metrics like revenue have high variance and heavy tails. Using a normal variance estimate from a sample can badly underestimate true variance. Run a pilot or use historical data.
Ignoring metric correlation: if control and treatment users aren't independent (e.g., social network effects, household-level experiments), standard error formulas underestimate true variance. Leads to false positives.
CUPED: variance reduction without more users#
CUPED (Deng et al., 2013) was developed at Microsoft and is now standard at Airbnb, Netflix, Booking.com.
Where X is the pre-experiment value of the metric (same users, before the test) and θ = Cov(Y, X) / Var(X).
Y_cuped has the same mean as Y but lower variance because X captures user-level baseline differences. With 60% variance reduction, you need 40% of the original sample size — a 2.5× speedup.
Running time vs sample size#
Sample size is a user count, not a time budget. Convert:
But never stop early because you hit significance — this is peeking (multiple testing). Pre-commit to your sample size or use sequential testing.
Novelty effects#
For UI changes, initial engagement is often inflated by novelty. Run for at least 1-2 weeks to let novelty decay, even if you hit your sample size faster.
Interview red flags#
- Picking a minimum detectable effect to make the test short instead of the smallest effect that matters to the business.
- Leaving baseline variance out of the sample size calculation, or pulling it from the wrong population.
- Stopping an underpowered test early and treating "not significant" as "no effect".
- Not knowing variance-reduction techniques like CUPED when traffic is the constraint.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Sample size depends on α (false positive tolerance), power/β (false negative tolerance), MDE (smallest effect worth detecting — business decision), and baseline variance. For α=0.05, 80% power, 10% baseline rate, detecting a 1pp lift needs ~147k users per variant. Overestimating MDE is the most common mistake — leads to underpowered tests that miss real effects. CUPED reduces required sample by 40-70% using pre-experiment data. Never stop early based on significance — pre-commit or use sequential testing.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How does CUPED reduce variance, and why does it require pre-experiment data for the same users?
- What is the difference between practical significance (MDE) and statistical significance, and why do both matter?
- How would you calculate sample size for a metric like revenue per user, which has very high variance and a heavy tail?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Walk me through how you'd design and analyze an A/B test
Effect size vs statistical significance — why significant results can be meaningless
How do you run A/B tests for ML models in production?
What is the multiple testing problem and how do you control for it?