Confidence intervals

A sample mean is a guess. A confidence interval adds an honest margin of error.

For a large enough sample, a 95% confidence interval for the mean is:

mean ± 1.96 × s ÷ √n

where s is the sample standard deviation and s ÷ √n the standard error. (1.96 comes from the normal curve: 95% of it lies within 1.96 SD of the centre.)

from math import sqrt
import statistics as st

heights = [162, 170, 168, 175, 159, 181, 166, 172, 169, 177,
           164, 171, 174, 160, 168, 173, 167, 179, 165, 170,
           176, 163, 169, 172, 171]
n = len(heights)
mean = st.mean(heights)
se = st.stdev(heights) / sqrt(n)
print("n =", n, " mean =", round(mean, 1), " standard error =", round(se, 2))
print("95% CI:", round(mean - 1.96 * se, 1), "to", round(mean + 1.96 * se, 1))
Open in compiler

What "95%" means

If you repeated the whole study many times, about 95% of the intervals built this way would contain the true mean. It does not mean "the true mean is in this interval with probability 95%": the true mean is fixed; it is the interval that varies.

Narrower intervals

The margin shrinks with √n: four times the data halves the margin. Asking for 99% confidence instead widens it (2.58 instead of 1.96).

For small samples (n below about 30), statisticians replace 1.96 with a slightly larger number from the t distribution.

Quick check

1. To halve the margin of error you need…
2. A 99% interval compared with a 95% one is…
3. The 1.96 in the formula comes from…
My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.