Tests for means
Is a mean what it is claimed to be? Do two means differ? The z and t tests answer with a p-value, the matching intervals with a range of plausible values. Here the statistic is exact, the p-value is a certified enclosure, and a decision that sits on the edge says so.
One sample, and pairs
// Student's sleep data (1908): extra hours of sleep for ten patients on two drugs
const drug1 = [0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0];
const drug2 = [1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4];
import { tTest, meanInterval, show } from "@ratmath/stats";
const p = tTest(drug2, drug1, { paired: true });
String(p); // "paired t test: p = 0.002833 (certified, two-sided)"
p.statisticSquared; // 56169/3404: t² exactly
show(p.statistic); // "4.062128"
p.df; // 9
p.interval(); // { low: 0.7001…, high: 2.4599…, level: 19/20, … }: the mean extra sleep
p.reject("0.01"); // true
For a sample with mean and variance , the t statistic for is
and for normal data it follows Student's t distribution with degrees of freedom. A paired test is the one-sample test applied to the differences within pairs, which removes the variation between patients: here the same data give p = 0.0028 paired and p = 0.079 unpaired.
How exactness survives the square root
involves , which is rarely rational. Its square
is, so that is what is kept, and the two-sided p-value is computed from it: if has the t distribution with degrees of freedom, has the distribution, so , evaluated as a certified enclosure at an exact argument. A one-sided p-value is half of that, or one minus half, according to the sign of . The z test works the same way with , the distribution of .
Two samples: Welch, or pooled
tTest(drug2, drug1); // Welch: p = 0.079394, df = 17.776474…
tTest(drug2, drug1).df; // an exact fraction
tTest(drug2, drug1, { equalVariance: true }); // pooled: p = 0.079187, df = 18
With two independent samples the standard error of is . Welch's test uses it with the Welch–Satterthwaite degrees of freedom
which is an exact rational here. The pooled test assumes equal variances and uses on degrees of freedom. When the variances differ and the smaller sample has the larger variance the pooled test rejects far too often; the testing the tests page measures by how much. Welch's test is the default.
Intervals are the tests turned around
The interval holds exactly the values that the two-sided test at level keeps. The quantile and the square root are enclosures, so each end is rounded outward to a rational: the interval returned contains the true interval.
meanInterval(drug1); // t interval, 95%
meanInterval(drug1, { level: "0.99" }); // wider
meanInterval(drug1, { alternative: "greater" }); // { low: …, high: null }: one-sided
zTest([102, 98, 110, 105, 95, 108, 101, 99], { mu: 100, sigma: 5 }); // σ known: p = 0.203092
Textbook problems often give only a summary. meanInterval takes { n, mean, sd } (or variance) in place of the data, with the sample standard deviation (divisor ); with sigma it is the z interval. For power and sample sizes see Power and sample size.
meanInterval({ n: 16, mean: "25.3", sd: "4.2" }); // t interval: 23.0620 to 27.5380 (t* = 2.1314, SE = 1.05)
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.