Sub topic 6
Is the difference real? Sample size and significance
Two ad variants rarely perform identically, even when they are equally good. This chapter covers how to tell a real difference from random variation, how many observations you need before you can tell, and how to run and read a basic significance test.
This chapter assumes you can compute the core funnel metrics (CTR, conversion rate, CPA, ROAS) and that you have a baseline to compare against. It also assumes you know what a proportion is. The code is optional Python 3 using only the standard library.
Why results wobble
Suppose a landing page truly converts 4% of visitors. Show it to 100 people and you will not always see exactly 4 conversions. You might see 2 or 7. This wobble is sampling noise, and it shrinks as the sample grows.
The size of the noise for a conversion rate is the standard error:
SE = sqrt(p * (1 - p) / n)At p = 0.04, the standard error is about 2 percentage points at n = 100 and 0.4 percentage points at n = 2,500. Quadrupling the sample only halves the noise, so each extra bit of certainty costs more traffic than the last.
The hypothesis-test framing
A significance test asks one question: if the two variants were actually identical, how often would random variation alone produce a gap at least this large?
- Null hypothesis (H0): the two true conversion rates are equal.
- Test statistic: the observed gap divided by its expected noise (the z-score).
- p-value: the probability of a gap at least this extreme if H0 were true.
- Significance level (alpha): the threshold you set before the test, usually 0.05. If p < alpha, you reject H0 and call the result statistically significant.
A p-value is not the probability that your variant is better, and it is not the probability that the result is a fluke. It is a statement about the data under the assumption of no real difference.
Running a two-proportion z-test
The test below compares two conversion rates. Variant A converted 100 of 2,500 visitors (4.0%). Variant B converted 130 of 2,500 (5.2%).
from math import sqrt, erf
def two_proportion_z(conv_a, n_a, conv_b, n_b):
p_a, p_b = conv_a / n_a, conv_b / n_b
pooled = (conv_a + conv_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 - 0.5 * (1 + erf(abs(z) / sqrt(2))))
return z, p_value
z, p = two_proportion_z(conv_a=100, n_a=2500, conv_b=130, n_b=2500)
print(round(z, 2), round(p, 3))This prints 2.03 0.043. The function does the following:
- It pools both groups into one combined rate (0.046), because H0 says they share a single true rate.
- It computes the standard error of the gap under that assumption.
- It converts the z-score to a two-sided p-value with the normal distribution. Two-sided means it counts a gap in either direction as evidence.
Since 0.043 is below 0.05, the result is significant at the 5% level. It is also close to the line. A few fewer conversions in B would flip the verdict, which is why you should look at more than the p-value.
Effect size and confidence intervals
Significance says whether a gap is detectable. It does not say whether the gap matters. With 2 million visitors per arm, a 0.01 percentage point lift can be highly significant and commercially worthless.
Report the effect size alongside the p-value:
- Absolute lift: 5.2% − 4.0% = 1.2 percentage points.
- Relative lift: 1.2 / 4.0 = 30%.
Then give the uncertainty with a 95% confidence interval for the difference, using the unpooled standard error:
from math import sqrt
def diff_interval(conv_a, n_a, conv_b, n_b, z_crit=1.96):
p_a, p_b = conv_a / n_a, conv_b / n_b
se = sqrt(p_a * (1 - p_a) / n_a + p_b * (1 - p_b) / n_b)
diff = p_b - p_a
return diff - z_crit * se, diff + z_crit * se
low, high = diff_interval(100, 2500, 130, 2500)
print(f"{low:.4f} to {high:.4f}")This prints 0.0004 to 0.0236. The plausible true lift runs from almost nothing (0.04 points) to large (2.4 points). That range is far more useful for a budget decision than "p = 0.043". The interval is wide because the sample is modest, and the next section shows how to plan for a narrower one.
Sample size: decide before you test
The sample you need depends on four inputs:
| Input | Meaning | Effect on required sample |
|---|---|---|
| Baseline rate | Current conversion rate | Lower rates need more traffic for the same relative lift |
| Minimum detectable effect (MDE) | Smallest lift worth detecting | Halving the MDE roughly quadruples the sample |
| Alpha | Tolerated false-positive rate (usually 0.05) | Stricter alpha needs more traffic |
| Power | Chance of detecting a real effect of MDE size (usually 0.80) | Higher power needs more traffic |
Power is the part people skip. A test with 50% power misses a real effect half the time. A "no significant difference" result from an underpowered test says very little.
from math import ceil
from statistics import NormalDist
def sample_size_per_variant(p_base, p_target, alpha=0.05, power=0.80):
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)
n = (z_alpha + z_beta) ** 2 * variance / (p_target - p_base) ** 2
return ceil(n)
print(sample_size_per_variant(0.04, 0.05))This prints 6743. Detecting a lift from 4% to 5% reliably takes roughly 6,700 visitors per variant, about 13,500 in total. The earlier test used 2,500 per arm and a larger lift (4.0% to 5.2%), which is why it only just reached significance.
Work backward from this number. If your campaign sends 1,000 visitors a day to the page, the test needs about 14 days. If that is too long, you have three options: accept a larger MDE, accept lower power, or pick a higher-volume metric such as click-through rate instead of purchases.
Common ways the test goes wrong
Peeking and early stopping
If you check the p-value every day and stop the first time it dips below 0.05, the real false-positive rate climbs well above 5%. Early in a test the gap swings widely, so it will cross the threshold by luck surprisingly often. Fix the sample size in advance and read the result once it is reached. If you need to monitor continuously, use a sequential testing method designed for that.
Multiple comparisons
Each test at alpha = 0.05 carries a 5% chance of a false positive. Slice one campaign into 20 segments and test each, and you expect about one "significant" segment from chance alone. Two defenses:
- Pre-register the few comparisons you care about.
- Tighten the threshold. The Bonferroni correction divides alpha by the number of tests, so 20 tests use alpha = 0.0025. It is conservative but simple.
Treat a significant result found by searching as a hypothesis for a new test, not as a finding.
Small expected counts
The z-test relies on a normal approximation. It is unreliable when a group has fewer than about 10 conversions or about 10 non-conversions. For rare events, use Fisher's exact test, or collect more data.
Unequal exposure and broken randomization
The test assumes visitors were assigned to variants at random. If one variant ran on weekdays and the other on weekends, or one got mobile traffic and the other desktop, the gap may reflect audience or timing instead of the creative. Check that the split is close to what you planned and that segment mixes match across variants.
Non-significant does not mean equal
A p-value of 0.20 means you lack evidence of a difference. It does not show the variants are the same. Look at the interval. If it spans from a meaningful loss to a meaningful gain, the test was inconclusive.
Applying this to non-conversion metrics
The z-test fits yes/no outcomes such as clicked or converted. Revenue per visitor, order value, and ROAS are continuous and heavily skewed, because a few large orders dominate. For those, a z-test on proportions does not apply. Use a t-test with caution, a bootstrap on the metric, or test the underlying conversion rate and order value separately.
Putting it into a decision
Combine the pieces before you pass the result to your scale, iterate, or stop rules:
- Was the planned sample reached?
- Is p below the pre-set alpha, after any multiple-comparison correction?
- Is the effect size large enough to matter commercially, even at the low end of the interval?
- Do the segment mix and timing look balanced across variants?
A result that fails the first check is inconclusive, whatever the p-value says. A result that passes the first two but has a low-end interval near zero deserves a follow-up test before a full rollout.
Exercise
Variant A converted 40 of 1,000 visitors and Variant B converted 52 of 1,000. Compute the z-score and two-sided p-value with the pooled formula, then state whether the difference is significant at alpha = 0.05 and what you would do next.
Show answer
Pooled rate = 92 / 2000 = 0.046. SE = sqrt(0.046 0.954 (1/1000 + 1/1000)) ≈ 0.00937. Gap = 0.052 − 0.040 = 0.012. z ≈ 1.28, so the two-sided p-value is about 0.20. That is above 0.05, so the result is not significant. It does not show the variants are equal. The sample is too small to detect a 1.2-point gap at this baseline. Roughly 6,700 visitors per variant would be needed for a 4% to 5% lift. Either keep the test running to a planned larger sample or stop and test a bolder change.
Exercise
You test one ad across 20 audience segments at alpha = 0.05 and exactly one segment shows p = 0.03. Is this a real winner? Give the corrected threshold and the right next step.
Show answer
Not yet. With 20 tests you expect about one false positive by chance. The Bonferroni-corrected threshold is 0.05 / 20 = 0.0025, and 0.03 does not meet it. Treat the segment as a hypothesis and run a new, pre-planned test on that segment alone.