Section 4 · Lesson 2
A/B testing conversion rates: comparing two proportions
Compare conversion rates between variants with lift, a two-proportion z-test, a chi-square test and a confidence interval for the difference.
This is the moment the whole course has been building toward. The marketing team ran a clean experiment: 4,000 visitors were randomly assigned to the current landing page (A) or a redesigned one (B). Did B convert better, by how much, and how sure are we?
Step 1: rates and lift
rate = conversions / users
absolute lift = rate_B - rate_A # in percentage points
relative lift = (rate_B - rate_A) / rate_A
In this test A converts at about 9.4% and B at about 11.7%: an absolute lift of about 2.2 percentage points, or a relative lift of about 24%. Both are correct, but they sound very different in a meeting, so always say which one you mean.
Step 2: the two-proportion z-test
H0 says both pages share one true conversion rate. If that is true, the best estimate of that shared rate pools both groups, and the standard error of the difference uses it:
p_pool = (x_A + x_B) / (n_A + n_B)
SE = sqrt(p_pool * (1 - p_pool) * (1/n_A + 1/n_B))
z = (rate_B - rate_A) / SE
p = 2 * norm.sf(abs(z)) # two-sided
The normal approximation is fine when each group has at least about 10 conversions and 10 non-conversions. Here we have hundreds.
Step 3: the chi-square view
The same question can be asked of the 2×2 table of counts (variant × converted). The chi-square test of independence compares observed counts with the counts you’d expect if conversion didn’t depend on variant:
chi2 = sum((observed - expected)**2 / expected)
For a 2×2 table, chi2 equals z² and the p-values are identical, as long as you turn off the
continuity correction. SciPy’s chi2_contingency applies Yates’ correction to 2×2 tables by
default, which gives a slightly larger (more conservative) p-value. Neither is wrong; just state
which one you used. The chi-square version also extends to more than two variants (A/B/C).
Step 4: a confidence interval for the difference
A p-value says whether the difference is distinguishable from zero. A confidence interval says how big it plausibly is. Here the SE is unpooled, because we no longer assume H0:
SE_diff = sqrt(rate_A*(1-rate_A)/n_A + rate_B*(1-rate_B)/n_B)
CI = (rate_B - rate_A) ± 1.96 * SE_diff
For this test the interval runs from roughly +0.3 to +4.1 percentage points: B is probably better, but the true lift could be small.
In Python
import numpy as np
import pandas as pd
from scipy import stats
ab = pd.read_csv("ab_test.csv")
s = ab.groupby("variant")["converted"].agg(["sum", "count"]) # conversions and users per variant
table = pd.crosstab(ab["variant"], ab["converted"]) # 2x2 counts
stats.chi2_contingency(table, correction=False)
.agg(["sum", "count"]) runs several aggregations at once and returns one column per function.
In the real world
- Check the split first. With a 50/50 design, 1,960 vs 2,040 users is fine (a binomial test gives p ≈ 0.21). A very lopsided split (a “sample ratio mismatch”) points to a tracking or assignment bug, and you shouldn’t trust the result until it’s fixed.
- Plan, then run. Pick the metric, α, minimum effect worth detecting and sample size in advance. Stopping as soon as p < 0.05 inflates false positives.
- Segments are for learning, not for rescuing a test. Slicing by device, browser and country multiplies your chances of a fluke. Treat segment wins as hypotheses for the next test.
- Translate to money. A +2.2 point lift on 100,000 monthly visitors is about 2,200 extra conversions a month; the lower end of the interval (+0.3 points) is about 300. That range is what the business decision should weigh.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Conversion rates and lift
DoneLoad ab_test.csv (A = control, B = new landing page). Create, each rounded to 4 decimals:
rate_a,rate_b: the conversion rate of each variantabs_lift:rate_b - rate_a(the difference in percentage points, as a fraction)rel_lift:abs_lift / rate_a(the relative lift)
Compute the lifts from the unrounded rates.
Need a hint?
rates["A"]picks A's rate from the grouped Series.- Relative lift divides by the control rate.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
rates = ab.groupby("variant")["converted"].mean()
print(rates)
rate_a = None
rate_b = None
abs_lift = None
rel_lift = None
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Two-proportion z-test
DoneTest H0: "A and B have the same conversion rate" with a pooled two-proportion z-test.
p_pool = (conversions_A + conversions_B) / (n_A + n_B)
se = sqrt(p_pool * (1 - p_pool) * (1/n_A + 1/n_B))
z = (rate_B - rate_A) / sez_stat: the z statistic (B minus A), rounded to 3 decimalsp_value: the two-sided p-value,2 * norm.sf(abs(z)), rounded to 4 decimals
Need a hint?
summary.loc["A", "sum"]is A's conversions andsummary.loc["A", "count"]is A's users.stats.norm.sf(abs(z))is the area in one tail; multiply by 2 for a two-sided test.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
from scipy import stats
ab = pd.read_csv("ab_test.csv")
summary = ab.groupby("variant")["converted"].agg(["sum", "count"])
print(summary) # sum = conversions, count = users
z_stat = None
p_value = None
print(z_stat, p_value)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)The same test as a chi-square
DoneBuild a 2×2 table of counts with pd.crosstab(ab["variant"], ab["converted"]) and run scipy.stats.chi2_contingency(table, correction=False).
table: the crosstabchi2_stat: the chi-square statistic, rounded to 3 decimalsp_chi2: its p-value, rounded to 4 decimals
Without the continuity correction, chi2 equals z² from the previous task and the p-values match. (SciPy's default correction=True applies Yates' correction to 2×2 tables.)
Need a hint?
chi2_contingencyreturns a result with.statistic,.pvalue,.dofand.expected_freq.result.expected_freqshows the counts you'd expect if variant and conversion were independent.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
from scipy import stats
ab = pd.read_csv("ab_test.csv")
table = None
chi2_stat = None
p_chi2 = None
print(table, chi2_stat, p_chi2)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)