Repository Public
1import { describe, expect, it } from "vitest";2import {3 createRng, deriveSeed, entropy, incompleteBeta, linspace, logGamma, mean, mulberry32, normalCdf, normalInv, normalPdf, ols, pearson,4 quantile, sd, softmax, studentTCdf, studentTPdf, studentTQuantile, topK, variance,5} from "@/sim/core";67describe("seeded PRNG", () => {8 it("is deterministic: same seed, same sequence, bit for bit", () => {9 const a = mulberry32(42), b = mulberry32(42);10 for (let i = 0; i < 1000; i++) expect(a()).toBe(b());11 const r1 = createRng(7, "x"), r2 = createRng(7, "x");12 for (let i = 0; i < 500; i++) { expect(r1.normal()).toBe(r2.normal()); expect(r1.studentT(4)).toBe(r2.studentT(4)); }13 });14 it("pins the first values of mulberry32 (a change here changes every published picture)", () => {15 const r = mulberry32(1);16 expect([r(), r(), r()]).toEqual([0.6270739405881613, 0.002735721180215478, 0.5274470399599522]);17 });18 it("gives different streams for different seeds and salts", () => {19 expect(mulberry32(1)()).not.toBe(mulberry32(2)());20 expect(deriveSeed(1, "a")).not.toBe(deriveSeed(1, "b"));21 expect(deriveSeed(1, "a")).not.toBe(deriveSeed(2, "a"));22 expect(createRng(1, "a").next()).not.toBe(createRng(1, "b").next());23 });24 it("draws uniforms in [0, 1) with the right mean", () => {25 const r = createRng(3);26 const xs = Array.from({ length: 20000 }, () => r.next());27 expect(Math.min(...xs)).toBeGreaterThanOrEqual(0);28 expect(Math.max(...xs)).toBeLessThan(1);29 expect(mean(xs)).toBeCloseTo(0.5, 2);30 for (let i = 0; i < 100; i++) { const k = r.int(6); expect(Number.isInteger(k) && k >= 0 && k < 6).toBe(true); }31 });32 it("draws normals with mean 0 and variance 1 (Box–Muller)", () => {33 const r = createRng(11);34 const xs = Array.from({ length: 40000 }, () => r.normal());35 expect(mean(xs)).toBeCloseTo(0, 1);36 expect(variance(xs)).toBeCloseTo(1, 1);37 expect(xs.filter((x) => Math.abs(x) < 1.96).length / xs.length).toBeCloseTo(0.95, 2);38 expect(xs.every(Number.isFinite)).toBe(true);39 const y = createRng(11); const ys = Array.from({ length: 20000 }, () => y.normal(3, 2));40 expect(mean(ys)).toBeCloseTo(3, 1); expect(sd(ys)).toBeCloseTo(2, 1);41 });42 it("draws Student-t variates with variance ν/(ν−2)", () => {43 const r = createRng(5);44 const xs = Array.from({ length: 40000 }, () => r.studentT(8));45 expect(mean(xs)).toBeCloseTo(0, 1);46 expect(variance(xs)).toBeGreaterThan(1.2); expect(variance(xs)).toBeLessThan(1.47); // 8/6 = 1.33347 });48});4950describe("statistics helpers", () => {51 it("mean, variance, quantile", () => {52 const xs = [2, 4, 4, 4, 5, 5, 7, 9];53 expect(mean(xs)).toBe(5);54 expect(variance(xs)).toBeCloseTo(32 / 7, 12);55 expect(quantile([1, 2, 3, 4], 0.5)).toBe(2.5);56 expect(quantile([1, 2, 3, 4], 0)).toBe(1);57 expect(quantile([4, 1, 3, 2], 1)).toBe(4);58 expect(quantile([10, 20, 30, 40, 50], 0.1)).toBeCloseTo(14, 12);59 expect(linspace(0, 1, 5)).toEqual([0, 0.25, 0.5, 0.75, 1]);60 });61 it("OLS recovers known coefficients on noiseless data", () => {62 const x = [0, 1, 2, 3, 4, 5], y = x.map((v) => 3 - 2 * v);63 const f = ols(x, y);64 expect(f.slope).toBeCloseTo(-2, 12); expect(f.intercept).toBeCloseTo(3, 12); expect(f.r2).toBeCloseTo(1, 12); expect(f.s).toBeCloseTo(0, 10);65 });66 it("OLS matches a hand computation", () => {67 // x = 1,2,3,4 ; y = 2,3,5,4 : Sxx = 5, Sxy = 4 → slope 0.8, intercept 1.5 ; SSR = 1.8, SST = 5 → R² = 0.6468 const f = ols([1, 2, 3, 4], [2, 3, 5, 4]);69 expect(f.slope).toBeCloseTo(0.8, 12); expect(f.intercept).toBeCloseTo(1.5, 12); expect(f.r2).toBeCloseTo(0.64, 12);70 expect(f.s).toBeCloseTo(Math.sqrt(0.9), 12); expect(f.seSlope).toBeCloseTo(Math.sqrt(0.9 / 5), 12);71 expect(pearson([1, 2, 3, 4], [2, 3, 5, 4])).toBeCloseTo(0.8, 12);72 });73 it("Pearson r is ±1 on a line", () => {74 expect(pearson([1, 2, 3], [2, 4, 6])).toBeCloseTo(1, 12);75 expect(pearson([1, 2, 3], [3, 2, 1])).toBeCloseTo(-1, 12);76 });77 it("softmax sums to 1, matches known values and respects temperature", () => {78 const p = softmax([1, 2, 3]);79 expect(p.reduce((a, b) => a + b, 0)).toBeCloseTo(1, 14);80 expect(p[0]).toBeCloseTo(0.09003057317038046, 12); expect(p[2]).toBeCloseTo(0.6652409557748219, 12);81 expect(softmax([1000, 1001]).every(Number.isFinite)).toBe(true);82 expect(softmax([1, 2, 3], 100)[2]).toBeLessThan(0.34);83 expect(softmax([1, 2, 3], 0.01)[2]).toBeGreaterThan(0.999);84 expect(softmax([0, -Infinity, 0])).toEqual([0.5, 0, 0.5]);85 expect(entropy([0.5, 0.5])).toBeCloseTo(Math.LN2, 14); expect(entropy([1, 0])).toBe(0);86 });87 it("top-k returns the k largest, ties to the lower index", () => {88 expect(topK([0.1, 0.5, 0.2, 0.5], 2)).toEqual([1, 3]);89 expect(topK([3, 1, 2], 1)).toEqual([0]);90 expect(topK([3, 1, 2], 5)).toEqual([0, 2, 1]);91 });92});9394describe("distributions", () => {95 it("normal pdf, cdf and inverse cdf", () => {96 expect(normalPdf(0)).toBeCloseTo(0.3989422804014327, 14);97 expect(normalCdf(0)).toBeCloseTo(0.5, 15);98 expect(normalCdf(1)).toBeCloseTo(0.8413447460685429, 13);99 expect(normalCdf(1.96)).toBeCloseTo(0.9750021048517795, 13);100 expect(normalCdf(-3)).toBeCloseTo(0.0013498980316301, 14);101 expect(normalCdf(12, 10, 2)).toBeCloseTo(0.8413447460685429, 13);102 expect(normalInv(0.975)).toBeCloseTo(1.959963984540054, 9);103 expect(normalInv(0.5)).toBeCloseTo(0, 12);104 expect(normalInv(0.001)).toBeCloseTo(-3.090232306167813, 8);105 for (const p of [0.0001, 0.01, 0.3, 0.77, 0.9999]) expect(normalCdf(normalInv(p))).toBeCloseTo(p, 11);106 });107 it("log-gamma and incomplete beta", () => {108 expect(logGamma(5)).toBeCloseTo(Math.log(24), 12);109 expect(logGamma(0.5)).toBeCloseTo(Math.log(Math.sqrt(Math.PI)), 12);110 expect(incompleteBeta(0.5, 2, 2)).toBeCloseTo(0.5, 12);111 expect(incompleteBeta(0.3, 1, 1)).toBeCloseTo(0.3, 12);112 expect(incompleteBeta(0.25, 2, 1)).toBeCloseTo(0.0625, 12);113 });114 it("Student-t pdf, cdf and quantile", () => {115 expect(studentTPdf(0, 1)).toBeCloseTo(1 / Math.PI, 12); // Cauchy116 expect(studentTCdf(1, 1)).toBeCloseTo(0.75, 12);117 expect(studentTCdf(0, 7)).toBeCloseTo(0.5, 12);118 expect(studentTCdf(2, 5)).toBeCloseTo(0.9490302605850685, 10);119 expect(studentTCdf(-2, 5)).toBeCloseTo(1 - 0.9490302605850685, 10);120 expect(studentTQuantile(0.975, 10)).toBeCloseTo(2.2281388519649385, 8);121 expect(studentTQuantile(0.975, 1)).toBeCloseTo(12.706204736, 6);122 expect(studentTQuantile(0.025, 10)).toBeCloseTo(-2.2281388519649385, 8);123 expect(studentTQuantile(0.975, 1e6)).toBeCloseTo(1.96, 3);124 });125});126