Confidence Intervals for a Mean: z and t

The interval for a population mean when the standard deviation is known (z) and when it must be estimated from the sample (t). Why substituting s for sigma breaks the z interval for small samples, Student's 1908 solution, the chi-squared and t distributions derived from the sampling distribution with every step shown, why the divisor is n - 1, Student's own sleep-drug data worked by hand, and how well the t interval holds up for skewed populations.

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.

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 we derive from three facts about normal samples. 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 where the course defines the chi-squared and $t$ distributions. Later articles that use them, such as The Two-Sample t-Test and One-Way Analysis of Variance, build on the facts proved here.

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.

$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.

Check your understanding. 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. Why don't the two errors cancel?

Because coverage does not respond symmetrically to width. 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 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.

Three facts about normal samples

Suppose $X_1, \dots, X_n$ are independent draws from a normal population with mean $\mu$ and standard deviation $\sigma$. Let $\bar{X}$ be their mean and $S^2$ their sample variance (with divisor $n - 1$).

1. The mean is normal. $\bar{X}$ has a normal distribution with mean $\mu$ and standard deviation $\sigma/\sqrt{n}$, so

$$Z = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}}$$

is standard normal. This is the pivot of the known-$\sigma$ interval in Confidence Intervals and Coverage.

2. The scaled variance is chi-squared. We need a name for the distribution of a sum of squared normals. A chi-squared distribution with $\nu$ degrees of freedom, written $\chi^2_\nu$, is the distribution of

$$Z_1^2 + Z_2^2 + \cdots + Z_\nu^2,$$

where $Z_1, \dots, Z_\nu$ are independent standard normal variables. Two properties follow at once from this definition, and later articles rely on both:

  • Its mean is $\nu$. Each $Z_j^2$ has expectation $\operatorname{Var}(Z_j) = 1$, and there are $\nu$ of them.
  • Independent chi-squared variables add. If $U$ is $\chi^2_a$ and $V$ is an independent $\chi^2_b$, then $U + V$ is a sum of $a + b$ independent squared standard normals, so it is $\chi^2_{a+b}$. The degrees of freedom add.

For a normal sample, the scaled sample variance

$$V = \frac{(n-1)S^2}{\sigma^2} = \sum_{i=1}^{n}\left(\frac{X_i - \bar{X}}{\sigma}\right)^2$$

has a $\chi^2_{n-1}$ distribution. It looks like a sum of $n$ squared standardized deviations, but the deviations are not independent. Deviations from the sample mean always sum to zero, so once $n - 1$ of them are known, the last is determined. One degree of freedom is used up by estimating $\mu$ with $\bar{X}$, and $n - 1$ remain. As a check, the mean of $V$ is $n - 1$, which says $E(S^2) = \sigma^2$; the section on the divisor below proves the same thing another way.

3. The mean and the variance are independent. For a normal population, and only for a normal population, $\bar{X}$ and $S^2$ are statistically independent. Knowing how spread out a sample is tells nothing about where its mean fell. Student derived the distribution of the sample standard deviation of a normal sample in Section I of his paper, and showed in Section II that it is uncorrelated with the sample mean.

Facts 2 and 3 need more machinery to prove than this article uses (a rotation of coordinates, of the kind One-Way Analysis of Variance uses for the between-groups sum of squares). We take them as given; the rest follows by algebra.

Dividing out sigma

The $t$ distribution with $\nu$ degrees of freedom is defined as the distribution of

$$T = \frac{Z}{\sqrt{V/\nu}},$$

where $Z$ is standard normal, $V$ is $\chi^2_\nu$, and the two are independent. In words: a standard normal divided by the square root of an independent chi-squared over its degrees of freedom.

Facts 1 to 3 supply exactly these ingredients, with $\nu = n - 1$. Substituting them,

$$\frac{Z}{\sqrt{V/(n-1)}} = \frac{(\bar{X} - \mu)\big/(\sigma/\sqrt{n})}{\sqrt{S^2/\sigma^2}} = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \cdot \frac{\sigma}{S} = \frac{\bar{X} - \mu}{S/\sqrt{n}}.$$

The unknown $\sigma$ cancels. That is the whole point. The ratio that replaces $\sigma$ by $S$ has a $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.

