A/B Testing
Run a fair test of a new search ranking, and decide whether to ship it.
You are StylePulse’s lead data scientist. Go through the seven steps in order: each choice you make changes what the later steps show.
Step 1 of 7
The problem: who to test, and what to measure
The VP of Product, on Slack:
“Hey! The ML team just finished RankNet-v2, our new search ranking. Let’s split all homepage traffic 50/50 and measure time on site!”
StylePulse is a made-up online clothes shop. 100,000 people search it every day. Should it replace its search ranking with RankNet-v2? An A/B test answers that: each searcher is put at random incontrol (the old ranking) or treatment(RankNet-v2), so any difference bigger than chance comes from the ranking. The VP’s plan has two problems. Find them.
Where do people join the test?
Half of homepage visitors never search, so they never see the new ranking. The effect per visitor halves, and the test needs 2,511,642 visitors per group: 28 days instead of 14.
What is the primary metric?
Guardrails, which must not get worse: search latency (at most +10 ms) and checkout errors (at most +0.10 percentage points).
Next: The contractStep 2 of 7
The contract: decide before you look
Before any data comes in, write down what counts as a win. Three numbers do it. Move the sliders, then sign.
δ = 2% × $5.00 = $0.10 per searcher per day: $10,000 a day for 100,000 searchers.
α = 0.05: a test of a useless change still “wins” 5% of the time.
1 − β = 0.80: a real lift of 2% is missed 20% of the time.
Why an MDE of 2%?The MDE (minimum detectable effect) is the smallest lift worth launching. For 100,000 searchers at $5.00 a day, 2% is $10,000 a day, about $3,650,000 a year. Anything smaller is not worth running and maintaining a new model. This is the line for practical significance.
Step 3 of 7
The design: how many users, how long, and an A/A test
How many searchers does the test need to spot the MDE, and how long must it run? The formula uses the contract’s α, power and MDE, and σ, how much spend varies from searcher to searcher.
n = 2 × (1.96 + 0.84)² × $20² / $0.10² = 627,910 users per group
At 50,000 users a group each day: 12.6 days of traffic, rounded up to whole weeks: 14 days.
Randomizing at the homepage (step 1) would need 2,511,642 visitors per group: 28 days.
How long should it run?
Before the real test: the A/A test
Show both groups the old ranking for 14 days. Nothing changed, so a working setup shows no difference.
Step 3 in Python: plan the test
The contract from step 2 turned into users and days. SciPy and Plain Python get the two z values the same way, from the normal distribution.
Step 4 of 7
Run it: watch the days come in
The test is live, and day 1 is in. Press Run to stream the days. The graph shows the lift so far with its interval (the band), against 0 and the MDE; the strip under it shows the p-value each day. Drag the day handle to go back, or the MDE line to change the plan.
Now running: Clear win (RankNet-v2 against the old ranking, on two ordinary weeks.)
What lands in the data
Five of the 1,400,000 rows the test logs: one searcher on one day. Most spend $0.
| user_id | day | week | group | spent ($) | latency (ms) |
|---|---|---|---|---|---|
| usr_92135 | 1 | 1 | treatment | 0.00 | 178 |
| usr_83481 | 5 | 1 | control | 0.00 | 149 |
| usr_74835 | 6 | 1 | control | 68.12 | 89 |
| usr_62045 | 11 | 2 | control | 0.00 | 118 |
| usr_97246 | 13 | 2 | treatment | 82.02 | 170 |
Day by day
| 1 | 50,019 / 50,115 | 4.99 / 5.28 | +5.72% | +0.7% to +10.7% | 0.026 |
| 2 | 49,735 / 50,412 | 5.07 / 5.20 | not yet | not yet | not yet |
| 3 | 50,166 / 49,979 | 4.89 / 5.15 | not yet | not yet | not yet |
| 4 | 50,342 / 50,054 | 5.13 / 5.06 | not yet | not yet | not yet |
| 5 | 50,445 / 50,024 | 4.90 / 5.34 | not yet | not yet | not yet |
| 6 | 49,821 / 50,033 | 4.89 / 4.98 | not yet | not yet | not yet |
| 7 | 49,734 / 49,672 | 5.05 / 5.13 | not yet | not yet | not yet |
| 8 | 49,801 / 50,101 | 5.17 / 5.21 | not yet | not yet | not yet |
| 9 | 50,172 / 50,261 | 5.05 / 5.23 | not yet | not yet | not yet |
| 10 | 50,408 / 50,323 | 5.10 / 5.31 | not yet | not yet | not yet |
| 11 | 49,837 / 49,980 | 4.83 / 5.24 | not yet | not yet | not yet |
| 12 | 50,100 / 50,078 | 4.97 / 5.15 | not yet | not yet | not yet |
| 13 | 49,922 / 50,091 | 4.99 / 5.12 | not yet | not yet | not yet |
| 14 (last) | 49,949 / 50,062 | 5.13 / 5.27 | not yet | not yet | not yet |
Week by week
| Period | Users C / T | Spend C / T ($) | Lift | 95% interval |
|---|---|---|---|---|
| Week 1 (days 1–7) | not yet | |||
| Week 2 (days 8–8) | not yet | |||
| All 1 days | 50,019 / 50,115 | 4.99 / 5.28 | +5.72% | +0.7% to +10.7% |
Step 5 of 7
Check it: can the data be trusted?
Hold on. Before anyone celebrates, a careful data scientist audits the data for broken tracking and for the outside world. Load the other tests below: each one changes steps 4, 6 and 7 too.
Always check for outside events (a holiday, a rival’s flash sale, news) and for broken tracking (users missing from one group, a new design’s novelty) before trusting a p-value. A/B tests do not happen in a vacuum.
Load another test
Clear win: RankNet-v2 against the old ranking, on two ordinary weeks.
What really happened (the simulation’s truth)
RankNet-v2 made 4% more searchers buy something.
- Split check: 50,019 vs 50,115 users, χ² = 0.09, p = 0.762. An even split.
- Guardrails: search +4 ms, checkout errors −0.31 points. Within the limits.
- Novelty: needs the second week (run step 4 past day 7).
- Outside events: none known during the test.
Step 5 in Python: check the data
The test up to today, as each group's daily totals. It runs the split check, then compares the lift in week 1 with week 2. SciPy has the χ² test built in; Plain Python works it out with math.erfc.
Step 6 of 7
Read it: significant, and big enough?
Welch’s z-test says how many standard errors apart the two groups’ average spend is, and the interval gives the range the true lift probably lies in. Pick a day in the graph or the table to see its numbers.
A p-value under α only says the effect is probably not zero(statistical significance). It does not say it is big enough to matter. For that, compare the whole interval with the MDE (practical significance), and check that the guardrails held.
Day 1: z = (5.2799 − 4.9944) / √(20.55² / 50,115 + 20.00² / 50,019) = 2.23, p = 0.026
95% interval: $0.285 ± $0.251, so the lift is +0.7% to +10.7% of the control’s $4.99.
Statistically significant (p = 0.026). Not shown to be practically significant: the interval reaches below the 2% MDE.
Step 6 in Python: test the difference
The test up to today, as each group's daily totals. It pools the days into one total per group, then works out z, the p-value and the interval above. SciPy gives the normal tails; Plain Python uses math.erfc.
Step 7 of 7
Decide: launch, or not?
The final meeting with the CEO. RankNet-v2 needs extra servers to run. Move the cost slider to see how the money and the test result decide together. The checks run in a fixed order: too early, uneven split, guardrails, a fading lift, the interval, then money.
- Gross lift
- $28,550 a day
- Running cost
- −$4,000 a day
- Net, expected
- $24,550 a day
- Net, cautious (95%)
- −$570 a day
- Break-even lift
- +0.80%
The decision, day 1 of 14
Stopping now would look like a +5.7% win (p = 0.026).
Search +4 ms (limit +10) · checkout errors −0.31 pts (limit +0.10)
Your choices
- Randomized at “Visit the homepage”: step 1 shows why that fails. This analysis uses searchers.
- No metric picked in step 1; this analysis uses spend per searcher.
- Never signed the contract in step 2.
- Skipped the A/A test in step 3: run it to trust the setup.
- Ran the full plan: 14 days.
- Did not stop early on a good-looking day.
The six cases
- Clear win: a test simulated with a true lift of +4% measured +4.4%, interval +3.0% to +5.7%.
- Borderline: a test simulated with a true lift of +2% measured +2.3%, interval +1.0% to +3.6%.
- Real but too small: a test simulated with a true lift of +1%, 4× the traffic measured +1.1%, interval +0.4% to +1.8%.
- Too uncertain: a test simulated with a true lift of +1% measured +1.2%, interval −0.2% to +2.5%.
- No useful effect: a test simulated with a true lift of 0% measured −1.0%, interval −2.3% to +0.3%.
- Worse: a test simulated with a true lift of −3% measured −2.9%, interval −4.2% to −1.5%.
Each row is a real test, simulated by the page’s Python data script (python/a_b_testing.py), and its interval is worked out with the same test as yours. The highlighted row is your test. Peeking makes the wrong row likely: of 10,000 simulated tests of a useless change, 21.2%looked like a win on some day.
Step 7 in Python: decide
The interval from step 6 and the cost slider: which of the six cases the interval falls in, and what the lift is worth a day. No library needed.
A/B testing in short
- Ask one question, and randomize only the people the change can reach.
- Measure one metric tied to the goal, plus guardrails that must not get worse.
- Sign the contract first: α, power and the MDE, and from them the users and the days, in whole weeks.
- Run an A/A test, then the full plan, without stopping early.
- Check the split, outside events and novelty before trusting any p-value.
- Read the interval against 0 and the MDE, not just the p-value.
- Decide, and make sure the lift pays for itself.
About the data
StylePulse and its numbers are made up, but they behave like a real shop’s: on a usual day 8% of searchers buy something, so most spend $0, and spend per searcher averages $5.00 with a standard deviation of $20.00. Each test was simulated searcher by searcher; the page gets each day’s totals for each group, which is all the test needs. Each searcher-day counts on its own here; real analyses group a person’s days together.