Confidence Intervals and Test Assumptions › Confidence Intervals: A Deeper Look
Machine Learning from ScratchAll chapters Confidence Intervals and Test Assumptions › Confidence Intervals: A Deeper LookOne section · Sections in this chapter2 / 9 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
You've probably seen confidence intervals before: "The average treatment effect was 5.2 with a 95% confidence interval of [3.1, 7.3]." But what does this mean, and how does it connect to hypothesis testing?
What a Confidence Interval Really Means
A 95% confidence interval does NOT mean there's a 95% probability that the true parameter lies within the interval. The true parameter is fixed (though unknown). It's either inside the interval or it isn't. There's no probability involved once you've computed the interval.
A 95% confidence interval means: if we repeated our sampling procedure many times and computed a confidence interval each time, 95% of those intervals would contain the true parameter value.
This is a subtle but important distinction. The confidence refers to the procedure, not to any particular interval. Over many repetitions, 95% of the intervals produced by this method will capture the true value.

Constructing a Confidence Interval Step by Step
Let's build a confidence interval from first principles. Suppose you have a sample of n observations and want a confidence interval for the population mean.
Step 1: Calculate the sample mean
Step 2: Calculate the standard error
The standard error measures how much the sample mean varies from sample to sample. If you know the population standard deviation :
If you don't know (the usual case), estimate it with the sample standard deviation:
Step 3: Find the critical value
For a 95% confidence interval (), you need the value that cuts off 2.5% in each tail of the distribution.
If you know , use the z critical value:
If you're estimating with , use the t critical value with degrees of freedom:
Step 4: Construct the interval
import numpy as np
from scipy import stats
# Sample data: reaction times in milliseconds
reaction_times = np.array(
[
245,
268,
252,
241,
259,
263,
248,
271,
255,
249,
262,
247,
258,
244,
266,
253,
260,
242,
257,
251,
]
)
# Step 1: Sample mean
n = len(reaction_times)
sample_mean = np.mean(reaction_times)
# Step 2: Standard error
sample_std = np.std(reaction_times, ddof=1)
standard_error = sample_std / np.sqrt(n)
# Step 3: Critical value for 95% CI
alpha = 0.05
t_critical = stats.t.ppf(1 - alpha / 2, df=n - 1)
# Step 4: Construct the interval
margin_of_error = t_critical * standard_error
ci_lower = sample_mean - margin_of_error
ci_upper = sample_mean + margin_of_error
print(f"Sample size: n = {n}")
print(f"Sample mean: x̄ = {sample_mean:.2f} ms")
print(f"Sample std: s = {sample_std:.2f} ms")
print(f"Standard error: SE = {standard_error:.2f} ms")
print(f"t critical value (df={n - 1}): t = {t_critical:.3f}")
print(f"Margin of error: {margin_of_error:.2f} ms")
print(f"\n95% Confidence Interval: [{ci_lower:.2f}, {ci_upper:.2f}] ms")Sample size: n = 20 Sample mean: x̄ = 254.55 ms Sample std: s = 8.80 ms Standard error: SE = 1.97 ms t critical value (df=19): t = 2.093 Margin of error: 4.12 ms 95% Confidence Interval: [250.43, 258.67] ms