Files
cup26/server/src/refit.test.ts
T
NilsBriggen d3e8df96ef Recency-weighted v4 model: fast-Elo blend, Team-DC majority, nightly refit
Validated on the strict 2018-2022 window and confirmed on the untouched
2022-2026 test set (RPS 0.1703 vs 0.1721 over 4,448 matches):
- the Elo member now blends 30% of a 3x-faster Elo walk, so recent
  results move ratings much harder
- ensemble weight shifts from 75/25 toward Elo to 45/55 toward the
  time-decayed Team-DC member — the recent-form model now leads
- Team-DC refits nightly (and at boot) on the 15-year window plus every
  finished 2026 match, via a new committed-at-build dcTrain.json
  (server-only, excluded from the PWA precache)
- a recent-form goal multiplier was also tested and did NOT validate;
  it ships disabled, and backtest.json records the whole decision

buildBacktest.ts grew the experiment harness: wider xi/k grids, the
fast-Elo and form variants, a pre-registered ship bar (>=0.0005
validation RPS), and a 7-day refit-cadence check. Older ratings.json
files still work — all new model fields are optional with golden
regression coverage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 23:34:18 +02:00

58 lines
2.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { refitTrainingSet, type DcRow } from './refit';
import type { Fixture } from '../../src/lib/types';
const slot = (team: string | null) => ({ team, placeholder: null, label: team ?? '?' });
const fx = (over: Partial<Fixture>): Fixture => ({
num: 1, stage: 'group', group: 'A', round: 'Matchday 1', groupRound: 1,
kickoff: '2026-06-14T18:00:00Z', venue: 'Boston (Foxborough)',
home: slot('Spain'), away: slot('Japan'),
status: 'finished', homeScore: 2, awayScore: 0, minute: null,
...over,
} as Fixture);
const base = {
asOf: '2026-06-10',
rows: [
['2026-06-01', 'Spain', 'France', 1, 1, 1],
['2010-01-01', 'Spain', 'France', 3, 0, 0], // outside a 15y window from 2026
] as DcRow[],
};
describe('refitTrainingSet', () => {
it('appends finished fixtures and advances asOf to the newest match', () => {
const { train, asOf } = refitTrainingSet(base, [fx({})], 70);
expect(asOf).toBe('2026-06-14');
const wc = train.find((m) => m.daysAgo === 0);
expect(wc).toMatchObject({ home: 'Spain', away: 'Japan', homeGoals: 2, awayGoals: 0, neutral: true });
});
it('recomputes daysAgo relative to the new asOf', () => {
const { train } = refitTrainingSet(base, [fx({})], 70);
const old = train.find((m) => m.home === 'Spain' && m.away === 'France');
expect(old?.daysAgo).toBe(13); // 2026-06-01 → 2026-06-14
});
it('drops rows that fall outside the window', () => {
const { train } = refitTrainingSet(base, [fx({})], 70);
expect(train.some((m) => m.homeGoals === 3)).toBe(false);
});
it('treats a host playing at home as non-neutral', () => {
const { train } = refitTrainingSet(base, [fx({ home: slot('USA'), venue: 'Los Angeles (Inglewood)' })], 70);
const wc = train.find((m) => m.daysAgo === 0);
expect(wc).toMatchObject({ home: 'USA', neutral: false });
});
it('flips orientation when the away side is the host at home', () => {
const { train } = refitTrainingSet(base, [fx({ away: slot('Mexico'), venue: 'Mexico City' })], 70);
const wc = train.find((m) => m.daysAgo === 0);
expect(wc).toMatchObject({ home: 'Mexico', away: 'Spain', homeGoals: 0, awayGoals: 2, neutral: false });
});
it('skips fixtures without teams or scores', () => {
const { train } = refitTrainingSet(base, [fx({ homeScore: null })], 70);
expect(train.every((m) => m.daysAgo > 0)).toBe(true);
});
});