Add Elo, pair selection and comparison metrics with tests
Ratings are replayed from an event log (undo = skip the target event). K decays 48 -> 16 over a school's first 15 games. Smart pairing favours rarely compared schools, then close ratings, with 10% exploration and random sides. metrics.js derives grant, out-of-pocket cost, language and subject fit and the duel rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
This commit is contained in:
parent
783c4d2f9a
commit
226e5f46f8
6 changed files with 672 additions and 0 deletions
79
test/pairing.test.js
Normal file
79
test/pairing.test.js
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { nextPair } from '../public/js/pairing.js';
|
||||
import { pairKey } from '../public/js/elo.js';
|
||||
|
||||
// mulberry32: small seeded PRNG. (A plain LCG's consecutive outputs are correlated enough to
|
||||
// bias the side coin-flip in these tests.)
|
||||
function seeded(a = 7) {
|
||||
return () => {
|
||||
a = (a + 0x6d2b79f5) | 0;
|
||||
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
const mk = (code, rating = 1500, games = 0, dropped = false) => [code, { code, rating, games, dropped }];
|
||||
|
||||
test('never pairs a school with itself and never uses dropped schools', () => {
|
||||
const stats = new Map([mk('A'), mk('B'), mk('C', 1500, 0, true)]);
|
||||
const rng = seeded();
|
||||
for (let i = 0; i < 200; i++) {
|
||||
const [a, b] = nextPair(stats, { rng });
|
||||
assert.notEqual(a, b);
|
||||
assert.ok(a !== 'C' && b !== 'C');
|
||||
}
|
||||
});
|
||||
|
||||
test('returns null with fewer than two active schools', () => {
|
||||
assert.equal(nextPair(new Map([mk('A'), mk('B', 1500, 0, true)])), null);
|
||||
});
|
||||
|
||||
test('smart mode favours rarely compared schools', () => {
|
||||
const stats = new Map([mk('NEW', 1500, 0), ...['A', 'B', 'C', 'D', 'E', 'F'].map((c) => mk(c, 1500, 30))]);
|
||||
const rng = seeded(3);
|
||||
let withNew = 0;
|
||||
for (let i = 0; i < 500; i++) if (nextPair(stats, { rng }).includes('NEW')) withNew++;
|
||||
// Uniform would give ~2/7 = 29%; the weighting should push it far above that.
|
||||
assert.ok(withNew / 500 > 0.6, `NEW appeared in ${withNew}/500 pairs`);
|
||||
});
|
||||
|
||||
test('smart mode prefers close ratings once schools are established', () => {
|
||||
const stats = new Map([mk('X', 1500, 20), mk('NEAR', 1520, 20), mk('FAR', 1900, 20)]);
|
||||
const rng = seeded(11);
|
||||
let near = 0;
|
||||
let far = 0;
|
||||
for (let i = 0; i < 600; i++) {
|
||||
const p = nextPair(stats, { rng });
|
||||
if (p.includes('X') && p.includes('NEAR')) near++;
|
||||
if (p.includes('X') && p.includes('FAR')) far++;
|
||||
}
|
||||
assert.ok(near > far * 3, `near=${near} far=${far}`);
|
||||
});
|
||||
|
||||
test('recently shown pairs are avoided', () => {
|
||||
const stats = new Map([mk('A'), mk('B'), mk('C'), mk('D')]);
|
||||
const rng = seeded(5);
|
||||
const recent = [pairKey('A', 'B'), pairKey('C', 'D')];
|
||||
let repeats = 0;
|
||||
for (let i = 0; i < 300; i++) {
|
||||
const [a, b] = nextPair(stats, { rng, recent });
|
||||
if (recent.includes(pairKey(a, b))) repeats++;
|
||||
}
|
||||
assert.ok(repeats < 15, `recent pairs reappeared ${repeats}/300 times`);
|
||||
});
|
||||
|
||||
test('sides are randomised', () => {
|
||||
const stats = new Map([mk('A', 1500, 0), mk('B', 1500, 50)]);
|
||||
const rng = seeded(9);
|
||||
let aLeft = 0;
|
||||
for (let i = 0; i < 400; i++) if (nextPair(stats, { rng })[0] === 'A') aLeft++;
|
||||
assert.ok(aLeft > 120 && aLeft < 280, `A on the left ${aLeft}/400`);
|
||||
});
|
||||
|
||||
test('random mode works', () => {
|
||||
const stats = new Map([mk('A'), mk('B'), mk('C')]);
|
||||
const p = nextPair(stats, { mode: 'random', rng: seeded(1) });
|
||||
assert.equal(p.length, 2);
|
||||
assert.notEqual(p[0], p[1]);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue