A/B test significance in Python comes down to one question: is the gap between two groups bigger than chance alone would produce? This guide answers it on a real experiment with 90,189 players, using a z-test, a confidence interval and a bootstrap, and then shows two things most tutorials skip: how many users you needed, and how checking results early inflates false positives.
The data is the Cookie Cats test from the mobile game of the same name. Players were randomly given a progression gate at level 30 or at level 40, and the table records whether each player came back one day and seven days after installing. Code was run with pandas 3.0.5, SciPy 1.18.1 and statsmodels 0.15.0. The question: does moving the gate hurt retention?

Look at the experiment before testing it
import pandas as pd, numpy as np
url = "https://raw.githubusercontent.com/zunaira-ahmad/cookie-cats-ab-test/main/cookie_cats.csv"
d = pd.read_csv(url)
print(d.shape, "unique ids:", d.userid.is_unique, "missing:", d.isna().sum().sum())
print(d.groupby("version").agg(players=("userid", "count"),
ret1=("retention_1", "mean"), ret7=("retention_7", "mean")).round(4))Output:
(90189, 5) unique ids: True missing: 0
players ret1 ret7
version
gate_30 44700 0.4482 0.1902
gate_40 45489 0.4423 0.1820There are 44,700 players in the gate-30 group and 45,489 in the gate-40 group, with no repeated ids and no missing values. The groups are not exactly equal, which is normal for random assignment. Day-1 retention is 44.82% against 44.23%, and day-7 retention is 19.02% against 18.20%. The raw gap on day 7 is 0.82 percentage points. Is that signal or noise?
A/B test significance in Python: the two-proportion z-test
Retention is a yes or no outcome, so we compare two proportions. The z-test asks how likely a gap this large would be if both versions had the same true rate.
from statsmodels.stats.proportion import proportions_ztest, confint_proportions_2indep
for col in ["retention_1", "retention_7"]:
a = d[d.version == "gate_30"][col]
b = d[d.version == "gate_40"][col]
count = np.array([b.sum(), a.sum()]); nobs = np.array([len(b), len(a)])
z, p = proportions_ztest(count, nobs)
lo, hi = confint_proportions_2indep(count[0], nobs[0], count[1], nobs[1], compare="diff")
print(f"{col}: diff {b.mean() - a.mean():+.4f} z {z:.3f} p {p:.4f} 95% CI [{lo:+.4f}, {hi:+.4f}]")Output:
retention_1: diff -0.0059 z -1.784 p 0.0744 95% CI [-0.0124, +0.0006]
retention_7: diff -0.0082 z -3.164 p 0.0016 95% CI [-0.0133, -0.0031]Day-1 retention: the gap is -0.59 points, p = 0.074 and the interval runs from -1.24 to +0.06 points. That includes zero, so at the usual 5% level we cannot call it a real difference. Day-7 retention: the gap is -0.82 points, p = 0.0016 and the interval is -1.33 to -0.31 points. That excludes zero. Moving the gate to level 40 lowered 7-day retention by somewhere between about a third of a point and 1.3 points.
Read the interval, not only the p-value. A p-value of 0.0016 says “unlikely to be chance”, nothing about size. The interval tells a product team that the loss is at most about 1.3 points out of 19, which may or may not matter for the business. That is a decision for people, not for the test.
Cross-check with a chi-square test and a bootstrap
One method can hide a mistake, so we confirm with two others. The chi-square test works on the table of counts and the bootstrap resamples players with replacement to get an interval without a formula.
from scipy import stats
ct = pd.crosstab(d.version, d.retention_7)
print(ct)
chi2, p, _, _ = stats.chi2_contingency(ct, correction=False)
print("chi-square", round(chi2, 3), "p", round(p, 4))
rng = np.random.default_rng(0)
a = d[d.version == "gate_30"].retention_7.values.astype(float)
b = d[d.version == "gate_40"].retention_7.values.astype(float)
diffs = np.array([rng.choice(b, len(b)).mean() - rng.choice(a, len(a)).mean() for _ in range(2000)])
print("bootstrap 95% interval:", np.percentile(diffs, [2.5, 97.5]).round(4))
print("share of resamples below zero:", (diffs < 0).mean())Output:
retention_7 False True
version
gate_30 36198 8502
gate_40 37210 8279
chi-square 10.013 p 0.0016
bootstrap 95% interval: [-0.0131 -0.003 ]
share of resamples below zero: 0.999The chi-square statistic is 10.013 with p = 0.0016, the same as the z-test (a z of -3.164 squared is 10.01, so they are the same test written two ways). The bootstrap interval is -0.0131 to -0.0030 and 99.9% of the 2,000 resamples showed a negative gap. Three methods agree.
How many users did this test need?
Before an experiment you decide the smallest difference worth detecting and compute the sample size. After it, you can check what size of effect this test could reliably see. Statsmodels does both with the effect size for proportions.
from statsmodels.stats.power import NormalIndPower
from statsmodels.stats.proportion import proportion_effectsize
power = NormalIndPower()
for lift in [0.0082, 0.01, 0.005]:
es = proportion_effectsize(0.19, 0.19 - lift)
n = power.solve_power(es, alpha=0.05, power=0.8)
print(f"to detect a {lift*100:.2f}-point drop from 19%: {n:,.0f} players per group")Output:
to detect a 0.82-point drop from 19%: 35,328 players per group
to detect a 1.00-point drop from 19%: 23,664 players per group
to detect a 0.50-point drop from 19%: 95,654 players per groupTo detect a drop of 0.82 points with 80% power you need about 35,300 players per group, and for a 1.0-point drop about 23,700. For a half-point drop the number is about 95,700 per group, more than either group in this experiment. So this test could reliably find a loss of roughly 0.8 points but would often miss a smaller one, which is also why the day-1 result of -0.59 points did not reach significance. A non-significant result is not proof of “no effect”: it may mean the test was too small.
The peeking problem
Many teams look at a running test every day and stop when p drops below 0.05. That inflates false positives. We can measure it with an A/A simulation: both groups have the same true 19% rate, so any “significant” result is a false alarm.
sim = np.random.default_rng(1)
final_only = looked_ten_times = 0
runs = 1000
for _ in range(runs):
x = sim.random(20000) < 0.19
y = sim.random(20000) < 0.19
ps = [proportions_ztest([x[:k].sum(), y[:k].sum()], [k, k])[1] for k in range(2000, 20001, 2000)]
final_only += ps[-1] < 0.05
looked_ten_times += min(ps) < 0.05
print("false positives, one look at the end:", final_only / runs)
print("false positives, 10 looks, stop at first p < 0.05:", looked_ten_times / runs)Output:
false positives, one look at the end: 0.044
false positives, 10 looks, stop at first p < 0.05: 0.179Testing once at the end gave 4.4% false positives, close to the promised 5%. Looking ten times and stopping at the first small p-value gave 17.9%, more than three times as many. In this simulation of 1,000 experiments, that is the price of peeking. If you need to monitor a test continuously, use a sequential method or fix the sample size in advance.
A second metric where the answer is different
The data also has the number of game rounds each player played. This is a skewed measure, so the choice of test matters.
print(d.sum_gamerounds.describe().round(1))
print(d.sort_values("sum_gamerounds").tail(2).sum_gamerounds.tolist())
print(d.groupby("version").sum_gamerounds.agg(["mean", "median"]).round(2))
t = stats.ttest_ind(d[d.version == "gate_30"].sum_gamerounds, d[d.version == "gate_40"].sum_gamerounds, equal_var=False)
print("Welch t-test p:", round(t.pvalue, 3))Output:
count 90189.0
mean 51.9
std 195.1
min 0.0
25% 5.0
50% 16.0
75% 51.0
max 49854.0
Name: sum_gamerounds, dtype: float64
[2961, 49854]
mean median
version
gate_30 52.46 17.0
gate_40 51.30 16.0
Welch t-test p: 0.376The median player played 16 rounds but one player played 49,854, and the next highest is 2,961. The means (52.46 and 51.30) differ by about one round, with a Welch t-test p of 0.376. This is a reminder that a handful of extreme users can dominate a mean: before testing the means, look at the distribution, as in outlier detection in Python. I did not look at segments, so treat the rounds comparison as a quick check rather than a finding.
What this test does and does not show
It shows that, in this sample, the later gate was followed by lower 7-day retention, and the three tests agree. It does not show why, whether revenue changed, or whether the effect lasts. The usual caveats apply: check that assignment was random (the group sizes are close but not identical), and decide the metric and the sample size before you start. For the building blocks, see pandas groupby for the summaries, logistic regression in Python if you want to adjust for other variables, and train test split in Python for another case where the order of steps decides whether the number is honest. The free descriptive statistics calculator checks means and spreads on pasted data.
The statistical functions are documented in statsmodels and SciPy. The dataset is a public copy of the Cookie Cats data, zunaira-ahmad/cookie-cats-ab-test on GitHub; I could not verify the original assignment process, so the randomisation is assumed. Not tested here: Bayesian A/B tests, sequential testing and multiple-metric corrections.
FAQ
How do I run an A/B test in Python?
Count successes and total users in each group, then use proportions_ztest from statsmodels for a yes/no metric, or a t-test for a continuous one. Report the difference and its confidence interval, not only the p-value.
What p-value is significant for an A/B test?
The usual cutoff is 0.05, but it must be chosen before the test. In this experiment day-7 retention gave p = 0.0016 and day-1 gave p = 0.074, so only one passed.
How many users do I need for an A/B test?
It depends on the baseline rate and the smallest drop or lift you care about. At a 19% baseline, detecting a 1-point change with 80% power needed about 23,700 users per group.
Why is peeking at A/B test results a problem?
Each look is another chance for noise to cross 0.05. In our A/A simulation, ten looks produced 17.9% false positives instead of about 5%.
