If you work with data, you will eventually have to say something like "the average is 71.5, give or take." A confidence interval (CI) for the mean is how you say that properly: a point estimate plus a range that reflects the uncertainty from only having a sample.
This post covers:
- The formula and what each piece does
- When to use z vs t (the part people get wrong most often)
- A fully worked example you can check by hand
- The same calculation in Python, R and Excel
- A free calculator that shows the steps, so you can sanity-check your own work
The formula
A CI for a mean is the sample mean plus or minus a margin of error:
CI = x̄ ± E
The margin of error depends on whether the population standard deviation (σ) is known.
σ known → z-interval
CI = x̄ ± z(α/2) · σ / √n
σ unknown → t-interval (this is almost always your real situation)
CI = x̄ ± t(α/2, n−1) · s / √n
Where:
-
x̄is the sample mean -
nis the sample size -
sis the sample standard deviation (divide byn − 1, notn) -
s / √nis the standard error -
zortis the critical value for your confidence level
Common z critical values: 1.645 (90%), 1.960 (95%), 2.576 (99%).
z or t? A quick rule
- σ is known (rare outside textbooks): use z.
-
σ is unknown and estimated from your sample: use t with
n − 1degrees of freedom.
The t-distribution has heavier tails, which accounts for the extra uncertainty of estimating σ. As n grows it converges to the normal, so for large samples the two give nearly identical answers. Many courses use n ≥ 30 as the point where z is a fine approximation, but using t when σ is unknown is never wrong.
Worked example
Say you measure the resting heart rate (bpm) of 8 adults:
72, 68, 75, 70, 74, 71, 69, 73
We want a 95% CI for the population mean. σ is unknown, so we use t.
1. Sample mean
x̄ = 572 / 8 = 71.5
2. Sample standard deviation
The sum of squared deviations is 42.00, so:
s = √(42 / 7) = √6 ≈ 2.4495
3. Standard error
SE = 2.4495 / √8 ≈ 0.8660
4. Critical value (df = 7, 95% confidence)
t = 2.3646
5. Margin of error
E = 2.3646 × 0.8660 ≈ 2.0477
6. Interval
71.5 ± 2.0477 → (69.4523, 73.5477)
So we are 95% confident the true mean resting heart rate lies between about 69.45 and 73.55 bpm.
Verify it in code
Python
import numpy as np
from scipy import stats
data = [72, 68, 75, 70, 74, 71, 69, 73]
n = len(data)
mean = np.mean(data)
se = stats.sem(data) # uses ddof=1 (sample std dev)
low, high = stats.t.interval(0.95, df=n - 1, loc=mean, scale=se)
print(f"95% CI: ({low:.4f}, {high:.4f})")
# 95% CI: (69.4523, 73.5477)
For a z-interval with a known σ:
sigma = 2.5 # known population std dev
se_z = sigma / np.sqrt(n)
low, high = stats.norm.interval(0.95, loc=mean, scale=se_z)
R
x <- c(72, 68, 75, 70, 74, 71, 69, 73)
t.test(x, conf.level = 0.95)$conf.int
Excel / Google Sheets
=CONFIDENCE.T(0.05, STDEV.S(A1:A8), COUNT(A1:A8))
That returns the margin of error. Add and subtract it from AVERAGE(A1:A8).
Try it without writing code
If you just want to paste in numbers and see the result, I use this free tool:
Confidence Interval Calculator for Mean
What it does:
- Accepts raw data (comma, space or newline separated) or summary statistics (mean, n, standard deviation)
- Supports z-interval and t-interval, with an auto-detect option
- Offers 90%, 95% and 99% confidence levels
- Returns the sample mean, standard error, critical value, margin of error and the interval bounds
- Shows the step-by-step working, which is the useful part if you are learning or checking your own calculations
Paste the heart-rate data from the example above and you should get the same (69.4523, 73.5477) interval.
Common mistakes
- Using z when σ is unknown. This understates the margin of error, especially for small samples.
- Misreading what "95%" means. It does not mean there is a 95% probability the true mean is inside your specific interval. It means that if you repeated the sampling process many times, about 95% of the intervals built this way would contain the true mean.
-
Dividing by
ninstead ofn − 1when computingsfrom a sample (Bessel's correction). - Ignoring assumptions. The method assumes roughly normal data or a large enough sample for the Central Limit Theorem to help. With a small, clearly skewed sample, consider bootstrapping instead.
What changes the width of the interval?
-
Sample size: the standard error shrinks with
√n, so you need to quadruple the sample to halve the margin of error. - Variability: noisier data means a wider interval.
- Confidence level: 99% is wider than 95%, which is wider than 90%. You trade precision for certainty.
Wrapping up
A CI for a mean is just x̄ ± critical value × standard error. Get the z vs t choice right, use n − 1 for the sample standard deviation, and interpret the result as a statement about the method, not a probability about one interval.
If you want to double-check your work, run your numbers through the free confidence interval calculator and compare it to your Python, R or Excel output.
Questions or edge cases (small samples, skewed data, two-sample intervals)? Drop them in the comments.
Top comments (0)