The Two-Sample t-Test

Comparing the means of two independent groups. The sampling distribution of a difference in means, the pooled two-sample t-test derived from a common variance with a full proof that its statistic has the t distribution on n1 + n2 - 2 degrees of freedom, the interval for the difference, worked on NHANES blood pressures of 20 men and 20 women, and the bias of the pooled standard error when the variances differ. A worked problem on the bone mineral content of newborns. Welch's test for unequal variances has its own article.

For independent samples from two normal populations with a common variance, the pooled statistic $t = (\bar{x}_1 - \bar{x}_2)/(s_p\sqrt{1/n_1 + 1/n_2})$ has exactly the $t$ distribution with $n_1 + n_2 - 2$ degrees of freedom under $H_0: \mu_1 = \mu_2$, proved here from the one-sample facts; the matching interval for $\mu_1 - \mu_2$ excludes zero exactly when the test rejects. For 20 men and 20 women from NHANES 2017 to 2018 it gives $t = 0.674$, $p = 0.504$ and a 95% interval of $-8.3$ to $16.6$ mm Hg, too wide to resolve the population difference of 2.04 mm Hg. When the variances differ, the pooled estimate of the variance of the difference is unbiased only if the sample sizes are equal; Welch's test, in its own article, then replaces the pooled test.

In the NHANES survey of 2017 to 2018, the 2,547 men examined had a mean systolic blood pressure of 127.41 mm Hg and the 2,700 women 125.37 mm Hg. A study that measures 20 men and 20 women will not see those numbers. In one such sample, drawn by this article's script, the men averaged 4.13 mm Hg higher than the women. Each sample mean is off from its population mean by an amount that depends on the spread of blood pressures and the size of the sample. Does a gap of 4.13 mm Hg between two sample means show a real difference between the populations? Not by itself: the test built in this article gives $p = 0.504$ for that sample, and its 95% interval for the difference runs from $-8.3$ to $16.6$ mm Hg.

Unlike the paired designs of the previous article, the men and women here have nothing to do with each other. No man is matched to a particular woman, and the groups could as well have different sizes. The two samples are independent, and the comparison has to account for the variation in both. The two-sample $t$-test makes that comparison.

Here is the route. First we find the sampling distribution of a difference between two means. Then we derive the classical test, which assumes the two populations share one variance, and prove that its statistic has a $t$ distribution. We work it on the men and women. Finally we derive what the pooled standard error estimates when the variances differ. A worked problem closes the article. Welch's test, which drops the assumption of a common variance, is the subject of the next article, Welch's t-Test and Unequal Variances, with the simulations of when the classical test fails and the choice between the two tests.

Two independent samples

The notation follows the course text (Pagano, Gauvreau and Mattie, 2022). From a population with mean $\mu_1$ and standard deviation $\sigma_1$, a random sample of size $n_1$ is drawn, with mean $\bar{x}_1$ and standard deviation $s_1$. From a second population, independently, a sample of size $n_2$ is drawn, with mean $\bar{x}_2$ and standard deviation $s_2$. The hypotheses are

$$H_0: \mu_1 = \mu_2 \ \ (\text{equivalently } \mu_1 - \mu_2 = 0), \qquad H_A: \mu_1 \ne \mu_2,$$

or a one-sided alternative such as $H_A: \mu_1 > \mu_2$.

The natural estimate of $\mu_1 - \mu_2$ is $\bar{X}_1 - \bar{X}_2$, and its mean is $E[\bar{X}_1] - E[\bar{X}_2] = \mu_1 - \mu_2$, by the linearity of expectation.

Example (the variances add). The two means are subtracted, but the variance of $\bar{X}_1 - \bar{X}_2$ is the sum of their variances, not the difference. Each mean wobbles on its own, and the wobbles do not cancel; they compound. The variance identity of the paired article, $\operatorname{Var}(X - Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) - 2\operatorname{Cov}(X, Y)$, applies with $X = \bar{X}_1$ and $Y = \bar{X}_2$. The samples are independent, so the covariance is zero, and each mean has variance $\sigma_i^2/n_i$ (The Sampling Distribution of the Mean):

$$\operatorname{Var}(\bar{X}_1 - \bar{X}_2) = \operatorname{Var}(\bar{X}_1) + \operatorname{Var}(\bar{X}_2) = \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}.$$

The covariance term that made paired differences precise is the one that is zero here.

