Skip to content
Hands-on Mentor
ENTR
Back to blog

A/B testing explained: how to analyze results and pick a sample size

A/B testing compares two versions on randomly split users. Learn to read the p-value, confidence interval and sample size with a Python example.

5 min read

A/B testing is an experiment that shows two versions of something (A and B) to users chosen at random and measures which one performs better. Random assignment makes the two groups similar on average, so a difference between them can be linked to the version, apart from chance. The key question when you read the result is: could this difference be just luck?

Example: a new checkout page

Bazaar, a fictional online store, tested a new mobile checkout page (B) against the old one (A) for two weeks. Half of the 1,000 sessions went to each page.

VersionSessionsAdded to cartPurchasedPurchase rate
A (old)5001556513.0%
B (new)500180408.0%

More people added items to the cart on B, which looks good. But fewer people finished buying. The team has to decide which number matters. A good test names one primary metric before it starts. For a store, that is the purchase rate.

How do you analyze A/B test results?

State the hypotheses first:

  • H₀: the true purchase rate is the same on both pages.
  • H₁: the rates are different.

To compare two rates, use a two-proportion z test:

from statsmodels.stats.proportion import proportions_ztest, confint_proportions_2indep

z, p = proportions_ztest([40, 65], [500, 500])
print(round(z, 2), round(p, 4))
lo, hi = confint_proportions_2indep(40, 500, 65, 500, method="wald")
print(round(lo * 100, 1), round(hi * 100, 1))
z, p = proportions_ztest([180, 155], [500, 500])
print(round(z, 2), round(p, 3))
-2.58 0.0099
-8.8 -1.2
1.67 0.094

For purchases, z is -2.58 and p ≈ 0.0099. The 95% confidence interval for the difference (B minus A) runs from -8.8 to -1.2 percentage points, and it stays below zero. B is very likely worse at selling. For add-to-cart, p is 0.094, which is above 0.05, so you cannot rule out luck for that increase. If you want a refresher on the p-value itself, read [what is a p-value](/blog/what-is-a-p-value).

What does the confidence interval add?

The p-value says whether the difference can be told apart from zero. The interval says how big the difference might be. Here the true gap could be as small as 1.2 points or as large as 8.8 points. Both are bad for B, and the interval shows that clearly. The idea is explained in [confidence intervals explained](/blog/confidence-interval-explained).

How does sample size change the A/B test p-value?

Keep the same rates, 13% vs 8%, and change only the group size:

for n in [100, 500, 2000]:
    b, a = round(n * 0.08), round(n * 0.13)
    z, p = proportions_ztest([b, a], [n, n])
    print(n, b, a, "<0.0001" if p < 0.0001 else round(p, 4))
100 8 13 0.2488
500 40 65 0.0099
2000 160 260 <0.0001

With 100 sessions per group the same gap gives p = 0.2488, which could easily be luck. With 2,000 per group it is far beyond luck. In small groups the rates jump around a lot, so a gap of 5 points happens by chance. As the sample grows, estimates settle and small gaps become visible.

How many users do you need for an A/B test?

Answer this before the test starts. You need four inputs: the current rate, the smallest difference worth catching, α (usually 0.05) and power (the chance to catch a real difference, usually 80%).

import numpy as np
from statsmodels.stats.power import NormalIndPower
from statsmodels.stats.proportion import proportion_effectsize

effect = proportion_effectsize(0.13, 0.08)
need = NormalIndPower().solve_power(effect, alpha=0.05, power=0.8)
print(round(float(np.ceil(need))))
small = proportion_effectsize(0.13, 0.12)
print(round(float(np.ceil(NormalIndPower().solve_power(small, alpha=0.05, power=0.8)))))
583
17164

To catch a change from 13% to 8% with 80% power you need about 583 sessions per group. Bazaar used 500, so its power was a little lower, and that belongs in the test report. To catch a change of only one point (13% vs 12%) you need about 17,164 per group. The smaller the gap you want to detect, the more data you need.

Before you start a test

1. Pick one primary metric. Choosing it after seeing the results is misleading.

2. Calculate the sample size in advance. Use the method above.

3. Fix the duration. Weekday and weekend behavior differ. Run at least one or two full weeks.

4. Do not stop early. Checking the p-value every day and stopping when it dips under 0.05 raises the false-positive rate far above 5%.

5. Watch a guardrail metric. A second number, such as checkout time, should not get worse.

Significant but important?

With a huge sample, a gap between 5.00% and 5.05% can be significant. It may be real, but is it worth acting on? Compare the cost of a change with the expected gain, and always report the size of the difference with its interval.

Common mistakes

  • Deciding with too little data. Ending the test because B led on day one.
  • Looking at many metrics and picking one. With 20 metrics, one will look significant by luck.
  • Breaking random assignment. Showing B only to new users mixes the version with the user type.
  • Reading a non-significant result as "no difference". It only means this data cannot show one.

What to read next

The lesson [A/B testing](/learn/statistics-data-analysis/ab-testing) analyzes the full Bazaar experiment from start to finish. Try a hands-on example in the project [Notification A/B test](/projects/notification-ab-test). For the idea behind p-values and power, see [correlation vs causation](/blog/correlation-vs-causation) and the [Statistics and Data Analysis course](/courses/statistics-data-analysis).

  • #statistics
  • #a/b testing
  • #experiment
  • #conversion rate
All posts