What a confidence interval means
A 95% confidence interval is not:
- “There is a 95% probability the true mean is in this interval.”
It is:
- “If we repeat this sampling process many times, ~95% of such intervals will contain the true mean.”
CI for a mean (using t-distribution)
Use when:
- Population std is unknown (common)
- Sample size is small-ish
import numpy as np
from scipy import stats
x = np.array([12, 13, 10, 9, 11, 12, 14, 8, 13, 11])
n = len(x)
mean = x.mean()
se = x.std(ddof=1) / np.sqrt(n)
alpha = 0.05
t_crit = stats.t.ppf(1 - alpha/2, df=n-1)
ci = (mean - t_crit * se, mean + t_crit * se)
print("mean:", mean)
print("95% CI:", ci)CI for a proportion
Example: conversion rate.
import numpy as np
# 60 conversions out of 1000
x = 60
n = 1000
p_hat = x / n
alpha = 0.05
z = 1.96
se = np.sqrt(p_hat * (1 - p_hat) / n)
ci = (p_hat - z * se, p_hat + z * se)
print("p_hat:", p_hat)
print("95% CI:", ci)Practical notes
- CI width depends on sample size: larger n → tighter CI.
- CI helps decision-making: overlap/non-overlap gives a quick signal (not a full test).
Building a CI, step by step
flowchart LR A["Sample mean"] --> B["Standard error
(s / sqrt n)"] B --> C["Critical value
(t or z)"] C --> D["Margin of error
= critical value x SE"] D --> E["CI = mean +/- margin"]
Visualize it
Widening the sample size shrinks the confidence interval around the mean. Watch the
amber band tighten around the estimate as n grows — the true mean (dashed line) stays
fixed, only your certainty about it improves:
Check your understanding
What is the correct reading of a 95% confidence interval?
The true mean is fixed and the interval is the random part, so the 95% describes the procedure.
Show answer
B — If you repeat the sampling many times, about 95% of such intervals contain the true mean — The true mean is fixed and the interval is the random part, so the 95% describes the procedure.
Why does the lesson use stats.t.ppf for the mean CI?
The t-distribution with n - 1 degrees of freedom is used when the population std is unknown.
Show answer
A — Population std is unknown and the sample is small-ish — The t-distribution with n - 1 degrees of freedom is used when the population std is unknown.
For 60 conversions out of 1000, what is p_hat and roughly what is the 95% CI?
p_hat = 0.06 and se = sqrt(0.06 times 0.94 / 1000) is about 0.0075, so 0.06 plus or minus 1.96 times that.
Show answer
B — 0.06, from about 0.045 to 0.075 — p_hat = 0.06 and se = sqrt(0.06 times 0.94 / 1000) is about 0.0075, so 0.06 plus or minus 1.96 times that.
What happens to CI width when the sample size n grows?
The standard error shrinks with the square root of n, so larger samples give tighter intervals.
Show answer
C — It tightens — The standard error shrinks with the square root of n, so larger samples give tighter intervals.
🧪 Try It Yourself
Exercise 1 – 95% CI for a mean
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – CI for a conversion rate
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Sample size shrinks the interval
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Next
Continue to Hypothesis Testing to turn "is the true value in this range" into a formal yes/no decision with p-values.