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.
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=False) is safer when variances differ. - Pooled assumes equal variances.
Paired t-test
Example: same users before/after a change.
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.
Which t-test do you need?
flowchart TD A["Comparing two means?"] --> B["Same subjects,
measured twice?"] B -->|"Yes"| C["Paired t-test
(ttest_rel)"] B -->|"No"| D["Independent t-test
(ttest_ind)"] D --> E["Equal variances?"] E -->|"Unsure/No"| F["Welch t-test
(equal_var=False)"] E -->|"Yes"| G["Pooled t-test
(equal_var=True)"]
Check your understanding
When should you use a paired t-test?
Paired data has the same users measured twice, which is stats.ttest_rel in the lesson.
Show answer
B — Before and after measurements on the same subjects — Paired data has the same users measured twice, which is stats.ttest_rel in the lesson.
What does equal_var=False in stats.ttest_ind do?
Welch does not assume equal variances. The pooled test does.
Show answer
A — Runs the Welch t-test, which is safer when variances differ — Welch does not assume equal variances. The pooled test does.
Group A has values 120 to 160 (mean 140) and B has 90 to 110 (mean 100). What does the independent t-test compare?
t-tests compare means. Here the difference of 40 is judged against the spread within each group.
Show answer
A — The two means, 140 against 100 — t-tests compare means. Here the difference of 40 is judged against the spread within each group.
Which assumption belongs on the t-test list?
The CLT helps with larger n, and shaky assumptions point to non-parametric alternatives.
Show answer
A — Approximate normality within each group, with no extreme outliers — The CLT helps with larger n, and shaky assumptions point to non-parametric alternatives.
🧪 Try It Yourself
Exercise 1 – Independent (Welch) t-test
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Paired t-test (before/after)
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Picking the right test
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Next
Continue to ANOVA (one-way) to compare means across three or more groups at once.