Sub topic 6
Estimating Sample Size and Duration
Prerequisites and the planning question
Assume you already have a testable hypothesis, a primary metric, a randomization unit, and an eligible audience. Sample size is the number of randomized units needed in each variant. Duration is the time required to collect that sample.
Estimate both before launch. A test that ends too early may miss a useful effect. A test that runs too long may spend engineering and traffic capacity without improving the decision.
The inputs that matter
For a binary metric such as checkout completion, define these inputs:
- Baseline rate: The current metric value without the change. For example, 10% of eligible users complete checkout.
- Minimum detectable effect (MDE): The smallest change worth detecting. An absolute MDE of 1 percentage point means detecting a change from 10% to 11%, not a 1% relative increase.
- Significance level, or alpha: The tolerated false-positive rate. A common two-sided choice is 0.05.
- Power: The probability of detecting the MDE when it is real. A common choice is 0.80.
- Allocation: The share of traffic assigned to each variant. Equal allocation usually gives the most statistical power for a fixed total sample.
The MDE is a product decision as well as a statistical input. A smaller MDE needs many more users, so choose an effect large enough to justify the cost of building and running the change.
import math
from statistics import NormalDist
def sample_size_per_variant(baseline, mde_abs, alpha=0.05, power=0.8):
"""Approximate users per variant for a two-sided test of two proportions."""
p1 = baseline
p2 = baseline + mde_abs
z = NormalDist().inv_cdf
z_alpha = z(1 - alpha / 2)
z_beta = z(power)
variance = p1 * (1 - p1) + p2 * (1 - p2)
return math.ceil((z_alpha + z_beta) ** 2 * variance / (p2 - p1) ** 2)
print(sample_size_per_variant(0.10, 0.01))This approximation returns about 14,700 users per variant. It assumes independent users, a stable baseline, equal allocation, and a two-sided comparison of two proportions. A data partner should confirm the calculation when the metric is continuous, heavily skewed, clustered, or affected by repeated observations from the same user.
Converting sample size into duration
Estimate the eligible traffic rate, not total site traffic. If 2,000 eligible users arrive each day and traffic is split equally, each variant receives about 1,000 users per day. With 14,700 users needed per variant, the collection period is approximately 15 days.
Add time for ramping and operational constraints. A practical estimate is:
- Calculate users needed per variant.
- Divide by expected eligible users per variant per day.
- Round up to a whole day.
- Add ramp time and a buffer for holidays, outages, or uneven traffic.
- Run long enough to cover the normal weekly usage pattern.
Common planning errors
- Using total visitors when only a subset is eligible.
- Confusing a relative lift with an absolute percentage-point lift.
- Ignoring allocation changes during a ramp.
- Underestimating duration because traffic varies by weekday.
- Changing the MDE after seeing early results.
- Treating repeated sessions from one user as independent observations.
Exercise
The baseline completion rate is 10%, the absolute MDE is 1 percentage point, and each variant needs 14,700 users. If 1,200 eligible users arrive per day and allocation is equal, what is the minimum collection time before adding a buffer?
Show answer
Each variant receives about 600 users per day. 14,700 divided by 600 is 24.5, so round up to 25 days.