A confidence interval is a range of values built around an estimate from a sample, such as a mean, that shows how much that estimate could change from sample to sample. Instead of saying "the average order was $63.64", you say "the 95% confidence interval for the average order is $45.65 to $81.64". The 95% describes the method: about 95 out of 100 intervals built this way contain the true value.
In this post you will build an interval step by step with orders from Bazaar, a fictional online store.
Why is one number not enough?
You look at 25 random Bazaar orders, not all of them. Their average is $63.64. If you picked 25 other orders tomorrow, you would get a different average, maybe $58 or $71. Every sample gives a slightly different answer. A confidence interval puts that wobble into your result.
How do you calculate a confidence interval?
Use four steps: find the standard error, find the critical value, find the margin of error, and add and subtract it. Here are 25 real completed orders from the Bazaar data (amounts in dollars):
import numpy as np from scipy import stats amounts = np.array([64.95, 49.46, 94.95, 14.95, 41.85, 19.95, 85.8, 22.45, 59.95, 124.95, 119.9, 24.9, 19.35, 22.35, 124.95, 20.21, 40.46, 124.95, 14.81, 114.95, 54.95, 61.55, 94.95, 151.11, 22.45]) n = len(amounts) mean = amounts.mean() sd = amounts.std(ddof=1) se = sd / n ** 0.5 t_value = stats.t.ppf(0.975, df=n - 1) margin = t_value * se print(n, round(mean, 2), round(sd, 2)) print(round(se, 2), round(t_value, 3), round(margin, 2)) print(round(mean - margin, 2), round(mean + margin, 2))
1. Standard error: the standard deviation divided by the square root of n. It measures how much the sample mean moves from sample to sample.
2. Critical value: for a small sample, use the t distribution with n - 1 degrees of freedom. For 24 degrees of freedom and 95% confidence, t is about 2.064.
3. Margin of error: the critical value times the standard error.
4. Interval: the mean minus and plus the margin of error.
You can also get the same interval in one call:
low, high = stats.t.interval(0.95, df=n - 1, loc=mean, scale=se) print(round(low, 2), round(high, 2))
What does 95% confidence mean?
It means that if you repeated the whole process many times, about 95% of the intervals you build would contain the true mean. The 95% belongs to the method, not to one interval.
A common wrong reading is: "there is a 95% chance the true mean is inside this interval." The true mean is one fixed number. Your specific interval either contains it or it does not. What changes from sample to sample is the interval.
You can check this with a simulation. The code below invents a population with a known mean of $64, draws 1,000 samples of 25 orders, builds an interval each time, and counts how many contain $64:
rng = np.random.default_rng(42) hits = 0 for _ in range(1000): sample = rng.normal(64, 44, 25) se_sample = sample.std(ddof=1) / 25 ** 0.5 low, high = stats.t.interval(0.95, df=24, loc=sample.mean(), scale=se_sample) if low <= 64 <= high: hits += 1 print(hits)
In this run, 948 of 1,000 intervals contained the true mean and 52 missed it. In real life you have one sample, and you cannot know if yours is one of the 948 or one of the 52. What you know is that your method is right about 19 times out of 20.
What makes a confidence interval narrower or wider?
A bigger sample, lower confidence, or less spread in the data make the interval narrower. Higher confidence or more spread make it wider.
| Change | Effect on the interval | Why |
|---|---|---|
| Larger sample | Narrower | The standard error shrinks with the square root of n |
| Lower confidence (90%) | Narrower | You accept missing the true value more often |
| Higher confidence (99%) | Wider | You need a wider net to miss less often |
| More spread in the data | Wider | The standard deviation is larger |
for level in (0.90, 0.95, 0.99): low, high = stats.t.interval(level, df=n - 1, loc=mean, scale=se) print(level, round(low, 2), round(high, 2)) print(round(stats.t.ppf(0.975, df=99) * sd / 100 ** 0.5, 2))
With the same 25 orders, the 90% interval is $48.73 to $78.56 and the 99% interval is $39.26 to $88.03. More confidence gives a less precise statement.
The last line shows the margin of error if you had 100 orders with the same standard deviation: $8.65, about half of the $17.99 you get with 25. To cut the margin in half, you need about four times as many observations.
What is the difference between a confidence interval and a p-value?
A [p-value](/blog/what-is-a-p-value) only answers one question: "is the result compatible with no effect?" A confidence interval answers that question and also shows how big the effect could be.
Say a new product page changes the average order by somewhere between $0.50 and $10 with 95% confidence. That tells you more than "p = 0.03". The data are compatible with a positive effect, but the effect could be as small as 50 cents.
What mistakes do people make with confidence intervals?
- Reading 95% as the probability for one interval. The confidence belongs to the method.
- Thinking the interval covers individual orders. The interval is for the mean. Check how many of the 25 orders fall inside it:
inside = ((amounts >= mean - margin) & (amounts <= mean + margin)).sum() print(inside, "of", n)
Only 5 of the 25 orders fall inside the interval, because single orders are spread much wider than the mean.
- Using a non-random sample. If you only ask loyal customers, the interval describes loyal customers. It does not fix a biased sample.
- Trusting a small sample of very skewed data. The t interval works best when the data are roughly symmetric or the sample is large. Bazaar orders have a long tail of a few big orders, so 25 observations can be too few.
What to read next
- [Bootstrap confidence interval](/learn/statistics-data-analysis/bootstrap-confidence-interval): build an interval on the full Bazaar data without a formula.
- [Sampling variability](/learn/statistics-data-analysis/sampling-variability): see where the standard error comes from.
- [What is a p-value?](/blog/what-is-a-p-value): the other half of reading a result.
- [Statistics and Data Analysis course](/courses/statistics-data-analysis): 44 free lessons.
- #statistics
- #confidence interval
- #sampling
- #estimation