Type at least 2 letters. Enter opens the first result, Esc closes.

    Probability and Statistics · Lesson 6 · From samples to populations

    The law of large numbers, the central limit theorem and confidence intervals

    Beginner 75 min 5 interactive figures 4 videos Colab notebook

    Before this lesson: lesson 02 (expectation, variance and their rules), lesson 03 (the common distributions and moment generating functions) and lesson 04 (the variance of a sum). Time: about 75 minutes, plus the notebook.


    Why this matters #

    • Model evaluation. A test accuracy of 0.91 on 500 examples is a sample mean of 0/1 values. Its standard error, about 0.91⋅0.09/500≈0.013\sqrt{0.91 \cdot 0.09/500} \approx 0.013, tells you whether 0.91 is really better than another model's 0.90 (here: not clearly).
    • A/B tests. The conversion rate of each version of a web page is a sample mean. The usual z-test and its sample-size formula come from the central limit theorem (lesson 12).
    • Monte Carlo. A Monte Carlo estimate, such as an expected reward in reinforcement learning or an integral in physics, is a sample mean. Its error is σf/n\sigma_f/\sqrt n in any dimension (lesson 14).
    • Stochastic gradient descent. A minibatch gradient is the mean of BB per-example gradients. Its noise has standard deviation proportional to 1/B1/\sqrt B (optimization 05).
    • Error bars in papers. "Mean ± standard error over 5 seeds" is a confidence interval in disguise, and with only 5 values it needs Student's tt, not 1.96.

    1. Predict first #

    2. The picture #

    A statistic is a random variable #

    Take a population with mean μ\mu and standard deviation σ\sigma: for example, the time (in minutes) that visitors spend on a web page. Draw a sample of nn values and compute its mean xˉ\bar x. A second sample gives a different xˉ\bar x. The distribution of Xˉn\bar X_n over many repeated samples is its sampling distribution.

    Two facts describe it (we prove them in section 3):

    • its centre is the true mean: E[Xˉn]=μ\mathbb{E}[\bar X_n] = \mu;
    • its spread is the standard error SE⁡=σ/n\operatorname{SE} = \sigma/\sqrt n, much smaller than the spread σ\sigma of the data.
    Interactive figure: loading…

    Press "Draw 1,000" several times, then change nn and draw again. The top chart shows the population density (orange) and the latest sample (blue dots). The bottom chart is the histogram of all sample means (blue) with the normal curve N(μ,σ2/n)\mathcal N(\mu, \sigma^2/n) (orange). Notice: the observed SD of the means matches σ/n\sigma/\sqrt n, and for n≥20n \ge 20 the histogram is close to a bell even for the skewed population.

    The law of large numbers: running means settle down #

    Flip a coin again and again, and after each flip compute the fraction of heads so far. This running mean jumps a lot at first and then settles near 0.50.5. The band μ±2σ/n\mu \pm 2\sigma/\sqrt n shows how fast: its width falls like 1/n1/\sqrt n, so 100 times more flips give a 10 times narrower band.

    Interactive figure: loading…

    Press Play and watch the paths. Each blue line is the running mean of one sequence of flips; path 1 is highlighted. The orange band is μ±2σ/n\mu \pm 2\sigma/\sqrt n. Switch to 20 paths: at every nn, about 95% of the paths are inside the band. Switch to the die: the band is wider (the die has σ=1.71\sigma = 1.71, the coin σ=0.5\sigma = 0.5), but it shrinks at the same rate.

    The central limit theorem: sums look Gaussian #

    Why a bell shape? A Galton board shows the mechanism. Each ball hits RR pegs and goes left or right with probability 1/21/2 at each one. The final bin is the number of right steps: a sum of RR independent coin flips, so a Binomial(RR, 1/2) count. Many small independent pushes add up to a bell curve.

    Interactive figure: loading…

    Press "Drop 100", then "Drop 1,000 at once". The blue bars count the balls in each bin, the orange ticks are the expected counts from the binomial distribution, and the dashed orange curve is the normal curve with mean R/2R/2 and SD R/2\sqrt R/2. Change the number of rows: the bell gets wider like R\sqrt R.

    The same happens for sums of almost any variables, not only coin flips. To compare different nn, we standardise: subtract the mean nμn\mu of the sum and divide by its SD σn\sigma\sqrt n,

    Zn=Sn−nμσn=n (Xˉn−μ)σ.Z_n = \frac{S_n - n\mu}{\sigma\sqrt n} = \frac{\sqrt n\,(\bar X_n - \mu)}{\sigma}.

    ZnZ_n always has mean 0 and variance 1. The central limit theorem says that its shape approaches the standard normal N(0,1)\mathcal N(0, 1).

    Interactive figure: loading…

    Choose a base distribution and increase nn. The blue histogram shows 20,000 simulated values of ZnZ_n; the orange curve is N(0,1)\mathcal N(0, 1). The KS distance (the largest gap between the two CDFs) shrinks as nn grows. Notice: the uniform and the die look Gaussian already at n=5n = 5, the exponential needs about n=30n = 30, and Bernoulli(0.1) is still lumpy at n=30n = 30. Then choose Cauchy: the mean of nn Cauchy values never gets narrower.

    Quick check

    You quadruple the sample size, from n=100n = 100 to n=400n = 400. What happens to the standard error of the mean?

    3. Definitions and main results #

    Throughout, X1,…,XnX_1, \dots, X_n are i.i.d. (independent and identically distributed) with mean μ\mu and variance σ2\sigma^2, and

    Sn=∑i=1nXi,Xˉn=Snn.S_n = \sum_{i=1}^n X_i, \qquad \bar X_n = \frac{S_n}{n}.

    The mean and the standard error #

    Theorem. E[Xˉn]=μ\mathbb{E}[\bar X_n] = \mu and Var⁡(Xˉn)=σ2/n\operatorname{Var}(\bar X_n) = \sigma^2/n.

    Proof.

    1. By linearity of expectation, E[Sn]=∑iE[Xi]=nμ\mathbb{E}[S_n] = \sum_i \mathbb{E}[X_i] = n\mu, so E[Xˉn]=μ\mathbb{E}[\bar X_n] = \mu.
    2. The XiX_i are independent, so all covariances are 0 and the variances add (lesson 04): Var⁡(Sn)=nσ2\operatorname{Var}(S_n) = n\sigma^2.
    3. Var⁡(cY)=c2Var⁡(Y)\operatorname{Var}(cY) = c^2 \operatorname{Var}(Y) with c=1/nc = 1/n gives Var⁡(Xˉn)=nσ2/n2=σ2/n\operatorname{Var}(\bar X_n) = n\sigma^2/n^2 = \sigma^2/n. ■\blacksquare

    The standard error of the mean is SE⁡(Xˉn)=σ/n\operatorname{SE}(\bar X_n) = \sigma/\sqrt n. In practice σ\sigma is unknown, and we use the estimate s/ns/\sqrt n, where ss is the sample standard deviation (with n−1n - 1).

    Standard deviation vs standard error. The SD σ\sigma describes the spread of the data; it does not shrink with nn. The SE σ/n\sigma/\sqrt n describes the spread of the mean; it shrinks. Report the SD when you describe the data, and the SE (or a confidence interval) when you describe how well you know μ\mu.

    Step 2 used only that the variables are uncorrelated. If they are correlated, the variance is larger. With equal pairwise correlation ρ\rho, Var⁡(Xˉn)=σ2n(1+(n−1)ρ)\operatorname{Var}(\bar X_n) = \frac{\sigma^2}{n}\big(1 + (n - 1)\rho\big), which tends to ρσ2\rho\sigma^2, not to 0.

    Markov's and Chebyshev's inequalities #

    These two inequalities bound a tail probability with only a mean or a variance.

    Markov's inequality. If X≥0X \ge 0 and a>0a > 0, then

    P(X≥a)≤E[X]a.P(X \ge a) \le \frac{\mathbb{E}[X]}{a}.

    Proof.

    1. For every outcome, X≥a 1{X≥a}X \ge a\,\mathbf 1\{X \ge a\}. If X≥aX \ge a, the right side is a≤Xa \le X. Otherwise it is 0≤X0 \le X; this is where X≥0X \ge 0 is used.
    2. Take expectations of both sides: E[X]≥a P(X≥a)\mathbb{E}[X] \ge a\,P(X \ge a). ■\blacksquare

    Tight case. X∈{0,a}X \in \{0, a\} with P(X=a)=qP(X = a) = q. Then P(X≥a)=q=E[X]/aP(X \ge a) = q = \mathbb{E}[X]/a. Equality needs all the mass at 00 and at aa, so for most distributions Markov is loose.

    Chebyshev's inequality. If Var⁡(X)=σ2<∞\operatorname{Var}(X) = \sigma^2 < \infty and E[X]=μ\mathbb{E}[X] = \mu, then for every ε>0\varepsilon > 0

    P(∣X−μ∣≥ε)≤σ2ε2.P(|X - \mu| \ge \varepsilon) \le \frac{\sigma^2}{\varepsilon^2}.

    Proof. Apply Markov to the nonnegative variable Y=(X−μ)2Y = (X - \mu)^2 with threshold ε2\varepsilon^2. The events {∣X−μ∣≥ε}\{|X - \mu| \ge \varepsilon\} and {Y≥ε2}\{Y \ge \varepsilon^2\} are the same, so P(∣X−μ∣≥ε)≤E[Y]/ε2=σ2/ε2P(|X - \mu| \ge \varepsilon) \le \mathbb{E}[Y]/\varepsilon^2 = \sigma^2/\varepsilon^2. ■\blacksquare

    Tight case. X=μ±εX = \mu \pm \varepsilon with probability σ2/(2ε2)\sigma^2/(2\varepsilon^2) each, and X=μX = \mu otherwise (this needs σ2≤ε2\sigma^2 \le \varepsilon^2).

    With ε=kσ\varepsilon = k\sigma: P(∣X−μ∣≥kσ)≤1/k2P(|X - \mu| \ge k\sigma) \le 1/k^2. So at least 75% of any distribution lies within 2 SD of the mean, and at least 89% within 3 SD. For a Gaussian the true values are 95.4% and 99.7%.

    Modes of convergence #

    "Xˉn\bar X_n goes to μ\mu" can mean different things. Let Y1,Y2,…Y_1, Y_2, \dots and YY be random variables.

    • In probability: for every ε>0\varepsilon > 0, P(∣Yn−Y∣>ε)→0P(|Y_n - Y| > \varepsilon) \to 0. For large nn, a big error is unlikely.
    • Almost surely (a.s.): P(lim⁡nYn=Y)=1P(\lim_n Y_n = Y) = 1. Almost every single path converges.
    • In distribution: P(Yn≤y)→P(Y≤y)P(Y_n \le y) \to P(Y \le y) at every yy where the CDF of YY is continuous. Only the distributions get close, not the values.

    Almost surely implies in probability, and in probability implies in distribution. The converses are false in general.

    Two counterexamples
    • In distribution, not in probability. Let Y∼N(0,1)Y \sim \mathcal N(0, 1) and Yn=−YY_n = -Y for every nn. Each YnY_n has exactly the distribution of YY, so Yn→YY_n \to Y in distribution. But ∣Yn−Y∣=2∣Y∣|Y_n - Y| = 2|Y| does not shrink.
    • In probability, not almost surely. Let Yn∼Bernoulli(1/n)Y_n \sim \text{Bernoulli}(1/n) be independent. Then P(Yn>ε)=1/n→0P(Y_n > \varepsilon) = 1/n \to 0, so Yn→0Y_n \to 0 in probability. But ∑n1/n=∞\sum_n 1/n = \infty, so by the second Borel–Cantelli lemma Yn=1Y_n = 1 for infinitely many nn, with probability 1: no path converges.

    One exception: convergence in distribution to a constant implies convergence in probability.

    The law of large numbers #

    Theorem (weak law of large numbers, WLLN). If X1,X2,…X_1, X_2, \dots are i.i.d. with mean μ\mu and finite variance σ2\sigma^2, then Xˉn→μ\bar X_n \to \mu in probability: for every ε>0\varepsilon > 0, P(∣Xˉn−μ∣≥ε)→0P(|\bar X_n - \mu| \ge \varepsilon) \to 0.

    Proof.

    1. E[Xˉn]=μ\mathbb{E}[\bar X_n] = \mu and Var⁡(Xˉn)=σ2/n\operatorname{Var}(\bar X_n) = \sigma^2/n (theorem above).
    2. Chebyshev applied to Xˉn\bar X_n: P(∣Xˉn−μ∣≥ε)≤σ2nε2P(|\bar X_n - \mu| \ge \varepsilon) \le \dfrac{\sigma^2}{n\varepsilon^2}.
    3. For fixed ε\varepsilon, the right side goes to 0 as n→∞n \to \infty. ■\blacksquare

    What this proof uses, and what it does not give:

    • It needs only uncorrelated variables with equal mean and variance. For correlated data (time series, MCMC samples) the variance of the mean can stay large.
    • It needs a finite variance. The WLLN also holds with only a finite mean (Khinchin's theorem), with a different proof.
    • The strong law of large numbers says more: Xˉn→μ\bar X_n \to \mu almost surely, for i.i.d. variables with E∣X∣<∞\mathbb{E}|X| < \infty. Its proof is harder; we only state it.
    • Without a mean, there is no law of large numbers. For the Cauchy distribution (density 1/(π(1+x2))1/(\pi(1 + x^2))), E∣X∣=∞\mathbb{E}|X| = \infty. The mean of nn standard Cauchy variables is again standard Cauchy, for every nn. Averaging does not help at all.
    Quick check

    You average n=100n = 100 independent standard Cauchy variables. What is the distribution of the average?

    The central limit theorem #

    Theorem (CLT). Let X1,X2,…X_1, X_2, \dots be i.i.d. with mean μ\mu and variance 0<σ2<∞0 < \sigma^2 < \infty. Then

    Zn=n (Xˉn−μ)σ  ⟶  N(0,1)Z_n = \frac{\sqrt n\,(\bar X_n - \mu)}{\sigma} \;\longrightarrow\; \mathcal N(0, 1)

    in distribution, that is, P(Zn≤z)→Φ(z)P(Z_n \le z) \to \Phi(z) for every zz, where Φ\Phi is the standard normal CDF.

    How to use it. For large nn, approximately

    Xˉn≈N(μ,σ2n),Sn≈N(nμ, nσ2).\bar X_n \approx \mathcal N\Big(\mu, \frac{\sigma^2}{n}\Big), \qquad S_n \approx \mathcal N(n\mu,\, n\sigma^2).

    LLN vs CLT. The LLN says where Xˉn\bar X_n goes (μ\mu). The CLT says how the error looks on the scale σ/n\sigma/\sqrt n: Gaussian. The data do not become Gaussian; only the mean (or the sum) does.

    Proof sketch with moment generating functions

    Assume that the MGF M(s)=E[esX]M(s) = \mathbb{E}[e^{sX}] is finite near 0 (lesson 03). Standardise so that μ=0\mu = 0 and σ=1\sigma = 1.

    1. Zn=∑iXi/nZ_n = \sum_i X_i/\sqrt n. The XiX_i are independent, so the MGF of the sum is the product: MZn(t)=M(t/n)nM_{Z_n}(t) = M(t/\sqrt n)^n.
    2. Taylor expansion at 0: M(s)=1+s E[X]+s22E[X2]+o(s2)=1+s22+o(s2)M(s) = 1 + s\,\mathbb{E}[X] + \tfrac{s^2}{2}\mathbb{E}[X^2] + o(s^2) = 1 + \tfrac{s^2}{2} + o(s^2).
    3. Hence ln⁡MZn(t)=nln⁡(1+t22n+o(1/n))→t22\ln M_{Z_n}(t) = n \ln\big(1 + \tfrac{t^2}{2n} + o(1/n)\big) \to \tfrac{t^2}{2}, using ln⁡(1+u)=u+o(u)\ln(1 + u) = u + o(u).
    4. et2/2e^{t^2/2} is the MGF of N(0,1)\mathcal N(0, 1). If MGFs converge on an interval around 0, the distributions converge (Curtiss' continuity theorem). ■\blacksquare

    With only a finite variance the MGF may not exist. The same steps then work with the characteristic function E[eitX]\mathbb{E}[e^{itX}], which always exists, and Lévy's continuity theorem.

    How fast? The Berry–Esseen theorem

    If also ρ=E∣X−μ∣3<∞\rho = \mathbb{E}|X - \mu|^3 < \infty, then for every nn

    sup⁡z∣P(Zn≤z)−Φ(z)∣≤C ρσ3n,C≤0.4748 (Shevtsova, 2011).\sup_z \big|P(Z_n \le z) - \Phi(z)\big| \le \frac{C\,\rho}{\sigma^3\sqrt n}, \qquad C \le 0.4748 \text{ (Shevtsova, 2011)}.

    The error of the normal approximation is O(1/n)O(1/\sqrt n), and this rate cannot be improved in general. For a fair coin, P(Sn=n/2)≈2/(πn)P(S_n = n/2) \approx \sqrt{2/(\pi n)}: the CDF of ZnZ_n jumps by about 1/n1/\sqrt n at 0, and the continuous Φ\Phi cannot follow the jump.

    For Exp(1) data the sum is Gamma(nn, 1), so the exact distance can be computed:

    nn 1 4 16 64 256
    sup⁡z∣P(Zn≤z)−Φ(z)∣\sup_z \lvert P(Z_n \le z) - \Phi(z)\rvert 0.159 0.067 0.033 0.017 0.008
    n×\sqrt n \times distance 0.159 0.133 0.133 0.133 0.133
    Berry–Esseen bound 1.146 0.573 0.287 0.143 0.072

    From n=4n = 4 on, n\sqrt n times the distance is constant, as the theorem predicts. The bound is valid but about 8 times too large here. Skewed data and rare events (large ρ/σ3\rho/\sigma^3) need a much larger nn.

    From random walks to Brownian motion

    Let Xi=±1X_i = \pm 1 with probability 1/21/2 each, and take steps of size Δt\sqrt{\Delta t} every Δt\Delta t time units. At time tt you have made t/Δtt/\Delta t steps, so the position has variance (t/Δt)⋅Δt=t(t/\Delta t) \cdot \Delta t = t. As Δt→0\Delta t \to 0, the CLT makes the position at time tt a N(0,t)\mathcal N(0, t) variable, and the whole path converges to Brownian motion (Donsker's theorem). This is why noise in stochastic differential equations scales like Δt\sqrt{\Delta t}, not Δt\Delta t (ML for science 12).

    When the CLT fails or is slow.

    • Infinite variance: Cauchy, or Pareto with tail index below 2. There is no Gaussian limit.
    • Strong dependence: a time series with long memory, or samples that share a common error.
    • Skewness and rare events: the approximation is poor until nn is large. For a proportion, a common rule is to want np≥10np \ge 10 and n(1−p)≥10n(1 - p) \ge 10.

    Bound or approximation? For 100 fair coin flips, "60 or more heads" means Xˉn−0.5≥0.1\bar X_n - 0.5 \ge 0.1:

    method value kind
    Chebyshev, Var⁡(Xˉn)/ε2=0.0025/0.01\operatorname{Var}(\bar X_n)/\varepsilon^2 = 0.0025/0.01 0.25 bound (always valid)
    CLT, P(Z≥2)P(Z \ge 2) with Z=0.1/0.05Z = 0.1/0.05 0.0228 approximation
    exact binomial, P(S100≥60)P(S_{100} \ge 60) 0.0284 exact

    Chebyshev is valid but 9 times too large. The CLT is close, but it is not a bound: here it is below the truth. Lesson 07 builds bounds that are valid and much tighter.

    Quick check

    A coin gives 61 heads in 100 flips. If the coin is fair, how many standard errors is p^=0.61\hat p = 0.61 away from 0.50.5?

    Confidence intervals #

    Known σ\sigma. By the CLT, P(∣Xˉn−μ∣≤1.96 σ/n)≈0.95P\big(|\bar X_n - \mu| \le 1.96\,\sigma/\sqrt n\big) \approx 0.95, because P(∣Z∣≤1.96)=0.95P(|Z| \le 1.96) = 0.95 for Z∼N(0,1)Z \sim \mathcal N(0, 1). Read the event the other way round: ∣Xˉn−μ∣≤1.96 σ/n|\bar X_n - \mu| \le 1.96\,\sigma/\sqrt n is the same as μ∈[Xˉn−1.96 σ/n, Xˉn+1.96 σ/n]\mu \in \big[\bar X_n - 1.96\,\sigma/\sqrt n,\ \bar X_n + 1.96\,\sigma/\sqrt n\big]. So

    Xˉn±1.96 σn\bar X_n \pm 1.96\,\frac{\sigma}{\sqrt n}

    is an (approximate) 95% confidence interval for μ\mu. For a level 1−α1 - \alpha, replace 1.96 by the quantile z1−α/2z_{1 - \alpha/2} (1.645 for 90%, 2.576 for 99%).

    What "95%" means. A confidence interval is a procedure: a rule that turns data into an interval. Its coverage is the probability, before you see the data, that the random interval contains the fixed number μ\mu. "95%" means: if you repeat the whole experiment many times, about 95% of the intervals contain μ\mu. After you computed one interval, say [2.1,3.4][2.1, 3.4], nothing is random any more: μ\mu is in it or not. In the frequentist view μ\mu is a fixed number, so "P(2.1≤μ≤3.4)=0.95P(2.1 \le \mu \le 3.4) = 0.95" is not a correct statement. (A Bayesian credible interval (lesson 10) does make such a statement, given a prior.)

    Interactive figure: loading…

    Press "Draw 100 intervals" a few times. Each bar is one 95% interval from a new data set; it is blue if it contains the true mean (orange line) and magenta if it misses. Then press "Draw 10,000" and read the coverage. With z=1.96z = 1.96 and the estimated SD at n=10n = 10, the coverage is about 0.92, not 0.95. Switch to tt: it goes back to 0.95. Then switch to skewed data: even tt gives only about 0.90 at n=10n = 10.

    Unknown σ\sigma: Student's tt. In practice we replace σ\sigma by the sample SD ss. This adds uncertainty, and the interval with 1.96 becomes too short for small nn. For normal data the exact distribution is known:

    T=Xˉn−μs/n∼tn−1,T = \frac{\bar X_n - \mu}{s/\sqrt n} \sim t_{n-1},

    Student's tt distribution with n−1n - 1 degrees of freedom. It is symmetric like N(0,1)\mathcal N(0, 1), but with heavier tails; as n→∞n \to \infty it approaches N(0,1)\mathcal N(0, 1). The t-interval is

    xˉ±tn−1, 0.975 sn.\bar x \pm t_{n-1,\,0.975}\,\frac{s}{\sqrt n}.

    Some 97.5% quantiles: t1=12.71t_{1} = 12.71, t4=2.776t_{4} = 2.776, t9=2.262t_{9} = 2.262, t29=2.045t_{29} = 2.045, t99=1.984t_{99} = 1.984, and z=1.960z = 1.960.

    For normal data the t-interval has coverage exactly 0.95 for every nn. For other data it is approximate. A simulation with 200,000 data sets per row (Monte Carlo error about ±0.001) gives:

    data nn t-interval 1.96⋅s/n1.96 \cdot s/\sqrt n
    normal 10 0.950 0.918
    normal 30 0.950 0.940
    exponential (skewed) 10 0.900 0.869
    exponential (skewed) 30 0.926 0.917
    exponential (skewed) 100 0.942 0.939

    Two lessons from the table. With small nn, use tt, not 1.96. And with skewed data, even tt under-covers until nn is large; the bootstrap (lesson 12) is one fix.

    Quick check

    From one data set you get the 95% confidence interval [2.1,3.4][2.1, 3.4] for μ\mu. Which statement is correct?

    Intervals for a proportion #

    For 0/1 data (clicked or not, correct or not), Xˉn=p^\bar X_n = \hat p is the fraction of ones, and σ2=p(1−p)\sigma^2 = p(1 - p). The Wald interval plugs p^\hat p into the SE:

    p^±1.96p^(1−p^)n.\hat p \pm 1.96\sqrt{\frac{\hat p(1 - \hat p)}{n}}.

    It fails for small nn or for pp near 0 or 1. Example: n=50n = 50 and p=0.02p = 0.02. With probability 0.9850≈0.360.98^{50} \approx 0.36 you see no success at all; then p^=0\hat p = 0 and the interval is [0,0][0, 0], which misses pp. The exact coverage (a sum over the binomial PMF) is only 0.635, not 0.95.

    The Wilson interval

    Instead of plugging p^\hat p into the SE, solve ∣p^−p∣≤zp(1−p)/n|\hat p - p| \le z\sqrt{p(1 - p)/n} for pp (a quadratic inequality in pp). With z=1.96z = 1.96, the solution is the Wilson interval

    p^+z22n1+z2n  ±  z1+z2np^(1−p^)n+z24n2.\frac{\hat p + \frac{z^2}{2n}}{1 + \frac{z^2}{n}} \;\pm\; \frac{z}{1 + \frac{z^2}{n}}\sqrt{\frac{\hat p(1 - \hat p)}{n} + \frac{z^2}{4n^2}}.

    Its centre is pulled towards 1/21/2, and it is never [0,0][0, 0]. Exact coverage at n=50n = 50:

    pp Wald Wilson
    0.5 0.935 0.935
    0.24 0.919 0.934
    0.02 0.635 0.922

    Use Wilson (or the exact Clopper–Pearson interval) by default. In Python: statsmodels.stats.proportion.proportion_confint(k, n, method="wilson").

    Error bars for Monte Carlo, and the delta method #

    Monte Carlo. To estimate I=E[f(X)]I = \mathbb{E}[f(X)], draw X1,…,XnX_1, \dots, X_n and average: I^=1n∑if(Xi)\hat I = \frac1n \sum_i f(X_i). This is a sample mean, so the CLT gives the error bar

    I^±1.96 sfn,\hat I \pm 1.96\,\frac{s_f}{\sqrt n},

    where sfs_f is the sample SD of the values f(Xi)f(X_i). The rate 1/n1/\sqrt n does not depend on the dimension of XX; only σf\sigma_f does. One more correct digit costs 100 times more samples.

    The delta method. Often we need a function of a mean: the log-odds of a click rate, the ratio of two means, Xˉ2\bar X^2. If n(Xˉn−μ)→N(0,σ2)\sqrt n(\bar X_n - \mu) \to \mathcal N(0, \sigma^2) and gg is differentiable at μ\mu with g′(μ)≠0g'(\mu) \ne 0, then

    n (g(Xˉn)−g(μ))  ⟶  N(0, g′(μ)2σ2),\sqrt n\,\big(g(\bar X_n) - g(\mu)\big) \;\longrightarrow\; \mathcal N\big(0,\ g'(\mu)^2\sigma^2\big),

    so

    SE⁡(g(Xˉn))≈∣g′(μ)∣ σn.\operatorname{SE}\big(g(\bar X_n)\big) \approx |g'(\mu)|\,\frac{\sigma}{\sqrt n}.

    Why: the first-order Taylor expansion g(Xˉn)≈g(μ)+g′(μ)(Xˉn−μ)g(\bar X_n) \approx g(\mu) + g'(\mu)(\bar X_n - \mu) is a linear function of Xˉn\bar X_n, and a linear function multiplies the SD by ∣g′(μ)∣|g'(\mu)|. The remainder is of smaller order than 1/n1/\sqrt n. In dd dimensions the variance is ∇g(μ)⊤Σ ∇g(μ)/n\nabla g(\mu)^\top \Sigma\, \nabla g(\mu)/n. The delta method is first order: it ignores the bias and skewness that a curved gg adds.

    Answers to section 1. (1) About 0.03: the exact value is 0.0284. The CLT gives P(Z≥2)=0.0228P(Z \ge 2) = 0.0228, since 60 heads is (0.6−0.5)/0.05=2(0.6 - 0.5)/0.05 = 2 standard errors above the mean. Chebyshev gives only the bound 0.25. (2) No. The 95% describes how often the procedure covers μ\mu in repeated experiments. A computed interval contains the fixed μ\mu or not. (3) 100 times more. The width of the interval is proportional to 1/n1/\sqrt n.

    4. Worked example #

    (a) A t-interval for exam scores. Eight students get the scores 72,85,64,90,78,69,81,7772, 85, 64, 90, 78, 69, 81, 77. We want a 95% confidence interval for the mean score μ\mu of all students who take this exam.

    1. Mean: xˉ=616/8=77\bar x = 616/8 = 77.
    2. Deviations: −5,8,−13,13,1,−8,4,0-5, 8, -13, 13, 1, -8, 4, 0. Squares: 25,64,169,169,1,64,16,025, 64, 169, 169, 1, 64, 16, 0, with sum 508508.
    3. Sample variance: s2=508/7=72.57s^2 = 508/7 = 72.57, so s=8.52s = 8.52.
    4. Standard error: s/8=8.52/2.828=3.01s/\sqrt 8 = 8.52/2.828 = 3.01.
    5. n−1=7n - 1 = 7 degrees of freedom: t7, 0.975=2.365t_{7,\,0.975} = 2.365.
    6. Half-width: 2.365⋅3.01=7.122.365 \cdot 3.01 = 7.12. Interval: 77±7.12=[69.9, 84.1]77 \pm 7.12 = [69.9,\ 84.1].

    With 1.96 instead of 2.365 the half-width would be 5.905.90: the interval would be 17% too short, and its coverage for normal data would be below 0.95.

    (b) The click rate of a button. A web page shows a new button to n=50n = 50 visitors; 1212 click it.

    1. Estimate: p^=12/50=0.24\hat p = 12/50 = 0.24.
    2. Standard error: p^(1−p^)/n=0.24⋅0.76/50\sqrt{\hat p(1 - \hat p)/n} = \sqrt{0.24 \cdot 0.76/50}, and this is 0.003648=0.0604\sqrt{0.003648} = 0.0604.
    3. Half-width: 1.96⋅0.0604=0.1181.96 \cdot 0.0604 = 0.118. Wald interval: 0.24±0.118=[0.122, 0.358]0.24 \pm 0.118 = [0.122,\ 0.358].
    4. Wilson interval (formula above): [0.143, 0.374][0.143,\ 0.374], slightly shifted towards 1/21/2.

    Delta method on the log-odds. Logistic regression works with θ=ln⁡p1−p\theta = \ln\frac{p}{1 - p}. With g(p)=ln⁡p1−pg(p) = \ln\frac{p}{1 - p} we have g′(p)=1p(1−p)g'(p) = \frac{1}{p(1 - p)}, so

    SE⁡(θ^)≈1p^(1−p^)p^(1−p^)n=1n p^(1−p^),\operatorname{SE}(\hat\theta) \approx \frac{1}{\hat p(1 - \hat p)}\sqrt{\frac{\hat p(1 - \hat p)}{n}} = \frac{1}{\sqrt{n\,\hat p(1 - \hat p)}},

    and with the numbers

    SE⁡(θ^)≈150⋅0.1824=0.331.\operatorname{SE}(\hat\theta) \approx \frac{1}{\sqrt{50 \cdot 0.1824}} = 0.331.

    θ^=ln⁡(0.24/0.76)=−1.153\hat\theta = \ln(0.24/0.76) = -1.153. The half-width is 1.96⋅0.331=0.6491.96 \cdot 0.331 = 0.649, so the 95% interval is [−1.802, −0.504][-1.802,\ -0.504]. Map both ends back with the logistic function p=1/(1+e−θ)p = 1/(1 + e^{-\theta}): [0.142, 0.377][0.142,\ 0.377]. This interval is not symmetric around 0.24, and it always stays inside (0,1)(0, 1).

    How many visitors for ±0.05\pm 0.05? We want 1.96p(1−p)/n≤0.051.96\sqrt{p(1 - p)/n} \le 0.05. The worst case is p=1/2p = 1/2, so n≥(1.96⋅0.5/0.05)2=384.2n \ge (1.96 \cdot 0.5/0.05)^2 = 384.2: 385 visitors. This is a CLT approximation. Chebyshev gives a guarantee for P(∣p^−p∣≥0.05)≤0.05P(|\hat p - p| \ge 0.05) \le 0.05, but it needs n≥0.25/(0.05⋅0.052)=2000n \ge 0.25/(0.05 \cdot 0.05^2) = 2000. Lesson 07 finds a guarantee with 738.

    (c) What the i.i.d. assumption hides. Suppose the 50 visitors come in groups of friends who influence each other, and every pair of clicks has correlation ρ=0.2\rho = 0.2. Then Var⁡(p^)=σ250(1+49⋅0.2)=0.216 σ2\operatorname{Var}(\hat p) = \frac{\sigma^2}{50}(1 + 49 \cdot 0.2) = 0.216\,\sigma^2, against 0.02 σ20.02\,\sigma^2 for independent visitors. The true SE is 10.8≈3.3\sqrt{10.8} \approx 3.3 times larger than the formula says.

    5. In code #

    NumPy and SciPy check the worked example. The notebook for this lesson has these cells and the exercises.

    import numpy as np
    from scipy import stats
    
    # (a) exam scores: t-interval for the mean
    x = np.array([72, 85, 64, 90, 78, 69, 81, 77.])
    n, m, s = len(x), x.mean(), x.std(ddof=1)
    q = stats.t.ppf(0.975, n - 1)
    print(m, round(s, 2), round(s / np.sqrt(n), 2), round(q, 3))        # 77.0 8.52 3.01 2.365
    print(np.round(stats.t.interval(0.95, n - 1, loc=m, scale=s / np.sqrt(n)), 2))   # [69.88 84.12]
    
    # (b) 12 clicks out of 50 visitors: Wald interval and the delta method on the log-odds
    k, n = 12, 50
    p = k / n
    se = np.sqrt(p * (1 - p) / n)
    print(round(se, 4), np.round([p - 1.96 * se, p + 1.96 * se], 3))     # 0.0604 [0.122 0.358]
    theta, se_t = np.log(p / (1 - p)), 1 / np.sqrt(n * p * (1 - p))
    lo, hi = theta - 1.96 * se_t, theta + 1.96 * se_t
    print(round(theta, 3), round(se_t, 3), np.round(1 / (1 + np.exp(-np.array([lo, hi]))), 3))
    # -1.153 0.331 [0.142 0.377]
    
    # (c) Monte Carlo estimate of pi with a CLT error bar
    rng = np.random.default_rng(1)
    u = rng.random((10_000, 2))
    f = 4.0 * (np.sum(u**2, axis=1) <= 1)
    print(f.mean(), round(1.96 * f.std(ddof=1) / np.sqrt(len(f)), 3))    # 3.1564 0.032  (one run)
    
    # (d) coverage of the Wald interval for a rare event, exact
    z = stats.norm.ppf(0.975)
    k = np.arange(51); ph = k / 50
    cover = np.abs(ph - 0.02) <= z * np.sqrt(ph * (1 - ph) / 50)
    print(round(stats.binom.pmf(k, 50, 0.02)[cover].sum(), 3))            # 0.635, not 0.95
    

    In (c) the error bar ±0.032\pm 0.032 contains π=3.1416\pi = 3.1416. The SD of one value of ff is 4(π/4)(1−π/4)=1.6424\sqrt{(\pi/4)(1 - \pi/4)} = 1.642, so every run of 10,000 points has a half-width close to 1.96⋅1.642/100=0.0321.96 \cdot 1.642/100 = 0.032.

    6. Where it shows up #

    • Comparing models. Two classifiers with test accuracies 0.912 and 0.905 on 2,000 examples differ by 0.007. Each accuracy has a standard error of about 0.006, so the difference is not clear. Report intervals, and use a paired test on the same examples (lesson 11).
    • Seeds in deep learning. Results over 3–5 random seeds are a tiny sample. Use the tt quantile (t4=2.776t_{4} = 2.776 for 5 seeds), not 1.96, or report all values.
    • A/B tests. The sample size per arm, n≈16σ2/Δ2n \approx 16\sigma^2/\Delta^2 for a difference Δ\Delta at 5% level and 80% power, comes from the CLT (lesson 12).
    • Monte Carlo and MCMC. Error bars 1.96 sf/n1.96\,s_f/\sqrt n for integrals and expected rewards; for correlated MCMC samples, nn is replaced by an effective sample size (lesson 15).
    • Ensembles and averaging in science. Averaging nn independent sensor readings or ensemble members reduces the noise by n\sqrt n, but a shared error (the same model physics, the same calibration) does not average out.
    • Batch size in SGD. The gradient noise falls like 1/B1/\sqrt B, which is behind heuristics that scale the learning rate with the batch size.

    7. Common mistakes #

    • Mixing up SD and SE. The SD σ\sigma of the data does not shrink with nn; the SE σ/n\sigma/\sqrt n of the mean does. Error bars that use the SD are not confidence intervals for the mean.
    • "The data become Gaussian for large nn." No. The CLT is about the distribution of the mean (or sum). A histogram of 10,000 exponential values is still exponential.
    • "There is a 95% probability that μ\mu is in this interval." For a computed confidence interval this is wrong; the 95% belongs to the procedure.
    • Using 1.96 with a small sample. With n=10n = 10 and estimated ss, the coverage is about 0.92. Use tn−1t_{n-1}.
    • Using the Wald interval for rare events. At n=50n = 50, p=0.02p = 0.02 its coverage is 0.635. Use Wilson.
    • Treating the CLT as a bound. It is an approximation with error O(1/n)O(1/\sqrt n); for 100 coin flips it was below the exact tail. Use a concentration inequality when you need a guarantee.
    • Forgetting dependence. Var⁡(Xˉn)=σ2/n\operatorname{Var}(\bar X_n) = \sigma^2/n needs uncorrelated data. Time series, MCMC samples, users in the same group, or repeated measurements of one patient break it.
    • Markov on a variable that can be negative. Markov needs X≥0X \ge 0. Apply it to ∣X∣|X| or (X−μ)2(X - \mu)^2 instead.

    8. Check yourself #

    1. X≥0X \ge 0 and E[X]=3\mathbb{E}[X] = 3. Bound P(X≥12)P(X \ge 12). Can the bound be reached?
      AnswerMarkov gives 3/12=0.253/12 = 0.25. Yes: X∈{0,12}X \in \{0, 12\} with P(X=12)=1/4P(X = 12) = 1/4 has mean 3 and P(X≥12)=0.25P(X \ge 12) = 0.25.
    2. A die is rolled 100 times. Use Chebyshev to bound P(∣Xˉ−3.5∣≥0.5)P(|\bar X - 3.5| \ge 0.5).
      AnswerOne roll has σ2=35/12=2.917\sigma^2 = 35/12 = 2.917. So Var⁡(Xˉ)=0.02917\operatorname{Var}(\bar X) = 0.02917 and P≤0.02917/0.25=0.117P \le 0.02917/0.25 = 0.117. (The CLT gives about 2P(Z≥0.5/0.171)=2P(Z≥2.93)≈0.0032P(Z \ge 0.5/0.171) = 2P(Z \ge 2.93) \approx 0.003.)
    3. 25 measurements have xˉ=10.4\bar x = 10.4 and s=2.0s = 2.0. Give a 95% confidence interval for μ\mu (t24, 0.975=2.064t_{24,\,0.975} = 2.064).
      AnswerSE⁡=2.0/5=0.4\operatorname{SE} = 2.0/5 = 0.4, so 10.4±2.064⋅0.4=10.4±0.83=[9.57,11.23]10.4 \pm 2.064 \cdot 0.4 = 10.4 \pm 0.83 = [9.57, 11.23].
    4. A Monte Carlo estimate with n=10,000n = 10{,}000 samples has the error bar ±0.02\pm 0.02. How many samples give ±0.005\pm 0.005?
      AnswerThe error bar must shrink by 4, so nn must grow by 42=164^2 = 16: 160,000 samples.
    5. Xˉ\bar X is the mean of n=100n = 100 i.i.d. draws with μ=2\mu = 2 and σ=1\sigma = 1. Approximate the SD of Xˉ2\bar X^2.
      AnswerDelta method with g(x)=x2g(x) = x^2, g′(2)=4g'(2) = 4: SD⁡(Xˉ2)≈4⋅1/100=0.4\operatorname{SD}(\bar X^2) \approx 4 \cdot 1/\sqrt{100} = 0.4.
    6. Independent Yn∼Bernoulli(1/n2)Y_n \sim \text{Bernoulli}(1/n^2). Does Yn→0Y_n \to 0 in probability? Almost surely?
      AnswerBoth. In probability: P(Yn=1)=1/n2→0P(Y_n = 1) = 1/n^2 \to 0. Almost surely: ∑n1/n2<∞\sum_n 1/n^2 < \infty, so by the first Borel–Cantelli lemma only finitely many YnY_n equal 1, with probability 1. Compare Bernoulli(1/n)\text{Bernoulli}(1/n), which fails almost surely.
    Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
    1. What is the standard error of the mean of nn i.i.d. values with SD σ\sigma?

    2. State Chebyshev's inequality.

    3. State the weak law of large numbers and the tool of its short proof.

    4. State the central limit theorem with its assumptions.

    5. Give the 95% confidence interval for a mean with unknown σ\sigma.

    6. What does the "95%" of a confidence interval mean?

    7. Why does the Wald interval fail for n=50n = 50, p=0.02p = 0.02?

    8. A Monte Carlo error bar is too wide by a factor 10. How many more samples do you need?

    9. State the delta method for g(Xˉn)g(\bar X_n).

    10. Why is the mean of Cauchy samples useless?

    Open the review →

    9. Interview angle #

    • "State the CLT. What can break it?" — i.i.d. with finite variance; then the standardised mean tends to N(0,1)\mathcal N(0, 1). It breaks with infinite variance (Cauchy, heavy tails), strong dependence, and it is slow for skewed data and rare events.
    • "LLN versus CLT?" — The LLN says Xˉn→μ\bar X_n \to \mu. The CLT says the error Xˉn−μ\bar X_n - \mu is approximately N(0,σ2/n)\mathcal N(0, \sigma^2/n).
    • "How many samples to estimate a probability within ±1% at 95%?" — Worst case p=1/2p = 1/2: (1.96⋅0.5/0.01)2=9604(1.96 \cdot 0.5/0.01)^2 = 9604.
    • "What does a 95% confidence interval mean?" — The procedure covers the true value in 95% of repeated experiments. It is not a 95% probability for the one interval you computed.
    • "Why is the Monte Carlo error independent of the dimension?" — The estimate is a sample mean with SE σf/n\sigma_f/\sqrt n; the dimension enters only through σf\sigma_f.
    • "Your model gets 0.80 accuracy on 400 test examples. Error bar?" — 0.8⋅0.2/400=0.02\sqrt{0.8 \cdot 0.2/400} = 0.02, so about 0.80±0.040.80 \pm 0.04 at 95%.

    10. Practice #

    The notebook for this lesson has five exercises, each with a check cell and a folded solution:

    1. Running means of coin flips, and the fraction of paths inside the band μ±2σ/n\mu \pm 2\sigma/\sqrt n.
    2. The KS distance of standardised exponential means to N(0,1)\mathcal N(0, 1), and why n⋅D\sqrt n \cdot D stays constant.
    3. A t-based mean_confidence_interval and its coverage for normal and skewed data.
    4. Wald and Wilson intervals for a proportion, and their exact coverage at p=0.02p = 0.02.
    5. A Monte Carlo estimate of π\pi with a CLT error bar, and the coverage of that error bar.

    11. Watch and read #

    The CLT built from sums of dice, with the Galton board and a careful list of the assumptions.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The weak law from Chebyshev and the MGF proof of the CLT, the same route as section 3.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    A short visual answer to "what does a 95% confidence interval mean".
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    The difference between the standard deviation of the data and the standard error of the mean.
    Open on YouTube · the player loads from youtube-nocookie.com when you press play
    • Joseph Blitzstein and Jessica Hwang, Introduction to Probability, 2nd edition (free PDF from the authors): §10.1 (inequalities), §10.2 (law of large numbers), §10.3 (central limit theorem), §10.4 (chi-square and Student's tt).
    • Larry Wasserman, All of Statistics: ch. 5 (convergence of random variables, the delta method) and ch. 6 (confidence intervals).
    • Art Owen, Monte Carlo theory, methods and examples, ch. 2: the error of simple Monte Carlo, estimating probabilities, and intervals for rare events.