octo-deno/tests/random/mersennetwister.js
Jesse Schwartzentruber 8b20380b28 Add more random tests.
Use chi-squared goodness of fit tests for random distributions.
2017-04-12 17:03:57 -04:00

54 lines
2.1 KiB
JavaScript

QUnit.test("MersenneTwister test uniform distribution", function(assert) {
const N = Math.pow(2, 17), TRIES = 10, XSQ = 293.25; // quantile of chi-square dist. k=255, p=.05
let mt = new MersenneTwister();
mt.seed(Math.random() * 0x100000000);
for (let attempt = 0; attempt < TRIES; ++attempt) {
let data = new Uint32Array(N), sh;
for (let i = 0; i < data.length; ++i) {
data[i] = mt.int32();
}
for (sh = 0; sh <= 24; ++sh) {
let bins = new Uint32Array(256);
for (let b of data) {
++bins[(b >>> sh) & 0xFF];
}
let xsq = bins.reduce(function(a, v){ let e = N / bins.length; return a + Math.pow(v - e, 2) / e; }, 0);
/*
* XSQ = scipy.stats.chi2.isf(.05, 511)
* if xsq > XSQ, the result is biased at 95% significance
*/
if (xsq >= XSQ)
break;
assert.ok(true, "Expected x^2 to be < " + XSQ + ", got " + xsq + " on attempt #" + (attempt + 1));
}
if (sh == 25) {
return;
}
}
assert.ok(false, "Failed in " + TRIES + " attempts to get xsq lower than " + XSQ);
});
QUnit.test("MersenneTwister test float distribution", function(assert) {
const N = Math.pow(2, 17), TRIES = 3, XSQ = 564.7; // quantile of chi-square dist. k=511, p=.05
let tries = [], mt = new MersenneTwister();
mt.seed(Math.random() * 0x100000000);
for (let attempt = 0; attempt < TRIES; ++attempt) {
let bins = new Uint32Array(512);
for (let i = 0; i < N; ++i) {
let tmp = (mt.real2() * bins.length) >>> 0;
if (tmp >= bins.length) throw "random.float() >= 1.0";
++bins[tmp];
}
let xsq = bins.reduce(function(a, v){ let e = N / bins.length; return a + Math.pow(v - e, 2) / e; }, 0);
/*
* XSQ = scipy.stats.chi2.isf(.05, 511)
* if xsq > XSQ, the result is biased at 95% significance
*/
if (xsq < XSQ) {
assert.ok(true, "Expected x^2 to be < " + XSQ + ", got " + xsq + " on attempt #" + (attempt + 1));
return;
}
tries.push(xsq);
}
assert.ok(false, "Failed in " + TRIES + " attempts to get xsq lower than " + XSQ + ": "+ tries);
});