A/B Testing

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?

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).

Step 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.

H0 ⁣:μT−μC=0H1 ⁣:μT−μC≠0H_0\colon \mu_T - \mu_C = 0 \qquad H_1\colon \mu_T - \mu_C \neq 0

δ = 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 (z1−α/2+z1−β)2 σ2δ2n = \frac{2\,(z_{1-\htmlClass{sym-error}{\alpha}/2} + z_{\htmlClass{sym-b}{1-\beta}})^2\, \sigma^2}{\htmlClass{sym-a}{\delta}^2}

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.)

Day 1 of 14: 100,134 searchers so far
day 1

What lands in the data

Five of the 1,400,000 rows the test logs: one searcher on one day. Most spend $0.

user_iddayweekgroupspent ($)latency (ms)
usr_9213511treatment0.00178
usr_8348151control0.00149
usr_7483561control68.1289
usr_62045112control0.00118
usr_97246132treatment82.02170

Day by day

Each day: that day's users and average spend, and the test on all the days so far.
150,019 / 50,1154.99 / 5.28+5.72%+0.7% to +10.7%0.026
249,735 / 50,4125.07 / 5.20not yetnot yetnot yet
350,166 / 49,9794.89 / 5.15not yetnot yetnot yet
450,342 / 50,0545.13 / 5.06not yetnot yetnot yet
550,445 / 50,0244.90 / 5.34not yetnot yetnot yet
649,821 / 50,0334.89 / 4.98not yetnot yetnot yet
749,734 / 49,6725.05 / 5.13not yetnot yetnot yet
849,801 / 50,1015.17 / 5.21not yetnot yetnot yet
950,172 / 50,2615.05 / 5.23not yetnot yetnot yet
1050,408 / 50,3235.10 / 5.31not yetnot yetnot yet
1149,837 / 49,9804.83 / 5.24not yetnot yetnot yet
1250,100 / 50,0784.97 / 5.15not yetnot yetnot yet
1349,922 / 50,0914.99 / 5.12not yetnot yetnot yet
14 (last)49,949 / 50,0625.13 / 5.27not yetnot yetnot yet

Week by week

PeriodUsers C / TSpend C / T ($)Lift95% interval
Week 1 (days 1–7)not yet
Week 2 (days 8–8)not yet
All 1 days50,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.

z=xˉT−xˉCsT2/nT+sC2/nCz = \frac{\htmlClass{sym-line}{\bar{x}_T - \bar{x}_C}}{\sqrt{s_T^2 / n_T + s_C^2 / n_C}}

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.

0%2%4%6%8%10%12%no effectMDE 2%+0.7%+10.7%Lift (% of control)

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

Too early: 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

0MDEClear winLaunchBorderlineRun a longer testReal but too smallDon’t launchToo uncertainRun a bigger testNo useful effectDon’t launchWorseDon’t launch
  • 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

  1. Ask one question, and randomize only the people the change can reach.
  2. Measure one metric tied to the goal, plus guardrails that must not get worse.
  3. Sign the contract first: α, power and the MDE, and from them the users and the days, in whole weeks.
  4. Run an A/A test, then the full plan, without stopping early.
  5. Check the split, outside events and novelty before trusting any p-value.
  6. Read the interval against 0 and the MDE, not just the p-value.
  7. 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.