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:
agent 2026-11-12 20:55:35 +00:00
parent 783c4d2f9a
commit 226e5f46f8
6 changed files with 672 additions and 0 deletions

97
test/elo.test.js Normal file
View file

@ -0,0 +1,97 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { expected, kFactor, replay, ranking, pairKey } from '../public/js/elo.js';
import { nextPair } from '../public/js/pairing.js';
const codes = ['A', 'B', 'C', 'D'];
let n = 0;
const ev = (fields) => ({ id: `ev${String(++n).padStart(6, '0')}`, t: '2026-11-10T10:00:00Z', ...fields });
test('expected score is symmetric and 0.5 for equal ratings', () => {
assert.equal(expected(1500, 1500), 0.5);
assert.ok(Math.abs(expected(1600, 1400) + expected(1400, 1600) - 1) < 1e-12);
assert.ok(expected(1900, 1500) > 0.9);
});
test('K decays linearly from kMax to kMin over decayGames', () => {
const o = { kMax: 48, kMin: 16, decayGames: 16 };
assert.equal(kFactor(0, o), 48);
assert.equal(kFactor(8, o), 32);
assert.equal(kFactor(16, o), 16);
assert.equal(kFactor(100, o), 16);
});
test('a win moves both ratings; equal newcomers swap K/2', () => {
const { stats } = replay([ev({ type: 'match', a: 'A', b: 'B', r: 'a' })], codes);
assert.equal(stats.get('A').rating, 1524);
assert.equal(stats.get('B').rating, 1476);
assert.equal(stats.get('A').wins, 1);
assert.equal(stats.get('B').losses, 1);
});
test('draw between equals changes nothing but counts the game', () => {
const { stats } = replay([ev({ type: 'match', a: 'A', b: 'B', r: 'draw' })], codes);
assert.equal(stats.get('A').rating, 1500);
assert.equal(stats.get('A').draws, 1);
assert.equal(stats.get('A').games, 1);
});
test('undo removes the targeted event as if it never happened', () => {
const m = ev({ type: 'match', a: 'A', b: 'B', r: 'a' });
const d = ev({ type: 'drop', code: 'C' });
const { stats, lastUndoable, pairCounts } = replay([m, d, ev({ type: 'undo', target: m.id })], codes);
assert.equal(stats.get('A').rating, 1500);
assert.equal(stats.get('A').games, 0);
assert.equal(stats.get('C').dropped, true);
assert.equal(lastUndoable.id, d.id, 'the drop is now the latest undoable action');
assert.equal(pairCounts.size, 0);
});
test('drop / restore / review', () => {
const { stats } = replay([
ev({ type: 'drop', code: 'A' }), ev({ type: 'restore', code: 'A' }), ev({ type: 'review', code: 'B' }),
], codes);
assert.equal(stats.get('A').dropped, false);
assert.equal(stats.get('A').reviewed, true, 'a drop counts as reviewed');
assert.equal(stats.get('B').reviewed, true);
});
test('events for unknown schools are ignored', () => {
const { stats, matches } = replay([ev({ type: 'match', a: 'A', b: 'ZZZ', r: 'a' })], codes);
assert.equal(matches.length, 0);
assert.equal(stats.get('A').games, 0);
});
test('ranking sorts by rating and hides dropped schools', () => {
const { stats } = replay([
ev({ type: 'match', a: 'C', b: 'D', r: 'a' }), ev({ type: 'drop', code: 'C' }),
], codes);
const order = ranking(stats).map((s) => s.code);
assert.deepEqual(order.slice(-1), ['D']);
assert.ok(!order.includes('C'));
assert.ok(ranking(stats, { includeDropped: true }).some((s) => s.code === 'C'));
});
test('consistent preferences converge to the true order', () => {
// Hidden truth: A > B > C > D. Pick pairs with the real pairing code and answer by the truth.
const truth = { A: 4, B: 3, C: 2, D: 1 };
let seed = 42;
const rng = () => { // mulberry32
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
const events = [];
for (let i = 0; i < 40; i++) {
const { stats, pairCounts } = replay(events, codes);
const [a, b] = nextPair(stats, { rng, pairCounts });
events.push(ev({ type: 'match', a, b, r: truth[a] > truth[b] ? 'a' : 'b' }));
}
const { stats } = replay(events, codes);
assert.deepEqual(ranking(stats).map((s) => s.code), ['A', 'B', 'C', 'D']);
});
test('pairKey is order-independent', () => {
assert.equal(pairKey('X', 'Y'), pairKey('Y', 'X'));
});