DEV Community

Alan Matthew
Alan Matthew

Posted on

Confidence Intervals for a Mean: z vs t, a Worked Example, and a Free Calculator

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
Enter fullscreen mode Exit fullscreen mode

The margin of error depends on whether the population standard deviation (σ) is known.

σ known → z-interval

CI = x̄ ± z(α/2) · σ / √n
Enter fullscreen mode Exit fullscreen mode

σ unknown → t-interval (this is almost always your real situation)

CI = x̄ ± t(α/2, n−1) · s / √n
Enter fullscreen mode Exit fullscreen mode

Where:

  • x̄ is the sample mean
  • n is the sample size
  • s is the sample standard deviation (divide by n − 1, not n)
  • s / √n is the standard error
  • z or t is 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 − 1 degrees 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
Enter fullscreen mode Exit fullscreen mode

We want a 95% CI for the population mean. σ is unknown, so we use t.

1. Sample mean

x̄ = 572 / 8 = 71.5
Enter fullscreen mode Exit fullscreen mode

2. Sample standard deviation

The sum of squared deviations is 42.00, so:

s = √(42 / 7) = √6 ≈ 2.4495
Enter fullscreen mode Exit fullscreen mode

3. Standard error

SE = 2.4495 / √8 ≈ 0.8660
Enter fullscreen mode Exit fullscreen mode

4. Critical value (df = 7, 95% confidence)

t = 2.3646
Enter fullscreen mode Exit fullscreen mode

5. Margin of error

E = 2.3646 × 0.8660 ≈ 2.0477
Enter fullscreen mode Exit fullscreen mode

6. Interval

71.5 ± 2.0477  →  (69.4523, 73.5477)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

R

x <- c(72, 68, 75, 70, 74, 71, 69, 73)
t.test(x, conf.level = 0.95)$conf.int
Enter fullscreen mode Exit fullscreen mode

Excel / Google Sheets

=CONFIDENCE.T(0.05, STDEV.S(A1:A8), COUNT(A1:A8))
Enter fullscreen mode Exit fullscreen mode

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

Confidence Interval Calculator for Mean: Compute CI with Steps

Free confidence interval calculator for population mean. Supports z-interval and t-interval methods with detailed step-by-step calculations, margin of error, and LaTeX formula rendering.

favicon takemystatisticsclassforme.us

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

  1. Using z when σ is unknown. This understates the margin of error, especially for small samples.
  2. 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.
  3. Dividing by n instead of n − 1 when computing s from a sample (Bessel's correction).
  4. 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)