liveBook · Manning

Independent sample

As explained in Grokking Statistics

An independent sample is a sample whose observations are collected separately from those of another sample, typically from a separate population.

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

How Grokking Statistics covers it

How deep it goes
Defines unrelated groups, develops directional hypotheses, and implements z-statistic and p-value calculations while distinguishing paired tests as beyond scope.
Examples
Uses classroom math scores and adult-weight comparisons between Colorado and Texas as independent-group examples.
What it assumes
Introduces the distinction from one-sample and paired tests while using means, standard deviations, sample sizes, and hypotheses.
What you can do after
Apply it via Python and SciPy to calculate z statistics and tail-specific p-values for two-group mean comparisons.
How it connects
Connects independent samples with hypothesis testing, null hypotheses, alternative hypotheses, one-sample tests, and paired samples.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Statistics. The section links point to the parts of the book it draws on.