For $n \ge 2$ independent observations from a normal population whose standard deviation $\sigma$ is unknown, Student's $t$ interval $\bar{x} \pm t_{n-1,\,1-\alpha/2}\,s/\sqrt{n}$ contains the mean $\mu$ with probability exactly $1 - \alpha$. For Student's ten sleep patients the 95% interval is 0.70 to 2.46 hours. Keeping the normal value 1.96 with $s$ in place of $\sigma$ covers only $P(|T_{n-1}| \le 1.96)$ of the time: 0.81 at $n = 3$ and 0.92 at $n = 10$. For a population that is not normal the $t$ interval is approximate. In simulation it stayed within two points of 95% for moderately skewed blood pressures, but covered only 84% at $n = 10$ and 92% at $n = 100$ for a strongly skewed lognormal population.
In a small trial of two sleeping drugs, ten patients slept an average of 1.58 hours more on the second drug than on the first. How sure can we be that the second drug really helps, and by how much? A confidence interval would answer that. But the interval we have so far needs the population standard deviation, and nobody knows the standard deviation of extra sleep in a population of such patients. In most studies, nobody knows it for anything. This article gives the answer, Student's $t$ interval, stated in the paragraph above.
Confidence Intervals and Coverage built the interval
$$\bar{x} \pm z_{1-\alpha/2}\frac{\sigma}{\sqrt{n}}$$
for a population mean $\mu$, and showed by simulation that it contains $\mu$ in the stated share of samples. It assumed the population standard deviation $\sigma$ was known. The obvious repair is to use the sample standard deviation $s$ in its place. This article asks whether that repair works, and what to do when it does not.
First we measure how badly the plain substitution fails: for small samples, a "95%" interval can cover as little as 81% of the time. Then we follow William Sealy Gosset, writing as "Student" (1908), to the repair: a new critical value from the $t$ distribution, which rests on three facts about normal samples. They are stated here and proved in The Chi-Squared and t Distributions. Along the way we see why the sample variance divides by $n - 1$. We then work the interval by hand on the ten sleep patients, whose data Student himself used. Finally we test how well the $t$ interval holds up when the population is not normal.
This article is the course's home for the $t$ interval. The chi-squared and $t$ distributions themselves, with their densities and the proofs about normal samples, have their home in The Chi-Squared and t Distributions.
When is sigma known?
Rarely, but not never. Student (1908) described the situation in routine laboratory work. When the same kind of experiment is repeated many times, a long series of results can pin down the standard deviation "once and for all". That value can then be used for later short series.
A modern equivalent is a measuring instrument whose error has been characterized by its manufacturer or by a calibration study. In those cases the variability is a property of the measurement process, established independently of the current sample, and the $z$ interval is appropriate.
In most medical studies, though, the variability between patients is part of what is unknown. It has to be estimated from the same data as the mean.
What goes wrong when s replaces sigma
The natural estimate of $\sigma$ is the sample standard deviation,
$$s = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2},$$
where $x_1, \dots, x_n$ are the observations and $\bar{x}$ is their mean. (The divisor $n - 1$ has a reason, which a later section gives.) Then $s/\sqrt{n}$ is the estimated standard error of the mean.
Substituting $s$ for $\sigma$ gives $\bar{x} \pm 1.96\,s/\sqrt{n}$ at 95%. This was standard practice before 1908, and Student described it in the opening of his paper: assume the mean is normal with standard deviation $s/\sqrt{n}$, and use the normal tables. He also saw the weakness. As the number of experiments decreases, $s$ itself becomes subject to an increasing error. He complained that "no one has yet told us very clearly where the limit between 'large' and 'small' samples is to be drawn" (Student, 1908).
How large is the problem? Simulation can say. The script confidence-intervals-for-a-mean.py draws samples of size $n$ from a standard normal population, 10,000 samples for each of 30 seeds. It records how often $\bar{x} \pm 1.96\,s/\sqrt{n}$ contains the true mean of 0. The table gives the median over the 30 runs. (The script uses the exact quantile $z_{0.975} = 1.959964$ for "1.96" here and in the tables below. With 1.96 itself every entry is the same to three decimals: the coverage is $P(|T_{n-1}| \le c)$, and the two values of $c$ differ by 0.000036.)
| $n$ | 3 | 5 | 10 | 20 | 30 | 50 | 100 |
|---|---|---|---|---|---|---|---|
| Coverage of $\bar{x} \pm 1.96\,s/\sqrt{n}$ | 0.810 | 0.879 | 0.920 | 0.935 | 0.941 | 0.945 | 0.948 |
The population is perfectly normal, and still the interval that claims 95% covers 81% of the time with 3 observations, 88% with 5 and 92% with 10. It approaches 95% only as $n$ grows into the dozens.
Example (why the two errors do not cancel). The errors of a too-small $s$ and a too-large $s$ do not cancel, because coverage does not respond symmetrically to width. The sample standard deviation is sometimes smaller than $\sigma$ and sometimes larger. When it is small the interval is too narrow; when it is large, too wide. An interval of $\pm c$ true standard errors covers with probability $2\Phi(c) - 1$, where $\Phi$ is the standard normal distribution function. This curve bends. Below $c = 1.96$ it falls away quickly; above it, it creeps toward 1, because there is little probability left to catch. So a too-narrow interval loses more coverage than an equally too-wide one gains. On top of that, $s$ is on average slightly smaller than $\sigma$. On balance coverage falls, and the smaller the sample, the more $s$ varies and the larger the loss.
The formal version. The interval $\bar{x} \pm 1.96\,s/\sqrt{n}$ contains $\mu$ exactly when $|Z| \le 1.96\,S/\sigma$, where $Z = (\bar{X} - \mu)/(\sigma/\sqrt{n})$ is standard normal. For a normal sample $Z$ and $S$ are independent (fact 3 of the section "The t distribution" below, proved in The Chi-Squared and t Distributions). So for a sample whose standard deviation is $c$ times $\sigma$ the interval covers with probability $g(c) = 2\Phi(1.96\,c) - 1$, and the coverage is the average $E[g(S/\sigma)]$. The slope of $g$ is $g'(c) = 2 \times 1.96\,\phi(1.96\,c)$, with $\phi$ the standard normal density. It is positive and falls as $c$ rises from 0, so $g$ is concave and lies below its tangent line at $c = 1$:
$$g(c) \le g(1) + g'(1)\,(c - 1), \qquad \text{with equality only at } c = 1.$$
Taking expectations with $c = S/\sigma$, and noting that $S$ differs from $\sigma$ with probability 1,
$$E\left[g(S/\sigma)\right] < g(1) + g'(1)\left(\frac{E(S)}{\sigma} - 1\right), \qquad g(1) = 2\Phi(1.96) - 1 = 0.950004.$$
The last term is negative as well, because $E(S) < \sigma$. The reason is that $S$ varies: $0 < \operatorname{Var}(S) = E(S^2) - E(S)^2 = \sigma^2 - E(S)^2$, using $E(S^2) = \sigma^2$, which the section "Why the divisor is n - 1" below proves for independent observations. So, for a normal population, the coverage is below $g(1)$ at every sample size, by both effects named above. Here $g(1)$ is the coverage the same interval would have with $\sigma$ known.
One detail of rounding is worth stating exactly. With the exact quantile $z_{0.975} = 1.959964$ in place of 1.96, $g(1)$ is exactly 0.95, and the same argument shows that the coverage is below 95% for every $n$. With 1.96 itself, $g(1)$ exceeds 0.95 by 0.000004. The coverage, which is exactly $P(|T_{n-1}| \le 1.96)$, stays below 0.95 for every sample size of practical interest, but the script finds that it passes 0.95 once $n$ reaches about 65,900, where the deficit from estimating $\sigma$ has become smaller than that 0.000004.
The repair, then, is a larger critical value that pays for the variability of $s$. Finding it means finding the distribution of the ratio
$$T = \frac{\bar{X} - \mu}{S/\sqrt{n}},$$
where capital letters again mark quantities that are random before the sample is drawn. That was Student's problem.
The t distribution
Here is the plan of the derivation, before the details. The ratio $T$ is the familiar $z$ pivot with $\sigma$ replaced by $S$. We will write it as a standard normal variable divided by a random "correction factor" $S/\sigma$. If we know the distribution of that factor, and know it is independent of the numerator, the distribution of $T$ follows. Three facts about normal samples supply exactly that. They are stated here and proved in The Chi-Squared and t Distributions. Sampling Distributions from the Normal: Chi-Squared, t and F generalizes them to projections of a normal vector onto perpendicular subspaces (Cochran's theorem), which also yield the $F$ distribution.
Suppose $X_1, \dots, X_n$ are independent draws from a normal population with mean $\mu$ and standard deviation $\sigma$, with $n \ge 2$. Let $\bar{X}$ be their mean and $S^2$ their sample variance (with divisor $n - 1$). A chi-squared distribution with $\nu$ degrees of freedom, written $\chi^2_\nu$, is the distribution of a sum of the squares of $\nu$ independent standard normal variables. Then:
- The mean is normal. $Z = (\bar{X} - \mu)/(\sigma/\sqrt{n})$ is standard normal.
- The scaled variance is chi-squared. $V = (n-1)S^2/\sigma^2$ has the $\chi^2_{n-1}$ distribution.
- The mean and the variance are independent. $\bar{X}$ and $S^2$ are independent, and so $Z$ and $V$ are.
The $t$ distribution with $\nu$ degrees of freedom is the distribution of $Z/\sqrt{V/\nu}$, where $Z$ is standard normal, $V$ is $\chi^2_\nu$, and the two are independent. The three facts supply exactly these ingredients with $\nu = n - 1$, and in the ratio $Z/\sqrt{V/(n-1)} = (\bar{X} - \mu)/(S/\sqrt{n})$ the unknown $\sigma$ cancels. That is the theorem on Student's ratio, proved in the same article: for a normal population, $T = (\bar{X} - \mu)/(S/\sqrt{n})$ has the $t$ distribution with $n - 1$ degrees of freedom, whatever the values of $\mu$ and $\sigma$. It is therefore a pivot: a quantity involving the unknown $\mu$ whose distribution is completely known.
The density of $t$, derived in that article, is symmetric and bell-shaped like the normal, but with heavier tails, and closer to the normal as the degrees of freedom grow. The heavier tails are the variability of $S$ made visible: a sample whose $S$ happens to be small produces a large ratio.
The tail numbers in the figure explain the coverage table above. With 9 degrees of freedom, the probability of a value beyond $\pm 1.96$ is 0.082 rather than 0.05, so $\bar{x} \pm 1.96\,s/\sqrt{n}$ covers about 92% of the time for samples of 10, as the simulation found.
Inverting the pivot
The interval now follows by the steps used for $z$. Let $t_{n-1,\,1-\alpha/2}$ be the point with probability $1 - \alpha/2$ below it under the $t$ curve with $n - 1$ degrees of freedom. The density is symmetric about 0, so the probability below $-t_{n-1,\,1-\alpha/2}$ is $\alpha/2$ as well, and
$$P\left(-t_{n-1,\,1-\alpha/2} \le \frac{\bar{X} - \mu}{S/\sqrt{n}} \le t_{n-1,\,1-\alpha/2}\right) = 1 - \alpha.$$
Multiply the inequalities by $S/\sqrt{n}$, which is positive, subtract $\bar{X}$, and multiply by $-1$, which reverses both. The event is unchanged, so
$$P\left(\bar{X} - t_{n-1,\,1-\alpha/2}\frac{S}{\sqrt{n}} \le \mu \le \bar{X} + t_{n-1,\,1-\alpha/2}\frac{S}{\sqrt{n}}\right) = 1 - \alpha.$$
For a normal population this holds exactly, for every sample size $n \ge 2$. The resulting $t$ interval is
$$\bar{x} \pm t_{n-1,\,1-\alpha/2}\frac{s}{\sqrt{n}}.$$
It has the same shape as the $z$ interval. Only the critical value has changed, and it now depends on $n$.
Critical values
How much larger is the new critical value? A lot for small samples, and very little for large ones. The values below are computed by the article's script. They agree with scipy to within $2 \times 10^{-11}$, and with the NIST handbook's printed table (§1.3.6.7.2) to its three decimals.
| $n$ | Degrees of freedom | $t_{n-1,\,0.95}$ | $t_{n-1,\,0.975}$ | $t_{n-1,\,0.995}$ |
|---|---|---|---|---|
| 2 | 1 | 6.314 | 12.706 | 63.657 |
| 3 | 2 | 2.920 | 4.303 | 9.925 |
| 5 | 4 | 2.132 | 2.776 | 4.604 |
| 10 | 9 | 1.833 | 2.262 | 3.250 |
| 20 | 19 | 1.729 | 2.093 | 2.861 |
| 30 | 29 | 1.699 | 2.045 | 2.756 |
| 100 | 99 | 1.660 | 1.984 | 2.626 |
| normal | $\infty$ | 1.645 | 1.960 | 2.576 |
The middle column gives the multiplier for a two-sided 95% interval. With two observations it is 12.7, an honest admission of how little two numbers say about spread. By $n = 30$ it is 2.045, about 4% above 1.96, and by $n = 100$ the difference is 1%.
The often-quoted rule that the normal can replace $t$ "above 30" is, in the light of this table, a statement about gradual convergence, not a threshold where anything changes.
Why the divisor is n - 1
The sample variance divides by $n - 1$ rather than $n$. Why? Because squared deviations from the sample mean run systematically small. The sample mean is, by construction, the value closest to the data, closer than the true mean $\mu$. So the sum of squared deviations from $\bar{X}$ is smaller than the sum from $\mu$, and the derivation below shows by exactly how much.
Add and subtract $\mu$ inside each deviation and expand:
$$\sum_{i=1}^{n}(X_i - \bar{X})^2 = \sum_{i=1}^{n}(X_i - \mu)^2 - n(\bar{X} - \mu)^2.$$
(The cross term is $-2(\bar{X} - \mu)\sum_i (X_i - \mu) = -2n(\bar{X} - \mu)^2$, which combines with the $+n(\bar{X} - \mu)^2$ from the squares.) Now take expectations. Each $(X_i - \mu)^2$ has expectation $\sigma^2$, and $(\bar{X} - \mu)^2$ has expectation $\sigma^2/n$, the squared standard error. So
$$E\left[\sum_{i=1}^{n}(X_i - \bar{X})^2\right] = n\sigma^2 - n\cdot\frac{\sigma^2}{n} = (n-1)\sigma^2.$$
Dividing by $n - 1$ therefore gives an estimate of $\sigma^2$ that is right on average, an unbiased estimate. Dividing by $n$ gives one that is too small on average by the factor $(n-1)/n$. This holds for independent observations from any population with a finite variance, not only normal ones. (Independence is used in $E(\bar{X} - \mu)^2 = \sigma^2/n$.) The divisor-$n$ variance is the plug-in estimate, and The Jackknife proves that its leave-one-out bias correction turns it into exactly the divisor-$(n - 1)$ variance.
The script checks it with samples of 5 from a normal population with $\sigma^2 = 1$. Over 30 runs of 10,000 samples, the average of $s^2$ was 0.998 with the divisor $n - 1$ and 0.798 with the divisor $n$, against the theoretical 1 and 0.8.
Student himself used the divisor $n$, which was the custom at the time, and tabulated the distribution of $z = (\bar{x} - \mu)/s$ with that $s$. The modern statistic is a rescaling of his. Since $s_{n-1} = s_n\sqrt{n/(n-1)}$,
$$t = \frac{\bar{x} - \mu}{s_{n-1}/\sqrt{n}} = \frac{\bar{x} - \mu}{s_n}\sqrt{n-1} = z\sqrt{n-1}.$$
The two carry the same information, and give the same intervals once the matching table is used.
Student's sleep data
Now the method can meet real data, and the data have a story of their own.
"Student" was the pen name of William Sealy Gosset, an Oxford chemistry graduate employed by Guinness. The brewery allowed its staff to publish only under pseudonyms (Trkulja and Hrabač, 2020). His paper's first illustration took data from a table published by Cushny and Peebles, from a within-patient trial of sleeping drugs at Kalamazoo (Senn and Richardson, 1994). Student's table gives, for each of ten patients, the average hours of sleep gained with each of two drugs, and the difference between them.
His citation does not quite match the original. He dated the paper 1904 and named the drugs as the two optical isomers of hyoscyamine hydrobromide. The paper in fact appeared in 1905, titled "The action of optical isomers. II. Hyoscines". Senn and Richardson (1994) trace how the data were misquoted by Student and later by Fisher, and discuss the trial itself. This article uses Student's numbers as printed and calls the drugs simply drug 1 and drug 2.
Because the same patients took both drugs, the comparison reduces to one sample: the ten differences, drug 2 minus drug 1. The question is the mean difference $\mu$ in a population of such patients. (Treating paired data this way is the subject of The Paired Samples t-Test. The mean and standard deviation of the differences are written $\bar{d}$ and $s_d$.)
| Patient | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Drug 1 | 0.7 | −1.6 | −0.2 | −1.2 | −0.1 | 3.4 | 3.7 | 0.8 | 0.0 | 2.0 |
| Drug 2 | 1.9 | 0.8 | 1.1 | 0.1 | −0.1 | 4.4 | 5.5 | 1.6 | 4.6 | 3.4 |
| Difference $d_i$ | 1.2 | 2.4 | 1.3 | 1.3 | 0.0 | 1.0 | 1.8 | 0.8 | 4.6 | 1.4 |
| $d_i - \bar{d}$ | −0.38 | 0.82 | −0.28 | −0.28 | −1.58 | −0.58 | 0.22 | −0.78 | 3.02 | −0.18 |
Step 1, the mean. The differences sum to 15.8, so $\bar{d} = 15.8/10 = 1.580$ hours.
Step 2, the standard deviation. The squared deviations in the last row sum to 13.616, so
$$s_d = \sqrt{\frac{13.616}{9}} = 1.2300 \text{ hours}.$$
Step 3, the standard error. $s_d/\sqrt{n} = 1.2300/\sqrt{10} = 0.3890$ hours.
Step 4, the critical value. With $n - 1 = 9$ degrees of freedom, $t_{9,\,0.975} = 2.2622$.
Step 5, the interval. The margin of error is $2.2622 \times 0.3890 = 0.8799$. The 95% confidence interval for the mean extra sleep from drug 2 over drug 1 is
$$1.580 \pm 0.880 = [0.700,\ 2.460] \text{ hours}.$$
What does the interval say? The data are consistent with drug 2 adding anything from about 0.7 to about 2.5 hours of sleep over drug 1. They are not consistent with the drugs being equally effective, since zero lies outside. At other levels, $t_{9,\,0.95} = 1.8331$ gives the 90% interval $[0.867, 2.293]$, and $t_{9,\,0.995} = 3.2498$ gives the 99% interval $[0.316, 2.844]$, which still excludes zero. The same endpoints, $[0.700, 2.460]$, are also a Bayesian 95% credible interval for the mean under the improper prior $1/\sigma^2$, with a different meaning (Credible Intervals and How They Differ from Confidence Intervals).
What would the plain substitution have reported? With 1.96 in place of 2.2622, the 95% interval would have been $1.580 \pm 1.96 \times 0.3890 = [0.818, 2.342]$. Its margin is 13% narrower ($2.2622/1.96 = 1.154$), and with ten observations the simulation above says such an interval covers about 92% of the time, not 95%.
Student posed his own calculation differently, as the probability that drug 2 is the better soporific. In his notation $z = 1.580/1.1669 = 1.354$, using the standard deviation with divisor $n$. His table for ten experiments gave a probability of 0.9985, "or the odds are about 666 to one that 2 is the better soporific" (Student, 1908). The modern version of the same number is $t = 1.354 \times \sqrt{9} = 4.062$, and $P(T_9 \le 4.062) = 0.99858$. Today we would not read that number as the probability that the drug is better. It reappears as a one-sided $p$-value in One-Sided Confidence Bounds and Sample Size and Hypothesis Tests for a Mean.
When the population is not normal
The $t$ interval's exact 95% rests on all three facts about normal samples. Real data are rarely normal, and this section measures how much that matters.
Student (1908) assumed normality. His introduction notes that "a small sample will give no real information as to the manner in which the population deviates from normality". His conclusions state that his curve "represents the facts fairly well even when the distribution of the population is not strictly normal". The simulation can test how fairly. The script repeated the coverage experiment for three populations:
- the normal population used above;
- the NHANES blood pressure population of Confidence Intervals and Coverage, moderately skewed (skewness 1.04);
- a strongly skewed lognormal population, the exponential of a standard normal variable (skewness 6.18).
| $n$ | Normal, $t$ | Normal, 1.96 with $s$ | Blood pressure, $t$ | Blood pressure, 1.96 with $s$ | Lognormal, $t$ | Lognormal, 1.96 with $s$ |
|---|---|---|---|---|---|---|
| 3 | 0.949 | 0.810 | 0.939 | 0.795 | 0.837 | 0.675 |
| 5 | 0.951 | 0.879 | 0.934 | 0.859 | 0.821 | 0.741 |
| 10 | 0.950 | 0.920 | 0.936 | 0.904 | 0.840 | 0.808 |
| 30 | 0.950 | 0.941 | 0.943 | 0.933 | 0.883 | 0.873 |
| 100 | 0.951 | 0.948 | 0.948 | 0.945 | 0.917 | 0.915 |
The three populations tell three different stories.
- Normal. The $t$ interval is exact, as the theory says, from $n = 3$ upward.
- Blood pressure. The $t$ interval loses one to two points of coverage at small $n$. That is roughly what Student's "fairly well" suggests, and far less than the plain substitution loses.
- Lognormal. Both intervals fail: 84% coverage at $n = 10$, and still 92% at $n = 100$.
Why does strong skewness hurt so much? The script's samples point to the third fact. For a skewed population, the sample mean and standard deviation are no longer independent, as Student (1908) had already observed: if the distribution is not normal, "the mean and the standard deviation of a sample will be positively correlated". In one run of 10,000 samples of 10 (seed 0), their correlation was 0.55 for blood pressure and 0.88 for the lognormal. For the normal population it was −0.02. The true value there is exactly 0 by fact 3, and a correlation from 10,000 samples has a standard error of about 0.01, so −0.02 is sampling noise.
The mechanism is easy to picture. With a long right tail, a sample that happens to miss the tail has both a low mean and a small standard deviation. Its interval is then too low and too narrow at once. So almost all the misses fall on one side. For the lognormal population at $n = 10$, in one run of 10,000 samples (seed 1), 16.3% of the $t$ intervals fell entirely below the true mean and 0.1% entirely above. That run missed slightly more often than the table's median coverage of 0.840 implies (16.0% missed), which is within its sampling error.
For strongly skewed data, a transformation (often the logarithm, with the interval then describing the geometric mean) or a resampling method such as the bootstrap is usually a better choice; Bootstrap Confidence Intervals measures how often bootstrap intervals cover the mean of a skewed population. With very small samples, though, no method can make up for the missing information about the tail. Significance Testing works a bootstrap interval for a mean difference.
Choosing between z and t
The two intervals have the same form, point estimate plus or minus critical value times standard error:
| Case | Interval for $\mu$ | Critical value from |
|---|---|---|
| $\sigma$ known | $\bar{x} \pm z_{1-\alpha/2}\,\dfrac{\sigma}{\sqrt{n}}$ | standard normal |
| $\sigma$ unknown | $\bar{x} \pm t_{n-1,\,1-\alpha/2}\,\dfrac{s}{\sqrt{n}}$ | $t$ distribution, $n - 1$ degrees of freedom |
Both appear in this form, with $\bar{Y}$ and $N$ for $\bar{x}$ and $n$, in the NIST/SEMATECH e-Handbook of Statistical Methods (§7.1.4 for the first, §1.3.5.2 for the second). Choosing between them is straightforward.
- Use $z$ when $\sigma$ comes from outside the sample: a known measurement process, or a value established by long experience. This is rare in medical research.
- Use $t$ whenever $\sigma$ is estimated from the same sample. It is exact for a normal population at every sample size. For large samples it differs negligibly from $z$, so there is no sample size at which switching to $z$ is needed.
- Check the shape of the data before trusting either for a small sample. The $t$ interval tolerates moderate skewness. It does not tolerate strong skewness or outliers, which the nonparametric methods of a later topic, such as the sign and signed-rank tests, are designed for.
How much skewness matters was measured in the section "When the population is not normal". NHANES blood pressure (skewness 1.04) cost the $t$ interval 1 to 2 points of coverage at $n \le 10$. A lognormal population (skewness 6.18) left it at 84% for $n = 10$ and 92% for $n = 100$, with nearly all misses on the same side. For such data, consider a log transformation or a bootstrap interval (Bootstrap Confidence Intervals). For paired data such as the sleep differences, a sign-flip permutation test needs no normality at all. Its p-values are exact when the differences are independent and each is symmetric about zero under the null hypothesis. Permutation Tests works it on Student's sleep data and inverts it into a confidence interval, 0.83 to 2.47 hours, against the $t$ interval's 0.70 to 2.46.
Worked problems
The first problem is an exercise from the course text's chapter on confidence intervals (Pagano, Gauvreau and Mattie, 2022), restated in our words and solved here; its last two parts, on sample size, are solved in One-Sided Confidence Bounds and Sample Size. The second is posed on Student's sleep data, to exercise the $t$ distribution at the smallest possible sample size. Problems on the rotation and on the chi-squared distribution are in The Chi-Squared and t Distributions. Every number is recomputed by the article's script.
Problem 1 (gestational ages). Among infants who undergo fetal surgery for congenital anomalies, gestational age at birth is approximately normal with unknown mean $\mu$ and unknown standard deviation. A random sample of 14 such infants has mean gestational age 29.6 weeks and standard deviation 3.6 weeks. (a) Construct a 95% confidence interval for $\mu$. (b) Find the length of the interval.
Solution. (a) The interval is $[27.52, 31.68]$ weeks. The standard deviation is estimated from the sample, so the interval is $\bar{x} \pm t_{n-1,\,1-\alpha/2}\,s/\sqrt{n}$, exact if gestational age is normal, and close to exact since it is approximately normal. The estimated standard error is $3.6/\sqrt{14} = 0.9621$ weeks. With $14 - 1 = 13$ degrees of freedom, $t_{13,\,0.975} = 2.1604$, so the margin of error is $2.1604 \times 0.9621 = 2.079$ and the interval is $29.6 \pm 2.079$.
(b) The length is twice the margin, $2 \times 2.079 = 4.16$ weeks. Using 1.96 in place of 2.1604 would have given $[27.71, 31.49]$, about 9% shorter, and for 14 observations the coverage of that interval is $P(|T_{13}| \le 1.96) = 0.928$, not 0.95.
Problem 2 (the smallest sample). For $n = 2$, express Student's ratio through the two observations, find the distribution function of $t$ with 1 degree of freedom, and derive the critical values 6.314, 12.706 and 63.657 in the first row of the table. Then find the 95% interval from the first two sleep patients alone, whose differences are 1.2 and 2.4 hours.
Solution. The interval is $1.8 \pm 7.62 = [-5.82, 9.42]$ hours. For $n = 2$, Helmert's matrix, the rotation used in The Chi-Squared and t Distributions, has rows $(1, 1)/\sqrt{2}$ and $(1, -1)/\sqrt{2}$. So $\bar{X} = (X_1 + X_2)/2$, and the sum of squared deviations is the square of the second coordinate, $S^2 = (X_1 - X_2)^2/2$. The estimated standard error is $S/\sqrt{2} = |X_1 - X_2|/2$, half the distance between the two observations, and
$$T = \frac{\bar{X} - \mu}{|X_1 - X_2|/2}.$$
By the theorem on Student's ratio, $T$ has the $t$ distribution with 1 degree of freedom, whose density is $1/[\pi(1 + t^2)]$ (the Cauchy density, found in that article). An antiderivative is $\arctan(t)/\pi$, which tends to $-\tfrac12$ as $t \to -\infty$, so the distribution function is
$$P(T \le t) = \frac12 + \frac{\arctan t}{\pi}, \qquad\text{and}\qquad t_{1,\,p} = \tan\!\left[\pi\left(p - \tfrac12\right)\right].$$
That gives $t_{1,\,0.95} = \tan(0.45\pi) = 6.314$, $t_{1,\,0.975} = \tan(0.475\pi) = 12.706$ and $t_{1,\,0.995} = \tan(0.495\pi) = 63.657$, the first row of the table. For the two patients, $\bar{x} = 1.8$ and $|x_1 - x_2|/2 = 0.6$, so the margin is $12.706 \times 0.6 = 7.62$. Two observations locate the mean of extra sleep only to within several hours either way. The ten patients together gave a margin of 0.88.
What we have learned
Replacing $\sigma$ by $s$ looks harmless, but for small samples it costs real coverage, because $s$ itself varies. Student's repair keeps the form of the interval and changes only the critical value, taking it from the $t$ distribution with $n - 1$ degrees of freedom. That distribution follows from three facts about normal samples: the mean is normal, the scaled variance is chi-squared, and the two are independent. The Chi-Squared and t Distributions proves all three with one rotation of the sample. The same facts explain the divisor $n - 1$, and their failure for skewed data explains where the $t$ interval breaks down.
The same pivot appears wherever a mean is divided by its estimated standard error. The interval for a regression slope in Simple Linear Regression uses $t$ with $n - 2$ degrees of freedom, because two coefficients are estimated. The two-sample intervals of The Two-Sample t-Test use $n_1 + n_2 - 2$, by adding two chi-squared variables. And the ratio of two independent chi-squared variables, each over its degrees of freedom, gives the $F$ distribution of One-Way Analysis of Variance.
Two matters remain within this topic: a bound for questions with only one interesting direction, such as how much sleep the drug adds at the least, and the number of patients a new trial would need for a narrower interval. One-Sided Confidence Bounds and Sample Size derives both.
In Python
scipy provides the $t$ quantiles and, through its one-sample test, the interval itself.
import numpy as np
from scipy import stats
d = np.array([1.2, 2.4, 1.3, 1.3, 0.0, 1.0, 1.8, 0.8, 4.6, 1.4])
n, dbar, s = len(d), d.mean(), d.std(ddof=1) # ddof=1: divisor n - 1
se = s / np.sqrt(n)
t = stats.t.ppf(0.975, df=n - 1)
print(round(dbar, 3), round(s, 4), round(t, 4)) # 1.58 1.23 2.2622
print(dbar - t * se, dbar + t * se) # about 0.700 and 2.460
res = stats.ttest_1samp(d, popmean=0)
print(res.confidence_interval(0.95)) # the same interval
NumPy's std divides by $n$ unless told otherwise; ddof=1 gives the sample standard deviation. Other library defaults catch analysts too. statsmodels' DescrStatsW(x).std also divides by $n$, though its intervals use $n - 1$, and scipy.stats.bootstrap returns BCa intervals unless told otherwise. Inferential Statistics in Python goes through these defaults on NHANES blood pressures. Every number in this article is computed by tools/experiments/applied-statistics/confidence-intervals-for-a-mean.py in the site's repository, which implements the $t$ distribution directly and is checked against scipy 1.17.1.
References
- Cushny, A. R., and Peebles, A. R. (1905). The action of optical isomers. II. Hyoscines. The Journal of Physiology, 32(5–6), 501–510. record · 3 pages
- NIST/SEMATECH. e-Handbook of Statistical Methods, §1.3.5.2 (Confidence limits for the mean), §1.3.6.7.2 (Critical values of the Student's t distribution) and §7.1.4 (What are confidence intervals?). National Institute of Standards and Technology. https://www.itl.nist.gov/div898/handbook/ record · 37 pages
- Pagano, M., Gauvreau, K., and Mattie, H. (2022). Principles of Biostatistics (3rd ed.). CRC Press. The course text. record · 48 pages
- Senn, S., and Richardson, W. (1994). The first t-test. Statistics in Medicine, 13(8), 785–803. record · 2 pages
- Student (1908). The probable error of a mean. Biometrika, 6(1), 1–25. record · 17 pages
- Trkulja, V., and Hrabač, P. (2020). The role of t test in beer brewing. Croatian Medical Journal, 61(1), 69–72. record
Reviewed: verified, 16 Nov 2026 · second opinion, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 11 revisions