* feat: EV simulation framework with F1 Monte Carlo simulator - Add EV snapshot tables (participant_ev_snapshots, team_ev_snapshots) and simulation_status column on sports seasons - Add ev-snapshot model with upsert and history query functions - Add simulator framework: types, bracket/F1/golf simulators, registry - F1 simulator: vig-removed ICM weighted draw (pre-season) + race-by-race Monte Carlo from current standings (in-season); per-position column normalization to prevent floating-point EV drift - Add admin simulate route and Run Simulation button on sports season page - Rework futures-odds admin page to save odds then run simulation in one action - Remove recalculate-probabilities route (superseded by simulate route) - Remove EV trend chart panel and associated DB queries Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat: map simulators to sports via simulatorType field Adds a `simulator_type` enum column to the `sports` table so each sport can be assigned a specific simulation algorithm rather than deriving it from the sports season's scoring pattern. - Add `simulatorTypeEnum` (f1_standings, indycar_standings, golf_qualifying_points, playoff_bracket) + `simulatorType` nullable column on `sports` table; migration 0037 - Rewrite simulator registry to key off `SimulatorType` instead of `ScoringPattern`; indycar_standings shares F1Simulator for now - `findSportsSeasonById` now returns `SportsSeasonWithSport` so callers have typed access to `sport.simulatorType` - Simulate and futures-odds actions read `sport.simulatorType`; guard fires before setting `simulationStatus: running` - Admin sport edit page gains a Simulator Type dropdown Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
98 lines
3.5 KiB
TypeScript
98 lines
3.5 KiB
TypeScript
/**
|
|
* Bracket Simulator
|
|
*
|
|
* Wraps the existing Monte Carlo bracket simulator (app/services/bracket-simulator.ts)
|
|
* to conform to the Simulator interface. Loads current participant Elo ratings
|
|
* from participantExpectedValues and runs 100k simulations.
|
|
*
|
|
* The existing bracket simulator uses Elo ratings derived from futures odds
|
|
* (via the probability engine pipeline). These should be imported via the
|
|
* admin "Futures Odds" page before running simulation.
|
|
*/
|
|
|
|
import { database } from "~/database/context";
|
|
import { participantExpectedValues, participants } from "~/database/schema";
|
|
import { eq, and, isNotNull } from "drizzle-orm";
|
|
import { simulateBracket } from "~/services/bracket-simulator";
|
|
import { convertFuturesToElo } from "~/services/probability-engine";
|
|
import type { Simulator, SimulationResult } from "./types";
|
|
|
|
export class BracketSimulator implements Simulator {
|
|
async simulate(sportsSeasonId: string): Promise<SimulationResult[]> {
|
|
const db = database();
|
|
|
|
// Load all participants with their current EVs (which hold probability distributions)
|
|
const evRows = await db
|
|
.select({
|
|
participantId: participantExpectedValues.participantId,
|
|
probFirst: participantExpectedValues.probFirst,
|
|
sourceOdds: participantExpectedValues.sourceOdds,
|
|
})
|
|
.from(participantExpectedValues)
|
|
.where(eq(participantExpectedValues.sportsSeasonId, sportsSeasonId));
|
|
|
|
if (evRows.length === 0) {
|
|
throw new Error(
|
|
`No participant EVs found for sports season ${sportsSeasonId}. ` +
|
|
`Import futures odds first via Admin → Futures Odds.`
|
|
);
|
|
}
|
|
|
|
// Build Elo ratings from source odds (American odds format) if available,
|
|
// otherwise fall back to using probFirst as a proxy for win probability.
|
|
let eloMap: Map<string, number>;
|
|
|
|
const hasOdds = evRows.some((r) => r.sourceOdds !== null);
|
|
|
|
if (hasOdds) {
|
|
const oddsInput = evRows
|
|
.filter((r) => r.sourceOdds !== null)
|
|
.map((r) => ({ participantId: r.participantId, odds: r.sourceOdds! }));
|
|
eloMap = convertFuturesToElo(oddsInput);
|
|
} else {
|
|
// Fall back: treat probFirst (as %) as championship win probability,
|
|
// convert to a rough Elo by mapping [min, max] prob → [1250, 1750]
|
|
const probs = evRows.map((r) => parseFloat(r.probFirst));
|
|
const minProb = Math.min(...probs);
|
|
const maxProb = Math.max(...probs);
|
|
const range = maxProb - minProb || 1;
|
|
|
|
eloMap = new Map(
|
|
evRows.map((r) => {
|
|
const prob = parseFloat(r.probFirst);
|
|
const normalised = (prob - minProb) / range;
|
|
const elo = 1250 + normalised * 500;
|
|
return [r.participantId, elo];
|
|
})
|
|
);
|
|
}
|
|
|
|
const teamsForSimulation = evRows
|
|
.filter((r) => eloMap.has(r.participantId))
|
|
.map((r) => ({
|
|
participantId: r.participantId,
|
|
elo: eloMap.get(r.participantId)!,
|
|
}));
|
|
|
|
if (teamsForSimulation.length === 0) {
|
|
throw new Error(`Could not build Elo ratings for sports season ${sportsSeasonId}.`);
|
|
}
|
|
|
|
const probMap = await simulateBracket(teamsForSimulation);
|
|
|
|
return Array.from(probMap.entries()).map(([participantId, probs]) => ({
|
|
participantId,
|
|
probabilities: {
|
|
probFirst: probs[0],
|
|
probSecond: probs[1],
|
|
probThird: probs[2],
|
|
probFourth: probs[3],
|
|
probFifth: probs[4],
|
|
probSixth: probs[5],
|
|
probSeventh: probs[6],
|
|
probEighth: probs[7],
|
|
},
|
|
source: "bracket_monte_carlo",
|
|
}));
|
|
}
|
|
}
|