Inference

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 (n−1)s2/σ2(n-1)s^2/\sigma^2 has the chi-square distribution with n−1n - 1 degrees of freedom. So

((n−1)s2χn−1, 1−α/22, (n−1)s2χn−1, α/22) \left( \frac{(n-1)s^2}{\chi^2_{n-1,\,1-\alpha/2}},\ \frac{(n-1)s^2}{\chi^2_{n-1,\,\alpha/2}} \right)

is a confidence interval for σ2\sigma^2, and its square roots one for σ\sigma. 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 (36/19.023,36/2.7004)≈(1.8925,13.331)(36/19.023, 36/2.7004) \approx (1.8925, 13.331).

The two-sided p-value of an asymmetric statistic is twice the smaller tail, capped at 1.

Two variances

The ratio (s12/σ12)/(s22/σ22)(s_1^2/\sigma_1^2)/(s_2^2/\sigma_2^2) has the F(n1−1,n2−1)F(n_1 - 1, n_2 - 1) distribution. fTest tests a stated ratio (1 by default) and its interval is for σ12/σ22\sigma_1^2/\sigma_2^2.

const f = fTest(drug1, drug2);
f.statistic;     // 28805/36081
f.interval();    // from 0.1983… to 3.2141…

Proportions

With kk successes in nn trials, H0:p=p0H_0: p = p_0 is tested by

z2=(k−np0)2np0(1−p0), z^2 = \frac{(k - np_0)^2}{np_0(1 - p_0)},

compared with χ12\chi^2_1. For two samples the pooled proportion p^=(k1+k2)/(n1+n2)\hat p = (k_1 + k_2)/(n_1 + n_2) gives z2=(p^1−p^2)2/(p^(1−p^)(1/n1+1/n2))z^2 = (\hat p_1 - \hat p_2)^2 / \bigl(\hat p(1 - \hat p)(1/n_1 + 1/n_2)\bigr), 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.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs