Variances and proportions
The spread of normal data is judged with the chi-square and F distributions, and proportions with the normal approximation. The statistics are exact fractions; the p-values and interval ends are certified.
One variance
For normal data has the chi-square distribution with degrees of freedom. So
is a confidence interval for , and its square roots one for . Unlike the t interval it is not symmetric, and it leans heavily on normality: with heavy tails its coverage drops well below the level.
// 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 { varianceInterval, varianceTest, fTest, proportionTest, show } from "@ratmath/stats";
varianceInterval(drug1); // σ² between 1.5142… and 10.6670…; estimate 3.200556; sd from 1.2305 to 3.2660
varianceTest(drug1, 4).statistic; // 5761/800 = (n − 1)s²/σ0², exactly
String(varianceTest(drug1, 4)); // "chi-square test for a variance: p = 0.767649 (certified, two-sided)"
With only a summary, pass { n, variance } or { n, sd }: varianceInterval({ n: 10, variance: 4 }) is .
The two-sided p-value of an asymmetric statistic is twice the smaller tail, capped at 1.
Two variances
The ratio has the distribution. fTest tests a stated ratio (1 by default) and its interval is for .
const f = fTest(drug1, drug2);
f.statistic; // 28805/36081
f.interval(); // from 0.1983… to 3.2141…
Proportions
With successes in trials, is tested by
compared with . For two samples the pooled proportion gives , which is exactly the chi-square statistic of the 2×2 table. The interval for one proportion is Wilson's; for a difference it is the Wald interval.
proportionTest(527, 1000); // p = 0.087705; z² = 729/250
binomialTest(527, 1000); // the exact test: p = 0.093687
proportionTest(527, 1000, "1/2", { correct: true }); // with Yates's continuity correction
proportionTest([45, 30], [100, 100]); // p = 0.028460; the difference 3/20
proportionTest([45, 30], [100, 100]).interval(); // from 0.0174… to 0.2826…
The normal approximation is a convenience. For small counts the exact binomial and Fisher tests give p-values as fractions and need no approximation at all.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.