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 <30), we can use the 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.

Using scale=st.sem(data) for samples with size n > 30 gives ridiculous results.

I believe it is necessary to use scale = np.std(data) instead.

Please advise.

Thanks for the help in implementation! However, I believe your final interpretation of the CI is incorrect. The population mean either is or is not inside the CI you calculated and we cannot say that “it is with 95% probability.” All we can say is that if we were to repeat the identical sampling procedure for plants 100 times, and we calculated the corresponding CI each time, then approximately 95 out of 100 CIs we calculated will contain the true population mean. Note, again, that this DOES NOT mean that any particular CI will contain the true population mean with 95% probability, only that the collection will.

Gracias