*47*

AÂ **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:

**x:Â**sample 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 t.interval() function 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 Central Limit Theorem) and can instead use the norm.interval() function 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.