10.1 Confidence Interval for Mean μ (z distribution)
Consider a random sample size of from a population with an unknow mean and known standard deviation . To construct an interval estimate of the population mean with confidence, use the following:
(the confidence level)
(the Margin of Error)
(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 where 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.
# 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.
- Find the margin of error for estimating the population mean.
We use the error_z_mean function above with arguments: C = 0.9, std = 3, n = 40.
- Click
playbutton in the above codedef error_z_mean(C,std\,n) - Click
+ Code - Type `error_z_mean(.90,3,40)
error_z_mean(.90,3,40)
Show expected output
The Margin of Error is 0.780222581813336Confidence 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.
- Find a 90% confidence interval for the true (population) mean of the exam scores.
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:
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.