Power & Sample Size
After this lesson, you will be able to:
- Define statistical power as 1 minus beta, and predict how it moves when effect size, noise, sample size or alpha changes
- Separate a statistically significant result from an important one: a tiny effect with a huge sample gives a tiny p-value and still changes nothing
- Compute the sample size for an A/B test on a conversion rate from a formula, then confirm it by simulation
- Estimate how many evaluation examples a small accuracy gain needs, and explain why a 500-example benchmark cannot separate models 1 point apart
Before you start
#Why Power Matters
#The Four Levers of Power
Power for a two-group comparison of means depends on four things, and only four. Let delta be the true difference between group means, sigma the standard deviation of one observation, n the number of observations per group, and alpha the significance level.
Start with a picture, before any formula. Imagine running the experiment thousands of times and recording the observed difference between the two group means each time. If there is no real effect, those differences pile up in a bell centred on 0 (the grey bell, the null). If the true difference is delta, they pile up in a second bell of the same width centred on delta (the blue bell, the alternative). The test rejects whenever the observed difference lands beyond a cut-off line. The red area of the grey bell beyond the line is alpha, the false alarms. The amber area of the blue bell short of the line is beta, the misses. The green area of the blue bell beyond the line is power.
Move delta, sigma and n in the picture below and watch the bells slide apart or narrow; the Baseline preset has power of about 29%.
One caution. The effect delta in these formulas is a guess you make before any data exist: the smallest real difference you would care to detect. Change that guess and every number changes with it. With delta = 0.5 and sigma = 2, reaching 2.80 standard errors needs 2 x 4 x 2.80^2 / 0.25 = 251.2, so 252 per group, which gives power 0.801.
norm.ppf, the inverse of Phi.)A note on letters. Here alpha is the false alarm rate and beta is the missed discovery rate. The Bayesian Inference in Practice lesson uses alpha and beta for the shape of a Beta curve, which is unrelated. Also, sigma is the true spread of the data, while a lowercase s is the spread you estimate from a sample.
The distance between the bells, measured in standard errors, is the effect divided by the standard error of a difference of two group means. Each mean has standard error sigma over the square root of n, and the two variances add, giving a standard error of sigma times the square root of 2/n for the difference. Read the area to the right of the cut-off line under the blue bell:
Read the ratio inside the parentheses: effect divided by standard error. Every lever acts by moving that ratio, or by moving the critical value it has to beat.
- Larger effect (delta up): the signal is bigger, power rises.
- Less noise (sigma down): the signal is clearer, power rises.
- More data (n up): the standard error shrinks like 1 over the square root of n, power rises. Quadrupling n halves the standard error.
- Looser alpha (alpha up): the critical value drops, so you reject more easily. Power rises, but so does the false alarm rate. This lever is not free.
The first two combine into one number, Cohen's d = delta / sigma, the effect measured in units of noise. Here d = 0.5 / 2 = 0.25.
Open the derivation to see where the power formula comes from, and why power depends on d and n only through d times the square root of n over 2.
Baseline: effect 0.5, noise sigma 2, n = 64 per group, alpha 0.05, giving about 29% power. Which single change brings power to about 80%?
Run the cell and verify every lever with numbers.
Notice the asymmetry in the last two lines of the table: loosening alpha to 0.10 lifts power from 0.29 to 0.41, but you paid for it by doubling your false alarm rate. More data, or less noise per observation, is what lets you push both errors down together: you can hold alpha at 0.05 and still shrink beta.
#Effect Size vs Statistical Significance
A p-value answers "is the effect nonzero?" It does not answer "is the effect big?" Those are different questions, and sample size is what separates them.
One notation note for the rest of the lesson, because the letter p has two jobs in statistics. A proportion, such as a conversion rate, is always written p1 or p2 (or pbar for the average of the two). A p-value is always written out as "p-value" in the text. Inside the code cells, proportions keep names like p1 and p2, and p-values are called pvals or pv.
Two measures of how big an effect is:
- Absolute lift: the raw difference, such as a conversion rate going from 10.0% to 10.1%, a lift of 0.1 percentage points.
- Cohen's d: the difference divided by the standard deviation (for two groups, the two standard deviations combined into one, called the pooled standard deviation). Rough conventions are 0.2 small, 0.5 medium, 0.8 large, though what counts as important depends on cost and context, not on those labels.
Because the standard error shrinks as n grows, any nonzero effect becomes significant if you collect enough data. Here is a constructed example. Variant B converts at 10.1% versus 10.0% for control, and each arm has 3 million users.
With 3 million users per arm the lift gives z about 4.07 and a p-value of about 0.00005. The effect is real, and yet it is 0.1 percentage point, or 1,000 extra conversions per million visitors, with Cohen's d of 0.003. Whether that justifies the engineering cost of the change is a business question the p-value cannot answer. At 100,000 per arm the very same effect is nowhere near significant.
Team A reports p = 0.0004 for a new ranking model. Team B reports p = 0.08 for a different one. Which statement is safe?
#Sample Size for an A/B Test
The two bells must sit far enough apart that the cut-off clears the false alarm line and still leaves 80% of the blue bell beyond it. That distance is the z for alpha plus the z for power, counted in standard errors. Solve for n and you get:
Worked with the lesson's baseline numbers (sigma = 2, delta = 0.5): z_ + z_power = 1.960 + 0.842 = 2.802, squared is 7.85, and n = 2 x 4 x 7.85 / 0.25 = 251.2. Always round up, so 252 per group. That agrees with the quadrupling you saw earlier (256 per group gave 80.7%). Never trust a formula you have not simulated, so check it:
At 252 per group the simulated power is 0.803, near the 0.80 target, and at half that sample it is only about 0.50. The last line shows the squared effect at work: halving delta to 0.25 needs 1,005 per group, four times as many.
Now the A/B test itself. For a yes/no outcome there is no separate noise number sigma. A visitor converts or does not, and the variance of one visitor's outcome with rate p is p(1 - p). So sigma squared becomes pbar(1 - pbar), where pbar = (p1 + p2) / 2 is the average of the two rates, and delta becomes the lift p2 - p1:
Two conventions are hiding in that formula. The first is "pooled": we treat the two arms as one group with a single shared rate pbar and use one variance for both bells. That is exactly right for the grey bell, where there is no effect and both arms share one rate. For the blue bell the two arms really have different rates, 10% and 11%, with variances 0.09 and 0.0979. A more careful textbook version uses each arm's own variance for the power part; it gives 14,750.8, which rounds to 14,751. The two answers differ by one visitor because the rates are close, and they drift apart only when the lift is large compared with the baseline. This lesson and the planner below use the simpler pooled version.
Run the proportions version, then check it by brute-force simulation rather than trusting it.
#Power Curves
Two things to read off the right-hand chart. First, the curve starts at 0.05 when the true lift is zero: with no effect, "power" is just the false positive rate, which is alpha. Second, the 14,752-per-arm design reaches 80% only at the 1-point lift it was built for. If the true lift is half that, you will see it only about 29% of the time, and you will confidently report "no effect" most of the time you have a real one.
Now try it yourself: drag the lift and the sample size and watch both curves and the required n move. The tool also has a "Metrics tested" slider; leave it at 1 for now, because Multiple Testing, Peeking & P-Hacking explains what it does and brings this tool back. The StreamBox preset starts from the shared table's 22% churn rate.
#Connection to Machine Learning
A benchmark is an experiment, and its size is the number of examples. The sample size formula above therefore tells you how big an evaluation set must be.
#How many eval examples you need
Check the claim with code. The function below is the A/B formula from Sample Size for an A/B Test, reused with accuracies in place of conversion rates.
A 1-point gain from 90% needs about 13,500 examples per model if the two scores come from independent samples, or about 3,900 if you compare the two on the same examples and they disagree on 5% of them. The 5% is an assumption for the example; measure the disagreement rate on your own models.
The same two-bell argument gives that paired size. Open it to see why only the disagreements count.
#Try It Yourself
Build the first half of the "Experiment Planner" from the top of this lesson. You will write the sample size formula as a function, see how fast n grows as the minimum detectable lift shrinks, and check the plan by simulation. The derived formula from the Sample Size section is the only maths you need. The next lesson adds the number of metrics to this same planner.
Tests: plan_ab_test(0.10, 0.01) returns n_per_arm 14,752, total 29,504 and days 15; an MDE of 0.02 needs 3,843 per arm (4 days), 0.005 needs 57,764 (58 days) and 0.0025 needs 228,556 (229 days); baseline 0.02 with MDE 0.002 needs 80,683 per arm (81 days); the first MDE on the grid with n at most 8,000 is 1.4 points; the simulated power at n = 14,752 is near 0.80 (0.803 with seed 0).
When you run the solution you should see 14,752 per arm and 15 days for the 1-point MDE. The table above it is the cost of ambition, with n rounded up: 3,843 per arm (4 days) for a 2-point lift, 57,764 (58 days) for 0.5 points, and 228,556 (229 days) for 0.25 points, so each halving of the MDE multiplies the sample by about four. The rare-outcome case (2% baseline, 0.2-point lift) needs 80,683 per arm, 81 days at 1,000 users a day, since a 10% relative lift on a rare outcome is a tiny absolute lift. With 8,000 per arm the smallest MDE on the grid is 1.4 points, and the simulation of the 1-point plan returns power 0.803.
#Key Takeaways
- Power is 1 minus beta, the chance of catching a real effect — it depends on effect size, noise, sample size and alpha; with effect 0.5, sigma 2 and n = 64 per group it is only about 29%, and doubling the effect, halving sigma, or quadrupling n each lift it to about 81%
- Significance is not importance — a 0.1-point lift in conversion with 3 million users per arm gives p about 0.00005 and Cohen's d of 0.003; report the effect size with a confidence interval, not just the p-value
- Two bells give the sample size — the distance between the bells must cover z for alpha plus z for power, so n per group = 2 x sigma^2 x (1.96 + 0.84)^2 / delta^2, which is 252 for sigma 2 and delta 0.5 and checks out in simulation
- Size the test before you run it — detecting a lift from 10% to 11% at alpha 0.05 and 80% power needs about 14,752 users per arm, confirmed by simulation (about 80% power, about 5% false positives); halving the MDE multiplies that by almost four
- In ML, small gains need big evaluation sets — detecting a 1-point accuracy gain from 90% needs about 13,500 examples per model, or roughly 3,900 with a paired test under the stated disagreement assumption, and a 500-example benchmark has a standard error near 1.3 points
#Quick Check
An experiment has 25% power to detect the effect you care about and returns p = 0.30. What is the best reading?