Skip to content

Confidence intervals

An interval built from a sample catches the true value in about 95 of every 100 samples; the true value never moves, the intervals do.

Helps to know first
This topic

First, the picture

A true value sits still. A hundred samples each throw an interval around their own estimate. Watch how many catch it.

Most intervals catch the truth

100 samples of 25 from a bell with mean 10 and spread 2, each with its interval (seed 6400).

The true mean is 10, the line that never moves. Each run draws 25 values and builds a 95 % interval from them.

level
95 %
contain the true mean
0 of 0

Predict first. Of 100 intervals at 95 %, how many will contain the true mean?

0.00 / 10.20 s
Describe this picture

A vertical line, labelled “true mean 10”, marks the true mean of a bell-shaped population with standard deviation 2; it never moves. Each run draws a sample of 25 values (seed 6400) and draws its 95 % interval, the tt interval with the sample’s own ss, as a thin horizontal bar, one run per row, with a dot at the sample mean, on an axis from 7.5 to 12.5. Before the clip a question asks how many of 100 intervals will contain the true mean: all 100, about 95, or about 50, drawn as small bars. A tap picks one and starts the clip. In the clip the 100 rows appear one after another; the bars that miss the line are drawn thicker in a second colour, and a bar at the top counts the hits: 98 of 100 for this seed, with rows 3 and 53 missing. At the end your guess is a dotted mark on that count bar. Readouts give the level and the hits. After the clip a slider, “Confidence level, %”, sets the level from 50 % to 99 % in steps of 1, and every bar widens or narrows: 48 hits at 50 %, 91 at 90 %, 99 at 99 %.

Intervals around a fixed true mean

Before the clip you guessed. About 95 of the 100 intervals contain the true mean; a few miss. The clip’s 100 runs gave 98 hits, two misses: ordinary, because the count of hits wobbles around 95 by about 2.

Look at what moves. The line never moves: the true mean is a fixed number, 10. The intervals move, because each one is built from a different random sample. Some are wider than others, because each sample’s spread is different too.

An interval like this is a confidence interval. Its confidence level, here 95 %, is a property of the recipe: if you used the recipe on sample after sample, it would catch the true value in 95 % of them.

Building the interval

The recipe starts from the sample mean xˉ\bar x. Over repeated samples of size nn, the sample mean has mean μX\mu_X and standard deviation σX/n\sigma_X/\sqrt n, and by The central limit theorem its distribution is close to a bell. Its standard deviation is the standard error of the mean.

For a bell, 95 % of the area lies within 1.96 standard deviations of the centre. I write that edge z0.975=1.96z_{0.975}=1.96: the point of the standard bell with 0.975 of the area below it, leaving 0.025 in each tail. So, before the sample is drawn,

Pr⁡{∣Xˉn−μX∣<1.96 σXn}=0.95.\Pr\Big\{\lvert\bar X_n-\mu_X\rvert<1.96\,\frac{\sigma_X}{\sqrt n}\Big\}=0.95.

Turn the inequality around: Xˉn\bar X_n is within 1.96 σX/n1.96\,\sigma_X/\sqrt n of μX\mu_X exactly when μX\mu_X is within that distance of Xˉn\bar X_n. That gives the interval

xˉ±z0.975 σXn.\bar x\pm z_{0.975}\,\frac{\sigma_X}{\sqrt n}.

The probability statement is about Xˉn\bar X_n, a random variable, before the data arrive. Once a sample gives a number xˉ\bar x, the interval is two fixed numbers, and the true mean is either inside it or not.

For other levels, swap the 1.96 for another edge: 0.67450.6745 for 50 %, 1.28161.2816 for 80 %, 1.64491.6449 for 90 %, 2.57582.5758 for 99 %. After the clip, drag the level: higher confidence needs wider intervals.

When the spread is unknown: the t interval

In practice σX\sigma_X is unknown too, so I put the sample standard deviation ss in its place. That adds a second source of wobble: ss itself varies from sample to sample, and with small samples it is sometimes much too small. To keep 95 % coverage, the edge must be wider than 1.96.

The right edge comes from the t law with n−1n-1 degrees of freedom, a bell with heavier tails, worked out for bell-shaped data. I write its 0.975 point t0.975, n−1t_{0.975,\,n-1}:

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

For n=25n=25, t0.975, 24=2.0639t_{0.975,\,24}=2.0639; for n=8n=8, t0.975, 7=2.3646t_{0.975,\,7}=2.3646. As nn grows it approaches 1.96. The picture uses this t interval for each sample of 25.

What “95 %” does and does not say

Watch out

Three readings of a 95 % interval are common and wrong. In one study, researchers and students accepted on average more than three of six such false statements.

  • “There is a 95 % chance that the true value lies in this interval.” Once computed, the interval either contains the fixed true value or it does not; the 95 % belongs to the recipe, over many samples.
  • “95 % of the data lie in the interval.” The interval is about the mean, not about single values; most single values lie well outside an interval for the mean.
  • “A repeat experiment will give a mean inside this interval 95 % of the time.” The next sample’s mean has its own scatter around the true value, not around this interval’s centre.

What it does say, in one sentence: the recipe that made this interval catches the true value in 95 % of the samples it is used on. The hundred bars in the picture are that sentence, drawn.

A proportion

For a share, such as the fraction of voters for a party, the estimate is p^\hat p, the share in the sample. By Discrete random variables the count of yes answers is binomial, so p^\hat p has standard error p(1−p)/n\sqrt{p(1-p)/n}, which is estimated by putting p^\hat p for pp:

p^±z0.975p^(1−p^)n.\hat p\pm z_{0.975}\sqrt{\frac{\hat p(1-\hat p)}{n}}.

