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
90 lines
3.7 KiB
JavaScript
90 lines
3.7 KiB
JavaScript
// Elo ratings computed by replaying the event log. Pure functions: shared by the browser and
|
|
// the Node test suite, no DOM access.
|
|
//
|
|
// K-factor decays with experience: a school's first comparisons move it a lot (it is still
|
|
// finding its level), later ones fine-tune. With kMax=48, kMin=16, decayGames=15 a school's
|
|
// K goes 48 -> 16 linearly over its first 15 games. Like FIDE's provisional ratings this makes
|
|
// a single match non-zero-sum when the two schools have different experience; that is intended.
|
|
|
|
export const DEFAULTS = Object.freeze({ initial: 1500, kMax: 48, kMin: 16, decayGames: 15 });
|
|
export const PROVISIONAL_GAMES = 5;
|
|
const UNDOABLE = new Set(['match', 'drop', 'restore', 'review']);
|
|
|
|
/** Probability that a player rated `ra` beats one rated `rb`. */
|
|
export function expected(ra, rb) {
|
|
return 1 / (1 + 10 ** ((rb - ra) / 400));
|
|
}
|
|
|
|
export function kFactor(games, opts = DEFAULTS) {
|
|
const { kMax, kMin, decayGames } = { ...DEFAULTS, ...opts };
|
|
if (games >= decayGames) return kMin;
|
|
return kMax - ((kMax - kMin) * games) / decayGames;
|
|
}
|
|
|
|
/** Order-independent key for a pair of schools. */
|
|
export function pairKey(a, b) {
|
|
return a < b ? `${a}|${b}` : `${b}|${a}`;
|
|
}
|
|
|
|
function blankStats(code, initial) {
|
|
return { code, rating: initial, games: 0, wins: 0, losses: 0, draws: 0, dropped: false, reviewed: false, history: [] };
|
|
}
|
|
|
|
/**
|
|
* Replay events (already in authoritative order) into per-school stats.
|
|
* Events whose id is the target of an `undo` are skipped entirely, as if they never happened.
|
|
* Events naming unknown schools are ignored (the dataset may change between versions).
|
|
*/
|
|
export function replay(events, codes, opts = {}) {
|
|
const o = { ...DEFAULTS, ...opts };
|
|
const undone = new Set(events.filter((e) => e.type === 'undo').map((e) => e.target));
|
|
const stats = new Map(codes.map((c) => [c, blankStats(c, o.initial)]));
|
|
const matches = [];
|
|
const pairCounts = new Map();
|
|
|
|
for (const ev of events) {
|
|
if (ev.type === 'undo' || undone.has(ev.id)) continue;
|
|
if (ev.type === 'match') {
|
|
const A = stats.get(ev.a);
|
|
const B = stats.get(ev.b);
|
|
if (!A || !B || A === B) continue;
|
|
const ea = expected(A.rating, B.rating);
|
|
const sa = ev.r === 'a' ? 1 : ev.r === 'b' ? 0 : 0.5;
|
|
const da = kFactor(A.games, o) * (sa - ea);
|
|
const db = kFactor(B.games, o) * (1 - sa - (1 - ea));
|
|
A.rating += da;
|
|
B.rating += db;
|
|
A.games += 1;
|
|
B.games += 1;
|
|
if (sa === 1) { A.wins += 1; B.losses += 1; }
|
|
else if (sa === 0) { A.losses += 1; B.wins += 1; }
|
|
else { A.draws += 1; B.draws += 1; }
|
|
const m = { id: ev.id, t: ev.t, a: ev.a, b: ev.b, r: ev.r, da, db, ratingA: A.rating, ratingB: B.rating };
|
|
matches.push(m);
|
|
A.history.push(m);
|
|
B.history.push(m);
|
|
const key = pairKey(ev.a, ev.b);
|
|
pairCounts.set(key, (pairCounts.get(key) ?? 0) + 1);
|
|
} else {
|
|
const s = stats.get(ev.code);
|
|
if (!s) continue;
|
|
if (ev.type === 'drop') { s.dropped = true; s.reviewed = true; }
|
|
else if (ev.type === 'restore') s.dropped = false;
|
|
else if (ev.type === 'review') s.reviewed = true;
|
|
}
|
|
}
|
|
|
|
let lastUndoable = null;
|
|
for (let i = events.length - 1; i >= 0; i--) {
|
|
const ev = events[i];
|
|
if (UNDOABLE.has(ev.type) && !undone.has(ev.id)) { lastUndoable = ev; break; }
|
|
}
|
|
return { stats, matches, pairCounts, undone, lastUndoable };
|
|
}
|
|
|
|
/** Schools ordered best-first. Ties broken by more games, then code for stability. */
|
|
export function ranking(stats, { includeDropped = false } = {}) {
|
|
return [...stats.values()]
|
|
.filter((s) => includeDropped || !s.dropped)
|
|
.sort((x, y) => y.rating - x.rating || y.games - x.games || x.code.localeCompare(y.code));
|
|
}
|