The idea: one sample, one interval

We want the average height μ of all adults, but we can only measure a sample of n people. Their mean x̄ is our best guess, yet a different sample would give a different x̄. A confidence interval turns that one guess into a range, x̄ ± a margin, wide enough that the recipe catches the true μ in a chosen share of samples, the confidence level.

Top: the population bell of heights from 140 to 200 cm, centred on the true mean 170, with 25 black ticks for the heights in one sample. Middle: their mean x-bar = 171.72. Bottom: the green interval from 167.80 to 175.64, which crosses the dashed line at mu = 170, with the formula x-bar plus or minus 1.96 times 10 over root 25 = 171.72 plus or minus 3.92.
One sample of 25 heights → x̄ = 171.72 → the 95% interval [167.80, 175.64], which happens to contain μ = 170.

The page models heights as normal with μ = 170 cm and σ = 10 cm. The canvas knows μ so it can colour each interval, green if it contains μ and red if it misses; in real life μ is exactly what we don't know. The samples come from a seeded random generator, so every demo gives the same numbers each time.

The recipe

With σ known, the interval for a sample of n values is

x̄ ± z* · σ / √n          SE = σ / √n,   E = z* · SE

The standard error SE is how much x̄ wobbles from sample to sample, and the margin of error E is z* standard errors. For n = 25 and 95%: SE = 10/5 = 2 and E = 1.96 × 2 = 3.92, so every interval is 7.84 cm wide. Sample #1 has x̄ = 171.72, giving [167.80, 175.64] (Demo: one sample → one interval).

Why about 95% of the intervals catch μ

By the central limit theorem, x̄ over many samples is normal with mean μ and standard deviation SE. 95% of a normal lies within 1.96 standard deviations of its centre, so in 95% of samples x̄ lands within E of μ. And "x̄ is within E of μ" is the same statement as "μ is within E of x̄", which is exactly "the interval contains μ". On the canvas the light blue band is μ ± E: an interval turns red exactly when its x̄ dot falls outside the band.

Top: the bell of x-bar over many samples, N(170, 2 squared), with the middle 95% between 166.08 and 173.92 shaded and 2.5% in each tail. Below, a band over the same range and eight intervals: seven have their x-bar inside the band and cross mu = 170 (green, covers mu); the one with x-bar = 175.29 lies outside the band and misses (red).
An interval misses μ exactly when its x̄ lands in one of the two 2.5% tails, outside μ ± E.

With seed 2475, 19 of the first 20 intervals contain μ; sample #11 (x̄ = 175.29, 5.29 cm above μ) misses (Demo: 20 intervals, one misses). After 100 samples, 95 contain μ and #11, #43, #49, #64 and #78 miss (Demo: 100 intervals → 95 cover μ). Over a short run the count wanders: 18 or 20 out of 20 is just as normal as 19.

Bigger n: narrower intervals

SE = σ/√n shrinks as the sample grows, so the interval narrows like 1/√n: four times the data halves the width. The coverage does not change, because the margin shrinks exactly as fast as the wobble of x̄ (Demo: bigger n → narrower: 19 of 20 cover at n = 5 and at n = 100, and the narrow n = 100 rows sit under the faded wide n = 5 rows).

nSE = 10/√n95% width 2E
54.4717.53 cm
103.1612.40 cm
252.007.84 cm
1001.003.92 cm

Higher confidence: wider intervals

z* is the number with the chosen share of the standard normal between −z* and z*. Asking to miss less often means a bigger z* and a wider interval (Demo: 80% vs 99% confidence: at 80%, 16 of 20 intervals contain μ; at 99%, all 20 do, but each is twice as wide).

Levelz*MissesWidth at n = 25
80%1.281 in 55.13 cm
90%1.6451 in 106.58 cm
95%1.961 in 207.84 cm
99%2.5761 in 10010.30 cm
Left: at n = 25 the interval widths for 80, 90, 95 and 99% confidence are 5.13, 6.58, 7.84 and 10.30 cm. Right: at 95% the widths for n = 5, 10, 25 and 100 are 17.53, 12.40, 7.84 and 3.92 cm.
More confidence costs width; more data buys it back: width = 2 · z* · σ/√n.

So precision and confidence pull against each other for a fixed n. The only way to get a narrow interval and a high level is a bigger sample: to halve the width at the same level, take four times as many people.

What "95% confidence" means

The 95% belongs to the method. Before the sample is drawn, the recipe has a 95% chance to produce an interval that contains μ. After it is drawn, μ is a fixed number and the interval is fixed too: it either contains μ or it does not. Saying "μ is in [167.80, 175.64] with probability 95%" mixes the two up; a probability for μ itself needs a prior, the Bayesian view (a credible interval).

The same intervals decide A/B tests: if the 95% interval for the difference between two variants excludes 0, the difference is significant at the 5% level. See also Normal Distribution, z-Scores and Confidence Intervals for the z table behind z*, and Monte Carlo estimation of π, where the error of an estimate also shrinks like 1/√n.

What the page leaves out

  • Unknown σ: with the sample standard deviation s in place of σ, z* is replaced by Student's t with n − 1 degrees of freedom (2.78 for n = 5, 2.06 for n = 25), so small samples get wider intervals.
  • Intervals for proportions (p̂ ± z*·√(p̂(1 − p̂)/n)), differences of means and regression slopes.
  • Skewed or heavy-tailed data at small n, and the bootstrap, which builds intervals by resampling.
  • One-sided intervals and the link to hypothesis tests and p-values.
  • Bayesian credible intervals, which do give a probability for μ.