Contents/ Part XXIX · Capstone Projects: Design & Causal Inference/ Chapter 181

Designing an A/B Test Properly

Capstone 21. On day 4 the new checkout was ahead by 15 percent, with a p-value of 0.02. It has no effect whatsoever. The difference between those two sentences is a document written before any traffic arrived.

⏱️ ~20 min read
🎯 Experiment design
📊 Chapter 181

Every capstone so far has analyzed data that already existed. This one starts a week earlier, at the point where the decisions that determine whether the analysis can work at all are still available to be made.

The brief
Setting
An online retailer rebuilt its checkout and ran the new version against the old one for 14 days, splitting visitors 50/50 and logging 80,236 assignment rows.
The question
Does the redesigned checkout increase the rate at which visitors place an order?
Why it matters
A team spent three months building the redesign, and the company wants to ship it everywhere. A lift below 10 percent does not pay back that much engineering time, so the decision needs a number that can be compared against a threshold, not just a verdict.
What we do
Fix the sample size against a stated minimum detectable effect before collecting anything, check the randomization for sample ratio mismatch, look at what a day-4 dashboard would have shown, measure by simulation how often that happens when nothing is going on, calibrate a stopping rule that survives daily monitoring, and use pre-experiment behavior to buy precision back.
The minimum detectable effect is the smallest true difference a test is built to find, and it is a business decision rather than a statistical one. Everything else follows from it: sample size scales with the inverse square of the MDE, so halving the effect you want to catch quadruples the traffic you need.
The finding, up front

The true lift is zero. On day 4 the treatment led by 15.3 percent at p = 0.023, and a team watching a dashboard would have shipped. At the pre-registered end the lift is −1.1 percent at p = 0.75. Simulation puts the chance of at least one significant day, on a test where nothing is happening, at 18.7 percent.

1

The Plan, Written Before the Traffic

Twelve decisions, all made on day 0 and all recorded on the ExperimentPlan sheet of the workbook. They are the reason this test can be defended afterward, and several of them become impossible to make honestly the moment a result is visible.

LineDecisionWhy it has to be fixed in advance
P2 Primary metricConversion rate, orders per assigned visitor. One metric.Choosing the metric after seeing which one moved is the oldest trick there is
P4 MDEA 10 percent relative lift, 4.00 percent to 4.40 percentSets the sample size, and encodes what the business would actually act on
P5 Power80 percent at α = 0.05, two-sidedTwo-sided because a drop must be detectable too
P7 Stopping ruleOne analysis, at the planned endThe single most important line on the sheet
P10 ExclusionsDuplicate rows, cross-arm visitors, refund sentinelsNamed before anyone knows which direction dropping them moves the result
P11 SecondariesReported as intervals, never as testsStops a null primary being rescued by a hunt through the rest

Read P4 and P7 together, because between them they turn this from a search into an experiment. P4 says that a lift below 10 percent is not worth having, which is a judgment about build cost and margin that no statistician can supply. P7 says there will be exactly one analysis. Neither line is difficult to write. Both are nearly impossible to write after the fact.

BEFORE ANY DATA EXISTS AFTER THE DATA ARRIVES Primary metric Minimum detectable effect Power and significance level Sample size and duration Stopping rule Exclusions Clean the log Run the one planned analysis Report the estimate and interval Publish the result either way a decision made after seeing the result
The line everything depends on. Nothing on the left is difficult, and nothing on the left can be reconstructed later. Pre-registration is simply the act of putting the six decisions on the left in writing, so that the arrow at the bottom cannot be drawn without somebody noticing.
2

How Big, and How Long

Four numbers determine the sample size, and none of them comes from the data: the baseline rate, the smallest effect worth detecting, the power, and the significance level. At a 4.0 percent baseline and a 10 percent relative MDE, with 80 percent power at α = 0.05, the requirement is 39,472 visitors per arm, or 78,944 in total. At roughly 5,700 visitors a day that is 13.8 days, so the plan says 14.

The interesting part is what happens if the business asks for more sensitivity. Because n scales with the inverse square of the effect, the cost of caring about smaller lifts rises brutally.

Relative MDEDetectsn per armDays at 5,700 per day
20%4.00% to 4.80%10,3134
15%4.00% to 4.60%17,9406
10% (used)4.00% to 4.40%39,47214
7%4.00% to 4.28%79,45828
5%4.00% to 4.20%154,30154
3%4.00% to 4.12%424,617149

