Sub topic 10
Testing the difference between two rates
This chapter assumes you know what a click rate is, can read short Python snippets, and have the basic idea of probability. You should also already have fixed your sample size before sending, as covered in the previous chapter.
The question the test answers
After the send, you have clicks and recipients for variants A and B. The rates will almost never match exactly, even if the subject lines are equally good. Random assignment alone creates small gaps. The test asks how often a gap this large would appear if A and B really performed the same.
That frequency is the p-value. The assumption "no real difference" is the null hypothesis.
The two-proportion z-test
For click rates, the standard test is the pooled two-proportion z-test. It works like this:
- Compute each rate:
p_a = clicks_a / sent_aandp_b = clicks_b / sent_b. - Pool the data into one rate, as if both groups were one. This models the null hypothesis.
- Compute the standard error. This is the typical size of random gaps at your sample sizes.
- Divide the observed gap by the standard error. The result is the z statistic, the gap measured in standard errors.
- Convert z to a two-sided p-value using the normal distribution.
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}")The output is lift=0.0035 z=2.40 p=0.0164. Variant A clicked at 2.00% and variant B at 2.35%. A gap this large would appear in about 1.6% of experiments where the two subject lines were truly equal.
Reading the result
Compare the p-value with the alpha you chose before the test, usually 0.05. Here 0.0164 is below 0.05, so you reject the null hypothesis.
A p-value is not the probability that B is better. It also says nothing about how large the improvement is. Report the lift alongside it. An approximate 95% confidence interval for this lift runs from about +0.06 to +0.64 percentage points. B is probably better, but the true gain could be small.
Common errors
- Using counts that are too small. The normal approximation needs roughly at least 10 successes and 10 failures per variant. Below that, use Fisher's exact test.
- Testing the wrong denominator. Use recipients assigned to each variant, not only delivered or opened emails, unless you chose that denominator in advance.
- Treating "not significant" as "equal". A non-significant result often means the test was too small to detect the difference. Check it against the minimum detectable effect you planned for.
- Running the test repeatedly. Calculate it once at the planned sample size. Repeated checks inflate false winners, as the next chapters explain.
- Picking a one-sided test after seeing the data. Choose two-sided unless you decided otherwise before the send.
Exercise
A test of two send times gives a lift of +0.0003 (0.03 percentage points) on a base click rate of 2%, with p = 0.03 at alpha = 0.05. Is the result statistically significant, and should you automatically adopt the new time?
Show answer
It is statistically significant because 0.03 is below 0.05. Adoption is a separate decision. The lift is tiny, so check the confidence interval and whether a gain of 0.03 points justifies any cost of changing your schedule. If the test was planned for a larger minimum detectable effect, treat the result cautiously and consider a replication.