Inference

Testing the tests

A 95% interval should catch the truth 95% of the time; a test at 5% should reject a true null 5% of the time. Both are claims about repeated sampling, so they can be checked by drawing from a seeded model whose truth is known. The package's own test suite does exactly that.

Coverage

import { coverage, meanInterval, normal, Rational } from "@ratmath/stats";

const sample = (dist, n, rng) =>
  Array.from({ length: n }, () => new Rational(BigInt(Math.round(dist.sample(rng) * 100)), 100n));

const c = coverage({
  draw: (rng) => sample(normal(10, 2), 12, rng),      // twelve readings, true mean 10
  interval: (d) => meanInterval(d, { bits: 32 }),
  truth: 10, runs: 300, seed: 3,
});
String(c);              // the fraction covered, with its own 95% interval from the 300 runs
c.compare("0.95");      // "inside": 95% is consistent with what was seen

Each run draws a fresh sample from the seeded generator, builds the interval, and records whether it contains the true value. The proportion covered is itself an estimate, so coverage reports it with a binomial interval, and compare says whether the nominal level is "inside" that interval, or the procedure is clearly "above" or "below" it. With 300 runs the interval is about ±2.5 percentage points wide, so this catches gross failures, not subtle ones. Because the generator is seeded, the same call gives the same answer every time.

Size

const unequal = (rng) => [sample(normal(0, 1), 25, rng), sample(normal(0, 4), 5, rng)];
rejectionRate({ draw: unequal, test: ([a, b]) => tTest(a, b, { bits: 32 }), runs: 300, seed: 6 });
                // Welch: about 5%, as stated
rejectionRate({ draw: unequal, test: ([a, b]) => tTest(a, b, { equalVariance: true, bits: 32 }), runs: 300, seed: 6 });
                // pooled: well above 10%

The size of a test is how often it rejects when the null hypothesis is true. Here both samples have mean 0, so every rejection is a false alarm. The two samples have different spreads, and the small one has the large spread. Welch's test still rejects about 5% of the time. The pooled test assumes equal variances and rejects far more often. The test suite checks this: the lower end of the pooled test's interval stays above 10%.

What the suite checks

  • The t interval for a mean covers at 95% (normal data, n = 12).
  • The one-sample t test has size 5%; Welch's two-sample test keeps it with unequal variances; the pooled test does not.
  • The chi-square goodness-of-fit test of a fair die, with 60 rolls, has size 5%.
  • The regression slope interval covers the true slope of a seeded line.

Alongside these, exact identities tie the procedures to each other: the pooled t² equals the ANOVA F, the 2×2 chi-square equals the two-proportion z², the correlation t² equals the slope t², and Cook's distance matches leaving each point out. An identity catches a wrong formula; an experiment catches a right formula used where its assumptions fail.

Breaking the assumptions on purpose

The models page builds data from a known truth plus noise, contamination or censoring. Feeding those models to the tests shows how robust each one is: the t interval tolerates mild skew at moderate n, the chi-square interval for a variance does not tolerate heavy tails, and the pooled t test fails when spreads and sample sizes are mismatched. The experiments page has the floating-point versions, which are fast enough for thousands of runs.

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