Skip to content

t-test (independent and paired)

Use t-tests when you want to compare means.

  • Independent (two-sample): two different groups
  • Paired: before/after measurements on the same subjects

Example: average spend differs between two groups.

Independent t-test
import numpy as np
from scipy import stats
 
A = np.array([120, 140, 130, 150, 160])
B = np.array([100, 90, 110, 95, 105])
 
t_stat, p = stats.ttest_ind(A, B, equal_var=False)  # Welch t-test
print("t:", t_stat)
print("p:", p)
  • Welch (equal_var=False) is safer when variances differ.
  • Pooled assumes equal variances.

Example: same users before/after a change.

Paired t-test
import numpy as np
from scipy import stats
 
before = np.array([10, 12, 11, 9, 13])
after  = np.array([11, 12, 12, 10, 14])
 
t_stat, p = stats.ttest_rel(after, before)
print("t:", t_stat)
print("p:", p)
  • Data points are independent (except paired case)
  • Approximately normal within each group (CLT helps with larger n)
  • No extreme outliers

If assumptions are shaky, consider non-parametric alternatives.

diagram Choosing between t-tests mermaid
The decision hinges on whether the two groups share the same subjects.

Exercise 2 – Paired t-test (before/after)

Section titled “Exercise 2 – Paired t-test (before/after)”

Continue to ANOVA (one-way) to compare means across three or more groups at once.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading