Sub topic 10
Significance, power, and sample size
This chapter assumes you can read short Python snippets and know what a proportion and a standard deviation are. You should also know the rollout vocabulary from earlier chapters: stable and candidate models, sticky assignment, and guardrail metrics. Here you learn how to decide, before an A/B test starts, how much traffic it needs. You also learn how to read the result when it ends.
Why a model comparison needs statistics
An A/B test compares two groups of users. Group A sees the stable model and group B sees the candidate. Each group's metric (for example, conversion rate) is a noisy sample of what that model would do over all future traffic. Even if both models were identical, the two groups would show slightly different rates by chance.
Statistical testing answers one question: how surprising is the observed difference if the two models actually behave the same? It does not tell you how big or how valuable the difference is. It also does not tell you that the candidate is better.
The ingredients
Four quantities drive every sample-size calculation.
| Quantity | Meaning | Typical value |
|---|---|---|
| Baseline rate | The stable model's current metric value | Measured from logs |
| Minimum detectable effect (MDE) | The smallest change worth detecting | Chosen by the business |
| Significance level (alpha) | Accepted chance of a false positive | 0.05 |
| Power (1 - beta) | Chance of detecting a real effect of size MDE | 0.80 |
Two kinds of error matter:
- A false positive (type I error) means declaring the candidate better when it is not. Alpha is the probability of this when there is no true difference.
- A false negative (type II error) means missing a real improvement. Beta is the probability of this when the true effect equals the MDE.
The p-value is the probability of seeing a difference at least as extreme as the observed one, assuming the models are truly equal. A p-value below alpha is called statistically significant. A p-value is not the probability that the candidate is better.
Running the test
The two-proportion z-test compares conversion rates. It pools both groups to estimate the standard error under the assumption of no difference, then measures the observed difference in standard errors.
from math import sqrt
from statistics import NormalDist
def two_proportion_z_test(x_a: int, n_a: int, x_b: int, n_b: int):
p_a, p_b = x_a / n_a, x_b / n_b
pooled = (x_a + x_b) / (n_a + n_b)
se = sqrt(pooled * (1 - pooled) * (1 / n_a + 1 / n_b))
z = (p_b - p_a) / se
p_value = 2 * (1 - NormalDist().cdf(abs(z)))
return z, p_value
z, p = two_proportion_z_test(x_a=480, n_a=10000, x_b=540, n_b=10000)
print(round(z, 2), round(p, 3))This prints 1.93 0.054. Group A converted at 4.8% and group B at 5.4%, a relative lift of 12.5%. Yet at alpha = 0.05 the result is not significant, because 0.054 is above 0.05. The data are consistent with a real improvement and also with chance.
Do not respond by nudging alpha to 0.06 after seeing the result. Fix alpha before the test. Then ask whether the experiment was big enough to detect the effect you cared about.
Power: was the test big enough?
Power is the probability that a test reaches significance when the true effect is real. A test with low power usually fails to find real improvements. When it does reach significance, it tends to overstate the effect size.
The functions below compute the required sample size per group and the power of a given design. They use the normal approximation, which is accurate when each group has at least a few hundred events of each outcome.
from math import ceil, sqrt
from statistics import NormalDist
def sample_size_per_group(p_base: float, mde_abs: float,
alpha: float = 0.05, power: float = 0.80) -> int:
p_new = p_base + mde_abs
nd = NormalDist()
z_alpha = nd.inv_cdf(1 - alpha / 2)
z_beta = nd.inv_cdf(power)
variance = p_base * (1 - p_base) + p_new * (1 - p_new)
return ceil((z_alpha + z_beta) ** 2 * variance / mde_abs ** 2)
def power_two_proportions(p_base: float, mde_abs: float,
n_per_group: int, alpha: float = 0.05) -> float:
p_new = p_base + mde_abs
nd = NormalDist()
z_alpha = nd.inv_cdf(1 - alpha / 2)
se = sqrt((p_base * (1 - p_base) + p_new * (1 - p_new)) / n_per_group)
shift = abs(mde_abs) / se
return nd.cdf(shift - z_alpha) + nd.cdf(-shift - z_alpha)
print(sample_size_per_group(0.048, 0.006))
print(round(power_two_proportions(0.048, 0.006, 10000), 2))The first line prints about 21,100 users per group. The second prints 0.49. The earlier experiment, with 10,000 users per group, had roughly a coin-flip chance of detecting a 0.6 percentage point lift even when that lift was real. The non-significant result said little about whether the candidate was better.
What moves the sample size
The sample size depends on each input in a predictable way.
| Change | Effect on required n |
|---|---|
| Halve the MDE | About four times larger |
| Raise power from 0.80 to 0.90 | About 34% larger |
| Lower alpha from 0.05 to 0.01 | About 49% larger |
| Baseline rate closer to 0.5 | Larger, because variance is higher |
| Lower variance metric (see below) | Smaller |
The MDE term is squared, which is why small effects are expensive. Detecting a 0.1 percentage point lift on a 5% conversion rate takes hundreds of thousands of users per group. If your traffic cannot supply that, you cannot use an A/B test to verify such a small change. Use a canary with guardrails to confirm safety, and accept that you cannot measure the benefit directly.
From sample size to run length
Divide the total required sample by the eligible traffic per day. Count only users who actually reach the point where the models differ, not all site visitors.
With 21,101 users per group and a 50/50 split, you need about 42,200 users. If 10,000 eligible users arrive each day, that is 4.2 days. Round up to full weeks, so the answer is 7 days. User behavior differs between weekdays and weekends. A test that covers only part of a weekly cycle can give a biased answer.
Metrics that are not proportions
Many model metrics are continuous: revenue per user, session length, latency. For these, use a two-sample t-test (or Welch's t-test) and the sample size formula based on the standard deviation. Heavy-tailed metrics such as revenue need extra care, because a few large values dominate the variance. Common remedies are capping outliers or testing a transformed metric. You can also reduce variance by adjusting for a pre-experiment covariate, such as each user's earlier spend (the CUPED method). Lower variance means a smaller required n.
Multiple metrics and guardrails
If you test 20 metrics at alpha = 0.05 with no true effects, you expect about one false positive. Choose one primary metric before the test and base the ship decision on it. Treat other metrics as secondary and explanatory. When you must judge several metrics together, apply a correction such as Bonferroni, which divides alpha by the number of tests.
Guardrail metrics use a different question. You are not trying to prove the candidate is better on latency or error rate. You want to show it is not meaningfully worse. This is a non-inferiority test: you pick a margin, such as "p95 latency no more than 10% above stable," and check whether the evidence rules out a bigger regression. Guardrails often need their own power calculation. A test sized for conversion may be too small to detect a modest error-rate increase on a rare event.
Correlated observations
The formulas above assume each observation is independent. If you randomize by user but count events (clicks, predictions), one user contributes many correlated events, and the standard error shrinks falsely. Either analyze at the randomization unit (one value per user) or use methods that account for clustering. A related case is randomizing by region or store, where groups are few and the effective sample size is the number of clusters, not the number of events.
Practical checklist
- Fix the primary metric, alpha, power, and MDE before launch.
- Compute the sample size and convert it to full weeks of traffic.
- Record the plan and the stopping date.
- Analyze once, at the planned end, using the unit you randomized.
- Report the effect size and its confidence interval, not only the p-value.
Exercise
A recommender's click-through rate is 10%. You want 80% power at alpha = 0.05 to detect a lift to 10.5%. A colleague proposes detecting a lift to 10.25% instead. Without computing exact numbers, roughly how does the required sample size per group change, and why?
Show answer
It grows to about four times larger. Halving the MDE (0.5 to 0.25 percentage points) enters the formula squared in the denominator, so n scales with 1 / MDE squared. The variance term changes only slightly.
Exercise
An experiment with 8,000 users per group shows p = 0.12 for the primary metric. The team concludes the candidate model has no effect. What is wrong with this conclusion, and what should they report instead?
Show answer
A p-value above alpha means the test did not find evidence of a difference, not that the difference is zero. The test may have had low power for the effect that matters. They should report the observed difference with its confidence interval, state the power for the MDE, and either collect more data or accept that only larger effects are ruled out.