Each sample mean is normal for a normal population. It is approximately normal for large samples from any population with a finite variance, by the central limit theorem. A difference of independent normal variables is normal: $-\bar{X}_2$ is normal, as a linear change of a normal variable (The Normal Distribution), and a sum of independent normal variables is normal (The Sampling Distribution of the Mean cites the proof). So for normal populations

$$Z = \frac{(\bar{X}_1 - \bar{X}_2) - (\mu_1 - \mu_2)}{\sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}}$$

is exactly standard normal. If $\sigma_1$ and $\sigma_2$ were known, this would be the test statistic: the two-sample $z$-test, which the course text lists beside each $t$-test for the case of known variances. In practice the standard deviations are estimated, and how to estimate them is where the two versions of the test part ways.

Equal variances: the pooled t-test

Suppose first that the two populations have the same standard deviation, $\sigma_1 = \sigma_2 = \sigma$. The variance of the difference in means becomes

$$\sigma^2\left(\frac{1}{n_1} + \frac{1}{n_2}\right),$$

and only one unknown, $\sigma^2$, has to be estimated. Both samples carry information about it: $s_1^2$ estimates $\sigma^2$ from $n_1 - 1$ degrees of freedom, and $s_2^2$ from $n_2 - 1$. The pooled variance combines them, weighting each by its degrees of freedom:

$$s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}.$$

The numerator is the sum of the squared deviations of every observation from its own group's mean. The denominator is the total number of observations less the two means estimated. The weights make sense: a variance computed from 40 observations deserves more say than one computed from 10. Since $E[S_1^2] = E[S_2^2] = \sigma^2$, the weighted average is unbiased:

$$E[S_p^2] = \frac{(n_1 - 1)\sigma^2 + (n_2 - 1)\sigma^2}{n_1 + n_2 - 2} = \sigma^2.$$

The t distribution with n1 + n2 - 2 degrees of freedom

The test statistic replaces $\sigma$ by $s_p$:

$$t = \frac{(\bar{x}_1 - \bar{x}_2) - (\mu_1 - \mu_2)}{\sqrt{s_p^2\left(\dfrac{1}{n_1} + \dfrac{1}{n_2}\right)}}.$$

Here is the plan of the proof. A $t$ variable is, by definition, a standard normal variable divided by the square root of an independent chi-squared variable over its degrees of freedom. The numerator of the statistic supplies the normal variable, and the pooled variance supplies the chi-squared one, because two independent chi-squared variables add. The proof uses three results of The Chi-Squared and t Distributions, which defines the two distributions and proves, for one normal sample of size $n$ with variance $\sigma^2$:

  • $(n - 1)S^2/\sigma^2$ has the chi-squared distribution with $n - 1$ degrees of freedom;
  • $\bar{X}$ and $S^2$ are independent;
  • independent chi-squared variables add: if $U$ is $\chi^2_a$ and $V$ is an independent $\chi^2_b$, then $U + V$ is $\chi^2_{a+b}$.

Theorem. Let $X_{11}, \dots, X_{1n_1}$ and $X_{21}, \dots, X_{2n_2}$ be independent, the first sample from a normal population with mean $\mu_1$ and the second from a normal population with mean $\mu_2$, both with variance $\sigma^2$, and let $n_1, n_2 \ge 2$. Then

$$T = \frac{(\bar{X}_1 - \bar{X}_2) - (\mu_1 - \mu_2)}{S_p\sqrt{1/n_1 + 1/n_2}}$$

has the $t$ distribution with $n_1 + n_2 - 2$ degrees of freedom.

Proof. Step 1, the numerator. By the previous section with $\sigma_1 = \sigma_2 = \sigma$,

$$Z = \frac{(\bar{X}_1 - \bar{X}_2) - (\mu_1 - \mu_2)}{\sigma\sqrt{1/n_1 + 1/n_2}}$$

is standard normal.

Step 2, the denominator. Write $V_1 = (n_1 - 1)S_1^2/\sigma^2$ and $V_2 = (n_2 - 1)S_2^2/\sigma^2$. By the first result, $V_1$ is $\chi^2_{n_1 - 1}$ and $V_2$ is $\chi^2_{n_2 - 1}$. They are independent, because $V_1$ is computed from the first sample only and $V_2$ from the second only, and the samples are independent. By the third result their sum is $\chi^2_{n_1 + n_2 - 2}$, and by the definition of the pooled variance the sum is

$$V = V_1 + V_2 = \frac{(n_1 - 1)S_1^2 + (n_2 - 1)S_2^2}{\sigma^2} = \frac{(n_1 + n_2 - 2)S_p^2}{\sigma^2}.$$

