Experiments
Draw many data sets from a model that is known, run a procedure on each, and count how often it is right. An experiment's answer is itself a statistic, so it comes with its own interval: you can tell a real shortfall in coverage from the luck of the draw.
How often
import { howOften, die, dice } from "@ratmath/stats";
// five dice all different: by simulation, then exactly
const sim = howOften({ draw: (rng) => die().samples(5, rng), event: (roll) => new Set(roll).size === 5, runs: 5000, seed: 9 });
String(sim); // "0.0930 (simulated: 465 of 5000 runs, seed 9; 95% interval 0.0851 to 0.1014)"
sim.rate; // 93/1000: hits over runs, exactly
sim.interval; // { low, high, level }: Clopper and Pearson's interval for the true rate
const exact = dice(5).prob((roll) => new Set(roll).size === 5); // 5/54 = 0.0926
sim.compare(exact); // "inside"
Data come from { model, n } (a model and a sample size) or from draw(rng), any function of the generator. The number of hits in independent runs is binomial where is the true rate, so the exact interval for a proportion applies to the experiment itself. compare(value) says whether a stated value is "below", "inside" or "above" that interval.
Coverage of an interval
import { coverage, binomial, proportionInterval } from "@ratmath/stats";
const wald = coverage({
draw: (rng) => binomial(30, "1/10").sample(rng), // a count out of 30, true p = 1/10
interval: (k) => { const ci = proportionInterval(k, 30, { method: "wald" }); return [ci.low, ci.high]; },
truth: "1/10", runs: 2000, seed: 1,
});
String(wald); // "0.8200 (simulated: 1640 of 2000 runs, seed 1; 95% interval 0.8024 to 0.8366)"
wald.compare("0.95"); // "above": 95% is above anything the experiment supports
wald.meanWidth; // 0.2015…
A 95% confidence interval is a procedure that contains the truth in 95% of samples. coverage measures that directly. Here the textbook interval for a proportion, at and , covers about 82% of the time, and the experiment's own interval shows that the shortfall is not luck. With method: "wilson" the same experiment gives 0.9815 and with "exact" 0.9920.
Size and power of a test
import { rejectionRate, powerCurve, binomialTest } from "@ratmath/stats";
const toss = (p) => (rng) => binomial(20, p).sample(rng);
const test = (k) => binomialTest(k, 20, "1/2");
String(rejectionRate({ draw: toss("1/2"), test, alpha: "0.05", runs: 2000, seed: 1 }));
// "0.0415 (simulated: 83 of 2000 runs, …)": the null is true, so this is the test's size
String(rejectionRate({ draw: toss("7/10"), test, runs: 2000, seed: 1 }));
// "0.4110 (simulated: 822 of 2000 runs, …)": the null is false, so this is its power
powerCurve({ effects: ["1/2", "6/10", "7/10", "8/10"], draw: toss, test, runs: 1000, seed: 1 });
// rates 0.033, 0.131, 0.417, 0.804
test returns a p-value, a test result, or true for "reject". When the model satisfies the null hypothesis the rejection rate estimates the test's size, which should not exceed ; otherwise it estimates the power. powerCurve runs the same experiment at each of several effects with the same seed, so the curve is not roughened by different random numbers at each point.
An estimator's bias and error
import { estimatorStudy, model, normal, fast } from "@ratmath/stats";
const m = model({ truth: 10, noise: normal(0, 1), contamination: { rate: 0.1, noise: normal(0, 10) } });
const s = estimatorStudy({ model: m, n: 25, estimator: (data) => fast.median(data.y), truth: 10, runs: 4000, seed: 2 });
String(s); // "mean 10.003, sd 0.27412, bias 0.0031262 ± 0.008495, rmse 0.2741 (simulated: 4000 runs, seed 2)"
The result has the estimates themselves, their mean and standard deviation, and with a truth the bias and the root mean squared error . The bias is reported as , where is the standard error of the mean of the estimates: an approximate 95% interval, in floating point, and labelled simulated.
What an experiment can and cannot say
An experiment is evidence about one model, one sample size and one procedure. Its interval covers the sampling error of the simulation and nothing else. When an exact answer is available, as for the dice above or the power of an exact test (a finite sum), compute it; use simulation for what cannot be counted, and say which you did. The library's labels are there so that this is never ambiguous.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.