Sub topic 7
Comparing two means: independent and paired samples
This chapter extends the single-mean t-test to a question that comes up constantly in investing: do two groups have different average returns? It assumes you can already set up a null and alternative hypothesis, compute a t-statistic for one mean, and compare it with a critical value or p-value.
The question and the hypotheses
Two-sample tests ask whether the difference between two population means equals some value, usually zero. For a two-tailed test the hypotheses are:
- H0: μ1 − μ2 = 0
- Ha: μ1 − μ2 ≠ 0
For a one-tailed test, Ha becomes μ1 − μ2 > 0 or μ1 − μ2 < 0. The test statistic has the same shape as before:
t = (observed difference - hypothesized difference) / standard error of the differenceThe only new work is finding the right standard error and degrees of freedom. Both depend on one design question: are the two samples independent or paired?
Independent versus paired samples
Samples are independent when the observations in one group tell you nothing about the observations in the other. An example is the returns of two unrelated funds in different periods, or the earnings of two separate sets of firms.
Samples are paired (also called dependent) when each observation in one group is matched with one in the other. Typical cases are the same fund before and after a manager change, or the same stocks measured in two years. The pairing means the two values share a source of variation, so they are correlated.
Independent samples with equal variances
When you can assume both populations have the same variance, you combine the two sample variances into one pooled variance. It is a weighted average, with weights equal to each sample's degrees of freedom:
sp^2 = [(n1 - 1) s1^2 + (n2 - 1) s2^2] / (n1 + n2 - 2)
t = [(x1bar - x2bar) - (mu1 - mu2)] / sqrt( sp^2 / n1 + sp^2 / n2 )
df = n1 + n2 - 2The test also assumes both populations are normal, or the samples are large enough for the sampling distribution of each mean to be close to normal.
Worked example
Fund A has n1 = 12 monthly returns with mean 1.10% and standard deviation 2.0%. Fund B has n2 = 10 returns with mean 0.50% and standard deviation 1.8%. Test H0: μ1 − μ2 = 0 against a two-tailed alternative at the 5% level.
- Pooled variance: (11 × 4.00 + 9 × 3.24) / 20 = 73.16 / 20 = 3.658.
- Standard error: sqrt(3.658 × (1/12 + 1/10)) = sqrt(0.6706) ≈ 0.819.
- t = (1.10 − 0.50) / 0.819 ≈ 0.733.
- Degrees of freedom = 20, so the two-tailed critical value is ±2.086.
Since 0.733 lies inside ±2.086, you fail to reject H0. The 0.6 percentage point gap is small relative to its standard error, so the data do not show a difference in mean return.
Independent samples with unequal variances
If the variances differ materially, pooling them is wrong. Use each sample variance separately:
t = [(x1bar - x2bar) - (mu1 - mu2)] / sqrt( s1^2 / n1 + s2^2 / n2 )The degrees of freedom are no longer a simple count. They come from an approximation (the Welch–Satterthwaite formula), and software reports a non-integer value. The CFA curriculum asks you to know that this version exists and when to use it, not to compute those degrees of freedom by hand.
You can decide which version to use with the F-test for equal variances, covered in a later chapter. If in doubt, the unequal-variance test is the safer default. It loses little power when the variances are in fact equal.
Paired samples
With paired data you do not treat the two columns as separate samples. You subtract within each pair to get one column of differences, then run a one-sample t-test on it:
d_i = x1_i - x2_i
t = (dbar - mu_d0) / (s_d / sqrt(n))
df = n - 1Here dbar is the mean difference, sd is the sample standard deviation of the differences, n is the number of pairs, and mud0 is the hypothesized mean difference (usually 0).
Suppose 8 funds are measured before and after a fee cut. The mean difference is 0.45 percentage points and s_d is 0.60. Then the standard error is 0.60 / sqrt(8) ≈ 0.212, and t ≈ 2.12. With df = 7 the 5% two-tailed critical value is 2.365. You fail to reject.
Why pairing helps
Matched observations are positively correlated, because the same fund or stock appears on both sides. Differencing removes that shared variation, which shrinks the standard error. A paired design can therefore detect a small effect that an independent-samples test on the same numbers would miss. The gain exists only if the pairing is real. Unmatched data cannot be paired just to get a smaller standard error.
Running both tests in Python
The first block repeats the worked independent-samples example from summary statistics. The second block runs a paired test on raw data and checks the hand calculation against scipy.
from math import sqrt
from scipy import stats
n1, m1, s1 = 12, 1.10, 2.0
n2, m2, s2 = 10, 0.50, 1.8
sp2 = ((n1 - 1) * s1**2 + (n2 - 1) * s2**2) / (n1 + n2 - 2)
se = sqrt(sp2 * (1 / n1 + 1 / n2))
t_manual = (m1 - m2) / se
t_crit = stats.t.ppf(1 - 0.05 / 2, df=n1 + n2 - 2)
res = stats.ttest_ind_from_stats(m1, s1, n1, m2, s2, n2, equal_var=True)
print(t_manual, res.statistic, res.pvalue, t_crit)
print("Reject H0" if abs(res.statistic) > t_crit else "Fail to reject H0")
# Unequal-variance (Welch) version of the same test
welch = stats.ttest_ind_from_stats(m1, s1, n1, m2, s2, n2, equal_var=False)
print(welch.statistic, welch.pvalue)import numpy as np
from scipy import stats
before = np.array([5.1, 4.8, 6.0, 5.5, 4.9, 5.7, 6.2, 5.3])
after = np.array([5.6, 5.0, 6.1, 6.2, 5.0, 6.4, 6.3, 5.9])
d = after - before
n = len(d)
t_manual = d.mean() / (d.std(ddof=1) / np.sqrt(n))
t_stat, p_value = stats.ttest_rel(after, before)
t_crit = stats.t.ppf(1 - 0.05 / 2, df=n - 1)
print(t_manual, t_stat, p_value, t_crit)
print("Reject H0" if abs(t_stat) > t_crit else "Fail to reject H0")The two t values in each block should agree. The pooled and Welch statistics are close here because the sample sizes and standard deviations are similar.
Common errors
- Wrong degrees of freedom. Pooled independent samples use n1 + n2 − 2. Paired samples use n − 1, where n is the number of pairs, not the number of observations.
- Unequal sample sizes in a paired test. Pairs require equal counts. If the counts differ, the data are not paired.
- Dividing the standard error wrongly. In the pooled formula, sp² is divided by n1 and n2 separately inside the square root. It is not divided by n1 + n2.
- Testing means when you mean variances. A difference in volatility is a different hypothesis. It is tested with an F-test, not a t-test on means.
- Ignoring non-normality in small samples. If the differences, or the two samples, are clearly non-normal and small, a nonparametric test is safer (see the later chapter on nonparametric tests).
Summary of the three tests
| Test | When to use | Standard error | Degrees of freedom |
|---|---|---|---|
| Pooled t-test | Independent samples, equal variances | sqrt(sp²/n1 + sp²/n2) | n1 + n2 − 2 |
| Unequal-variance t-test | Independent samples, unequal variances | sqrt(s1²/n1 + s2²/n2) | Approximate, from software |
| Paired t-test | Matched observations | s_d / sqrt(n) | n − 1 |
Exercise
A researcher compares the average P/E ratios of 15 firms in sector X (mean 18.0, standard deviation 4.0) with 11 unrelated firms in sector Y (mean 15.0, standard deviation 3.0). Assuming equal variances, test H0: μX − μY = 0 against a two-tailed alternative at the 5% level. The critical value for df = 24 is 2.064.
Show answer
Pooled variance = (14 × 16 + 10 × 9) / 24 = 314 / 24 = 13.083. Standard error = sqrt(13.083 × (1/15 + 1/11)) = sqrt(13.083 × 0.15758) = sqrt(2.0617) ≈ 1.436. t = (18.0 − 15.0) / 1.436 ≈ 2.09. Degrees of freedom = 15 + 11 − 2 = 24. Since 2.09 is greater than 2.064, reject H0. The sectors have different mean P/E ratios at the 5% level, though the result is only just significant.
Exercise
You measure the same 20 portfolios' returns in two consecutive years. State the correct test, the degrees of freedom, and what you would compute first.
Show answer
Use a paired t-test, because each portfolio appears in both samples. Degrees of freedom = 20 − 1 = 19. First compute the 20 differences (year 2 minus year 1), then their mean and sample standard deviation, and use t = dbar / (s_d / sqrt(20)).