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>
This commit is contained in:
@@ -35,6 +35,7 @@ export function buildServer() {
|
||||
db(); // open + migrate SQLite before anything reads it
|
||||
const state = new TournamentState(STATIC_DIR);
|
||||
const model = new ModelEngine(STATIC_DIR);
|
||||
model.refitTeamDc(state.allFixtures()); // current Team-DC from day one (covers restarts mid-tournament)
|
||||
model.recompute(state.allFixtures());
|
||||
const clients = new Set<WebSocket>();
|
||||
|
||||
@@ -151,6 +152,12 @@ export function buildServer() {
|
||||
});
|
||||
app.addHook('onClose', async () => stopIngest());
|
||||
|
||||
// ---- nightly Team-DC refit (backtest-validated): learn from this tournament ----
|
||||
const refitTimer = setInterval(() => {
|
||||
if (model.refitTeamDc(state.allFixtures())) broadcast();
|
||||
}, 24 * 60 * 60 * 1000);
|
||||
app.addHook('onClose', async () => clearInterval(refitTimer));
|
||||
|
||||
// ---- static SPA (prod) with history fallback ----
|
||||
if (existsSync(STATIC_DIR)) {
|
||||
void app.register(fastifyStatic, { root: STATIC_DIR, wildcard: false });
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
// Pure helpers for the nightly in-tournament Team-DC refit (no fs/db imports,
|
||||
// so vitest can load them). The training base comes from dcTrain.json — compact
|
||||
// [date, home, away, hs, as, neutral01] rows with absolute dates — plus every
|
||||
// finished World Cup fixture so far.
|
||||
import { hostAdvantage } from '../../src/lib/model/hosts';
|
||||
import type { DcMatch } from '../../src/lib/model/teamDc';
|
||||
import type { Fixture } from '../../src/lib/types';
|
||||
|
||||
export type DcRow = [string, string, string, number, number, number];
|
||||
|
||||
const DAY = 24 * 60 * 60 * 1000;
|
||||
|
||||
/** Build the refit training set, with daysAgo relative to the newest match in
|
||||
* the combined data. Host-nation fixtures are oriented so the host is the
|
||||
* DC home side (the CSV convention for non-neutral rows). */
|
||||
export function refitTrainingSet(
|
||||
base: { asOf: string; rows: DcRow[] },
|
||||
finished: Fixture[],
|
||||
homeAdvElo: number,
|
||||
windowYears = 15,
|
||||
): { train: DcMatch[]; asOf: string } {
|
||||
let asOf = base.asOf;
|
||||
const extra: { date: string; home: string; away: string; hs: number; as: number; neutral: boolean }[] = [];
|
||||
for (const f of finished) {
|
||||
if (!f.home.team || !f.away.team || f.homeScore == null || f.awayScore == null) continue;
|
||||
const date = f.kickoff.slice(0, 10);
|
||||
if (date > asOf) asOf = date;
|
||||
const adv = hostAdvantage(f.home.team, f.away.team, f.venue, homeAdvElo);
|
||||
if (adv < 0) {
|
||||
extra.push({ date, home: f.away.team, away: f.home.team, hs: f.awayScore, as: f.homeScore, neutral: false });
|
||||
} else {
|
||||
extra.push({ date, home: f.home.team, away: f.away.team, hs: f.homeScore, as: f.awayScore, neutral: adv === 0 });
|
||||
}
|
||||
}
|
||||
|
||||
const nowT = new Date(asOf).getTime();
|
||||
const windowMs = windowYears * 365 * DAY;
|
||||
const train: DcMatch[] = [];
|
||||
for (const [date, home, away, hs, as, neutral] of base.rows) {
|
||||
const t = new Date(date).getTime();
|
||||
if (nowT - t > windowMs) continue;
|
||||
train.push({ daysAgo: (nowT - t) / DAY, home, away, homeGoals: hs, awayGoals: as, neutral: neutral === 1 });
|
||||
}
|
||||
for (const m of extra) {
|
||||
train.push({
|
||||
daysAgo: (nowT - new Date(m.date).getTime()) / DAY,
|
||||
home: m.home, away: m.away, homeGoals: m.hs, awayGoals: m.as, neutral: m.neutral,
|
||||
});
|
||||
}
|
||||
return { train, asOf };
|
||||
}
|
||||
Reference in New Issue
Block a user