TExcelTAD
Home › Probability Theory › Using the CLT

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 Xˉn\bar{X}_n of nn independent trials. By the CLT it is approximately normal with standard deviation σ/n\sigma/\sqrt{n}, estimated by s/ns/\sqrt{n} from the trials themselves. So

Xˉn±1.96sn\bar{X}_n \pm 1.96 \frac{s}{\sqrt{n}}

covers the true value about 95% of the time. Always report simulation results with such an error bar.

python · try it yourself
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 pp within ±d\pm d with 95% confidence, you need about

n≈1.962 p(1−p)d2≤1.9624d2≈1d2n \approx \frac{1.96^2 \, p(1-p)}{d^2} \le \frac{1.96^2}{4d^2} \approx \frac{1}{d^2}

trials. For d=0.01d = 0.01 that is about 10,000; for d=0.001d = 0.001 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.

python · try it yourself
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 nμn\mu and standard deviation σn\sigma\sqrt{n}. A storage system with 10,000 users, each using on average 2 GB with standard deviation 3 GB (very skewed), needs about 20,000+2.33⋅300≈20,70020{,}000 + 2.33 \cdot 300 \approx 20{,}700 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.

python · try it yourself
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 RR (in "server units"), the number of busy servers is roughly normal with mean RR and standard deviation R\sqrt{R}, so staffing R+βRR + \beta\sqrt{R} servers gives a fixed quality of service. Doubling the load does not double the safety margin, which only grows by 2\sqrt{2}.

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 ±2.4\pm 2.4 percentage points.

Solution

1.960.4⋅0.6/1600=1.96⋅0.01225≈0.0241.96\sqrt{0.4 \cdot 0.6/1600} = 1.96 \cdot 0.01225 \approx 0.024.

Exercise 2: Simulation size

A simulation estimates a probability near 0.2. How many trials give a 95% margin of ±0.002\pm 0.002?

Answer

About 153,700.

Solution

1.962⋅0.2⋅0.8/0.0022=3.8416⋅0.16/0.000004≈153,6641.96^2 \cdot 0.2 \cdot 0.8 / 0.002^2 = 3.8416 \cdot 0.16 / 0.000004 \approx 153{,}664.

Exercise 3: Pooling (code)

For n=100,104,106n = 100, 10^4, 10^6 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 2.326⋅3n/(2n)2.326 \cdot 3\sqrt{n}/(2n), shrinking like 1/n1/\sqrt{n}.

python · try it yourself
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…

  1. twice as many trials
  2. four times as many trials
  3. half as many trials
  4. the same number

Why does pooling many independent users reduce the relative safety margin?

  1. Users become more predictable
  2. The total's sd grows like n\sqrt{n} while the mean grows like nn
  3. Storage gets cheaper
  4. Because of Markov's inequality