* refactor(schema): rename per-window tables to season_* prefix Renames participants, participant_expected_values, participant_qualifying_totals, participant_results, participant_surface_elos to season_* prefixed names. Renames event_results.participant_id to season_participant_id. Phase 1a of canonical tournament layer migration. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor: rename participant.ts model file to season-participant.ts Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(models): update model layer to use renamed schema exports Updated all model files to use the renamed schema exports from Task 1: - participants → seasonParticipants - participantExpectedValues → seasonParticipantExpectedValues - participantQualifyingTotals → seasonParticipantQualifyingTotals - participantResults → seasonParticipantResults - participantSurfaceElos → seasonParticipantSurfaceElos - eventResults.participantId → eventResults.seasonParticipantId - db.query relation accessors updated - Relation field .participant → .seasonParticipant where applicable - Import paths updated: ./participant → ./season-participant Files updated (14 model files + 3 test files): - draft-pick.ts - draft-utils.ts - event-result.ts - group-stage-match.ts - participant-result.ts - qualifying-points.ts - scoring-calculator.ts - scoring-event.ts - sports-season.ts - surface-elo.ts - team-score-events.ts - cs2-major-stage.ts - golf-skills.ts - participant-expected-value.ts - __tests__/sports-season.clone.test.ts - __tests__/auto-pick.test.ts - __tests__/executeAutoPick.timer.test.ts Typecheck errors decreased: 779 → 499 (280 fewer) All model file errors related to renamed schemas resolved. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(routes): update route layer to use renamed schema exports - Update model import from ~/models/participant to ~/models/season-participant - Rename schema.participants to schema.seasonParticipants - Rename schema.participantResults to schema.seasonParticipantResults - Rename db.query.participants to db.query.seasonParticipants - Update 9 route files and 1 test file Affected files: - admin.sports-seasons.$id.events.$eventId.bracket.server.ts - admin.sports-seasons.$id.participants.tsx - api/draft.force-manual-pick.ts - api/draft.make-pick.ts - api/draft.replace-pick.ts - api/seasons.$seasonId.draft.ts - leagues/$leagueId.draft-board.$seasonId.tsx - leagues/$leagueId.sports-seasons.$sportsSeasonId.server.ts - admin/__tests__/sports-seasons-participants.test.ts Error count reduced from 499 to 453 (46 errors fixed). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(routes): update route files for schema rename Update route imports from ~/models/participant to ~/models/season-participant and fix references to .participant/.participantId on event results to use .seasonParticipant/.seasonParticipantId after schema rename. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(services): update simulators and services for renamed schema Update all simulators, services, and server files to use renamed schema tables: - participants → seasonParticipants - participantExpectedValues → seasonParticipantExpectedValues - participantResults → seasonParticipantResults - eventResults.participantId → eventResults.seasonParticipantId Files updated: - 20 sport simulators (NBA, NHL, NFL, MLB, etc.) - probability-updater.ts - standings-sync/index.ts - sports-data-sync.server.ts - server/socket.ts Typecheck errors reduced from 365 to 0. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * migration: rename per-window tables to season_* prefix * fix(tests): update mock query keys after participants table rename Change mock db.query.participants to db.query.seasonParticipants in test files to match the schema rename from commit66145a9. This fixes "Cannot read properties of undefined (reading 'findFirst'/'findMany')" errors that occurred when production code queries db.query.seasonParticipants but test mocks only defined the old participants key. Files updated: - app/services/simulations/__tests__/world-cup-simulator.test.ts - app/routes/api/__tests__/draft.force-manual-pick.test.ts - app/routes/api/__tests__/draft.force-manual-pick.timer-mode.test.ts - app/routes/api/__tests__/draft.make-pick.timer-mode.test.ts - server/__tests__/timer-autodraft.test.ts - app/models/__tests__/team-score-events.test.ts Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(tests): update remaining mock paths and keys after schema rename * fix(tests): final two mock stragglers after schema rename - draft-pick.test.ts: assertion on db.query.participantQualifyingTotals - process-match-result.test.ts: mock key participants → seasonParticipants Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore: add post-phase1a baseline capture (temp, for diff verification) * chore: capture pre-migration baselines * chore: remove post-phase1a capture helper after verification * schema: add canonical tournament & participant tables Adds tournaments, participants (canonical), tournament_results, and participant_surface_elos (canonical). Adds nullable tournament_id to scoring_events and nullable participant_id to season_participants. Phase 1b of canonical tournament layer migration. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(models): add canonical tournament, participant, result, surface-elo models Adds CRUD modules for the canonical tables created in commit775b905. Each module mirrors existing app/models conventions (database() from ~/database/context, schema from ~/database/schema, mock-based tests). Key implementation notes: - participant.ts exports use "Canonical" prefix (CanonicalParticipant, createCanonicalParticipant, etc.) to avoid collision with existing season-participant.ts exports - All four models include comprehensive unit tests following the audit-log.test.ts pattern - Tests use mocked db responses (no real database access) - Upsert functions use onConflictDoUpdate for appropriate unique constraints Part of Phase 1b of canonical tournament layer migration. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * migration: create canonical tables, add nullable FKs * scripts: add extractTournamentIdentity helper for backfill Pure function that derives canonical (name, year) identity from a scoring_events row, stripping trailing 4-digit years from the name or falling back to eventDate. Used by the Phase 2 backfill to group per-window events into canonical tournaments. * scripts: add backfill orchestrator for canonical layer Populates canonical tournaments, participants, tournament_results, and participant_surface_elos from per-window data for qualifying-points sports. Skips already-linked rows, is idempotent, and supports dry-run mode. Critical invariants enforced by the implementation: - qualifying_points_awarded is never copied to tournament_results - season_participant_qualifying_totals is never touched - conflicting surface-Elo values between windows raise a loud error (recorded in report.errors) rather than overwriting * scripts: add backfill CLI with dry-run default Wires backfill-canonical-layer.ts to a CLI entry point exposed as `npm run backfill:canonical`. Defaults to --dry-run; requires --apply to actually write. Supports --sport=<uuid> to limit to a single sport. Exits 2 if the backfill reports errors (e.g., surface-Elo conflicts). * fix(backfill-cli): wrap runBackfill in DatabaseContext.run The orchestrator uses database() from ~/database/context, which requires AsyncLocalStorage to be populated. Wrap the CLI invocation with DatabaseContext.run(db, ...) using server/db's cached connection pool. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(backfill-cli): exit 0 on success so pg pool doesn't block The cached postgres connection pool keeps the Node event loop open after main() returns. Explicit process.exit(0) on success mirrors the pattern in scripts/capture-baseline.ts. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Chris Parsons <chrisp@extrahop.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
/**
|
|
* Auto Racing Season Standings Simulator
|
|
*
|
|
* Generic simulator for points-based auto racing championships (F1, IndyCar, etc.).
|
|
* The race points table is injected at construction time so different series can
|
|
* use their own scoring systems.
|
|
*
|
|
* Algorithm:
|
|
* 1. Load participants + current championship points from DB
|
|
* 2. Count remaining races (incomplete non-schedule scoring events)
|
|
* 3. Convert sourceOdds → vig-removed probability weights
|
|
* 4. Two simulation paths:
|
|
* a. remainingRaces === 0 (pre-season): pure weighted draws from odds
|
|
* b. remainingRaces > 0 (in-season): simulate each remaining race,
|
|
* starting from real standings, awarding series-specific points per finish
|
|
* 5. Convert finish counts → probability distributions + normalize columns
|
|
*
|
|
* Notes:
|
|
* - Drivers without odds fall back to uniform probability (1/N)
|
|
* - PARTICIPANT_VOLATILITY and RACE_NOISE only apply to the in-season path
|
|
*/
|
|
|
|
import { database } from "~/database/context";
|
|
import { eq } from "drizzle-orm";
|
|
import * as schema from "~/database/schema";
|
|
import { getAllParticipantEVsForSeason } from "~/models/participant-expected-value";
|
|
import { getSeasonResults } from "~/models/participant-season-result";
|
|
import type { Simulator, SimulationResult } from "./types";
|
|
|
|
// ─── Simulation parameters (mirrors Python constants) ────────────────────────
|
|
|
|
const NUM_SIMULATIONS = 10000;
|
|
|
|
/** Per-race performance variance. 0 = no noise, 1 = fully random each race. */
|
|
const RACE_NOISE = 0.50;
|
|
|
|
/**
|
|
* Season-long multiplier range per driver.
|
|
* Each driver gets uniform(1 - V, 1 + V) applied to their base probability
|
|
* for the entire season, capturing "cars that over/underperform expectations".
|
|
*/
|
|
const PARTICIPANT_VOLATILITY = 1.5;
|
|
|
|
/**
|
|
* Optional smoothing toward the mean after vig removal.
|
|
* 0.0 = use vig-removed market odds exactly (recommended).
|
|
* Increase slightly (e.g. 0.1) to soften extreme probabilities.
|
|
*/
|
|
const UNCERTAINTY_FACTOR = 0.0;
|
|
|
|
/** Lookup points for a finishing position; returns 0 for unscored positions. */
|
|
function getRacePoints(racePoints: Record<number, number>, position: number): number {
|
|
return racePoints[position] ?? 0;
|
|
}
|
|
|
|
// ─── Odds helpers ─────────────────────────────────────────────────────────────
|
|
|
|
/** Convert American odds to implied probability (no vig removal). */
|
|
function americanToImpliedProb(americanOdds: number): number {
|
|
if (americanOdds > 0) {
|
|
return 100 / (americanOdds + 100);
|
|
}
|
|
return Math.abs(americanOdds) / (Math.abs(americanOdds) + 100);
|
|
}
|
|
|
|
// ─── Core simulation helper ───────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Weighted sequential draw without replacement.
|
|
* Returns all items in a simulated finishing order.
|
|
* Each draw is proportional to remaining weights.
|
|
*/
|
|
function weightedDrawWithoutReplacement(ids: string[], weights: number[]): string[] {
|
|
const pool = ids.slice();
|
|
const w = weights.slice();
|
|
const result: string[] = [];
|
|
|
|
while (pool.length > 0) {
|
|
const total = w.reduce((s, v) => s + v, 0);
|
|
let r = Math.random() * total;
|
|
let idx = 0;
|
|
while (idx < w.length - 1 && r > w[idx]) {
|
|
r -= w[idx];
|
|
idx++;
|
|
}
|
|
result.push(pool[idx]);
|
|
pool.splice(idx, 1);
|
|
w.splice(idx, 1);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// ─── Simulator ────────────────────────────────────────────────────────────────
|
|
|
|
export class AutoRacingSimulator implements Simulator {
|
|
constructor(
|
|
private readonly racePoints: Record<number, number>,
|
|
private readonly source: string,
|
|
) {}
|
|
|
|
async simulate(sportsSeasonId: string): Promise<SimulationResult[]> {
|
|
const db = database();
|
|
|
|
// 1. Load all participants for this sports season
|
|
const participants = await db.query.seasonParticipants.findMany({
|
|
where: eq(schema.seasonParticipants.sportsSeasonId, sportsSeasonId),
|
|
});
|
|
|
|
if (participants.length === 0) {
|
|
throw new Error(`No participants found for sports season ${sportsSeasonId}.`);
|
|
}
|
|
|
|
// 2. Load current championship standings (existing points earned this season)
|
|
const seasonResults = await getSeasonResults(sportsSeasonId);
|
|
const currentPointsMap = new Map<string, number>(
|
|
seasonResults.map((r) => [r.participant.id, parseFloat(r.currentPoints ?? "0")])
|
|
);
|
|
|
|
// 3. Count remaining races: incomplete scoring events, excluding schedule_event entries
|
|
const allEvents = await db.query.scoringEvents.findMany({
|
|
where: eq(schema.scoringEvents.sportsSeasonId, sportsSeasonId),
|
|
});
|
|
const remainingRaces = allEvents.filter(
|
|
(e) => !e.isComplete && e.eventType !== "schedule_event"
|
|
).length;
|
|
|
|
// 4. Load EV data for championship win probabilities
|
|
const evs = await getAllParticipantEVsForSeason(sportsSeasonId);
|
|
const evMap = new Map(evs.map((ev) => [ev.participantId, ev]));
|
|
|
|
const ids = participants.map((p) => p.id);
|
|
|
|
// 5. Build raw implied championship win probabilities from odds.
|
|
// americanToImpliedProb includes vig (sum > 1.0), so we normalize to sum = 1.0
|
|
// before using as weights. This is standard "vig removal" and ensures a driver
|
|
// with -200 odds (~66.7% implied) gets ~55% weight when the total vig is ~1.2.
|
|
const fallbackProb = 1 / participants.length;
|
|
const rawProbs = new Map<string, number>();
|
|
|
|
for (const p of participants) {
|
|
const ev = evMap.get(p.id);
|
|
rawProbs.set(p.id, ev !== undefined && ev.sourceOdds !== null && ev.sourceOdds !== undefined ? americanToImpliedProb(ev.sourceOdds) : fallbackProb);
|
|
}
|
|
|
|
// Normalize to remove vig
|
|
const rawSum = [...rawProbs.values()].reduce((a, b) => a + b, 0);
|
|
for (const [id, prob] of rawProbs) {
|
|
rawProbs.set(id, prob / rawSum);
|
|
}
|
|
|
|
// 6. Optionally smooth toward the mean (no-op when UNCERTAINTY_FACTOR = 0)
|
|
const baseProbs = new Map<string, number>();
|
|
if (UNCERTAINTY_FACTOR === 0) {
|
|
for (const [id, prob] of rawProbs) baseProbs.set(id, prob);
|
|
} else {
|
|
const avgProb = [...rawProbs.values()].reduce((a, b) => a + b, 0) / participants.length;
|
|
for (const [id, prob] of rawProbs) {
|
|
baseProbs.set(id, prob * (1 - UNCERTAINTY_FACTOR) + avgProb * UNCERTAINTY_FACTOR);
|
|
}
|
|
}
|
|
|
|
// 7. Accumulate finish counts across simulations
|
|
// rankCounts[id][0..7] = number of times driver finished 1st..8th
|
|
const rankCounts = new Map<string, number[]>();
|
|
for (const id of ids) {
|
|
rankCounts.set(id, Array.from({ length: 8 }, () => 0));
|
|
}
|
|
|
|
if (remainingRaces === 0) {
|
|
// Pre-season: no races to simulate, derive placement probabilities
|
|
// from sourceOdds via pure weighted draws.
|
|
const weights = ids.map((id) => baseProbs.get(id) ?? fallbackProb);
|
|
for (let sim = 0; sim < NUM_SIMULATIONS; sim++) {
|
|
const finishOrder = weightedDrawWithoutReplacement(ids, weights);
|
|
for (let rank = 0; rank < Math.min(8, finishOrder.length); rank++) {
|
|
const counts = rankCounts.get(finishOrder[rank]);
|
|
if (counts) counts[rank]++;
|
|
}
|
|
}
|
|
} else {
|
|
// In-season: simulate remaining races from current standings.
|
|
for (let sim = 0; sim < NUM_SIMULATIONS; sim++) {
|
|
// 7a. Season-long performance multiplier per driver
|
|
const seasonWeights = new Map<string, number>();
|
|
for (const id of ids) {
|
|
const base = baseProbs.get(id) ?? fallbackProb;
|
|
const mult = Math.max(
|
|
0.05,
|
|
1 - PARTICIPANT_VOLATILITY + Math.random() * PARTICIPANT_VOLATILITY * 2
|
|
);
|
|
seasonWeights.set(id, base * mult);
|
|
}
|
|
|
|
// 7b. Start from current championship points
|
|
const simPoints = new Map<string, number>(
|
|
ids.map((id) => [id, currentPointsMap.get(id) ?? 0])
|
|
);
|
|
|
|
// 7c. Simulate each remaining race
|
|
for (let race = 0; race < remainingRaces; race++) {
|
|
const raceWeights = ids.map((id) => {
|
|
const sw = seasonWeights.get(id) ?? fallbackProb;
|
|
const noise = Math.max(0.01, 1 - RACE_NOISE + Math.random() * RACE_NOISE * 2);
|
|
return sw * noise;
|
|
});
|
|
|
|
const finishOrder = weightedDrawWithoutReplacement(ids, raceWeights);
|
|
|
|
for (let pos = 0; pos < finishOrder.length; pos++) {
|
|
const pts = getRacePoints(this.racePoints, pos + 1);
|
|
if (pts === 0) break; // unscored positions earn no points
|
|
simPoints.set(finishOrder[pos], (simPoints.get(finishOrder[pos]) ?? 0) + pts);
|
|
}
|
|
}
|
|
|
|
// 7d. Sort by final championship points, record top-8 finishes
|
|
const finalOrder = [...simPoints.entries()]
|
|
.toSorted((a, b) => b[1] - a[1])
|
|
.map(([id]) => id);
|
|
|
|
for (let rank = 0; rank < Math.min(8, finalOrder.length); rank++) {
|
|
const counts = rankCounts.get(finalOrder[rank]);
|
|
if (counts) counts[rank]++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 8. Convert counts → probability distributions
|
|
const results: SimulationResult[] = participants.map((p) => {
|
|
const counts = rankCounts.get(p.id) ?? [0,0,0,0,0,0,0,0];
|
|
return {
|
|
participantId: p.id,
|
|
probabilities: {
|
|
probFirst: counts[0] / NUM_SIMULATIONS,
|
|
probSecond: counts[1] / NUM_SIMULATIONS,
|
|
probThird: counts[2] / NUM_SIMULATIONS,
|
|
probFourth: counts[3] / NUM_SIMULATIONS,
|
|
probFifth: counts[4] / NUM_SIMULATIONS,
|
|
probSixth: counts[5] / NUM_SIMULATIONS,
|
|
probSeventh: counts[6] / NUM_SIMULATIONS,
|
|
probEighth: counts[7] / NUM_SIMULATIONS,
|
|
},
|
|
source: this.source,
|
|
};
|
|
});
|
|
|
|
// 9. Per-position normalization: each column should sum to exactly 1.0 but
|
|
// floating-point division (count / 10000) accumulates small errors across
|
|
// ~20 drivers, causing the total EV to drift (e.g. 340.02 instead of 340).
|
|
// Fix: add the residual (1.0 - colSum) to the largest probability in each
|
|
// column so the sum is exactly 1.0 in IEEE 754 arithmetic.
|
|
const positionKeys: Array<keyof typeof results[0]["probabilities"]> = [
|
|
"probFirst", "probSecond", "probThird", "probFourth",
|
|
"probFifth", "probSixth", "probSeventh", "probEighth",
|
|
];
|
|
for (const key of positionKeys) {
|
|
const colSum = results.reduce((s, r) => s + r.probabilities[key], 0);
|
|
const residual = 1.0 - colSum;
|
|
if (residual !== 0) {
|
|
const maxResult = results.reduce((best, r) =>
|
|
r.probabilities[key] > best.probabilities[key] ? r : best
|
|
);
|
|
maxResult.probabilities[key] += residual;
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
}
|