Statistics for analysis · lesson 12 of 13
Parts in this lesson
Arng.permutation(n) < n // 2a random half for the treatment
Bconfint_proportions_2indep(a, n1, b, n2)an interval for the difference between two shares
CNormalIndPower().solve_power(effect, power=0.8)how many in each group, before you start

Experiments and A/B tests

Last updated

Lesson 8.11 ended with the one sure way around confounders: let a coin decide. The Tindahan owners want to know whether a text message to their suki card holders brings them back to the store. In this lesson you'll split the members at random, analyse the result with a test and an interval, work out how many members an experiment needs before you run it, and fall into the trap that catches most first experiments: stopping the moment the result looks good.

By the end of this lesson you can

  • Assign a treatment at random, and check the groups are balanced
  • Analyse an A/B test with a difference, an interval and a p-value
  • Work out the sample size an experiment needs for a given power
  • Explain why peeking and stopping early produce false results

Randomise

The owners have 400 suki card holders. The idea: text half of them "₱5 off a kilo of rice this week" and see whether more of them come back. The half who get the text are the treatment group; the others, the control group. Comparing a group with a change against one without is an A/B test.

Who should get the text? Not the members the owners know best, nor the ones who spent most last month: then any difference could come from who they are rather than the text (8.11). Let the computer decide at random. Run the cells.

text_test.ipynb
  • rng.permutation(400) < 200 shuffles the numbers 0 to 399 and marks the members who got a number below 200: a random 200 for the text.
  • The balance check: the two groups spent ₱813.90 and ₱759.00 on average last month, and come from the four stores in similar shares. Not identical, because of chance, but nothing a person chose. Randomising balances every difference on average, including ones nobody measured.

Decide the outcome and the analysis before the experiment starts (pre-registration, 8.7): here, the share of members who visit the store in the following week, compared with a two-proportion test.

Analysing the result

A week later, the till records which card holders came in. (The cell makes it up: a 30% chance without the text and 38% with it.) Run the cells.

text_test.ipynb
  • 42% of the texted members came back, against 31% of the control group: 11.0 percentage points more (9.4 explains percentage points).
  • confint_proportions_2indep gives a 95% interval for the difference: 1.6 to 20.2 points. The text probably helped, but by anything from a little to a lot.
  • proportions_ztest (8.8): p = 0.022.

Report all of it: "Texting card holders raised the share who came back within a week from 31% to 42%, a difference of 11 points (95% CI 1.6 to 20.2; p = 0.022)." Because the groups were randomised, this difference is evidence that the text caused it, which no observational comparison could give.

Notice that the true lift, built into the cell, was 8 points, and the experiment saw 11. Small experiments that come out significant tend to overestimate the effect, because only the lucky high ones cross the line. Step 3 is about making experiments big enough.

Sample size and power

Before running an experiment, ask: if the effect is as big as we hope, how likely is the experiment to detect it? That chance is the experiment's power. Drag the sliders:

Powera two-group test at 0.05
−₱100₱0₱100₱200no effecttrue effect

Power: 24%. If the true difference is ₱50, a test with 200 a group would call it significant 24 times in 100. For 80% power you'd need about 1,005 a group.

The grey curve shows the differences an experiment would see by chance if there were no effect; the pink one, if the true effect is δ. The test calls a result significant past the dashed line, so the pink area beyond it is the power. More members per group make both curves narrower, and the pink one moves clear of the line.

For comparing two means, the members needed in each group for 80% power at α=0.05 are approximately

n≈2(z1−α/2+z1−β)2σ2δ2

where δ is the smallest difference worth detecting, σ the spread of the outcome, 1−β the power, and the zs come from the normal distribution (1.96 and 0.84). Run the cells.

text_test.ipynb
  • The text experiment had only a 39% chance of detecting a lift from 30% to 38% with 200 members a group. It got lucky. For 80% power it needed 549 a group: more card holders than the owners have.
  • To detect ₱50 more spending a month, when spending varies by about ₱400, the formula says 1,005 a group.

Small effects in noisy data need big experiments. If you can't get enough, it's better to know that before you start, and to test a bolder change, or run for longer, than to run an experiment that can't answer its question.

Wrong way: stop when it looks good

The owners run a second experiment and check the p-value every evening. On day 9 it dips below 0.05. Success: stop and roll it out! Try it on an A/A test, where both groups get exactly the same thing, so every "effect" is a false alarm:

Peeking at an A/A testthere is no real difference
10.50.20.050.01day 1day 10day 20day 30

Each day, 20 new members join each group, and both groups have the same 30% chance of visiting. Below the pink line, p < 0.05.

A p-value wanders up and down as data arrive. Each daily look is another chance for it to wander below 0.05, so stopping the first time it does is the many-tests problem from 8.7 in disguise. Run the simulation in Python:

peeking.ipynb

Looking once, at the end, found an "effect" in 5.5% of 2,000 A/A tests: about the 5% promised. Looking every day for 30 days and stopping at the first p below 0.05: 28.9%. More than one experiment in four would announce an effect that doesn't exist.

The check: fix the sample size (or the end date) before you start, from a power calculation, and analyse once at the end. If you must watch as it runs, for safety or cost, use a method made for it: a stricter threshold for each look (a sequential design), not 0.05 every time.

An experiment plan

Everything in this lesson fits in a few lines, written and dated before the experiment starts:

PartThe text experiment
QuestionDoes a ₱5-off text bring card holders back within a week?
Units and assignment400 card holders, half texted, chosen by rng.permutation
OutcomeShare who visit in the 7 days after the text
Smallest effect worth finding8 percentage points (30% to 38%)
Sample size549 a group for 80% power; we have 200, so power is only 39%
AnalysisTwo-proportion z-test and a 95% interval, once, on day 7
ChecksBalance of past spending and store between the groups

The sample-size row is the one that most often changes the plan. Here it says this experiment is likely to miss an 8-point effect; the owners could text for two weeks, add the next month's card holders, or accept that only a bigger effect will show.

Check what you learned

The owners give the text to the 200 members who visited most last year. Later, more of them come back. What’s wrong?
An experiment has 20% power for the effect you care about. What does that mean?
Predict: you peek at an A/A test every day for a month and stop at the first p < 0.05. Roughly how often will you announce an effect?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis