Sub topic 9
Calculating sample size before you send
This chapter assumes you know what a click rate is, can work with proportions, and have met the idea of a p-value. The Python snippets use only the standard library. You should be able to read them, but you don't need to write them from scratch.
Earlier chapters covered the hypothesis, the primary metric, and random assignment. Here you decide how many recipients each variant needs before you press send. The next chapter covers the test you run afterward.
Why size the test first
If you pick a sample size after seeing results, you can stop whenever the numbers look good. That produces false winners. If you pick it from your list size alone, you may send a test too small to detect anything. A small test often ends with "no significant difference". That result doesn't mean the variants perform the same. It means the test couldn't tell.
Sizing first forces you to state what you are trying to detect. It also tells you whether the test is worth running on your list.
The four inputs
Sample size per variant depends on four quantities.
| Input | Meaning | Typical value |
|---|---|---|
| Baseline rate | The current rate of your primary metric, such as clicks per delivered email | From your last 5 to 10 campaigns |
| Minimum detectable effect (MDE) | The smallest improvement worth acting on | A relative lift of 10 to 25 percent |
| Significance level (alpha) | The chance of a false winner when the variants are truly equal | 0.05 |
| Power | The chance of detecting a real effect of the MDE size | 0.8 |
Alpha is the false-positive risk. Power is one minus the false-negative risk. With alpha at 0.05 and power at 0.8, a test that is truly flat declares a winner 5 percent of the time. A test with a real MDE-sized difference finds it 80 percent of the time.
The formula in code
For two proportions, the normal approximation gives the size per variant:
n = (zalpha + zbeta)² × [p1(1 − p1) + p2(1 − p2)] / (p2 − p1)²
Here p1 is the baseline rate and p2 is the baseline plus the MDE. The value zalpha is the normal quantile for a two-sided test, 1.96 at alpha 0.05. The value zbeta is the quantile for your power, 0.84 at power 0.8.
from math import ceil
from statistics import NormalDist
def sample_size_per_variant(p_base, p_target, alpha=0.05, power=0.8):
z_alpha = NormalDist().inv_cdf(1 - alpha / 2)
z_beta = NormalDist().inv_cdf(power)
variance = p_base * (1 - p_base) + p_target * (1 - p_target)
return ceil((z_alpha + z_beta) ** 2 * variance / (p_target - p_base) ** 2)
print(sample_size_per_variant(0.02, 0.025))This prints 13807. A click rate moving from 2.0 percent to 2.5 percent is a 25 percent relative lift, and detecting it needs about 13,800 recipients in each variant. An A/B test therefore needs 27,614 recipients in total.
The function uses ceil because you cannot send to a fraction of a person, and rounding down would leave the test slightly underpowered.
How the inputs move the answer
The required size is very sensitive to the MDE. Because the difference sits squared in the denominator, halving the MDE multiplies the sample size by about four.
| Baseline | Target | Relative lift | Per variant |
|---|---|---|---|
| 2.0% | 3.0% | 50% | ≈3,820 |
| 2.0% | 2.5% | 25% | 13,807 |
| 2.0% | 2.25% | 12.5% | ≈52,200 |
You can check the effect of power the same way. Raising power from 0.8 to 0.9 for the 2.0 to 2.5 percent case raises the requirement to about 18,500 per variant, roughly a third more. Tightening alpha from 0.05 to 0.01 also raises it. Neither change is free, so choose them deliberately and leave them alone once the test starts.
Lower baselines need larger samples for the same relative lift. A rare event such as a purchase from email has a small p, so the variance relative to the difference is large. This is one reason to use a more frequent metric than conversion when your list is small, as long as the metric still reflects your goal.
Which count to use
Sample size counts the units that are randomized and measured. Be careful about which count that is.
- Use delivered emails, or recipients assigned to a variant, as the denominator for rates. Decide this before the test, and use the same denominator in the calculation and in the analysis.
- If your primary metric is a click rate per recipient, count each person once, even if they click several times. Counting total clicks breaks the independence the formula assumes.
- Plan for loss. Bounces and suppressed addresses reduce the delivered count, so assign a little more than the minimum.
Fitting the test into the list
Suppose your list has 40,000 deliverable subscribers, your click baseline is 2.0 percent, and your MDE is a lift to 2.5 percent. Two variants need 27,614 recipients. That leaves 12,386 recipients who are not in the test.
If you plan a test-and-send-winner rollout, those remaining recipients are the ones who receive the winner. This works, but the test cells are large relative to the list. The gain from the test only applies to the remaining 31 percent.
Now suppose you want three subject lines instead of two. You need 3 × 13,807 = 41,421 recipients, which exceeds your list. You must either raise the MDE, drop a variant, or test across several campaigns. A multi-variant test also increases the false-winner risk. The chapter on multiple comparisons covers that adjustment, which usually raises the requirement further.
When the arithmetic does not fit, you have three honest options:
- Raise the MDE and accept that you can only detect large effects.
- Test something with a bigger expected effect. A radically different offer in the subject line moves clicks more than a changed word.
- Do not test. Send your best judgment, and save testing effort for a campaign with a bigger audience.
A fourth option, running the test with too few recipients and reading the result anyway, is not on this list. An underpowered test mostly produces noise. When it does cross the significance threshold, the observed effect tends to be an exaggeration of the real one.
Checking your plan with a simulation
The formula is an approximation. If you want to confirm it for your numbers, simulate it. This code draws many fake experiments where B truly has the target rate, then counts how often the z-test detects it.
import random
from math import sqrt
from statistics import NormalDist
def detects(p_a, p_b, n, alpha=0.05):
clicks_a = sum(random.random() < p_a for _ in range(n))
clicks_b = sum(random.random() < p_b for _ in range(n))
pooled = (clicks_a + clicks_b) / (2 * n)
se = sqrt(pooled * (1 - pooled) * (2 / n))
if se == 0:
return False
z = (clicks_b / n - clicks_a / n) / se
return 2 * (1 - NormalDist().cdf(abs(z))) < alpha
random.seed(7)
runs = 300
hits = sum(detects(0.02, 0.025, 13807) for _ in range(runs))
print(f"detected in {hits / runs:.0%} of {runs} simulated tests")The detection rate should land near 80 percent, give or take a few points of sampling noise. Pure-Python loops over 13,807 recipients are slow, so keep the run count modest. If your result is far from 80 percent, check that the baseline and target you entered match your plan.
Common errors
- Sizing for the wrong metric. You calculate for click rate, then declare a winner on open rate. The sample size only protects the metric it was computed for.
- Using a relative lift as an absolute one. A 25 percent lift on 2 percent is 0.5 percentage points. Entering 0.25 as the target gives a nonsense answer.
- Stopping when significance appears. The sample size is a commitment. Reaching significance earlier does not mean you can stop, as the peeking chapter explains.
- Ignoring unequal splits. The formula assumes equal variants. Unequal groups need more total recipients to reach the same power.
- Reusing an old baseline. If your list or your tracking changed, remeasure the baseline before you size the test.
Exercise
Your click baseline is 5 percent, and you want to detect a lift to 6 percent with alpha 0.05 and power 0.8. What does samplesizeper_variant(0.05, 0.06) return, and how many total recipients does a two-variant test need? Your list has 14,000 deliverable subscribers. Does the test fit?
Show answer
The function returns 8155 per variant, found as (1.96 + 0.84)² × (0.05×0.95 + 0.06×0.94) / 0.01² = 7.849 × 0.1039 / 0.0001 ≈ 8155. A two-variant test needs 16,310 recipients in total. This exceeds the 14,000 on the list, so the test does not fit. You could raise the MDE (for example, detect a lift to 6.5 percent), test something with a larger expected effect, or skip the test.
Exercise
Why does halving the minimum detectable effect increase the required sample size by about four times rather than two times?
Show answer
The difference between the two rates appears squared in the denominator of the formula. Halving the difference divides the denominator by four, so the sample size grows about fourfold. The variance term in the numerator changes only slightly.