Statistics for analysis · lesson 5 of 13
Parts in this lesson
Astats.t.ppf(0.975, df=n - 1)the t multiplier for a 95% interval
Bstats.ttest_1samp(s, 0).confidence_interval(0.95)the same interval from SciPy
Cstats.bootstrap((s,), np.median, method="percentile")a bootstrap interval for any statistic

Confidence intervals

Last updated

A single number from a sample sounds more certain than it is. A confidence interval says the same thing honestly: the mean receipt is probably between ₱79 and ₱127. In this lesson you'll compute intervals with the t formula and with the bootstrap, watch a hundred of them rain down to see what “95%” really promises, and learn the one sentence about intervals that almost everyone gets wrong.

By the end of this lesson you can

  • Compute a confidence interval for a mean with the t formula and with SciPy
  • Compute a bootstrap interval, for a mean or any other statistic
  • Say correctly what a 95% confidence interval does and doesn't mean

An interval from a sample

Lesson 8.4 found that a sample mean is usually within about two standard errors of the true mean. A confidence interval turns that into a range: the sample mean, plus or minus a multiple of its standard error. For a 95% interval for a mean:

x¯±tn−1,0.975sn

tn−1,0.975 is the 97.5th percentile of Student's t distribution with n−1 degrees of freedom. It's a little bigger than the normal's 1.96, to allow for s being only an estimate of σ; with big samples it comes down to 1.96. (William Gosset worked it out in 1908 for small samples at the Guinness brewery, and published as "Student".) Run the cells: the same sample of 40 receipts as in 8.4.

intervals.ipynb
  • The mean is ₱102.68 and its standard error ₱11.78. With 39 degrees of freedom t=2.023, so the 95% confidence interval is ₱78.85 to ₱126.50.
  • stats.ttest_1samp(sample, 0).confidence_interval(0.95) gives the same. (The 0 is for the test in lesson 8.6; the interval doesn't use it.) Using 1.96 instead of 2.023 gives a slightly narrower interval.
  • The third cell shows the trade-off: more confidence needs a wider interval. 80%: ₱30.71 wide; 99%: ₱63.79 wide. 95% is a convention, not a law.

Report it as "the mean receipt was ₱103 (95% CI ₱79 to ₱127)". The interval is the honest version of the number.

What 95% means

Here the population is the whole week of receipts, so the true mean is known: ₱109.22. Make it rain: each line is a 95% interval from a different random sample.

Interval rain100 samples, 100 intervals
₱50₱75₱100₱125₱150₱175

92 of 100 intervals caught the true mean, ₱109.22 (dashed); 8 missed it. Each 95% interval is a sample mean ± 2.02 standard errors. In the long run, 95% of intervals made this way catch the truth; any one of them either does or doesn't.

Most intervals catch the dashed line; a few, in pink, miss it, because their sample happened to be unusually high or low. Rain again a few times: the count moves around 95.

That's what the 95% is about: the method. If you take sample after sample and make an interval from each, about 95% of the intervals will contain the true value. Try the other settings: a 99% level catches more, with wider lines; samples of 160 give lines half as long as samples of 40, at the same level.

Samples of 10 catch a little less than they promise: about 92 in 100, on average over many rains, with most misses on the low side. The t formula assumes the sample means are close to normal, and as 8.4 showed, means of only a few skewed receipts aren't yet. With 40 or more, the count settles close to 95.

The bootstrap

The t formula works for means. What about a median, a percentile, or the ratio of two means? There's often no simple formula. The bootstrap (Bradley Efron, 1979) gets an interval for almost anything by using the computer instead:

The bootstrap. Your one sample: eight receipts, mean ₱105.0. There's no other sample to compare it with, so how much would the mean change in another one?mean 105.0your sample₱0₱50₱100₱150₱200₱250
1 of 4 Your one sample: eight receipts, mean ₱105.0. There's no other sample to compare it with, so how much would the mean change in another one?

B resamples, each the same size as your sample and drawn from it with replacement; work out the statistic for each; the 2.5th and 97.5th percentiles of the B results are a 95% percentile interval. Run the cells.

intervals.ipynb
  • With NumPy, rng.choice(sample, size=(10_000, 40)) makes 10,000 resamples in one go. The bootstrap interval for the mean, ₱81.57 to ₱126.17, is close to the t interval from step 1.
  • stats.bootstrap does the same for any function. For the median there's no simple formula, and the bootstrap gives ₱67 to ₱104.

The bootstrap needs a reasonable sample to start from: it can only resample what you have, so with 8 values, or a sample that missed the rare big receipts, it can't know what it hasn't seen. With 30 or more values it works well for most statistics.

Wrong way: “a 95% chance”

A report says: "The 95% confidence interval is ₱79 to ₱127, so there is a 95% chance that the true mean is between ₱79 and ₱127." It sounds right, and it's wrong. Run the cell: 20 samples, 20 intervals, and the true mean that you'd normally never know.

meaning.ipynb

19 of the 20 intervals caught the true mean of ₱109.22. Sample 5's interval, ₱65.30 to ₱92.40, missed it. Ask of sample 5's interval: "Is there a 95% chance the true mean is in it?" No: the true mean is ₱109.22, and it isn't in ₱65.30 to ₱92.40. There's no chance about it at all. Once an interval is made, it either contains the true value or it doesn't. You just don't know which.

What you can say:

  • "We're 95% confident the true mean is between ₱79 and ₱127", where "95% confident" means: the method we used catches the true value 95% of the time.
  • "If we repeated this survey many times, about 95% of the intervals would contain the true mean."

The difference matters most when people treat the edges as hard limits, or think values in the middle of the interval are "95% likely". The interval is a range of values that fit the data reasonably well, made by a method that's right 95% of the time.

The check: read your sentence back. If it gives a probability to the true value ("a 95% chance the mean is…"), rewrite it to be about the method or about your confidence.

Check what you learned

A 95% interval for the mean waiting time is 4.1 to 5.3 minutes. You want a 99% interval from the same data. It will be…
Which sentence about a 95% confidence interval of ₱79 to ₱127 is correct?
You need an interval for the 90th percentile of receipts. What do you use?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis