Okay, we've established the concept of a confidence interval: a range computed from sample data that is likely to contain the true value of an unknown population parameter, like the population mean (μ). Now, let's get practical and learn how to actually compute these intervals for the mean.
The general structure for a confidence interval is:
Point Estimate±Margin of Error
For the population mean (μ), our best point estimate is usually the sample mean (xˉ). The margin of error quantifies the uncertainty around this point estimate and depends on two main factors: the variability of the data and how confident we want to be. Specifically:
Margin of Error=Critical Value×Standard Error of the Mean
Before we build the full interval, we need to understand the standard error of the mean (SEM). Remember the Central Limit Theorem? It tells us that the distribution of sample means (the sampling distribution) tends to be normal, centered around the true population mean μ. The standard deviation of this sampling distribution is the SEM. It measures how much we expect sample means to vary from sample to sample.
If we know the true population standard deviation σ, the SEM is: SE=nσ where n is the sample size.
However, we usually don't know the population standard deviation σ. So, we estimate it using the sample standard deviation s. In this case, the estimated SEM is: SE=ns This estimated SEM is what we'll typically use in practice. Notice that as the sample size n increases, the SEM decreases. Larger samples lead to more precise estimates of the population mean.
The exact calculation depends on whether the population standard deviation (σ) is known or unknown.
This scenario is less common in real-world machine learning problems because if you knew the population standard deviation, you might already know the population mean, negating the need for an interval. However, it's a simpler starting point.
When σ is known, the sampling distribution of the mean follows a normal distribution (thanks to the CLT, assuming n is sufficiently large or the population is normally distributed). We use the standard normal (Z) distribution to find the critical value.
The confidence interval formula is: xˉ±Zα/2nσ
Here:
You can find Zα/2 using standard normal tables or Python's SciPy library:
import scipy.stats as stats
import numpy as np
# Example: Find Z critical value for a 95% confidence interval
confidence_level = 0.95
alpha = 1 - confidence_level
# Use ppf (percent point function, inverse of CDF)
# We need the point where 1 - alpha/2 of the distribution is to the left
z_critical = stats.norm.ppf(1 - alpha / 2)
print(f"Z critical value for {confidence_level*100}% confidence: {z_critical:.3f}")
# Output: Z critical value for 95.0% confidence: 1.960
So, for a 95% confidence interval with known σ, the margin of error is 1.960×nσ.
This is the much more common situation. We don't know σ, so we estimate it using the sample standard deviation s. When we substitute s for σ in the standard error calculation (SE=s/n), the sampling distribution of the statistic s/nxˉ−μ no longer follows a standard normal distribution perfectly, especially for smaller sample sizes.
Instead, it follows a t-distribution (also known as Student's t-distribution). The t-distribution is similar in shape to the normal distribution (bell-shaped, symmetric around zero) but has heavier tails. This means it accounts for the additional uncertainty introduced by estimating σ with s.
The t-distribution is characterized by its degrees of freedom (df), which depend on the sample size. For a confidence interval for a single mean, the degrees of freedom are: df=n−1
The confidence interval formula when σ is unknown is: xˉ±tα/2,n−1ns
Here:
As the sample size n (and thus the degrees of freedom) increases, the t-distribution approaches the standard normal distribution.
You can find the t-critical value using SciPy:
import scipy.stats as stats
import numpy as np
# Example: Find t critical value for a 95% CI with n=25
sample_size = 25
confidence_level = 0.95
alpha = 1 - confidence_level
degrees_freedom = sample_size - 1
# Use ppf for the t-distribution
t_critical = stats.t.ppf(1 - alpha / 2, df=degrees_freedom)
print(f"t critical value for {confidence_level*100}% confidence (df={degrees_freedom}): {t_critical:.3f}")
# Output: t critical value for 95.0% confidence (df=24): 2.064
Notice this t-critical value (2.064) is slightly larger than the Z-critical value (1.960) for the same confidence level, resulting in a wider interval, reflecting the added uncertainty.
Let's say we're evaluating the inference time (in milliseconds) of a machine learning model. We run the model on a sample of n=30 different inputs and record the times. We find:
We want to calculate a 99% confidence interval for the true average inference time (μ).
import scipy.stats as stats
t_critical = stats.t.ppf(1 - 0.01 / 2, df=29)
print(f"t_critical: {t_critical:.3f}")
# Output: t_critical: 2.756
Interpretation: We are 99% confident that the true average inference time for this model across all possible inputs lies between 78.96 ms and 91.04 ms, based on our sample data.
SciPy's stats.t.interval
function can compute the interval directly:
import scipy.stats as stats
import numpy as np
# Sample data parameters
sample_mean = 85
sample_std_dev = 12
sample_size = 30
confidence = 0.99
# Calculate standard error
sem = sample_std_dev / np.sqrt(sample_size)
degrees_freedom = sample_size - 1
# Calculate the confidence interval
ci = stats.t.interval(confidence, degrees_freedom, loc=sample_mean, scale=sem)
print(f"Sample Mean: {sample_mean}")
print(f"Standard Error: {sem:.3f}")
print(f"{confidence*100}% Confidence Interval: ({ci[0]:.3f}, {ci[1]:.3f})")
# Output:
# Sample Mean: 85
# Standard Error: 2.191
# 99.0% Confidence Interval: (78.961, 91.039)
This confirms our manual calculation.
The width of the confidence interval (Upper Limit - Lower Limit) reflects the precision of our estimate. Two primary factors influence this width:
The plot illustrates how the relative width of a confidence interval (proportional to Critical Value / sqrt(n)) decreases rapidly as sample size increases. Higher confidence levels consistently produce wider intervals for any given sample size. (Note: Uses Z-values for simplicity, t-values show similar behavior).
Calculating confidence intervals provides a practical way to quantify the uncertainty in our estimates based on sample data, moving beyond simple point estimates to provide a range of plausible values for population parameters like the mean. This is a foundational technique for drawing reliable conclusions from data.
© 2025 ApX Machine Learning