# Calculate Confidence Intervals in Python

A Confidence Interval is a range of values that is likely to contain the mean of a population. In Python, it is possible to calculate a confidence interval using the mean and standard deviation of a dataset. This is done by using the stats.norm.interval() function, which takes in the sample mean, sample standard deviation, and the desired confidence level as parameters, and returns the lower and upper bounds of the confidence interval. This interval can then be used to draw statistical inferences about the population from which the sample was taken.


confidence interval for a mean is a range of values that is likely to contain a population mean with a certain level of confidence.

It is calculated as:

Confidence Interval = x  +/-  t*(s/√n)

where:

  • xsample mean
  • t: t-value that corresponds to the confidence level
  • s: sample standard deviation
  • n: sample size

This tutorial explains how to calculate confidence intervals in Python.

Confidence Intervals Using the t Distribution

If we’re working with a small sample (n <30), we can use the from the scipy.stats library to calculate a confidence interval for a population mean.

The following example shows how to calculate a confidence interval for the true population mean height (in inches) of a certain species of plant, using a sample of 15 plants:

import numpy as np
import scipy.stats as st

#define sample data
data = [12, 12, 13, 13, 15, 16, 17, 22, 23, 25, 26, 27, 28, 28, 29]

#create 95% confidence interval for population mean weight
st.t.interval(alpha=0.95, df=len(data)-1, loc=np.mean(data), scale=st.sem(data)) 

(16.758, 24.042)

The 95% confidence interval for the true population mean height is (16.758, 24.042).

You’ll notice that the larger the confidence level, the wider the confidence interval. For example, here’s how to calculate a 99% C.I. for the exact same data:

#create 99% confidence interval for same sample
st.t.interval(alpha=0.99, df=len(data)-1, loc=np.mean(data), scale=st.sem(data)) 

(15.348, 25.455)

The 99% confidence interval for the true population mean height is (15.348, 25.455). Notice that this interval is wider than the previous 95% confidence interval.

Confidence Intervals Using the Normal Distribution

If we’re working with larger samples (n≥30), we can assume that the sampling distribution of the sample mean is normally distributed (thanks to the ) and can instead use the from the scipy.stats library.

The following example shows how to calculate a confidence interval for the true population mean height (in inches) of a certain species of plant, using a sample of 50 plants:

import numpy as np
import scipy.stats as st

#define sample data
np.random.seed(0)
data = np.random.randint(10, 30, 50)

#create 95% confidence interval for population mean weight
st.norm.interval(alpha=0.95, loc=np.mean(data), scale=st.sem(data))

(17.40, 21.08)

The 95% confidence interval for the true population mean height is (17.40, 21.08).

And similar to the t distribution, larger confidence levels lead to wider confidence intervals. For example, here’s how to calculate a 99% C.I. for the exact same data:

#create 99% confidence interval for same sample
st.norm.interval(alpha=0.99, loc=np.mean(data), scale=st.sem(data))

(16.82, 21.66)

The 95% confidence interval for the true population mean height is (17.82, 21.66).

How to Interpret Confidence Intervals

Suppose our 95% confidence interval for the true population mean height of a species of plant is:

95% confidence interval = (16.758, 24.042)

The way to interpret this confidence interval is as follows:

There is a 95% chance that the confidence interval of [16.758, 24.042] contains the true population mean height of plants.

Another way of saying the same thing is that there is only a 5% chance that the true population mean lies outside of the 95% confidence interval. That is, there’s only a 5% chance that the true population mean height of plants is less than 16.758 inches or greater than 24.042 inches.

x