Confidence intervals show a range where a parameter likely lies. They help us understand how sure we are about our estimates.
0
0
Confidence intervals on parameters in SciPy
Introduction
When you want to know the range of possible values for a mean from sample data.
When estimating the effect size in an experiment and want to express uncertainty.
When comparing two groups and want to see if their means differ significantly.
When reporting results in a way that shows reliability, not just a single number.
Syntax
SciPy
from scipy import stats # Example: Calculate confidence interval for the mean mean = sample_data.mean() sem = stats.sem(sample_data) # standard error of the mean confidence = 0.95 interval = stats.t.interval(confidence, len(sample_data)-1, loc=mean, scale=sem)
stats.sem calculates the standard error of the mean.
stats.t.interval returns the confidence interval using the t-distribution.
Examples
This example calculates a 95% confidence interval for the mean of a small dataset.
SciPy
import numpy as np from scipy import stats data = np.array([5, 7, 8, 9, 10]) mean = np.mean(data) sem = stats.sem(data) ci = stats.t.interval(0.95, len(data)-1, loc=mean, scale=sem) print(ci)
This example calculates a 99% confidence interval, which is wider because we want more certainty.
SciPy
import numpy as np from scipy import stats # 99% confidence interval data = np.array([12, 15, 14, 16, 13, 15]) mean = np.mean(data) sem = stats.sem(data) ci = stats.t.interval(0.99, len(data)-1, loc=mean, scale=sem) print(ci)
Sample Program
This program calculates the average test score and the 95% confidence interval around that average. It shows the range where the true average likely falls.
SciPy
import numpy as np from scipy import stats # Sample data: test scores scores = np.array([88, 92, 85, 91, 87, 90, 93]) # Calculate mean and standard error mean_score = np.mean(scores) sem_score = stats.sem(scores) # Calculate 95% confidence interval for the mean confidence_level = 0.95 ci_lower, ci_upper = stats.t.interval(confidence_level, len(scores)-1, loc=mean_score, scale=sem_score) print(f"Mean score: {mean_score:.2f}") print(f"95% confidence interval: ({ci_lower:.2f}, {ci_upper:.2f})")
OutputSuccess
Important Notes
Confidence intervals depend on sample size; bigger samples give narrower intervals.
The t-distribution is used when the sample size is small and population standard deviation is unknown.
Always check assumptions like normality when interpreting confidence intervals.
Summary
Confidence intervals give a range for parameter estimates, showing uncertainty.
Use stats.t.interval with sample mean and standard error to calculate them.
Higher confidence levels mean wider intervals.