A/B testing
An A/B test shows two random groups of customers two versions and compares one metric chosen in advance. Write the hypothesis first, calculate the sample size, and run the test for full weeks without stopping early. Watch for novelty effects and guardrail metrics, check that the split itself is healthy, and know when a test is the wrong tool.
Author: bezzad
Contents
The problem: “conversion went up after the change”
Our online shop changed the product page. The new page shows the expected delivery date next to the price. One week later, conversion (the share of visitors who buy) is up from 3.0 to 3.4 percent. The team celebrates.
But the same week, three other things happened:
- A marketing email went to all past customers. Past customers buy more easily.
- A competitor had a two-day outage.
- It was the week before a holiday. People buy more gifts then.
So did the new page raise conversion? We cannot know. Many things change at the same time, and a before-and-after comparison mixes all of them.
Why random groups help
In an A/B test, we split visitors into two groups at random. Group A sees the old product page (the control). Group B sees the new one (the variant). Both groups live through the same week: the same email, the same competitor, the same holiday.
- Random means fair. Every visitor has the same chance to be in each group. So the two groups are similar in every way, on average.
- Everything else affects both groups. The email and the holiday raise both groups the same way.
- So the difference comes from the change. If group B converts more, the new page is the most likely reason, with some known chance of luck.
Start with a hypothesis
Before any code, write down what you believe and how you will judge it:
Because support gets many calls about delivery time, we believe that showing the delivery date on the product page will raise conversion by at least 10 percent for all visitors. Return rate and page speed must not get worse.
This sentence has the important parts:
- The reason. Why do we think this will work? This links the test to discovery.
- The change and the users. What exactly is different, and for whom.
- One primary metric. The number that decides success. Here, conversion: the share of visitors who buy.
- The minimum effect we care about. Here, a 10 percent relative rise, from 3.0 to 3.3 percent.
- Guardrail metrics. Numbers that must not get worse.
Decide all of this before the test starts. If you choose the metric after you see the data, you will always find some number that went up.
Sample size and power
Small differences need many users to see. If the groups are too small, a real effect looks like noise, and noise can look like an effect.
Three numbers decide the sample size:
- The minimum detectable effect. The smallest change you want to be able to see. Smaller effects need many more users.
- The significance level. Usually 5 percent. This is the chance of seeing a “winner” when there is really no difference.
- The power. Usually 80 percent. This is the chance of finding a real effect of the size you chose.
Here is the usual calculation for two conversion rates, with only the standard library:
from math import ceil
from statistics import NormalDist
def users_per_group(base_rate: float, relative_lift: float,
alpha: float = 0.05, power: float = 0.80) -> int:
"""Users needed in EACH group.
Two-sided test of two proportions.
"""
p1 = base_rate
p2 = base_rate * (1 + relative_lift)
z = NormalDist().inv_cdf
z_total = z(1 - alpha / 2) + z(power)
variance = p1 * (1 - p1) + p2 * (1 - p2)
return ceil(z_total ** 2 * variance / (p2 - p1) ** 2)
n = users_per_group(base_rate=0.03, relative_lift=0.10)
print(n) # 53208 users in each group
# 4,000 users per group per day
print(n / 4_000) # about 13.3 days
What do these numbers mean for our shop?
- We need about 53,000 users in each group. Many people are surprised by this. A conversion rate of 3 percent is small, so we need many visitors.
- About 8,000 different users visit the shop each day. Each group gets 4,000, so the test needs about 14 days.
- We round up to full weeks. People buy differently on weekends and weekdays. Two full weeks cover both.
- If we only care about big effects, we need fewer users. A 20 percent lift needs roughly a quarter of the users. But then small real wins stay invisible.
The peeking problem
The test is running. The product manager opens the dashboard every morning. On day 4, the p-value is below 0.05. “It is significant! Let’s stop and ship it.”
This is the peeking problem.
Why is it a problem?
- The p-value moves a lot by chance, especially in the first days when there is little data.
- The 5 percent risk is for one look. It assumes you look once, at the planned end.
- Every extra look is another chance for luck. If you check every day and stop at the first “win”, the real chance of a false winner becomes much higher than 5 percent.
- Early winners often shrink. Many “big wins” on day 4 disappear by day 14.
The fix:
- Decide the duration in advance from the sample size, and only make the decision at the end.
- Look only for emergencies. It is fine to look early to find bugs or a disaster in a guardrail. Not to declare a winner.
- If you must look often, use a method made for it. Some experiment tools offer sequential testing methods, which are designed for repeated looks.
Novelty effect and change aversion
People react to change itself, not only to the new design.
- Novelty effect. Returning customers click a new, shiny thing because it is new. The effect is big in the first days and then fades.
- Change aversion. Sometimes the opposite happens. Returning customers are used to the old page, so a better design first looks worse. Later it recovers.
- How to see it. Plot the effect day by day. Compare new users (who have no habit) with returning users.
- What to do. Run the test long enough for the curve to become flat. For big changes, consider a long-term holdout: keep a small group on the old version for a few months.
Check the test itself
Before you read the result, check that the experiment works:
- Sample ratio mismatch. If you planned a 50/50 split but got 52,000 users in one group and 48,000 in the other, something is broken. Maybe the new page crashes for some browsers and those users are lost. A simple statistical test on the group sizes can show if the gap is too big to be chance. Do not trust the result until you find the cause.
- The same users in both groups. Check that users do not move between groups.
- The metric is tracked the same way in both versions. A new page with a missing tracking event can create a fake drop.
Reading the result
- Look at the size of the effect and its range, not only the p-value. “Plus 0.3 points, with a likely range of plus 0.1 to plus 0.5” is more useful than “significant”.
- Check if it is worth it. A real effect can still be too small to pay for the extra work.
- Check the guardrails. If conversion is up but returns are up too, it is not a win.
- Do not go fishing. If you split the result into twenty segments, one of them will look significant by chance. Choose a few segments in advance.
- Write it down. The hypothesis, the result and the decision. A failed test is also learning.
When not to A/B test
A/B testing is a strong tool, but not always the right one:
- Too little traffic. If the test would need six months, use other methods: interviews, prototype tests, or a before-and-after with care.
- Clear fixes. A broken button or a legal requirement does not need a test. Just fix it.
- Big, hard to reverse changes. A new pricing model or a new brand affects people for a long time. A two-week test does not show the long-term effect.
- Ethics and trust. Do not test things that can hurt users, like showing different prices for the same product in a way that is unfair or not allowed.
- When users affect each other. In a marketplace or a social feature, the two groups influence each other, and a simple split gives wrong answers.
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| Before-and-after comparison | Other changes look like your effect. | Random groups at the same time. |
| Choosing the metric after the test | Some number always goes up. | One primary metric, chosen in advance. |
| No sample size calculation | Small tests show noise as wins. | Calculate the users you need first. |
| Stopping at the first significant day | Many false winners. | Fixed duration, decide at the end. |
| Ignoring novelty | An early lift disappears after launch. | Watch the effect over time, compare new users. |
| Not checking the split | A bug decides the result. | Check the sample ratio first. |
| Testing everything | Slow, and wrong for big or ethical decisions. | Know when to use other methods. |
Summary in six lines
- Random groups at the same time separate the effect of the change from everything else.
- Write the hypothesis, one primary metric and the guardrails before the test.
- Calculate the sample size and run the test for full weeks.
- Do not stop when the p-value first crosses 0.05. Decide at the planned end.
- Watch for novelty and change aversion, and check the split ratio.
- Do not A/B test when traffic is too low, the fix is clear, or the change is big and hard to reverse.