Step 3, independence. The four variables $\bar{X}_1$, $S_1^2$, $\bar{X}_2$, $S_2^2$ are mutually independent. Within each sample the mean and the variance are independent, by the second result, and the pair from the first sample is independent of the pair from the second, because the samples are. Their joint distribution therefore factors into the four separate distributions. $Z$ is a function of $\bar{X}_1$ and $\bar{X}_2$ only, and $V$ is a function of $S_1^2$ and $S_2^2$ only, so $Z$ and $V$ are independent.

Step 4, the ratio. By Steps 1 to 3 and the definition of the $t$ distribution, $Z/\sqrt{V/(n_1 + n_2 - 2)}$ has the $t$ distribution with $n_1 + n_2 - 2$ degrees of freedom. And

$$\frac{Z}{\sqrt{V/(n_1 + n_2 - 2)}} = \frac{(\bar{X}_1 - \bar{X}_2) - (\mu_1 - \mu_2)}{\sigma\sqrt{1/n_1 + 1/n_2}} \cdot \frac{\sigma}{S_p} = T,$$

since $\sqrt{V/(n_1 + n_2 - 2)} = S_p/\sigma$. $\blacksquare$

The unknown $\sigma$ cancels, so $T$ is a pivot: its distribution is known whatever $\mu_1$, $\mu_2$ and $\sigma$ are. Under $H_0$ the term $\mu_1 - \mu_2$ is zero, and the statistic $t$ above is computed from the data alone. This is the pooled two-sample $t$-test. It is exact for normal populations with equal variances at any sample sizes of at least 2. The NIST/SEMATECH e-Handbook of Statistical Methods (§1.3.5.3) gives it in the same form. Sampling Distributions from the Normal: Chi-Squared, t and F proves the general theorem on projections of a normal vector, of which the chi-squared and independence steps of this proof are the case of two groups.

The $100(1-\alpha)\%$ confidence interval for $\mu_1 - \mu_2$ comes from the same pivot. Write $c = t_{n_1+n_2-2,\,1-\alpha/2}$. The event $|T| \le c$ has probability $1 - \alpha$, and it rearranges to

$$(\bar{x}_1 - \bar{x}_2) - c\,s_p\sqrt{\frac{1}{n_1} + \frac{1}{n_2}} \;\le\; \mu_1 - \mu_2 \;\le\; (\bar{x}_1 - \bar{x}_2) + c\,s_p\sqrt{\frac{1}{n_1} + \frac{1}{n_2}}.$$

The two-sided test at level $\alpha$ rejects $H_0: \mu_1 = \mu_2$ exactly when this interval excludes zero. The test rejects when $|\bar{x}_1 - \bar{x}_2| > c\,s_p\sqrt{1/n_1 + 1/n_2}$, and that is the statement that zero lies more than the interval's half-width from its center.

Men and women

Back to the 20 men and 20 women. The NHANES population used in this course splits into groups with these spreads:

Population (NHANES 2017–2018 adults) Size Mean (mm Hg) Standard deviation (mm Hg)
Men 2,547 127.41 18.37
Women 2,700 125.37 21.60
Aged 18 to 39 1,745 114.83 12.85
Aged 60 and over 1,923 136.85 21.02

Men's mean is 2.04 mm Hg higher than women's, and women's standard deviation is 1.18 times men's. With equal group sizes, a difference in spread that small does the pooled test little harm, as the simulations of Welch's t-Test and Unequal Variances show. The script drew 20 men and 20 women at random (seed 1):

Sample $n$ $\bar{x}$ $s$ $s^2$
Men 20 126.950 18.201 331.27
Women 20 122.817 20.515 420.85

Step 1, the pooled variance. With equal sample sizes it is the plain average of the two variances:

$$s_p^2 = \frac{19(331.27) + 19(420.85)}{38} = 376.06, \qquad s_p = 19.392.$$

Step 2, the standard error. $s_p\sqrt{1/20 + 1/20} = 19.392 \times 0.3162 = 6.132$ mm Hg.

Step 3, the test statistic. The difference in means is $126.950 - 122.817 = 4.133$ mm Hg, so

$$t = \frac{4.133}{6.132} = 0.674$$

on $20 + 20 - 2 = 38$ degrees of freedom, and $p = 2\,P(T_{38} \ge 0.674) = 0.504$.

Step 4, the interval. With $t_{38,\,0.975} = 2.0244$, the 95% interval is $4.133 \pm 2.0244 \times 6.132 = [-8.28, 16.55]$ mm Hg.

