Contents/ Part XI · Estimation & Confidence Intervals/ Chapter 72

Confidence Intervals for a Mean

When σ is known we use the z-interval; when it is unknown, which is almost always, we estimate it with s and use the t-distribution. We build both, check coverage and conditions, and put a 95% interval on the mean delivery time from a real logistics spreadsheet.

⏱️ ~16 min read
🐍 Notebook included
📊 Chapter 72

The Point vs. Interval Estimation chapter promised an interval; here we build it for the most common target, a population mean. The recipe is always estimate ± (critical value)·(standard error), and the only real question is which critical value to use.

x̄ ± t·s/√n
A confidence interval for a mean is x̄ ± (critical value)·SE. If σ is known, the critical value is z (the z-interval). If σ is unknown, estimate it by s and use t with n−1 degrees of freedom (the t-interval).
📏
In practice, it is always the t-interval

We almost never know the population σ, so the t-interval is the default. The z-interval is the clean textbook starting point that shows where the t correction comes from.

1

The z-Interval (σ known)

If the population standard deviation were known, the 95% interval is simply x̄ ± 1.96·(σ/√n). It is clean and exact, but it leans on a quantity we rarely have.

x̄ ± z · σ/√n z = 1.96 (95%) SE = σ/√n exact when σ is known · but σ is almost never known

Plugging the sample standard deviation s into this z-formula understates the uncertainty for small samples, because s is itself a noisy estimate of σ. The proper fix is to widen the interval using the t-distribution.

2

The t-Interval (σ unknown)

Replace σ with the sample sd s, and replace z with t*, the critical value of Student's t with n−1 degrees of freedom. The t-distribution has heavier tails, so t* > z, and the interval is a little wider, the honest price of estimating σ.

Student's t: heavier tails → t* > z* normal (z) t (heavier tails) more probability in the tails means a wider 95% interval
Degrees of freedom (n−1)t* (95%)z* (95%)
42.7761.960
92.2621.960
292.0451.960
991.9841.960
10001.9621.960

At df = 4 the critical value is 2.78, far above 1.96; by n ≈ 30–60 the gap is negligible, and for large samples t* and z* are interchangeable. In Python it is one line: scipy.stats.t.interval(0.95, df=n-1, loc=xbar, scale=s/√n).

3

Conditions & Coverage

A confidence interval is only as good as its assumptions. The t-interval needs the data to be roughly normal, or the sample to be large enough that the Central Limit Theorem makes x̄ approximately normal.

Sample size nSimulated coverage (skewed data)Verdict
1093.5%under-covers; CLT has not kicked in
3094.0%close; the usual rule-of-thumb threshold
10094.7%on target

The notebook draws thousands of intervals from a right-skewed population. With n = 10 the interval under-covers, but by n = 30 coverage is near the promised 95% and improves further as n grows. The practical rule: trust the t-interval when the data is roughly symmetric, or when n ≥ ~30. For heavily skewed data and small samples, prefer the bootstrap of Resampling & Simulation.

4

Real-World Example: Mean Delivery Time

A logistics team exports 180 completed shipments and wants to report the average door-to-door delivery time with quantified confidence. σ is unknown, so this is a textbook t-interval, after a quick shape check.

📂 Dataset · confidence-intervals-for-a-mean--delivery_times.xlsx

Each row is a shipment with delivery_hours, distance_mi, warehouse, and carrier.

QuantityValue
Sample size n180 shipments
Mean delivery time (x̄)35.85 hours
Sample sd (s)10.11 hours
Standard error (s/√n)0.753 hours
t* (df = 179)1.973
95% confidence interval34.37 to 37.34 hours

The histogram is reasonably symmetric and n = 180 is large, so the conditions hold. The team can now state, with 95% confidence, that the true average delivery time is between about 34.4 and 37.3 hours, "roughly a day and a half, give or take an hour", instead of a single unqualified number. That interval is the difference between a defensible service-level claim and a guess.

5

Confidence Intervals in Machine Learning & AI

The t-interval is the everyday tool for putting error bars on any average a model or experiment produces, from mean latency to mean reward.

Idea (this chapter)In ML / AI it becomesExample
t-interval for a meanError bars on a mean metricmean inference latency ± CI
Degrees of freedomHow many independent runs you have5 cross-validation folds → df = 4
Conditions / CLTWhen a normal approximation is safeaverage over enough batches/episodes
t vs zSmall-sample honestyfew seeds → wider intervals (don't overclaim)
🤖
Why this matters for AI research

Reporting "mean reward over 5 seeds" without an interval is one of the most common reproducibility failures in ML. With only 5 runs, df = 4 and t* = 2.78, so the interval is much wider than a normal approximation would suggest, and two methods whose intervals overlap are not distinguishable. The t-interval is exactly the right tool: it widens automatically when you have few runs, protecting you from declaring a winner on noise.

🐍

Build z- and t-intervals in Python

The companion notebook builds the z-interval, derives the t-interval and tabulates how t* shrinks toward z*, checks coverage on skewed data, and loads confidence-intervals-for-a-mean--delivery_times.xlsx to put a 95% interval on the mean delivery time with scipy.stats.t.interval.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy, pandas, scipy, matplotlib, and openpyxl and launch jupyter notebook.

🎓 Key Takeaways

  • CI for a mean = x̄ ± (critical value)·SE, with SE = σ/√n (or s/√n).
  • σ known → z-interval (z = 1.96 for 95%); σ unknown → t-interval with df = n−1.
  • t* > z* (heavier tails), converging to z as n grows; 2.78 at df=4, ~1.96 by df=1000.
  • Conditions: roughly normal data, or n ≥ ~30 for the CLT; skewed + small n under-covers.
  • Real data: mean delivery time 35.9 h with a 95% interval of 34.4–37.3 h; in ML, report metric intervals (few seeds → wider).
6

Practice Challenges

Five short challenges, beginner to intermediate. Try them with NumPy and SciPy before checking the solutions.

1

z-interval (σ known)

With σ = 8 known, build a 95% z-interval for the mean from a sample of 40 drawn from Normal(50, 8).

Hint: x̄ ± 1.96·(8/√40).
2

t-interval (σ unknown)

Now treat σ as unknown: use the sample sd and stats.t.interval for the same sample.

Hint: df = n−1; the interval is slightly wider.
3

t* shrinks toward z*

Tabulate the 95% t critical value for df = 4, 9, 29, 99 and compare to z* = 1.96.

Hint: stats.t.ppf(0.975, df).
4

Coverage check

From a skewed Gamma population, confirm the t-interval coverage is near 95% at n = 40.

Hint: count how often the interval brackets the true mean.
5

Real data: mean delivery time

Load confidence-intervals-for-a-mean--delivery_times.xlsx and report the 95% t-interval for the mean delivery time.

Hint: pd.read_excel(..., sheet_name="Shipments").
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on confidence intervals for a mean. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

🧭
Up next

Means are not the only target. The Confidence Intervals for Proportions & Differences chapter builds intervals for proportions and differences, the foundation of polling and A/B testing, where the interval for a difference tells you whether two groups truly differ.