How to Calculate Confidence Intervals in Python?

Published on Aug. 22, 2023, 12:15 p.m.

To calculate confidence intervals in Python, you can use the scipy.stats library. The library provides functions to calculate confidence intervals for both the normal and t-distributions.

Here’s an example of how to calculate a 95% confidence interval for a normally distributed set of data:

import numpy as np
from scipy import stats

# Generate some example data
data = np.random.normal(size=100)

# Calculate the mean and standard deviation of the data
mean = np.mean(data)
std = np.std(data, ddof=1)

# Calculate the 95% confidence interval
n = len(data)
se = std / np.sqrt(n)
z = stats.norm.ppf(0.975)
ci = (mean - z * se, mean + z * se)

print("Sample mean:", mean)
print("Standard deviation:", std)
print("95% Confidence interval:", ci)

In this example, we first generate some example data using numpy.random.normal(). We then calculate the sample mean and standard deviation of the data using numpy.mean() and numpy.std(). To calculate the 95% confidence interval, we first calculate the standard error of the mean using std / np.sqrt(n), where n is the sample size. We then calculate the z-score for a 95% confidence interval using stats.norm.ppf(0.975). Finally, we calculate the confidence interval using (mean - z * se, mean + z * se).

You can use the stats.t functions in a similar way to calculate confidence intervals for t-distributions.

Tags: