MLCrack

    PROBABILITY & STATISTICS · INTERVIEW QUESTION

    How do you determine sample size for an A/B test?

    🟡 intermediate · Experimental Design

    Hint: Think about significance level, power, minimum detectable effect, and baseline rate.

    Answer

    Sample size determines how long an A/B test must run to detect a meaningful difference with statistical confidence.

    Key Inputs

    ParameterTypical ValueDescription
    α\alpha0.05Significance level (Type I error rate)
    1−β1-\beta0.80Statistical power
    MDE (δ\delta)VariesMinimum detectable effect
    σ2\sigma^2From dataMetric variance

    Formula

    n=(z1−α/2+z1−β)2⋅2σ2δ2n = \frac{(z_{1-\alpha/2} + z_{1-\beta})^2 \cdot 2\sigma^2}{\delta^2}

    python
    from math import ceil from statsmodels.stats.power import NormalIndPower analysis = NormalIndPower() n = analysis.solve_power( effect_size=0.05, # standardized mean difference delta / sigma alpha=0.05, power=0.80, alternative='two-sided' ) print(f'Required sample size per group: {ceil(n)}')

    This normal-approximation formula gives the required sample size per arm, with equal allocation, common variance, absolute mean difference delta, and standard-normal quantiles. For conversion rates, specify baseline and alternative rates and use a two-proportion power calculation; a relative MDE is not the standardized effect above.

    Practical Considerations

    • Use a prespecified sequential testing procedure if monitoring for early stopping
    • Account for multiple comparisons (Bonferroni correction)
    • Consider CUPED for variance reduction
    • Ensure the test runs for at least one full business cycle