This plain interval works when the counts of yes and no are both reasonably large. With small counts it can run below 0 or above 1. A better recipe, the Wilson interval, solves for the values of pp that sit within 1.96 standard errors of p^\hat p, and always stays between 0 and 1. Software computes it; the worked example quotes it.

How much data

With a known spread, an interval for a mean has half-width z0.975 σX/nz_{0.975}\,\sigma_X/\sqrt n, so the width falls like 1/n1/\sqrt n.

Four times the data, half the margin

The half-width of a 95 % interval for a mean, with known spread 2, against the number of draws.

The half-width of a 95 % interval for a mean, 1.96 × 2/√n, against the sample size n.

draws n
1
margin
3.920
0.00 / 8.50 s
Describe this picture

One panel: the half-width of a 95 % interval for the mean of a bell-shaped population with known standard deviation 2, 1.96×2/n1.96\times2/\sqrt n, plotted against the sample size nn from 1 to 200. The clip traces the curve and marks two points, with dashed guides to both axes: 0.784 at 25 draws and 0.392 at 100 draws. Four times the data gives half the margin. Readouts give nn and the margin. The panel has a play button and no other control.

To make the half-width at most a chosen margin, solve for nn:

n≥(z0.975 σXmargin)2.n\ge\left(\frac{z_{0.975}\,\sigma_X}{\text{margin}}\right)^2.

For a proportion, p^(1−p^)\hat p(1-\hat p) is never more than 14\tfrac14, its value at p^=12\hat p=\tfrac12. So the worst case needs

n≥(z0.9752×margin)2.n\ge\left(\frac{z_{0.975}}{2\times\text{margin}}\right)^2.

Worked example

1. An opinion poll. Of 1000 people asked, 540 support a plan, so p^=0.54\hat p=0.54. The standard error is 0.54×0.46/1000=0.01576\sqrt{0.54\times0.46/1000}=0.01576, the margin 1.96×0.01576=0.03091.96\times0.01576=0.0309, and the 95 % interval runs from 0.5090.509 to 0.5710.571.

At the worst case p^=0.5\hat p=0.5 the margin is 0.03100.0310: the familiar “plus or minus 3 points” of a poll of 1000. For plus or minus 2 points you need n=(1.96/(2×0.02))2=2401n=(1.96/(2\times0.02))^2=2401 people, and for 1 point, 96049604. Halving the margin costs four times the people.

2. Eight response times. The eight times of Describing data have xˉ=16.25\bar x=16.25 ms and s=2.9155s=2.9155 ms. The standard error is 2.9155/8=1.03082.9155/\sqrt8=1.0308. With t0.975, 7=2.3646t_{0.975,\,7}=2.3646 the margin is 2.4372.437, and the interval runs from 13.8113.81 to 18.6918.69 ms.

Using 1.96 instead would give a margin of 2.022.02, too narrow: with only eight values, ss is too uncertain to treat as the true spread.

3. A small count. Three of 20 parts fail a test, so p^=0.15\hat p=0.15. The plain interval is 0.15±0.1570.15\pm0.157, which runs below 0, an impossible failure rate. The Wilson interval is 0.0520.052 to 0.3600.360.

4. Known spread. With σX=2\sigma_X=2 and n=25n=25, the 95 % half-width is 1.96×0.4=0.7841.96\times0.4=0.784. With n=100n=100 it halves, to 0.3920.392.

Try it yourself. A large sample with xˉ=50\bar x=50, s=10s=10 and n=100n=100 gives the interval 50±1.9650\pm1.96. The same sample gives a wider interval at 99 %: catching the truth more often needs more room.

Where you’ll meet this

Polls and elections. Every published poll carries a margin of error, the half-width of a 95 % interval for a share, usually quoted for the worst case p^=12\hat p=\tfrac12.

Experiments. An A/B test on a website reports the difference in sign-up rates between two versions with an interval. An interval for the difference that excludes 0 is evidence of a real difference, the question of Significance tests.

Measurements. A lab result such as "9.81±0.029.81\pm0.02" is an estimate with an uncertainty. Whether the ±\pm is one standard error or a 95 % half-width must be stated; it differs by a factor of about 2.

Planning. The sample-size formula decides how many users to survey, patients to enrol or parts to test before the work starts.

In signal processing · error bars on a spectrum

A spectrum estimated by averaging KK periodograms has a spread, relative to its value, of about 1/K1/\sqrt K, the variance of a mean again. Averaged periodograms: Bartlett and Welch in the signal processing course uses it to put error bars on a spectrum.

Reference card

QuantityFormulaNotes
Standard error of the meanσX/n\sigma_X/\sqrt n, estimated by s/ns/\sqrt nspread of Xˉn\bar X_n over samples
Known spreadxˉ±z0.975 σX/n\bar x\pm z_{0.975}\,\sigma_X/\sqrt nz0.975=1.96z_{0.975}=1.96
Unknown spreadxˉ±t0.975, n−1 s/n\bar x\pm t_{0.975,\,n-1}\,s/\sqrt nt0.975, 24=2.0639t_{0.975,\,24}=2.0639; t0.975, 7=2.3646t_{0.975,\,7}=2.3646
Proportionp^±z0.975p^(1−p^)/n\hat p\pm z_{0.975}\sqrt{\hat p(1-\hat p)/n}small counts: Wilson
Other levels50, 80, 90, 99 %: 0.6745, 1.2816, 1.6449, 2.5758edges of the standard bell
Sample size for a meann≥(z0.975 σX/margin)2n\ge(z_{0.975}\,\sigma_X/\text{margin})^2
Sample size for a sharen≥(z0.975/(2 margin))2n\ge(z_{0.975}/(2\,\text{margin}))^22401 for 2 points
Meaning of 95 %the recipe catches the truth in 95 % of samplesnot a chance for this one interval

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look