t-test (independent and paired)
When to use t-tests
Use t-tests when you want to compare means.
- Independent (two-sample): two different groups
- Paired: before/after measurements on the same subjects
Independent two-sample t-test
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)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 vs pooled variance
- Welch (
equal_var=Falseequal_var=False) is safer when variances differ. - Pooled assumes equal variances.
Paired t-test
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)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)Assumptions (important)
- 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.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