Its density is

$$f(t) = \frac{\Gamma\!\left(\frac{\nu+1}{2}\right)}{\sqrt{\nu\pi}\;\Gamma\!\left(\frac{\nu}{2}\right)}\left(1 + \frac{t^2}{\nu}\right)^{-(\nu+1)/2},$$

where $\Gamma$ is the gamma function, which extends the factorial to non-integers ($\Gamma(k) = (k-1)!$ for whole numbers $k$). This is the density given in the NIST handbook (§1.3.6.6.4), written there with the beta function, which is equivalent.

What does it look like? 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.

Left: the t densities with 1, 4 and 9 degrees of freedom against the standard normal. They look alike in the middle. Right: the upper tail magnified, which is where they differ and where a 95% interval's critical value lives. The labels give the probability of falling beyond 1.96 in either direction: 0.050 for the normal, 0.082 with 9 degrees of freedom, 0.122 with 4 and 0.300 with 1. That extra tail probability is exactly the coverage the plain substitution loses.
Left: the t densities with 1, 4 and 9 degrees of freedom against the standard normal. They look alike in the middle. Right: the upper tail magnified, which is where they differ and where a 95% interval's critical value lives. The labels give the probability of falling beyond 1.96 in either direction: 0.050 for the normal, 0.082 with 9 degrees of freedom, 0.122 with 4 and 0.300 with 1. That extra tail probability is exactly the coverage the plain substitution loses.

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. For $\nu > 2$ the variance of $T$ is $\nu/(\nu - 2)$, for example $9/7 = 1.29$ with 9 degrees of freedom, against 1 for the standard normal.

Inverting the pivot

The interval now follows exactly as 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. By symmetry,

$$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.$$

Rearranging the inequalities as before gives

$$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 any population with a finite variance, not only normal ones.

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 notation $\bar{d}$ and $s_d$ for the mean and standard deviation of the differences follows the course's formula sheets.)

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}.$$

The ten differences in Student's sleep data (dots; two patients share the value 1.3) and the 95% t interval for their mean, 0.70 to 2.46 hours. Notice that the whole interval sits well clear of zero: nine patients slept longer on drug 2 and the tenth slept the same. Patient 9, at 4.6 hours, is the one large difference.
The ten differences in Student's sleep data (dots; two patients share the value 1.3) and the 95% t interval for their mean, 0.70 to 2.46 hours. Notice that the whole interval sits well clear of zero: nine patients slept longer on drug 2 and the tenth slept the same. Patient 9, at 4.6 hours, is the one large difference.

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.

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, so how much does that matter?

Student (1908) assumed normality, noting that a small sample gives little information about the shape of the population anyway. He concluded that his curve represented the facts fairly well even when the population was 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.841 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
Coverage of the nominal 95% interval as the sample size grows, median of 30 runs of 10,000 samples. Solid: the t interval. Dashed: 1.96 times the estimated standard error. Read across the three panels: the t interval is exact for a normal population at every n, within two points of 95% for blood pressure, and well short for the strongly skewed lognormal population, where even 100 observations do not bring coverage back to 95%.
Coverage of the nominal 95% interval as the sample size grows, median of 30 runs of 10,000 samples. Solid: the t interval. Dashed: 1.96 times the estimated standard error. Read across the three panels: the t interval is exact for a normal population at every n, within two points of 95% for blood pressure, and well short for the strongly skewed lognormal population, where even 100 observations do not bring coverage back to 95%.

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? Much of the failure comes from the third fact. For a skewed population, the sample mean and standard deviation are no longer independent. In the script's samples of 10, their correlation was −0.02 for the normal population, 0.55 for blood pressure and 0.88 for the lognormal.

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$, 16.3% of intervals fell entirely below the true mean and 0.1% entirely above.

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. 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.

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 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 questions remain within this topic. What if only one direction matters, such as how much sleep the drug adds at the least? And how many patients would a new trial need for a narrower interval? Both are answered in One-Sided Confidence Bounds and Sample Size.

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. 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.6.4 (t distribution), §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 · 30 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 · 11 pages
  • Trkulja, V., and Hrabač, P. (2020). The role of t test in beer brewing. Croatian Medical Journal, 61(1), 69–72. record