Sub topic 10
Testing the difference between two rates
This chapter assumes you know what a click rate is, how to read a percentage, and what a p-value is in rough terms. It also assumes you have already sized the test and assigned recipients at random. The question here is what to do after the send: how to decide whether variant B's click rate really differs from variant A's, or whether the gap is random noise.
The setup: two proportions
Each recipient either clicked or did not. That makes every variant a set of yes/no outcomes, and the click rate is the proportion of yes outcomes. Suppose you sent 20,000 emails per variant:
| Variant | Sent | Clicks | Click rate |
|---|---|---|---|
| A (control) | 20,000 | 400 | 2.00% |
| B (new subject line) | 20,000 | 470 | 2.35% |
B looks better by 0.35 percentage points. But if you sent the same email to two random halves of the list, the click rates would still differ a little. Significance testing asks how often a gap this large would appear if the variants were actually identical.
The null hypothesis and the pooled estimate
The test starts from the null hypothesis: both variants share the same true click rate. Under that assumption, the best estimate of that shared rate uses all recipients together. This is the pooled rate:
pooled = (clicks A + clicks B) / (sent A + sent B)
Here that is 870 / 40,000 = 2.175%.
Next you need the standard error, which measures how much the difference between two observed rates would typically wobble from sample to sample. For the pooled test:
SE = sqrt( pooled × (1 − pooled) × (1/nA + 1/nB) )
The z statistic is the observed difference divided by that standard error. It tells you how many standard errors apart the two rates are. The p-value is the probability, under the null hypothesis, of seeing a z at least this far from zero in either direction.
Running the test in Python
This function implements those steps with only the standard library.
from math import sqrt
from statistics import NormalDist
def two_proportion_z_test(success_a, sent_a, success_b, sent_b):
p_a = success_a / sent_a
p_b = success_b / sent_b
pooled = (success_a + success_b) / (sent_a + sent_b)
se = sqrt(pooled * (1 - pooled) * (1 / sent_a + 1 / sent_b))
z = (p_b - p_a) / se
p_value = 2 * (1 - NormalDist().cdf(abs(z)))
return p_b - p_a, z, p_value
diff, z, p = two_proportion_z_test(400, 20000, 470, 20000)
print(f"lift={diff:.4f} z={z:.2f} p={p:.4f}")For the table above, the lift is 0.0035, z is about 2.40, and p is about 0.016. At the usual threshold of alpha = 0.05, you reject the null hypothesis. A gap this large would show up in roughly 1.6% of tests where the two subject lines were truly equal.
Three details of the function matter:
- Two-sided. The p-value uses
abs(z), so it counts differences in either direction. You decide this before the test, because you would also want to know if B were worse. A one-sided test halves the p-value, and choosing it after seeing the data is a form of cheating. - Direction. The lift is B minus A. Keep A as the control so a positive number always means the new version won.
- Counts, not rates. Pass whole counts. The pooled rate needs them, and rounded percentages would add error.
What the p-value does and does not say
A p-value of 0.016 is not a 1.6% chance that B is no better than A. It is the probability of data this extreme if the variants were the same. Two consequences follow:
- A small p-value says the gap is hard to explain by chance. It says nothing about whether the gap is big enough to matter.
- A p-value above alpha does not prove the variants are equal. It means the test could not distinguish them. With too few recipients, even a real improvement can produce p = 0.30.
Add a confidence interval
The p-value gives a yes/no signal. A confidence interval gives the range of lifts consistent with your data, which is more useful for a business decision. For the interval, use the unpooled standard error. The interval does not assume the null is true, so each variant keeps its own rate.
from math import sqrt
from statistics import NormalDist
def lift_confidence_interval(success_a, sent_a, success_b, sent_b, confidence=0.95):
p_a = success_a / sent_a
p_b = success_b / sent_b
se = sqrt(p_a * (1 - p_a) / sent_a + p_b * (1 - p_b) / sent_b)
z_crit = NormalDist().inv_cdf(1 - (1 - confidence) / 2)
diff = p_b - p_a
return diff - z_crit * se, diff + z_crit * se
low, high = lift_confidence_interval(400, 20000, 470, 20000)
print(f"95% CI for lift: {low:.4f} to {high:.4f}")The output is roughly 0.0006 to 0.0064. In words, B's true advantage is plausibly anywhere from 0.06 to 0.64 percentage points. The interval excludes zero, which matches the significant p-value. It also shows how uncertain the size is: the best case is about ten times the worst case. That matters when you project revenue from the winner.
Conditions the test relies on
The z-test is an approximation. It holds when these conditions are met:
- Independent recipients. Each person appears in one variant only, and one person's click does not affect another's. Random assignment by hashed user ID supports this. Sending to two addresses that belong to one person breaks it.
- Enough events. A common rule is at least 10 clicks and 10 non-clicks in each variant. Small campaigns with a rare outcome, such as conversions at 0.2%, may fall short. Use Fisher's exact test in that case.
- A fixed analysis plan. You choose the metric, the alpha, and the sample size before sending, and you analyze once at the planned end. Looking repeatedly and stopping when p dips below 0.05 inflates false positives. Peeking gets its own chapter.
- Count the denominator correctly. Use everyone assigned to a variant who was sent the email, not only those who opened it. Dividing clicks by opens changes the question, because the opened group was shaped by the subject line itself. That is the reason click-through per recipient is the safer primary metric.
Common mistakes
- Comparing rates by eye. A 0.35-point gap looks meaningful, but with 2,000 recipients per variant the same gap would give p well above 0.05.
- Mismatched group sizes. The formula handles unequal sizes, since it uses
1/n_A + 1/n_B. But badly unequal splits, such as 95/5, lose power. Check for unintended imbalance too: if you assigned 50/50 and see 52/48 on 40,000 recipients, something in your sending or filtering pipeline is off. That is called a sample ratio mismatch, and it means you should not trust the result until you find the cause. - Testing many metrics. If you check opens, clicks, conversions, and unsubscribes and report whichever has p < 0.05, you will get a false winner far more often than 5% of the time. Name one primary metric in advance.
- Treating alpha as a law. Alpha = 0.05 is a convention. If wrongly switching a subject line is cheap, you might accept a looser threshold. If the change is costly to reverse, tighten it.
Reporting the result
A useful result line contains the counts, the lift, the interval, and the p-value:
"B: 470/20,000 (2.35%) vs A: 400/20,000 (2.00%). Lift +0.35 points (95% CI +0.06 to +0.64), z = 2.40, p = 0.016."
This gives the reader enough to judge both the evidence and the size of the effect. The next chapters cover how to read that effect size and how to handle several variants at once.
Exercise
Variant A got 300 clicks from 15,000 sends. Variant B got 345 clicks from 15,000 sends. Compute the pooled click rate, then state whether the difference is significant at alpha = 0.05 using a two-sided z-test. Use the function from this chapter to check your answer.
Show answer
The pooled rate is 645 / 30,000 = 2.15%. The rates are 2.00% for A and 2.30% for B, so the lift is 0.30 points. The standard error is about 0.00168, so z is about 1.79 and the two-sided p-value is about 0.073. That is above 0.05, so the result is not significant. The test does not show that B is better, but it does not show they are equal either. A larger sample would be needed to settle it.
Exercise
A colleague runs a test and reports "p = 0.04, so there is a 96% chance B is better." Explain what is wrong with this statement and give a correct reading.
Show answer
The p-value is the probability of seeing data at least this extreme if A and B truly had the same rate. It is not the probability that B is better. A correct reading is: if the two subject lines performed identically, a difference this large would occur about 4% of the time, so the data are evidence against the null hypothesis. To describe how much better B might be, report a confidence interval for the lift.