An independent sample contains observations collected from a group unrelated to another group. Independent-sample testing compares the groups’ means to decide whether their difference is large enough to challenge an equal-means claim. section 6.0
Set up an independent-sample hypothesis test by:
- Treating each group as its own population and labeling its mean, standard deviation, and sample size with subscripts such as 1 and 2. section 6.14
- Writing the alternative hypothesis for the claim, then defining the null hypothesis as its opposite. section 6.14
- Choosing the tail from the claim: μ1 > μ2 creates a right-tailed test, while a difference can also be tested against a known value. section 6.14
Two classrooms, one exam
Compare math scores from Classroom A and Classroom B after the same final exam. If Classroom B appears lower, test the claim that μ1 > μ2 at a 5% significance level, using each class’s mean, standard deviation, and sample size. section 6.14
from math import sqrt
from scipy.stats import norm #1
LEFT_TAIL, RIGHT_TAIL, TWO_TAIL = 0, 1, 2
# MODIFY PARAMETERS IN THIS BOX
# ==============================
x_bar1, s1, n1 = 83, 11.2, 37 #2
x_bar2, s2, n2 = 75, 10.9, 35 #3
mu_diff = 0
alpha = 0.05
tail_type = RIGHT_TAIL
# ==============================
if n1 <= 30 or n2 <= 30: #4
raise ValueError("Sample size is too small.") #4
z_stat = ((x_bar1 - x_bar2) - mu_diff) / sqrt((s1 ** 2 / n1) + (s2 ** 2 / n2)) #5
if tail_type == TWO_TAIL: #6
print(f"TWO-TAIL TEST") #6
p_value = 2 * norm.cdf(-abs(z_stat)) #6
elif tail_type == LEFT_TAIL: #6
print(f"LEFT TAIL TEST") #6
p_value = norm.cdf(z_stat) #6
elif tail_type == RIGHT_TAIL: #6
print(f"RIGHT TAIL TEST") #6
p_value = 1 - norm.cdf(z_stat) #6
else: #6
raise ValueError("Invalid tail type. Choose from LEFT_TAIL, RIGHT_TAIL, or TWO_TAIL.") #6
print("Level of confidence: ", 1 - alpha) #7
print("Level of significance: ", alpha) #7
print(f"z-statistic: {z_stat:.4f}") #7
print(f"p-value: {p_value:.4f}") #7
if p_value < alpha: #8
print(f"Reject the null hypothesis at alpha = {alpha}. There is evidence to support the alternative hypothesis.") #8
else: #8
print(f"Fail to reject the null hypothesis at alpha = {alpha}. There is not enough evidence to support the alternative hypothesis.") #8
# RIGHT TAIL TEST
# Level of confidence: 0.95
# Level of significance: 0.05
# z-statistic: 3.0713
# p-value: 0.0011
# Reject the null hypothesis at alpha = 0.05. There is evidence to support the alternative hypothesis.
Modify the two groups’ means, standard deviations, sample sizes, hypothesized difference, significance level, and tail type; the Python code calculates the z statistic and p-value. section 6.14
