SafeZone AI Learn
Learn/ Mathematical Foundations/ Probability & Statistical Inference · lesson 7 of 9

A/B testing and experimentation

The experiment platform from first principles: sample-size arithmetic, the peeking problem demonstrated by running 400 A/A tests in front of you, sequential methods that price the peeks, and the design details that decide validity before any math runs.

The last lesson built the test; this one builds the experiment — because in industry the statistics is the easy half. An A/B test is a randomized controlled trial run on a product: split traffic, treat one arm, compare. Done right it is the only instrument that reliably converts “we changed X” into “X caused Y”. Done casually it manufactures confident nonsense at scale — and the single most common way to do it casually is so seductive, so universal, and so quantifiable that this lesson hands it to you as a simulation you can run until convinced.

Randomization is the active ingredient

Everything an A/B test can promise flows from one act: units assigned to arms by coin flip. Randomization severs every confounder, known and unknown — the causal-inference lesson ahead handles the world where you can’t randomize; here you can, so use it cleanly. The design details that decide validity before any statistics runs:

  • Unit of randomization = unit of analysis. Randomize users but analyze page-views and your nn is a lie (a heavy user contributes 500 correlated rows — the CLT lesson’s correlated-samples warning, now with a dashboard attached).
  • Hash-based assignment (hash(user_id) % 100 — the reproducibility lesson’s split trick) keeps a user in one arm across sessions and devices, forever, deterministically.
  • A/A tests as calibration: run the machinery with no treatment difference; it should fire at exactly α. If your platform’s A/A tests “find” effects 15% of the time, every A/B result it ever produced is suspect. This lesson’s widget is, precisely, an A/A test battery.
  • Guardrail metrics: alongside the success metric, monitor what must NOT regress (latency, errors, unsubscribes) — a treatment that wins its target by hurting a guardrail is a loss wearing a win’s clothes.

The sample-size arithmetic is last lesson’s power formula with business numbers in it. Detecting a 1% relative lift on a 5% conversion rate (δ ≈ 0.0005 absolute, binary outcome) needs roughly n16σ2/δ2n \approx 16\sigma^2/\delta^2 \approx 3 million users per arm — a number that shocks every product team the first time, explains why experiment platforms obsess over variance reduction (CUPED and friends: regress out pre-experiment behavior, shrink σ\sigma, cut nn by 30–50%), and sets up this lesson’s central sin. Because when the honest nn is millions and the dashboard updates hourly… people look.

The peeking problem, demonstrated rather than asserted

Fixed-horizon testing’s guarantee — false positives at rate α — holds for a test performed once, at a pre-registered nn. Watch the dashboard daily and stop “when it’s significant”, and you are running a different procedure with a very different error rate:

The chart shows one experiment’s z-statistic evolving as samples arrive: a random walk, drifting aimlessly between the ±1.96 rails — because there is genuinely nothing to find. Peek only at the end (slider fully right) and the battery of 400 A/A tests fires at ~5–6%: the guarantee, honored. Now peek every 50 samples: the false-”significant” rate jumps to ~25% — because you’re giving a random walk twenty chances to touch a rail, and random walks, given chances, take them (with unlimited looks it touches eventually with probability 1 — the law of the iterated logarithm, made managerial). Nothing was p-hacked, nobody was dishonest; the analyst just looked. That is the whole pathology: optional stopping silently converts α = 5% into α = 15–25%, and it is the default behavior of every human with a live dashboard. Re-run the battery a few times — the rates are stable; this is law, not luck.

The fixes form a ladder. Discipline: pre-register nn, look once — free, fragile against human nature. Alpha spending / group-sequential designs (O’Brien–Fleming): schedule a few interim looks with adjusted thresholds so early stopping is priced in — the clinical-trials standard. Always-valid inference (mSPRT and kin — what mature experiment platforms actually run): confidence sequences that remain valid under continuous monitoring, costing wider intervals in exchange for the right to peek freely. And the Bayesian reframe (posterior probabilities updated continuously) dissolves the stopping problem but re-imports the prior-choice one — the Bayes lesson’s trade, in production. The meta-lesson is the module’s oldest: the validity of an inference depends on the procedure that produced it, including the procedure’s stopping rule — which no p-value printed at the end can see.

Beyond the single test, three realities of experimentation at scale, so they’re on your map: interference (one user’s treatment affecting another’s outcome — marketplaces, social feeds — breaks the independence every formula above assumed; cluster or switchback designs respond); novelty and primacy effects (week-one lifts that decay — run long enough to cover a full behavioral cycle); and the winner’s curse (effects estimated from the experiments that passed the threshold are biased upward — the same selection artifact as the best-seed reporting trap, and the reason meta-analyses of shipped wins keep “finding” less lift than the launch decks claimed).

import numpy as np
rng = np.random.default_rng(0)

# the peeking law, in eight lines: A/A tests with continuous monitoring
def aa_test(peek_every, n=1000):
    x = rng.standard_normal(n).cumsum()
    z = x / np.sqrt(np.arange(1, n + 1))
    looks = z[peek_every - 1 :: peek_every]
    return np.abs(looks).max() > 1.96

for pe in [1000, 200, 50, 10]:
    fp = np.mean([aa_test(pe) for _ in range(2000)])
    print(f"peek every {pe:4d}: {fp:.1%} false positives")
# 1000 → ~5%.  10 → ~38%.  Same data, same math, different LOOKING.

Exercises

Work these before the next lesson

  1. Do the shocking arithmetic yourself: derive n per arm for detecting a relative lift r on a base conversion c (binary outcome, 80% power, α = 0.05), and evaluate at c = 5%, r = 1%. Then compute what a variance-reduction technique that halves σ² does to it.
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • R. Kohavi, D. Tang & Y. Xu, Trustworthy Online Controlled Experiments, 2020 — the industry bible; every pathology above, with case studies.
  • E. Miller, “How Not To Run An A/B Test”, 2010 — the peeking problem’s classic statement.
  • R. Johari et al., “Peeking at A/B Tests: Why It Matters and What to Do About It”, KDD 2017 — always-valid inference in production.
  • A. Deng et al., “Improving the Sensitivity of Online Controlled Experiments by Utilizing Pre-Experiment Data” (CUPED), WSDM 2013.