The test does not reject, and the interval shows why the result says little. It contains the true difference of 2.04 mm Hg. It also contains every difference from 8 mm Hg in favor of women to 16.5 mm Hg in favor of men. Forty people cannot resolve a difference of two millimeters of mercury between populations whose members vary by twenty. Sample Size for Comparing Two Means works out how many could: about 1,500 in each group for an 80% chance of detecting it. Measured on a scale that does not grow with the sample, the difference is about a tenth of a standard deviation, as Effect Sizes and Standardized Differences finds for the same survey.

Men and women have similar spreads. The next section says what changes for two groups that clearly do not.

When the variances differ

The pooled test needs the two populations to share one variance. When they do not, Welch's t-Test and Unequal Variances replaces it: Welch's statistic estimates each variance separately, $t = [(\bar{x}_1 - \bar{x}_2) - (\mu_1 - \mu_2)]/\sqrt{s_1^2/n_1 + s_2^2/n_2}$, and is referred to a $t$ distribution with the Welch–Satterthwaite degrees of freedom, an approximation. That article works the test, measures by simulation when the pooled test fails, and shows why a preliminary $F$ test of the variances is a poor way to choose between the two. What can be said here is how the pooled standard error behaves when the variances differ; Permutation Tests finds the same behavior in the permutation test of a difference in means. In a randomized experiment Welch's standard error is also Neyman's variance estimator for the average treatment effect, which Potential Outcomes and the Average Treatment Effect proves conservative.

The pooled standard error weights each sample variance by its degrees of freedom, $n_i - 1$. The true variance of the difference weights each population variance by $1/n_i$. The expected value of the pooled estimate of that variance is

$$E\left[S_p^2\left(\frac{1}{n_1} + \frac{1}{n_2}\right)\right] = \frac{(n_1 - 1)\sigma_1^2 + (n_2 - 1)\sigma_2^2}{n_1 + n_2 - 2}\cdot\frac{n_1 + n_2}{n_1 n_2},$$

and subtracting the true variance $\sigma_1^2/n_1 + \sigma_2^2/n_2$ and collecting terms leaves

$$\frac{(n_1 - n_2)(\sigma_1^2 - \sigma_2^2)}{n_1 n_2}\cdot\frac{n_1 + n_2 - 1}{n_1 + n_2 - 2}.$$

So the pooled estimate is unbiased exactly when the variances are equal or the sample sizes are. It is too small on average when the smaller sample comes from the more variable population, and too large when the larger sample does.

The simulations of Welch's t-Test and Unequal Variances show the consequence. With samples of 10 and 40 and the larger standard deviation in the smaller sample, a pooled test at the 0.05 level rejected a true null hypothesis in 12% to 26% of samples, while with equal sample sizes its error rate stayed within 0.006 of 0.05.

Worked problems

This problem is an exercise in the course text's chapter on the comparison of two means (Pagano, Gauvreau and Mattie, 2022), restated here with its data and solved in full. Welch's t-Test and Unequal Variances repeats it without the equal-variance assumption. The script recomputes every number.

Problem 1 (bone mineral content of newborns). A study asked whether a mother's smoking during pregnancy affects the bone mineral content of her otherwise healthy child. A sample of 77 newborns whose mothers smoked has mean bone mineral content $\bar{x}_1 = 0.098$ g/cm and standard deviation $s_1 = 0.026$ g/cm. A sample of 161 newborns whose mothers did not smoke has $\bar{x}_2 = 0.095$ g/cm and $s_2 = 0.025$ g/cm. The population variances are assumed equal. (a) Are the samples paired or independent? (b) Find separate 95% confidence intervals for the two population means and compare them. (c) State the hypotheses of the two-sided test of equal means, compute the test statistic and give its distribution under the null hypothesis. (d) Is the null hypothesis rejected at the 0.05 level? (e) State the conclusion. (f) Find a 95% confidence interval for the difference in population means.

Solution. The answers are: (a) independent; (b) $[0.0921, 0.1039]$ and $[0.0911, 0.0989]$ g/cm, which overlap; (c) $t = 0.855$, with the $t$ distribution on 236 degrees of freedom; (d) no, $p = 0.39$; (e) the data give no evidence of a difference in mean bone mineral content; (f) $[-0.0039, 0.0099]$ g/cm.

(a) The samples are independent. The newborns of the two groups are different children of different mothers, no child in one group corresponds to a particular child in the other, and the group sizes differ.

(b) Each interval is the one-sample $t$ interval $\bar{x} \pm t_{n-1,\,0.975}\,s/\sqrt{n}$ of Confidence Intervals for a Mean.

  • Mothers who smoked: $s_1/\sqrt{77} = 0.026/8.775 = 0.00296$ and $t_{76,\,0.975} = 1.9917$, so the interval is $0.098 \pm 0.0059 = [0.0921, 0.1039]$.
  • Mothers who did not: $s_2/\sqrt{161} = 0.025/12.689 = 0.00197$ and $t_{160,\,0.975} = 1.9749$, so the interval is $0.095 \pm 0.0039 = [0.0911, 0.0989]$.

Drawn side by side, the second interval lies almost wholly inside the first, and each contains the other group's sample mean. The picture gives no suggestion that the population means differ. It is not a test. The intervals would fail to overlap only if the sample means differed by more than $0.0059 + 0.0039 = 0.0098$, while the test of part (d) rejects once they differ by more than 0.0069. So two intervals can overlap when the test rejects.

(c) The hypotheses are $H_0: \mu_1 = \mu_2$ against $H_A: \mu_1 \ne \mu_2$. With a common variance assumed, the test is the pooled two-sample $t$-test. The pooled variance is

$$s_p^2 = \frac{76(0.026)^2 + 160(0.025)^2}{77 + 161 - 2} = \frac{0.051376 + 0.100000}{236} = 0.0006414, \qquad s_p = 0.02533.$$

The standard error of the difference is $s_p\sqrt{1/77 + 1/161} = 0.02533 \times 0.13856 = 0.003509$, and

$$t = \frac{0.098 - 0.095}{0.003509} = 0.855.$$

By the theorem of this article, when $H_0$ is true and the populations are normal with a common variance, the statistic has the $t$ distribution with $77 + 161 - 2 = 236$ degrees of freedom.

(d) The critical value is $t_{236,\,0.975} = 1.9701$. Since $0.855 < 1.9701$, the null hypothesis is not rejected at the 0.05 level. The $p$-value is $2P(T_{236} \ge 0.855) = 0.39$.

(e) These samples give no evidence that mean bone mineral content differs between newborns whose mothers smoked and those whose mothers did not. That is not a demonstration that the means are equal. Part (f) says how large a difference the data leave open.

(f) The interval is

$$(0.098 - 0.095) \pm 1.9701 \times 0.003509 = 0.003 \pm 0.0069 = [-0.0039, 0.0099] \text{ g/cm}.$$

It contains zero, in agreement with the test. A difference of up to about 0.004 g/cm in favor of the nonsmokers' children, or 0.010 g/cm in favor of the smokers', is compatible with the data. Against means near 0.095 g/cm, those are differences of roughly 4% and 10%.

What we have learned

The variance of a difference between independent means is the sum of the two variances. With a common variance, pooling the two estimates gives an exact $t$ test on $n_1 + n_2 - 2$ degrees of freedom, proved here from the one-sample facts. When the variances differ, the pooled estimate of the variance of the difference is biased unless the sample sizes are equal, and Welch's t-Test and Unequal Variances gives the test that does not assume a common variance. For 20 men and 20 women, the pooled test gave $p = 0.504$ and an interval too wide to say much.

That last result is a matter of sample size. Sample Size for Comparing Two Means derives the number of subjects a study needs in each group to detect a given difference: 279 per group for 5 mm Hg among older adults, and about 1,500 per group for the 2 mm Hg between men and women. Comparing more than two groups at once is the subject of one-way analysis of variance. Its within-groups variance $s_W^2$ is the pooled variance extended to $k$ groups, and it assumes equal variances in the same way the pooled test does. Comparing two groups on several measurements at once is the subject of Hotelling's T-Squared Test, whose two-sample version replaces the pooled variance by a pooled covariance matrix.

In Python

from scipy import stats

# from summary statistics: the 20 men and 20 women above
print(stats.ttest_ind_from_stats(126.950, 18.201, 20, 122.817, 20.515, 20, equal_var=True))
# pooled: t about 0.674, p about 0.504

# from raw data: res = stats.ttest_ind(x1, x2); res.confidence_interval(0.95) is the matching interval

scipy's ttest_ind pools by default (equal_var=True), and so does statsmodels' ttest_ind; Inference for Proportions and Means compares the two libraries' defaults on NHANES samples. Every number in this article is computed by tools/experiments/applied-statistics/two-sample-t-test.py in the site's repository. The script implements the $t$ and $F$ distributions directly and was checked against scipy 1.17.1.

References

Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 13 revisions