Login
📚 Python for Introductory Statistics
Chapters ▾

10.1 Confidence Interval for Mean μ (z distribution)

Watch demo video 1

Watch demo video 2

Consider a random sample size of n from a population with an unknow mean μ and known standard deviation σ. To construct an interval estimate of the population mean μ with (1α)100 confidence, use the following:

C=1α (the confidence level)

E=zα2σn (the Margin of Error)

x¯E<μ<x¯+E (the confidence interval)

For large sample size the sample standard deviation can be used for the population standard deviation σ.

Use scipy.stats for obtaining the value of zα2 where C=1α as follows:

from scipy import stats
stats.norm.ppf(C+(1-C)/2,mean,std)

Margin of Error for μ (summary statistics given)

To find the margin of error for estimating the population mean when summary statistics such as the sample size and population standard deviation are given, use the error_z_mean function below with arguments: confidence level C, Population standard deviation std and sample size n.

E = z α 2 σ n

# Margin of Error, given confidence level C, std, n
def error_z_mean(C,std,n):
  from scipy import stats
  er = (std/n**.5)*stats.norm.ppf(C+(1-C)/2,0,1)
  return print("The Margin of Error is",er)

Or you can use the code below by substituting std, n and C:

from scipy import stats
error = (std/n**.5)*stats.t.ppf(C+(1-C)/2,n-1,0,1)

Example: Midterm exam scores are normally distributed with an unknown population mean and a population standard deviation of three points. A random sample of 40 scores has mean of 72. Find a 90% margin of error and the confidence interval estimate for the population mean exam score.

We use the error_z_mean function above with arguments: C = 0.9, std = 3, n = 40.

  1. Click play button in the above code def error_z_mean(C,std\,n)
  2. Click + Code
  3. Type `error_z_mean(.90,3,40)
error_z_mean(.90,3,40)
Show expected output
The Margin of Error is 0.780222581813336

Confidence Interval for μ (summary statistics given)

To find the confidence interval for the population mean when summary statistics such as the sample size, mean and populaton standard deviation are given, use the conf_z_mean function below with arguments: confidence level C, mean, std, and n.

# Confidence interval, given confidence level C, std, n  
def conf_z_mean(C,mean,std,n):
     from scipy import stats
     ebm = (std/n**.5)*stats.norm.ppf(C+(1-C)/2,0,1)
     return print("(", mean-ebm, ",", mean+ebm, ')')

Example: Midterm exam scores are normally distributed with an unknown population mean and a population standard deviation of three points. A random sample of 40 scores has mean of 72. Find a 90% confidence interval estimate for the population mean exam score.

We use the conf_z_mean function above with arguments: C = 0.9, mean = 72, std = 3, n = 40.

conf_z_mean(.90,72,3,40)
Show expected output
( 71.21977741818667 , 72.78022258181333 )

Interpretation: 71.21977741818667<μ<72.78022258181333

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.