11.1 Hypothesis Testing for Mean μ (z-test)
Suppose a random sample of size is drawn from a population with unknow mean and either the standard deviation is given or the sample size is large.
To test a hypothesis about the mean of the form:
smaller
OR:
greater
OR:
two-sided
we use the following test-statistic
Use the function z_test_mean below with arguments: null_mean = , sigma = , sample_mean = , n, alternative = 'smaller' OR 'greater' OR 'two-sided'. By default it is set to 'two-sided'.
# z test with alternative 'smaller' or 'greater' or 'two-sided'
def z_test_mean(null_mean,sigma,sample_mean,n,alternative='two-sided'):
from scipy import stats
z = (sample_mean-null_mean)/(sigma/n**.5)
if alternative == 'smaller':
pv = stats.norm.cdf(z,0,1)
elif alternative == 'greater':
pv = 1-stats.norm.cdf(z,0,1)
elif alternative == 'two-sided':
if z< 0:
pv = 2*stats.norm.cdf(z,0,1)
else: pv = 2*(1-stats.norm.cdf(z,0,1))
else:
print("alternative option error")
return print("z test statistic = ", z, "and p-value = ",pv)
Example: The president of a company claims that the average number of minutes employees waste time surfing the internet is more than 100 minutes a day. The population number of hours wasted is normally distributed with standard deviation of 10 minutes. A random sample of 19 employees from this company has a mean of 105 minutes. Do this sample data provide evidence that the mean wasted time for this company is more than 100 minutes? Use .
Use the z_test_mean function above with arguments: null_mean = 100, sigma = 10, sample_mean = 105, n = 19, alternative = 'greater' (more than 100).
- Click
playbutton on code abovedef z_test_mean - Click
+ Code - Type
z_test_mean(100,10,105,19,'greater) - Click
playbutton
z_test_mean(100,10,105,19,'greater')
Show expected output
z test statistic = 2.179449471770337 and p-value = 0.014649147202275281Interpretation: p-value = 0.014649 < = 0.05. The null hpypothesis is rejected.
Alternatively, compare z = (sample_mean-null_mean)/(sigma/n**.5) with the critical z stats.norm.ppf(1-.05,0,1) = 1.6448 see code below. thus reject the null-hypothesis.
from scipy import stats
stats.norm.ppf(1-0.05,0,1) # right-tailed test
Show expected output
1.6448536269514722Example: A government official claims that the average recovery time for Covid19 patients with severe symptoms is 6 weeks. A random sample of 23 patients with severe symptoms has a mean recovery time of 5.4 weeks with standard deviation of 1.1 weeks. Assume recovery time is normally distributed with standard deviation of 1.5 () weeks. Test the government's official claim at of 0.05. Use the z_test_mean function below.
# z test with alternative 'smaller' or 'greater' or 'two-sided'
def z_test_mean(null_mean,sigma,sample_mean,n,alternative='two-sided'):
from scipy import stats
z = (sample_mean-null_mean)/(sigma/n**.5)
if alternative == 'smaller':
pv = stats.norm.cdf(z,0,1)
elif alternative == 'greater':
pv = 1-stats.norm.cdf(z,0,1)
elif alternative == 'two-sided':
if z< 0:
pv = 2*stats.norm.cdf(z,0,1)
else: pv = 2*(1-stats.norm.cdf(z,0,1))
else:
print("alternative option error")
return print("z test statistic = ", z, "and p-value = ",pv)
null_mean=6,sigma=1.5,sample_mean=5.4,n=23,alternative='two-sided
z_test_mean(6,1.5,5.4,23,'two-sided')
Show expected output
z test statistic = -1.9183326093250865 and p-value = 0.05506885012079212Interpretation: Because the p-value 0.05506885012079212 is not less than 0.05, reject the null hypothesis. There is no sufficient evidence to reject the official's claim.
Example: The work week of a particular organization workers is normally distributed with a mean of 47 hours. A newly hired manager at this company believes that employees average work hours is different from 47. She randomly asks 12 workers for the lengths in hours of their work week. Their responses are shown below. Test the claim using a 10% level of significance. The population standard deviation is 5.
Hours: 46, 43, 55, 50, 46, 67, 48, 47, 48, 49, 55, 54
We need to find the mean and sample size of the data before we call the function, z_test_mean.
import numpy as np
x = [46, 43, 55, 50, 46, 67, 48, 47, 48, 49, 55, 54]
m = np.mean(x)
n = np.size(x)
print('mean is',m,'\n n is',n)
Show expected output
mean is 50.666666666666664
n is 12Using the sample_mean=m and n from the above code, we call z_test_mean with arguments:47,5,50.666667,12,'two-sided'. But by default the function is set to 'two-sided'
z_test_mean(47,5,50.6666667,12) #you need to run the ttestmean function above first before you call it.
Show expected output
z test statistic = 2.540341207528364 and p-value = 0.011074436944822352Interpretation: The p-value is not less than . Fail to reject the null hypothesis.
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.