Chasing a 3 percent lift would take 149 days. By then the site, the season and the audience have all changed, so the thing being measured is no longer the thing that was launched. A test that cannot finish inside a stable period is not a cheaper test, it is an unanswerable one, and saying so is more useful to the business than quietly running it anyway.

💡
Underpowered tests are worse than no test

A test with 30 percent power does not merely fail to find real effects. When it does reach significance, the estimate it reports is inflated, because only the largest random excursions clear the bar. That is the winner's curse, and it is why a string of underpowered wins produces a roadmap of changes that never replicate.

3

The Log, and What Comes Out of It

A real assignment log is not a tidy table. Four faults here, and all four were named in P10 of the plan before anyone knew which direction removing them would push the answer.

StepRowsWhat happened
Raw log80,236One row per assignment event, not per visitor
Drop duplicate rows79,896340 removed: the logger retried on timeout
Drop cross-arm visitors79,704192 rows from 96 visitors who saw both checkouts
One row per visitor79,704The analysis file
Void refund sentinels120 valuesA revenue of −1 means refunded, not a negative sale

The cross-arm visitors are the exclusion worth dwelling on. Ninety-six people were bucketed into both arms by a bug. The tempting repair is to keep whichever assignment came first, which sounds harmless and quietly converts a randomized comparison into a comparison of whoever happened to get logged twice. They belong to neither arm, so they leave both. Ninety-six out of eighty thousand costs nothing; the principle costs nothing either, and it is the same principle that would matter if the bug had touched nine thousand.

4

The Health Check That Comes First

Before a single outcome is examined, check that the randomization did what it claimed. Assignment was 50/50, so the two arms should be the same size to within chance. If they are not, something in the bucketing or the logging is broken, and every number downstream is suspect no matter how good it looks.

Control
39,755
visitors
Treatment
39,949
visitors
Split
49.88%
expected 50.00%
SRM test
p = 0.49
chi-square 0.472, passes

That is what a healthy randomization looks like. Note that the threshold for this particular test is deliberately strict rather than loose: practitioners typically act only below p = 0.001 or so, because the sample is enormous and you want the alarm to fire on genuine pipeline faults rather than on ordinary noise. Note also the order. Running the SRM check after seeing the conversion numbers invites the analyst to decide how seriously to take it based on whether they liked the result.

Both arms clear the 39,472 the plan required, so the test has the power it was designed with. That is worth stating explicitly, because a test that ends short of its planned n is a different test from the one that was specified, and its result should be reported as such.

5

The Day-4 Result That Would Have Shipped

The team has a dashboard. On day 4 it shows a win, and not a marginal one.

 Day 4, the peekDay 14, the planned end
Visitors22,77379,704
Control4.0056%4.1102%
Treatment4.6185%4.0652%
Relative lift+15.30%−1.09%
95% interval[+2.13%, +28.47%][−7.78%, +5.59%]
Testz = 2.277, p = 0.0228z = −0.321, p = 0.7484

Significant, and comfortably above the 10 percent bar that justified building the thing. Every instinct says ship it and move on to the next project. The only thing standing in the way is a line in a document saying the test runs for 14 days, written by people who had not yet seen this number.

Look at what happens to the effect over the following ten days. It does not shrink toward zero, which is what most people expect. It changes sign. Two thirds of the eventual traffic arrived after the moment the team would have stopped, and it carried the estimate back to where the truth is, which is nowhere.

Left: cumulative z-statistic by day. Sixty faint gray lines show simulated experiments with no true effect, wandering above and below the dashed 1.96 boundary. A dark line shows this experiment, rising above 1.96 on day 4, circled and annotated ship it, then falling back toward zero for the rest of the test. Solid lines mark the calibrated boundary at 2.55. Right: false-positive rate against the number of daily looks, rising from about 5 percent at one look to 18.7 percent at thirteen, against a dashed line at 5 percent.
Left: sixty simulated experiments with no true effect, in gray. Several cross the dashed 1.96 line at some point, which is the whole problem. The dark line is our test, and the circled point is day 4. Right: what watching costs, measured in the next section.
6

How Often Day 4 Happens by Accident

One run proves nothing, and it would be dishonest to build a chapter on a single lucky draw. The question that can be answered properly is this: if the treatment does nothing at all, how often does a team that checks daily see at least one significant result? That is a simulation, not an argument.

Five thousand experiments were generated with a true lift of exactly zero, each accumulating 2,850 visitors per arm per day for 14 days, and each tested at the end of every day from day 2 onward.

How the test was runDeclared significant
One analysis, at the planned end4.7%
Checking after 3 days9.6%
Checking after 6 days13.8%
Checking after 9 days16.3%
Checking every day, 13 looks18.7%

