Lesson 11 of 18
Two-Sample t-Test
Comparing Two Groups
The two-sample t-test checks whether two independent groups have different means.
Hypotheses:
- : (no difference between groups)
- :
import math, statistics
group_a = [1, 2, 3, 4, 5]
group_b = [6, 7, 8, 9, 10]
n1, n2 = len(group_a), len(group_b)
m1, m2 = statistics.fmean(group_a), statistics.fmean(group_b)
v1, v2 = statistics.variance(group_a), statistics.variance(group_b)
# Welch's t-statistic
t_stat = (m1 - m2) / math.sqrt(v1/n1 + v2/n2)
print(round(t_stat, 4)) # -5.0
Welch's t-Test
Use Welch's formula (no equal-variance assumption) for the most robust results. The t-statistic is:
t = rac{ar{x}_1 - ar{x}_2}{sqrt{rac{s_1^2}{n_1} + rac{s_2^2}{n_2}}}
The degrees of freedom use the Welch-Satterthwaite equation:
ight)^2}{rac{(s_1^2/n_1)^2}{n_1-1} + rac{(s_2^2/n_2)^2}{n_2-1}}$$ ### Effect Size vs Statistical Significance A statistically significant result ($p < 0.05$) does not necessarily mean the difference is **practically important**. With large samples, even tiny differences become significant. ### Same Groups → t = 0 When both groups are identical, $t = 0$ and $p = 1.0$. ### Your Task Implement `ttest_independent(group_a, group_b)` that prints the $t$-statistic (rounded to 4 decimal places) and whether the result is significant ($p < 0.05$).Pyodide loading...
Loading...
Click "Run" to execute your code.