A two-tailed test asks whether a population parameter differs from a claimed value in either direction. Its alternative hypothesis uses “not equal to,” so unusually high and unusually low results can count as evidence against the null hypothesis. section 6.15 section 6.4
A two-tailed test works by:
- Setting the null hypothesis to equality, such as H0: μ = 142. section 6.6
- Setting the alternative hypothesis to difference, such as Ha: μ ≠ 142. section 6.6
- Splitting the significance level α between both tails of the distribution. section 6.15 section 6.5
- Rejecting the null when the test statistic enters either critical region or the p-value falls below α. section 6.15
Checking a potato-chip label
A researcher tests whether bags labeled 142 grams average 142 grams. After sampling 41 bags from different stores, the sample mean is 140.2 grams and the sample standard deviation is 4.79 grams. The difference alone is not enough: natural variation may explain it, so the sample statistic must be compared with two-tailed critical values. section 6.6
from math import sqrt #1
from scipy.stats import norm #1
mu = 142.0 #2
n = 41 #3
x_bar = 140.2 #4
s = 4.79 #5
alpha = 0.05 #6
z_stat = (x_bar - mu) / (s / sqrt(n)) #7
critical_z = norm.ppf(1 - (alpha / 2)) #8
print("Population mean: ", mu) #9
print("Sample mean: ", x_bar) #9
print("Sample standard deviation: ", s) #9
print("Sample size: ", n) #9
print("Level of confidence: ", 1 - alpha) #9
print("Level of significance: ", alpha) #9
print("z-statistic: ", z_stat) #9
print("Critical value: ", critical_z) #9
if -critical_z <= z_stat <= critical_z: #10
print("Decision: Fail to reject the null hypothesis. "
"The sample does not provide enough evidence the mean is different.") #11
else:
print("Decision: Reject the null hypothesis. "
"The sample provides enough evidence the mean is different.") #11
"""
Population mean: 142.0
Sample mean: 140.2
Sample standard deviation: 4.79
Sample size: 41
Level of confidence: 0.95
Level of significance: 0.05
z-statistic: -2.4061844733568267
Critical value: 1.959963984540054
Decision: Reject the null hypothesis. The sample provides enough evidence the
mean is different
"""
A Python listing calculates the standardized statistic, two-sided critical value, and corresponding fail-to-reject or reject decision. section 6.6
