Welch's t-test keeps each group's own variance, and its Welch-Satterthwaite degrees of freedom, NIST notes, are robust to unequal sizes and unequal variances. Student's original pooled test averages the two variances into one.
Welch's t is the gap in means over a standard error that adds each group's own squared standard error, sd²/n. Its degrees of freedom come from the Welch-Satterthwaite formula, which you'll write in the editor as welch_df.
def welch(mean_a, sd_a, n_a, mean_b, sd_b, n_b):
"""Welch's t and its degrees of freedom, from each group's mean, standard deviation and size."""
t = (mean_a - mean_b) / math.sqrt(sd_a ** 2 / n_a + sd_b ** 2 / n_b)
return t, welch_df(sd_a, n_a, sd_b, n_b)
# carrier A: 40 parcels, carrier B: 30
t, df = welch(4.1, 1.2, 40, 3.6, 0.6, 30)
print(f"Welch: t = {t:.2f}, df = {df:.1f}, p = {t_two_sided_p(t, df):.4f}")
pooled = ((40 - 1) * 1.2 ** 2 + (30 - 1) * 0.6 ** 2) / (40 + 30 - 2)
t_pooled = (4.1 - 3.6) / math.sqrt(pooled * (1 / 40 + 1 / 30))
print(f"pooled: t = {t_pooled:.2f}, df = 68, p = {t_two_sided_p(t_pooled, 68):.4f}")
Welch: t = 2.28, df = 60.3, p = 0.0260
pooled: t = 2.09, df = 68, p = 0.0402
Same data, different answers. Delacre, Lakens and Leys found that Welch's test controls false positives better when the variances differ and loses little when they're equal, and that choosing between the two with a variance test first often fails. Default to Welch. Watch out in SciPy: ttest_ind runs Welch's test only when you pass equal_var=False.
In the editor, write welch_df(sd_a, n_a, sd_b, n_b), the Welch-Satterthwaite degrees of freedom. It prints the two carriers' 60.3.