Using the CLT
The central limit theorem turns into a handful of practical recipes: error bars for simulations, sample sizes for experiments, and quick estimates of totals. This lesson collects them. The statistical side (confidence intervals and hypothesis tests from data) is developed in the Statistics course; here the focus is on probability calculations and on engineering uses.
Recipe 1: error bars for a simulation
A Monte Carlo estimate is an average of independent trials. By the CLT it is approximately normal with standard deviation , estimated by from the trials themselves. So
covers the true value about 95% of the time. Always report simulation results with such an error bar.
import numpy as np
rng = np.random.default_rng(1)
true = 1 - (5 / 6) ** 4 # P(at least one six in 4 rolls)
covered = 0
for rep in range(1000):
x = (rng.integers(1, 7, size=(2000, 4)) == 6).any(axis=1)
m, s = x.mean(), x.std(ddof=1)
half = 1.96 * s / np.sqrt(len(x))
covered += (m - half <= true <= m + half)
if rep == 0:
print(f"one run: {m:.4f} +- {half:.4f} (true {true:.4f})")
print("fraction of 1000 intervals that cover the truth:", covered / 1000)one run: 0.5160 +- 0.0219 (true 0.5177)
fraction of 1000 intervals that cover the truth: 0.945
Recipe 2: how many trials?
To estimate a probability within with 95% confidence, you need about
trials. For that is about 10,000; for about a million. In A/B testing, detecting a lift from a 5.0% to a 5.5% conversion rate (difference 0.5 percentage points) needs on the order of 30,000 users per arm at the usual 5% significance and 80% power.
import math
def n_for_margin(d, p=0.5, z=1.96):
return math.ceil(z ** 2 * p * (1 - p) / d ** 2)
for d in [0.05, 0.02, 0.01, 0.001]:
print(f"margin {d}: n = {n_for_margin(d):,}")
def n_ab(p1, p2, z_alpha=1.96, z_beta=0.8416):
pbar = (p1 + p2) / 2
return math.ceil((z_alpha * math.sqrt(2 * pbar * (1 - pbar)) + z_beta * math.sqrt(p1 * (1 - p1) + p2 * (1 - p2))) ** 2 / (p1 - p2) ** 2)
print(f"A/B test 5.0% vs 5.5%: about {n_ab(0.05, 0.055):,} users per arm")margin 0.05: n = 385
margin 0.02: n = 2,401
margin 0.01: n = 9,604
margin 0.001: n = 960,400
A/B test 5.0% vs 5.5%: about 31,234 users per arm
Recipe 3: totals and capacity
The total of many independent contributions is approximately normal with mean and standard deviation . A storage system with 10,000 users, each using on average 2 GB with standard deviation 3 GB (very skewed), needs about GB to cover the total with 99% probability. The relative safety margin shrinks as the user base grows: that is the economics of pooling, behind insurance and cloud computing.
import numpy as np
rng = np.random.default_rng(2)
n_users, trials = 10_000, 1000
# lognormal usage with mean 2 and sd 3 (GB)
sigma2 = np.log(1 + (3 / 2) ** 2)
mu_log = np.log(2) - sigma2 / 2
totals = np.array([rng.lognormal(mu_log, np.sqrt(sigma2), n_users).sum() for _ in range(trials)])
print("mean total:", round(totals.mean()), " sd:", round(totals.std()))
print("99th percentile (simulated):", round(np.quantile(totals, 0.99)))
print("CLT estimate:", round(20_000 + 2.326 * 3 * np.sqrt(n_users)))mean total: 20008 sd: 304
99th percentile (simulated): 20732
CLT estimate: 20698
Recipe 4: the square-root staffing rule
If calls arrive as a Poisson process with mean load (in "server units"), the number of busy servers is roughly normal with mean and standard deviation , so staffing servers gives a fixed quality of service. Doubling the load does not double the safety margin, which only grows by .
In software
Benchmarks report means with confidence intervals; performance regressions are flagged when the difference exceeds about two standard errors. Experimentation platforms compute sample sizes from Recipe 2 before a test starts, and capacity planners use Recipe 3 for storage, bandwidth and server pools.
Exercises
Exercise 1: A survey margin
A survey of 1600 people finds that 40% use a feature. Give a 95% margin of error.
Answer
About percentage points.
Solution
.
Exercise 2: Simulation size
A simulation estimates a probability near 0.2. How many trials give a 95% margin of ?
Answer
About 153,700.
Solution
.
Exercise 3: Pooling (code)
For users, each using on average 2 GB with standard deviation 3 GB, compute the 99% capacity by the CLT and the safety margin as a percentage of the mean total.
Answer
Margins of about 35%, 3.5% and 0.35%.
Solution
The margin is , shrinking like .
import math
for n in [100, 10 ** 4, 10 ** 6]:
mean, sd = 2 * n, 3 * math.sqrt(n)
cap = mean + 2.326 * sd
print(f"n={n:>8}: capacity {cap:,.0f} GB, margin {100 * (cap - mean) / mean:.2f}%")n= 100: capacity 270 GB, margin 34.89%
n= 10000: capacity 20,698 GB, margin 3.49%
n= 1000000: capacity 2,006,978 GB, margin 0.35%
Test yourself
To halve the margin of error of a simulation estimate you need…
- twice as many trials
- four times as many trials
- half as many trials
- the same number
Why does pooling many independent users reduce the relative safety margin?
- Users become more predictable
- The total's sd grows like while the mean grows like
- Storage gets cheaper
- Because of Markov's inequality