Five percent becomes nearly nineteen. The significance level you chose applies to one comparison, and thirteen looks at the same accumulating data is thirteen chances to cross the line. The looks are heavily correlated, since each one contains all the data from the last, which is why the rate lands near 19 percent rather than the 49 percent you would get from thirteen unrelated tests. Almost one flat test in five can be declared a winner by a team doing nothing more sinister than watching a dashboard.

⚠️
This is not the same as p-hacking

Nobody in this story did anything dishonest. There was no fishing through metrics, no dropping of awkward segments, no reruns with different filters. A team looked at its own dashboard and stopped when the answer looked clear. That is the point: the failure is structural, so the remedy has to be structural too.

7

A Stopping Rule That Survives Being Watched

Monitoring is not the sin. Monitoring against a boundary built for a single look is. Teams have perfectly good reasons to watch a running experiment, not least catching a change that is actively harming customers. The fix is to decide in advance that you will look, and to widen the boundary so that the error rate across the whole sequence is the one you wanted.

The same simulation that exposed the problem sets the threshold. Take the largest absolute z each simulated experiment ever reaches across its 13 looks, and find the 95th percentile of that distribution.

Single-look boundary
1.96
gives 18.7% across 13 looks
Calibrated boundary
2.552
gives 5.0% across 13 looks
As a p-value
p < 0.011
at any single look
The day-4 result
z = 2.277
does not cross it

The day-4 z of 2.28 clears 1.96 and does not clear 2.55. A team that had written this boundary into its plan would have looked at the dashboard on day 4, seen a 15 percent lift, noted that it had not crossed the line, and kept the test running. That is precisely the behavior the wider boundary is being paid for.

Note how much less severe the calibrated boundary is than a naive correction. Treating the 13 looks as independent, a Sidak adjustment would demand p < 0.0039 at each one. The looks are nested, not independent, so simulating the actual correlation buys back most of that severity: 0.011 rather than 0.0039. Published group-sequential designs such as Pocock and O'Brien-Fleming boundaries are the formal versions of the same idea, and O'Brien-Fleming in particular spends its error unevenly, staying very strict early and relaxing toward the planned end.

8

CUPED: The Same Precision on Less Traffic

Sample size is set by variance, so anything that removes variance without touching the treatment effect buys traffic back. CUPED, controlled experiment using pre-experiment data, subtracts off the part of the outcome that was predictable from behavior recorded before assignment. Because the covariate predates the treatment, nothing the treatment did can have influenced it, which is what makes the subtraction safe.

Here the outcome is revenue per visitor and the covariate is spend in the 30 days before assignment. They correlate at r = 0.475.

Correlation
r = 0.475
pre-spend and in-test revenue
Variance removed
22.5%
exactly r squared
SE of the difference
−12.0%
$0.3958 to $0.3483
Traffic saved
3.2 days
of the 14

CUPED buys you exactly r squared, and not one point more. That single fact is worth carrying around, because it tells you before you build anything whether the technique is worth the pipeline. A covariate correlated 0.475 with the outcome removes 22.5 percent of the variance. A covariate correlated 0.15 removes 2 percent and is not worth a meeting. In this book's own data the same method applied to the conversion metric rather than revenue would have been close to worthless, because a rare binary outcome is barely predictable from anything.

Notice what CUPED does not do. It does not change the estimate, only its precision. The adjusted and unadjusted differences agree; the adjusted one is measured more sharply. A variance-reduction method that moved the point estimate would be a red flag, not a feature.

Left: ten points, one per decile of pre-period spend, plotting mean pre-period spend against mean in-test revenue, with a straight fitted line of slope 0.31 running through them. Nine points sit close to the line and the highest decile falls below it. Right: two bars, required traffic at 100 percent and with CUPED at 77 percent, the second labeled 3.2 days saved.
Left: visitors grouped into deciles of prior spend, against mean in-test revenue, with the linear adjustment CUPED applies. The top decile sits below the line, which is worth noticing: the adjustment is least trustworthy exactly where the covariate is most extreme. Right: what that relationship is worth, in traffic.
VARIANCE OF REVENUE PER VISITOR 22.5% 77.5% predictable from pre-period spend genuine noise: no covariate removes this TRAFFIC NEEDED FOR THE SAME PRECISION 77.5% of the original, or 3.2 days of 14
The whole method in one picture. CUPED removes the shaded slice and nothing else, and the shaded slice is r squared. Because precision improves with the square root of traffic, removing 22.5 percent of the variance saves 22.5 percent of the traffic, not more.
9

