Before-and-after analysis
Compare measurements from the same subjects before and after an intervention.
SciPy · Function
Tests whether two related or repeated samples have different means. It returns a TtestResult containing the t-statistic, p-value, and degrees of freedom.
The function compares two same-shaped, paired samples by testing their within-pair differences against a zero mean. It can operate along a selected axis, choose how to handle missing values, test a directional or two-sided alternative, and optionally preserve reduced dimensions.
scipy.stats.ttest_rel(a, b, axis=0, nan_policy='propagate', alternative='two-sided', *, keepdims=False)| Name | Type | Description |
|---|---|---|
a | array_like | First paired sample; must have the same shape as b. |
b | array_like | Second paired sample; must have the same shape as a. |
axis | int or None, default 0 | Axis along which to compute the test. If None, compute over the whole arrays. |
nan_policy | {‘propagate’, ‘raise’, ‘omit’}, default ‘propagate’ | Defines how to handle NaNs: propagate, raise, or omit. |
alternative | {‘two-sided’, ‘less’, ‘greater’}, default ‘two-sided’ | Defines the alternative hypothesis: two-sided, less, or greater. |
keepdims | bool, default False | If True, reduced axes are retained with size one. |
TtestResult with statistic, pvalue, and df attributes. statistic is the t-statistic; pvalue is the p-value for the chosen alternative; df is one less than the sample size along the test axis.
import numpy as np
from scipy import stats
before = np.array([10, 12, 9, 11, 13])
after = np.array([11, 13, 10, 12, 14])
result = stats.ttest_rel(after, before)
print(result.statistic, result.pvalue, result.df)Compares paired measurements and prints the t-statistic, p-value, and degrees of freedom.
Compare measurements from the same subjects before and after an intervention.
Evaluate two conditions measured on each participant or experimental unit.
Run paired tests independently across rows, columns, or other dimensions.
It is a paired-sample t-test for related or repeated measurements. The null hypothesis is that the two related samples have identical average values, and the calculation is based on their paired differences.
Follow-up: How is the statistic formed? Compute the mean of a - b and divide it by the standard error of those differences; the degrees of freedom are one less than the paired sample size.
a and b must have the same shape because values at corresponding positions form pairs. The axis argument selects the dimension along which each paired test is computed; None flattens both inputs conceptually into one test.
Follow-up: What happens with multidimensional inputs? A result is produced for each remaining axis position, unless keepdims=True retains the reduced axis with length one.
The alternative can be two-sided, less, or greater. These correspond respectively to unequal means, the first sample having a smaller mean, or the first sample having a larger mean than the second.
Follow-up: How should missing values be handled? Choose propagate, raise, or omit depending on whether NaNs should invalidate, reject, or be excluded from each calculation.
scipy.stats.ttest_indscipy.stats.ttest_1sampscipy.stats.TtestResult