Learning Guides
Hypothesis Testing Using T-Test: Complete Guide
Quick answer: Learn the t-test for hypothesis testing, including one-sample, two-sample and paired t-tests, when to use each, and how to interpret results in Python.
What is a T-Test?
A t-test is a statistical hypothesis test used to determine whether the means of one or two groups differ significantly, when the population standard deviation is unknown and must be estimated from the sample itself. This is the most commonly used hypothesis test in practical data analysis.
Three Types of T-Tests
| Type | Used when |
|---|---|
| One-sample t-test | Comparing a sample mean to a known or hypothesised value |
| Independent two-sample t-test | Comparing means of two separate, unrelated groups |
| Paired t-test | Comparing two related measurements, such as before and after values for the same subjects |
One-Sample T-Test Example
from scipy import stats
scores = [78, 82, 85, 90, 76, 88, 79, 84]
hypothesized_mean = 80
t_stat, p_value = stats.ttest_1samp(scores, hypothesized_mean)
print(f"t-statistic: {t_stat:.2f}, p-value: {p_value:.4f}")
Independent Two-Sample T-Test Example
group_a = [78, 82, 85, 90, 76]
group_b = [65, 70, 72, 68, 74]
t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(f"t-statistic: {t_stat:.2f}, p-value: {p_value:.4f}")
Paired T-Test Example
before = [70, 75, 68, 80, 72]
after = [78, 82, 74, 85, 79]
t_stat, p_value = stats.ttest_rel(before, after)
print(f"t-statistic: {t_stat:.2f}, p-value: {p_value:.4f}")
A paired test is used here because each before and after value belongs to the same subject, which the independent two-sample test would incorrectly ignore.
Interpreting the P-Value
If the p-value falls below the chosen significance level, typically 0.05, the difference is considered statistically significant, meaning it is unlikely to be due to random chance alone under the null hypothesis of no real difference.
Assumptions of the T-Test
Data is approximately normally distributed, particularly important for small sample sizes
Observations are independent (except in the paired test, which specifically handles related measurements)
For the independent two-sample test, variances between the two groups are roughly equal, unless using Welch's correction, which relaxes this assumption
T-Test vs ANOVA
A t-test compares exactly two groups. When comparing three or more groups, ANOVA is the correct choice instead, since running multiple t-tests would inflate the overall false positive rate.
Common Interview Questions
When would you use a paired t-test instead of an independent two-sample t-test?
When the two sets of measurements come from the same subjects, such as before and after a treatment, since the paired test accounts for the natural correlation between each subject's own two measurements.
What do you do if the equal variance assumption is violated in a two-sample t-test?
Use Welch's t-test instead, a variant that does not assume equal variances between the two groups, available in most statistical software as an alternative option.
FAQ
Frequently Asked Questions
What is a t-test used for?
It tests whether the means of one or two groups differ significantly, when the population standard deviation is unknown and must be estimated from the sample.
What is the difference between an independent and a paired t-test?
An independent t-test compares two separate, unrelated groups. A paired t-test compares two related measurements from the same subjects, such as before and after values.
When should you use ANOVA instead of a t-test?
When comparing three or more groups. Running multiple t-tests instead would inflate the overall chance of a false positive across the comparisons.
What assumption does a t-test make about the data?
It assumes the data is approximately normally distributed, particularly important for small samples, and that observations are independent unless using the paired variant.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin