Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 57 ยท Estimation

Confidence Intervals for Means

Build and interpret confidence intervals for a population mean using z and t methods, learn margin of error, sample size planning and the correct interpretation, with Python.

A sample mean of 52 is a good guess for the population mean, but how good? A confidence interval (CI) answers that by giving a range of plausible values for the parameter, such as "52 plus or minus 3". It combines the point estimate with a margin of error derived from the sampling distribution.

The general recipe

Most confidence intervals have the shape

TEXT
estimate  +/-  critical value  x  standard error

The margin of error is the part after the plus-minus sign. The critical value depends on the confidence level: higher confidence means a bigger multiplier and a wider interval.

When sigma is known: the z interval

If the population standard deviation sigma is known (rare in practice), and the data are normal or n is large:

TEXT
x-bar  +/-  z* x sigma / sqrt(n)

where z* is 1.645 for 90%, 1.96 for 95% and 2.576 for 99% confidence.

When sigma is unknown: the t interval

Normally you estimate sigma with s and use a t critical value with n - 1 degrees of freedom (see the t distribution):

TEXT
x-bar  +/-  t* x s / sqrt(n)
Python A 95% t interval by hand and with SciPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How to interpret a confidence interval

A 95% confidence interval does not mean "there is a 95% probability that the true mean lies in this interval" (the true mean is a fixed number; it either is or is not in it). The 95% refers to the method: if you repeated the whole sampling process many times and built an interval each time, about 95% of those intervals would capture the true mean.

You can see this directly by simulation.

Python Coverage of 95% intervals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What controls the width?

TEXT
width = 2 x critical value x s / sqrt(n)
  • Higher confidence level means a larger critical value, so a wider interval.
  • Larger s (noisier data) means a wider interval.
  • Larger n means a narrower interval, but with diminishing returns: to halve the width you need four times the data.
Python Width versus confidence and sample size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Planning the sample size

To obtain a target margin of error E at a given confidence level, solve the margin formula for n, using a guess sigma from a pilot study or earlier work:

TEXT
n  =  (z* x sigma / E)^2      (round up)
Python How many people do I need?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical notes

  • Assumptions: a random sample, independent observations, and either a roughly normal population or a large enough n for the CLT. Heavy outliers or strong skew in small samples undermine the t interval.
  • Do not confuse the interval for the mean with the range of individual values. A 95% CI for the mean is far narrower than the range holding 95% of the observations.
  • Reporting: state the estimate, the interval and the level, for example "mean 10.4 h (95% CI 10.0 to 10.8)".
flowchart LR
    A["Sample data"] --> B["Mean and s"]
    B --> C["SE = s / sqrt(n)"]
    C --> D["Mean +/- t* x SE"]
    D --> E["Confidence interval"]

The steps for a one-sample t interval.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A sample of n = 25 has mean 40 and s = 10. What is the standard error?

    SE = s / sqrt(n) = 10 / 5 = 2.
  2. 2. Which interpretation of a 95% CI is correct?

    The confidence level describes the long-run success rate of the procedure.
  3. 3. Which change makes a confidence interval narrower?

    A larger n shrinks the standard error.
  4. 4. To cut the margin of error in half, the sample size must be:

    The margin is proportional to 1 / sqrt(n), so halving it requires four times the data.

Summary

  • A confidence interval is estimate +/- critical value x standard error.
  • Use t with n - 1 df when sigma is unknown, which is almost always.
  • The confidence level describes the method's long-run coverage, not the probability for one interval.
  • Width shrinks like 1 / sqrt(n) and grows with confidence level and variability.

Next: Confidence Intervals for Proportions