The Result, Reported as an Estimate

A p-value of 0.75 is not the finding. The finding is the interval, because the interval is what answers the question the business actually asked, which was never "is the effect exactly zero" but "is it big enough to be worth having".

Difference
−0.045 pp
4.1102% to 4.0652%
95% interval
−0.320 to +0.230
percentage points
The MDE
+0.411 pp
a 10% relative lift
Verdict
Ruled out
the interval sits below it

This is a well-powered null, and it is worth as much as a win. The test was designed to detect a 10 percent lift. The data are consistent with anything from a 7.8 percent drop to a 5.6 percent rise, and the entire interval sits below the bar the business set. The honest conclusion is not "we failed to find an effect", which sounds like a failure of the analysis. It is "we have ruled out an effect large enough to be worth having", which is a genuine answer to the question that was asked.

That conclusion is available only because the sample size was fixed in advance against a stated MDE. Had the test been run to an arbitrary two weeks with no power calculation, the same interval would have been uninterpretable: nobody could have said whether it was narrow enough to rule anything out. The pre-registration is what converts a null result from an absence of news into information.

The secondary metrics, reported the way P11 requires

Revenue per visitor came in at $6.51 in control and $7.09 in treatment. It is tempting to reach for that as a consolation prize. P11 forbids it, for the reason Capstone 4 established: a null primary followed by a search through secondaries is the multiple-comparisons problem wearing different clothes. The right treatment is to report the number with its interval, note that it was not the metric the test was powered for, and let it inform the next experiment's hypothesis rather than this one's conclusion.

10

What to Watch

11

Experiment Design in Data Science & AI

Online controlled experiments are the largest applied statistics operation in the world, and every large platform has rediscovered the contents of this chapter the expensive way.

Where it appearsThe same problem, in a different costume
Recommender ranking testsMetrics are heavily skewed by a few users, so variance reduction is the difference between a feasible test and an impossible one
Model rolloutsA new model is shipped behind a flag and monitored continuously, which is peeking unless a boundary was set
Bandits and adaptive allocationTraffic is reallocated toward the winner as evidence accrues, which is deliberate peeking with the inference rebuilt to match
Interleaving experimentsBoth rankings shown to the same user, which removes between-user variance the way pairing did in Capstone 3
LLM evaluationPairwise preference judgments on a shared prompt set, with the same MDE and power questions and usually none of the answers
Where the research went

CUPED comes from Deng, Xu, Kohavi and Walker at Microsoft in 2013, and the variance reduction it reports on real telemetry is broadly what this chapter reproduces. The sequential problem has two modern answers worth knowing: always-valid inference based on confidence sequences, which lets you look continuously with no boundary calculation at all, and group-sequential designs borrowed from clinical trials, where the same logic has been standard since the 1970s. The clinical-trials literature is where an experimenter should look first, because medicine had strong reasons to solve stopping rules honestly long before anybody was testing checkout buttons.

🐍

The full project, step by step

The companion notebook reads the pre-registered plan before touching the data, derives the sample size from first principles and tabulates what a smaller MDE would have cost, cleans four faults out of the assignment log, runs the sample ratio mismatch check, reproduces the day-4 result and the day-14 one, simulates five thousand flat experiments to price peeking at 18.7 percent, calibrates the sequential boundary from that same simulation, and implements CUPED from scratch.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub
Read the reports & get the data

The dataset (capstone-designing-an-ab-test.xlsx) holds the 80,236-row assignment log with its duplicates, cross-arm visitors and refund sentinels intact, the pre-registration written on day 0, and the true effect that was simulated so you can check your work. Two written reports accompany it: a plain-language brief for the product lead who wants to ship, and a technical report covering the power calculation, the sequential boundary and the variance reduction.

🎓 Key Takeaways

  • The MDE sets everything, and it is a business decision. n scales with its inverse square: 10 percent needs 14 days, 3 percent needs 149.
  • Peeking turns 5 percent into 18.7 percent. Thirteen daily looks at a test with no effect declared a winner almost one time in five.
  • Look all you like, against the right boundary. Calibrating by simulation gave 2.55 rather than 1.96, and the day-4 result of 2.28 would not have stopped the test.
  • CUPED buys exactly r squared. A covariate correlated 0.475 with the outcome removed 22.5 percent of the variance and 3.2 days of traffic.
  • A well-powered null is a real answer. The interval of −7.8 to +5.6 percent excludes the 10 percent lift that justified the build, which settles the decision.

Quiz: Test Yourself