Two-Sample (Independent) T-Test
Hypothesis Testing
Comparing Two Independent Groups
The independent two-sample t-test determines whether two groups have different population means. Choosing between pooled and Welch's versions depends on whether equal variances can be assumed.
- A/B Testing — Determining whether website changes produce meaningful differences
- Clinical Trials — Comparing treatment groups in randomized experiments
- Market Research — Evaluating differences between customer segments
The two-sample t-test is the backbone of comparative research.
Tests whether two independent groups have equal population means.
Two Versions
Pooled T-Test (equal variances assumed)
Welch's T-Test (unequal variances — default recommendation)
Complete Python Implementation
Visualization
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Box plot comparison
axes[0].boxplot([method_a, method_b], labels=['Method A', 'Method B'],
patch_artist=True,
boxprops=dict(facecolor='lightblue'))
axes[0].set_title('Score Distribution by Teaching Method')
axes[0].set_ylabel('Test Score')
# Distribution overlap
x = np.linspace(40, 120, 500)
axes[1].plot(x, stats.norm.pdf(x, method_a.mean(), method_a.std()), 'b-', linewidth=2, label='Method A')
axes[1].plot(x, stats.norm.pdf(x, method_b.mean(), method_b.std()), 'r-', linewidth=2, label='Method B')
axes[1].fill_between(x, stats.norm.pdf(x, method_a.mean(), method_a.std()), alpha=0.3, color='blue')
axes[1].fill_between(x, stats.norm.pdf(x, method_b.mean(), method_b.std()), alpha=0.3, color='red')
axes[1].set_title('Distribution Overlap')
axes[1].legend()
plt.tight_layout()
plt.savefig('two_sample_t.png', dpi=150)
plt.show()