09-01: Confidence Interval for a Mean¶
A single number estimate is almost certainly wrong. A confidence interval reports a range plus a statement of how often that procedure captures the truth — the first genuinely inferential tool in the course.
Point vs. Interval Estimates¶
Point estimate A single value: x̄ = 24.6
Best guess, but you know it isn't exactly μ.
Interval estimate A range: 24.6 ± 1.2 → (23.4, 25.8)
Carries its own uncertainty with it.
x̄ is the point estimate of μ, p̂ of p, and s² of σ². Each is unbiased — right on average across repeated samples.
The Anatomy of Every Confidence Interval¶
Everything in Chapters 09–11 is this one line with different pieces plugged in.
Case 1 — σ Known (the z-interval)¶
Use when the population standard deviation is genuinely known, or n is large and σ is well established.
Critical z-values¶
| Confidence | α |
α/2 |
z_(α/2) |
|---|---|---|---|
| 90% | 0.10 | 0.050 | 1.645 |
| 95% | 0.05 | 0.025 | 1.960 |
| 98% | 0.02 | 0.010 | 2.326 |
| 99% | 0.01 | 0.005 | 2.576 |
Case 2 — σ Unknown (the t-interval) — the usual case¶
When you must estimate σ with s, the extra uncertainty widens the interval. That is what the Student t-distribution encodes.
s
x̄ ± t_(α/2, df) · ─────── df = n − 1
√n
Conditions: random sample AND (population approximately normal OR n ≥ 30)
The t-distribution¶
- Bell-shaped and symmetric about 0, like
z, but with heavier tails. - Its shape depends on the degrees of freedom,
df = n − 1. - Smaller
df→ fatter tails → larger critical value → wider interval. - As
df → ∞,t → z. Atdf = 30,t = 2.042vs.z = 1.960; atdf = 100,t = 1.984.
Tip
Which one? If you computed s from your own data — and you almost always did — use t. Reserve z for when σ is given in the problem statement.
Interpreting a Confidence Interval Correctly¶
For a 95% CI of (23.4, 25.8):
✅ Correct. "We are 95% confident that the true population mean lies between 23.4 and 25.8." Or, more precisely: "If we repeated this sampling procedure many times, 95% of the intervals produced would contain μ."
❌ Wrong. "There is a 95% probability that μ is between 23.4 and 25.8." μ is a fixed constant, not a random variable — it either is or is not in this particular interval. The randomness lives in the interval, not in μ.
❌ Wrong. "95% of the data lies between 23.4 and 25.8." That would be a prediction interval, and it is much wider.
❌ Wrong. "95% of sample means fall in this interval."
What Changes the Width¶
| Change | Effect on width | Why |
|---|---|---|
| Higher confidence (95% → 99%) | Wider | Larger critical value |
Larger n |
Narrower (by √n) |
Smaller standard error |
Larger s |
Wider | More variable data |
You cannot have high confidence and a narrow interval and a small sample. Pick two.
Sample Size for a Desired Margin of Error¶
Solve E = z·σ/√n for n:
If σ is unknown, estimate it from a pilot study, from prior work, or with the range rule of thumb σ ≈ range/4.
Example. 95% confidence, E = 2, σ = 10:
To halve E to 1: n = (19.6)² = 384.2 → 385. Four times the sample for twice the precision.
Worked Examples¶
(a) σ known — z-interval¶
A sample of n = 50 bulbs has x̄ = 1,250 hours; the manufacturer's process has σ = 85 hours. Build a 95% CI for μ.
SE = 85 / √50 = 12.021
z = 1.96
E = 1.96 × 12.021 = 23.56
CI = 1250 ± 23.56 = (1226.4, 1273.6) hours
(b) σ unknown — t-interval¶
A sample of n = 15 commute times gives x̄ = 27.4 minutes and s = 6.2 minutes. Build a 95% CI.
df = 14, t(0.025, 14) = 2.145
SE = 6.2 / √15 = 1.601
E = 2.145 × 1.601 = 3.434
CI = 27.4 ± 3.43 = (23.97, 30.83) minutes
Note how much wider this is than the z-interval would be: using z = 1.96 would have given ± 3.14. The extra 0.29 minutes is the price of not knowing σ.
Reporting it: "The mean commute time is 27.4 minutes (95% CI: 24.0 to 30.8 minutes)."
Confidence Interval for a Variance or Standard Deviation¶
Uses the chi-square distribution, which is right-skewed — so the interval is not symmetric about s².
(n − 1)s² (n − 1)s²
─────────── < σ² < ─────────── df = n − 1
χ²_(α/2) χ²_(1−α/2)
Take square roots for σ. Requires the population to be NORMAL — this
procedure is not robust to departures from normality.
Excel¶
' ── z-interval (σ known) ───────────────────────────────────────────
' B1 = x̄ = 1250, B2 = σ = 85, B3 = n = 50, B4 = confidence = 0.95
=CONFIDENCE.NORM(1-B4, B2, B3) ' margin of error E -> 23.5604
=B1-CONFIDENCE.NORM(1-B4,B2,B3) ' lower bound -> 1226.44
=B1+CONFIDENCE.NORM(1-B4,B2,B3) ' upper bound -> 1273.56
' Built by hand
=NORM.S.INV(1-(1-B4)/2) ' critical z -> 1.95996
=B2/SQRT(B3) ' standard error -> 12.0208
=NORM.S.INV(0.975)*B2/SQRT(B3) ' E -> 23.5604
' ── t-interval (σ unknown — the usual case) ────────────────────────
' D1 = x̄ = 27.4, D2 = s = 6.2, D3 = n = 15, D4 = 0.95
=CONFIDENCE.T(1-D4, D2, D3) ' margin of error E -> 3.4335
=D1-CONFIDENCE.T(1-D4,D2,D3) ' lower bound -> 23.9665
=D1+CONFIDENCE.T(1-D4,D2,D3) ' upper bound -> 30.8335
' Built by hand
=T.INV.2T(1-D4, D3-1) ' two-tailed critical t -> 2.14479
=T.INV(1-(1-D4)/2, D3-1) ' same value, one-tailed form
=T.INV.2T(0.05,14)*D2/SQRT(D3) ' E -> 3.4335
' ── Straight from raw data ──────────────────────────────────────────
=AVERAGE(A2:A16)
=STDEV.S(A2:A16)/SQRT(COUNT(A2:A16)) ' standard error
=AVERAGE(A2:A16)-T.INV.2T(0.05,COUNT(A2:A16)-1)*STDEV.S(A2:A16)/SQRT(COUNT(A2:A16))
=AVERAGE(A2:A16)+T.INV.2T(0.05,COUNT(A2:A16)-1)*STDEV.S(A2:A16)/SQRT(COUNT(A2:A16))
' Analysis ToolPak ▸ Descriptive Statistics ▸ tick
' "Confidence Level for Mean: 95%" → reports the margin of error E
' ── Sample size ─────────────────────────────────────────────────────
=ROUNDUP((NORM.S.INV(0.975)*10/2)^2, 0) ' -> 97
=ROUNDUP((NORM.S.INV(0.975)*10/1)^2, 0) ' -> 385
' ── CI for a variance / standard deviation ──────────────────────────
=(D3-1)*D2^2/CHISQ.INV.RT(0.025, D3-1) ' lower bound for σ²
=(D3-1)*D2^2/CHISQ.INV.RT(0.975, D3-1) ' upper bound for σ²
=SQRT((D3-1)*D2^2/CHISQ.INV.RT(0.025,D3-1)) ' lower bound for σ
R¶
# ── From summary statistics ────────────────────────────────────────
ci_z <- function(xbar, sigma, n, conf = 0.95) {
z <- qnorm(1 - (1 - conf)/2)
e <- z * sigma / sqrt(n)
c(estimate = xbar, margin = e, lower = xbar - e, upper = xbar + e)
}
round(ci_z(1250, 85, 50), 3)
# estimate margin lower upper
# 1250.000 23.560 1226.440 1273.560
ci_t <- function(xbar, s, n, conf = 0.95) {
t <- qt(1 - (1 - conf)/2, df = n - 1)
e <- t * s / sqrt(n)
c(estimate = xbar, margin = e, lower = xbar - e, upper = xbar + e)
}
round(ci_t(27.4, 6.2, 15), 3)
# estimate margin lower upper
# 27.400 3.434 23.966 30.834
qt(0.975, df = 14) # critical t -> 2.144787
qnorm(0.975) # critical z -> 1.959964
# ── From raw data — t.test() gives the CI directly ─────────────────
# (a different 15-commute sample, to show the raw-data route)
commute <- c(22, 31, 28, 19, 35, 27, 24, 33, 29, 21, 30, 26, 32, 25, 29)
t.test(commute) # default: 95% CI for the mean
t.test(commute, conf.level = 0.99)$conf.int
t.test(commute)$conf.int # just the interval
# ── z-interval from raw data (BSDA package) ────────────────────────
# library(BSDA); z.test(commute, sigma.x = 6)$conf.int
# ── Sample size ────────────────────────────────────────────────────
n_for_mean <- function(sigma, E, conf = 0.95)
ceiling((qnorm(1 - (1 - conf)/2) * sigma / E)^2)
n_for_mean(10, 2) # 97
n_for_mean(10, 1) # 385
# ── CI for a variance / SD ─────────────────────────────────────────
ci_var <- function(s, n, conf = 0.95) {
a <- 1 - conf
lo <- (n - 1) * s^2 / qchisq(1 - a/2, n - 1)
hi <- (n - 1) * s^2 / qchisq(a/2, n - 1)
c(var_lower = lo, var_upper = hi, sd_lower = sqrt(lo), sd_upper = sqrt(hi))
}
round(ci_var(6.2, 15), 3)
# ── What "95% confident" actually means — simulate it ──────────────
set.seed(7)
covered <- replicate(1000, {
s <- rnorm(20, mean = 100, sd = 15)
ci <- t.test(s)$conf.int
ci[1] <= 100 && 100 <= ci[2]
})
mean(covered) # ≈ 0.95 — the intervals move, μ never does
Python¶
import numpy as np
from scipy import stats
# ── From summary statistics ────────────────────────────────────────
def ci_z(xbar, sigma, n, conf=0.95):
z = stats.norm.ppf(1 - (1 - conf) / 2)
e = z * sigma / np.sqrt(n)
return xbar, e, xbar - e, xbar + e
ci_z(1250, 85, 50) # (1250, 23.560, 1226.44, 1273.56)
def ci_t(xbar, s, n, conf=0.95):
t = stats.t.ppf(1 - (1 - conf) / 2, df=n - 1)
e = t * s / np.sqrt(n)
return xbar, e, xbar - e, xbar + e
ci_t(27.4, 6.2, 15) # (27.4, 3.4335, 23.9665, 30.8335)
stats.t.ppf(0.975, df=14) # critical t -> 2.144787
stats.norm.ppf(0.975) # critical z -> 1.959964
# ── From raw data ──────────────────────────────────────────────────
# (a different 15-commute sample, to show the raw-data route)
commute = np.array([22, 31, 28, 19, 35, 27, 24, 33, 29, 21, 30, 26, 32, 25, 29])
stats.t.interval(0.95,
df=commute.size - 1,
loc=commute.mean(),
scale=stats.sem(commute)) # scipy's SEM = s/√n
stats.norm.interval(0.95, loc=commute.mean(), scale=6 / np.sqrt(commute.size))
# statsmodels gives it in one call
# from statsmodels.stats.weightstats import DescrStatsW
# DescrStatsW(commute).tconfint_mean(alpha=0.05)
# ── Sample size ────────────────────────────────────────────────────
def n_for_mean(sigma, E, conf=0.95):
z = stats.norm.ppf(1 - (1 - conf) / 2)
return int(np.ceil((z * sigma / E) ** 2))
n_for_mean(10, 2) # 97
n_for_mean(10, 1) # 385
# ── CI for a variance / SD ─────────────────────────────────────────
def ci_var(s, n, conf=0.95):
a = 1 - conf
lo = (n - 1) * s**2 / stats.chi2.ppf(1 - a/2, n - 1)
hi = (n - 1) * s**2 / stats.chi2.ppf(a/2, n - 1)
return lo, hi, np.sqrt(lo), np.sqrt(hi)
ci_var(6.2, 15)
# ── Simulating the meaning of 95% ──────────────────────────────────
rng = np.random.default_rng(7)
hits = 0
for _ in range(1000):
s = rng.normal(100, 15, 20)
lo, hi = stats.t.interval(0.95, s.size - 1, s.mean(), stats.sem(s))
hits += lo <= 100 <= hi
hits / 1000 # ≈ 0.95
Quick Reference¶
| Task | Excel | R | Python |
|---|---|---|---|
| Critical z | NORM.S.INV(1-α/2) |
qnorm(1-α/2) |
norm.ppf(1-α/2) |
| Critical t | T.INV.2T(α, df) |
qt(1-α/2, df) |
t.ppf(1-α/2, df) |
| Margin of error (z) | CONFIDENCE.NORM(α,σ,n) |
qnorm(...)*σ/sqrt(n) |
norm.ppf(...)*σ/√n |
| Margin of error (t) | CONFIDENCE.T(α,s,n) |
qt(...)*s/sqrt(n) |
t.ppf(...)*s/√n |
| Standard error | STDEV.S/SQRT(COUNT) |
sd(x)/sqrt(length(x)) |
stats.sem(x) |
| CI from raw data | ToolPak ▸ Descriptive Stats | t.test(x)$conf.int |
stats.t.interval(...) |
| Sample size | ROUNDUP((z*σ/E)^2,0) |
ceiling((qnorm*σ/E)^2) |
ceil((z*σ/E)**2) |
CI for σ² |
(n-1)s²/CHISQ.INV.RT |
(n-1)*s^2/qchisq(...) |
(n-1)*s**2/chi2.ppf(...) |
Common Mistakes¶
- Using
zwhenσis unknown. If you computedsfrom the data, uset. - Using
df = ninstead ofn − 1. - Confusing Excel's
T.INV(one tail) withT.INV.2T(two tails). For a 95% CI:T.INV.2T(0.05, df)orT.INV(0.975, df)— both give 2.145 atdf = 14. - Saying "there is a 95% probability that
μis in this interval". - Rounding the sample size down. Always round up.
- Building a CI from a convenience sample. The formula assumes random sampling (08-01).
Exercises: 09-01: Exercises — Confidence Interval for a Mean
⬅️ Previous: 08-02: Sampling Distributions and the Central Limit Theorem ➡️ Next: 09-02: Confidence Interval for a Proportion