An AFL club seeded into an Elimination Final is awarded 15 fantasy points the
moment the bracket is generated — afl_10's entryFloor: 7 — and cannot finish
worse than the 7th-8th tier. Its EV still read 13.
AFLSimulator was stateless with respect to the live bracket. It read only
participants, sourceElo and the regular-season standings, then re-projected the
whole ladder and re-seeded 1-10 from Elo on every one of its 10,000 iterations.
So a team with a locked Elimination Final berth was re-drawn into the Wildcard
Round, or out of the finals entirely, in a slice of them — and there it scores 0.
Even with the ladder complete the Math.random() tiebreaker reshuffled every club
tied on ladder points, which in the AFL is most of the middle of the table.
Games already played were re-played the same way, so a completed Wildcard win was
worth 50% of nothing. Mixing zeroes into a distribution whose floor is 15 is what
produced 13.
afl_10 is the only template that defines entryFloor at all, which is why this
surfaced here and not on LLWS, whose floors only exist once a team has won
something.
The simulator now mirrors llws-simulator's bracket-aware mode:
- readAflBracketSeeds reads the 10 seeds from the slots generateAFL10Bracket
writes them into. The two Elimination Final participant2 slots are TBD by
design and are never read as seeds, leaving exactly 10 named slots. No draw
at all falls back to the ladder projection; a partially seeded, duplicated or
unknown draw throws rather than silently discarding the draw and every
recorded result with it.
- makePlayGame replays a completed match from its recorded result whenever both
recorded teams are the two the simulation routed into that game, so an
already-played result sticks across all iterations.
- simAFLFinals labels each game with the round and match number
generateAFL10Bracket and advanceAFLWinner use, so a result is looked up
against the game it was played in. Its routing was already correct.
EV >= the banked floor now holds by construction, with no clamping: a team seeded
into an Elimination Final is in that game in every iteration. Column sums stay
exactly 1.0 and the 340-point total-EV invariant is unchanged, since teams
outside the bracket simply score nothing.
Fixing the simulator alone would not have held. processMatchResult calls
updateProbabilitiesAfterResult on every result, and its ICM branch re-derives
each still-alive participant's whole distribution from P(1st) alone, knowing
nothing about the bracket — so the next finals result would have put the EV
straight back under the floor. That branch was built for futures-odds seasons.
It now runs only when the season's EVs did not come from a bracket-aware
simulator; when they did, that simulator is re-run instead, since it already
knows the completed matches. Both conditions matter: re-running a bracket-blind
simulator would re-draw the field and hand equity back to knocked-out teams, so
a new manifest `bracketAware` flag limits this to AFL and LLWS. A failed re-run
leaves probabilities untouched rather than falling back to the ICM path that is
being replaced.
The finalized-participant pinning loop now runs after that recalculation rather
than before. A simulation run rewrites every participant in the season, the
finalized ones included; a finalized placement is a fact, not a projection, so it
is written last and wins.
Tests: seeds clear the entry floors their seeding banked, a Qualifying Final
entrant is structurally absent from the 7th-8th tier, the bracket's draw beats
Elo (the weakest club seeded 1, the strongest seeded 10), completed Wildcard and
Qualifying Finals are replayed with the winner banking its floor, teams outside
the bracket are zeroed, and the column sums and 340 total survive. The bracket
fixtures deliberately seed the ten weakest clubs, because seeding the strongest
ten lets the ladder projection reproduce much the same field by accident. Six of
the seven were confirmed to fail against the previous behavior. Plus the
probability-updater branch in each direction, its failure path, and the pin
ordering.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxDnSEKVoKx9HFcQ6gmcS
368 lines
14 KiB
TypeScript
368 lines
14 KiB
TypeScript
/**
|
|
* Probability Updater Service
|
|
*
|
|
* Updates probability distributions when real results come in.
|
|
*
|
|
* Key behaviors:
|
|
* - Finished participants: Set to 100% at their placement, 0% elsewhere
|
|
* - Unfinished participants: Re-run ICM calculation with remaining participants
|
|
* - Handles partial results correctly
|
|
*/
|
|
|
|
import { findParticipantResultsBySportsSeasonId } from "~/models/participant-result";
|
|
import {
|
|
getAllParticipantEVsForSeason,
|
|
upsertParticipantEV,
|
|
type ParticipantEV,
|
|
} from "~/models/participant-expected-value";
|
|
import { calculateICMFromOdds } from "./icm-calculator";
|
|
import type { ProbabilityDistribution } from "./ev-calculator";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import { DEFAULT_SCORING_RULES } from "~/lib/scoring-types";
|
|
import { getSportsSeasonSimulatorConfig } from "~/models/simulator";
|
|
import { getManifestSimulatorProfile } from "~/services/simulations/manifest";
|
|
import { logger } from "~/lib/logger";
|
|
|
|
/**
|
|
* Result of probability update operation
|
|
*/
|
|
export interface ProbabilityUpdateResult {
|
|
finishedParticipants: number;
|
|
unfishedParticipants: number;
|
|
updated: number;
|
|
errors: string[];
|
|
}
|
|
|
|
/**
|
|
* Before/after probabilities for display
|
|
*/
|
|
export interface ProbabilityComparison {
|
|
participantId: string;
|
|
participantName: string;
|
|
before: number[]; // [P(1st), P(2nd), ..., P(8th)]
|
|
after: number[]; // [P(1st), P(2nd), ..., P(8th)]
|
|
status: 'finished' | 'recalculated' | 'unchanged';
|
|
}
|
|
|
|
/**
|
|
* Convert probability array to ProbabilityDistribution
|
|
*/
|
|
function arrayToProbabilityDistribution(probs: number[]): ProbabilityDistribution {
|
|
return {
|
|
probFirst: probs[0],
|
|
probSecond: probs[1],
|
|
probThird: probs[2],
|
|
probFourth: probs[3],
|
|
probFifth: probs[4],
|
|
probSixth: probs[5],
|
|
probSeventh: probs[6],
|
|
probEighth: probs[7],
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert ParticipantEV to probability array
|
|
*/
|
|
function evToProbabilityArray(ev: ParticipantEV): number[] {
|
|
return [
|
|
parseFloat(ev.probFirst),
|
|
parseFloat(ev.probSecond),
|
|
parseFloat(ev.probThird),
|
|
parseFloat(ev.probFourth),
|
|
parseFloat(ev.probFifth),
|
|
parseFloat(ev.probSixth),
|
|
parseFloat(ev.probSeventh),
|
|
parseFloat(ev.probEighth),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Create a probability distribution where participant finished at a specific position
|
|
*
|
|
* @param finalPosition The position where participant finished
|
|
* - 1-8: 100% at that position, 0% elsewhere
|
|
* - 0: Eliminated (didn't make playoffs) - 0% for all positions
|
|
* - >8: Finished outside scoring - 0% for all positions
|
|
* @returns Array of probabilities with 100% at finalPosition (if 1-8), or all 0%
|
|
*/
|
|
function createFinishedProbabilities(finalPosition: number): number[] {
|
|
const probs = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
// Handle positions 1-8
|
|
if (finalPosition >= 1 && finalPosition <= 8) {
|
|
probs[finalPosition - 1] = 1.0; // 100% at their position
|
|
}
|
|
// finalPosition = 0 (eliminated) or > 8 (finished outside top 8)
|
|
// Keep all probabilities at 0%
|
|
|
|
return probs;
|
|
}
|
|
|
|
/** EV sources written by a simulation run rather than by odds import or manual entry. */
|
|
const SIMULATOR_EV_SOURCES = new Set(["elo_simulation", "performance_model"]);
|
|
|
|
/**
|
|
* Decide whether a season's still-alive participants should be refreshed by re-running its
|
|
* simulator instead of by the ICM recalculation below.
|
|
*
|
|
* Two conditions, and both matter:
|
|
*
|
|
* - The simulator must be bracket-aware (manifest `bracketAware`). Re-running a
|
|
* bracket-blind simulator after a result would re-draw the field and hand championship
|
|
* equity back to teams that have already been knocked out — strictly worse than ICM.
|
|
* Only AFL and LLWS read the real draw and replay completed matches.
|
|
* - The EVs must actually have come from that simulator. If an admin entered them by hand
|
|
* or imported them from futures odds, overwriting them with a simulation is not a refresh.
|
|
* The finished-participant loop below rewrites rows to `manual`, so only the unfinished
|
|
* rows — the ones about to be recalculated — are consulted.
|
|
*/
|
|
async function shouldRerunSimulator(
|
|
sportsSeasonId: string,
|
|
unfinishedEVs: ParticipantEV[]
|
|
): Promise<boolean> {
|
|
if (!unfinishedEVs.some((ev) => SIMULATOR_EV_SOURCES.has(ev.source ?? ""))) return false;
|
|
|
|
const simulatorConfig = await getSportsSeasonSimulatorConfig(sportsSeasonId);
|
|
if (!simulatorConfig) return false;
|
|
|
|
return getManifestSimulatorProfile(simulatorConfig.simulatorType)?.bracketAware === true;
|
|
}
|
|
|
|
/**
|
|
* Update probabilities for a sports season after results come in
|
|
*
|
|
* Process:
|
|
* 1. Get all participant results (finished participants)
|
|
* 2. Get all existing participant EVs
|
|
* 3. For finished participants: set 100% at their placement
|
|
* 4. For unfinished participants: re-run the season's bracket-aware simulator if it has one,
|
|
* otherwise recalculate using ICM with remaining participants
|
|
*
|
|
* @param sportsSeasonId Sports season to update
|
|
* @param recalculateUnfinished Whether to recalculate unfinished participants (default true)
|
|
* @returns Update result summary
|
|
*/
|
|
export async function updateProbabilitiesAfterResult(
|
|
sportsSeasonId: string,
|
|
recalculateUnfinished = true
|
|
): Promise<ProbabilityUpdateResult> {
|
|
const errors: string[] = [];
|
|
let updated = 0;
|
|
|
|
try {
|
|
// Get all results (finished participants)
|
|
const results = await findParticipantResultsBySportsSeasonId(sportsSeasonId);
|
|
|
|
// Get all existing EVs
|
|
const existingEVs = await getAllParticipantEVsForSeason(sportsSeasonId);
|
|
|
|
// Create map of participantId -> finalPosition.
|
|
//
|
|
// Provisional rows (isPartialScore) are NOT finished: they are the guaranteed
|
|
// minimum for someone still alive — a bracket entry floor, or the floor banked
|
|
// by winning a round. Treating them as finished pins the participant to 100% at
|
|
// that floor and drops them from the ICM recalculation below, which would zero
|
|
// the championship odds of every team still playing. They belong in the
|
|
// unfinished set until a real result lands.
|
|
const finishedMap = new Map(
|
|
results
|
|
.filter(r => r.finalPosition !== null && !r.isPartialScore)
|
|
.map(r => [r.participantId, r.finalPosition ?? 0])
|
|
);
|
|
|
|
// Recalculate unfinished participants if requested
|
|
if (recalculateUnfinished) {
|
|
const unfinishedEVs = existingEVs.filter(
|
|
ev => !finishedMap.has(ev.participantId)
|
|
);
|
|
|
|
if (unfinishedEVs.length > 0 && (await shouldRerunSimulator(sportsSeasonId, unfinishedEVs))) {
|
|
// The simulator reads the bracket, so it already knows this result: it seeds from the
|
|
// real draw and replays every completed match. Re-running it keeps each participant's
|
|
// distribution consistent with the games actually played — including the placement
|
|
// floors a bracket entry or a non-scoring-round win has already banked, which the ICM
|
|
// branch below cannot see and would value below points the league has paid out.
|
|
//
|
|
// Imported lazily: probability-updater → runner → scoring-calculator →
|
|
// probability-updater is a module cycle, and a static import leaves the binding
|
|
// undefined at module-init time.
|
|
try {
|
|
const { runSportsSeasonSimulation } = await import("~/services/simulations/runner");
|
|
await runSportsSeasonSimulation(sportsSeasonId);
|
|
updated += unfinishedEVs.length;
|
|
} catch (error) {
|
|
// runSportsSeasonSimulation throws on a completed season, on a run already in
|
|
// flight, and on failed readiness. Leave the existing probabilities alone rather
|
|
// than falling back to ICM: for these seasons ICM is the thing being replaced, and
|
|
// a completed season has nothing unfinished left to recalculate anyway.
|
|
logger.error(
|
|
`[ProbabilityUpdater] Failed to re-run simulator for sports season ${sportsSeasonId}; ` +
|
|
`leaving existing probabilities in place:`,
|
|
error
|
|
);
|
|
errors.push(`Failed to re-run simulator for sports season ${sportsSeasonId}: ${error}`);
|
|
}
|
|
} else if (unfinishedEVs.length > 0) {
|
|
// Get their current championship probabilities (use existing P(1st) as proxy)
|
|
const unfinishedOdds = unfinishedEVs.map(ev => {
|
|
const pFirst = parseFloat(ev.probFirst);
|
|
// Convert probability back to odds (approximate)
|
|
// probability = 100 / (odds + 100) => odds = (100 / probability) - 100
|
|
const odds = pFirst > 0 ? Math.round((100 / pFirst) - 100) : 100000;
|
|
|
|
return {
|
|
participantId: ev.participantId,
|
|
odds: odds,
|
|
};
|
|
});
|
|
|
|
// Recalculate ICM for unfinished participants
|
|
const icmResults = calculateICMFromOdds(unfinishedOdds);
|
|
|
|
// Sequential for the same reason as the finished loop above:
|
|
// upsertParticipantEV rewrites shared per-season state via syncVorpForSeason.
|
|
for (const [participantId, icmResult] of icmResults.entries()) {
|
|
try {
|
|
const probs = [
|
|
icmResult.probabilities.first,
|
|
icmResult.probabilities.second,
|
|
icmResult.probabilities.third,
|
|
icmResult.probabilities.fourth,
|
|
icmResult.probabilities.fifth,
|
|
icmResult.probabilities.sixth,
|
|
icmResult.probabilities.seventh,
|
|
icmResult.probabilities.eighth,
|
|
];
|
|
|
|
const probabilities = arrayToProbabilityDistribution(probs);
|
|
|
|
await upsertParticipantEV({
|
|
participantId,
|
|
sportsSeasonId,
|
|
probabilities,
|
|
scoringRules: DEFAULT_SCORING_RULES,
|
|
source: 'futures_odds', // Recalculated from remaining odds
|
|
});
|
|
|
|
updated++;
|
|
} catch (error) {
|
|
errors.push(`Failed to recalculate participant ${participantId}: ${error}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update finished participants. The shared default table is used because we only
|
|
// care about setting probabilities here, not the EV — each league re-derives its own
|
|
// EV from the stored probabilities in calculateTeamProjectedScore.
|
|
//
|
|
// This runs *after* the recalculation above, not before, because re-running a simulator
|
|
// rewrites every participant in the season — the finalized ones included. A finalized
|
|
// placement is a fact, not a projection, so it is written last and wins: if a simulator
|
|
// ever puts a knocked-out team back in contention (a bracket-aware one whose bracket has
|
|
// since been cleared and not re-seeded, say), the pin still zeroes them.
|
|
|
|
// Sequential: upsertParticipantEV calls syncVorpForSeason internally, which
|
|
// reads and rewrites every seasonParticipants row for this sportsSeasonId.
|
|
// Running these in parallel would race on that shared state.
|
|
for (const [participantId, finalPosition] of finishedMap.entries()) {
|
|
try {
|
|
const probs = createFinishedProbabilities(finalPosition);
|
|
const probabilities = arrayToProbabilityDistribution(probs);
|
|
|
|
await upsertParticipantEV({
|
|
participantId,
|
|
sportsSeasonId,
|
|
probabilities,
|
|
scoringRules: DEFAULT_SCORING_RULES,
|
|
source: 'manual', // Result is from actual outcome
|
|
});
|
|
|
|
updated++;
|
|
} catch (error) {
|
|
errors.push(`Failed to update participant ${participantId}: ${error}`);
|
|
}
|
|
}
|
|
|
|
return {
|
|
finishedParticipants: finishedMap.size,
|
|
unfishedParticipants: existingEVs.length - finishedMap.size,
|
|
updated,
|
|
errors,
|
|
};
|
|
} catch (error) {
|
|
errors.push(`Failed to update probabilities: ${error}`);
|
|
return {
|
|
finishedParticipants: 0,
|
|
unfishedParticipants: 0,
|
|
updated,
|
|
errors,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get before/after comparison of probabilities for display
|
|
*
|
|
* Shows what will change when updateProbabilitiesAfterResult runs.
|
|
* Useful for preview before committing changes.
|
|
*
|
|
* @param sportsSeasonId Sports season to preview
|
|
* @returns Array of probability comparisons
|
|
*/
|
|
export async function previewProbabilityUpdate(
|
|
sportsSeasonId: string
|
|
): Promise<ProbabilityComparison[]> {
|
|
const db = database();
|
|
|
|
// Get all results (finished participants)
|
|
const results = await findParticipantResultsBySportsSeasonId(sportsSeasonId);
|
|
|
|
// Get all existing EVs with participant names
|
|
const existingEVs = await db.query.seasonParticipantExpectedValues.findMany({
|
|
where: eq(schema.seasonParticipantExpectedValues.sportsSeasonId, sportsSeasonId),
|
|
with: {
|
|
participant: true,
|
|
},
|
|
});
|
|
|
|
// Create map of participantId -> finalPosition
|
|
const finishedMap = new Map(
|
|
results
|
|
.filter(r => r.finalPosition !== null)
|
|
.map(r => [r.participantId, r.finalPosition ?? 0])
|
|
);
|
|
|
|
const comparisons: ProbabilityComparison[] = [];
|
|
|
|
for (const ev of existingEVs) {
|
|
const before = evToProbabilityArray(ev);
|
|
const finalPosition = finishedMap.get(ev.participantId);
|
|
|
|
let after: number[];
|
|
let status: 'finished' | 'recalculated' | 'unchanged';
|
|
|
|
if (finalPosition !== undefined) {
|
|
// Finished participant
|
|
after = createFinishedProbabilities(finalPosition);
|
|
status = 'finished';
|
|
} else {
|
|
// For preview, we'd need to recalculate - for now just show unchanged
|
|
// In a real implementation, we'd run the ICM calculation here too
|
|
after = before;
|
|
status = 'unchanged';
|
|
}
|
|
|
|
comparisons.push({
|
|
participantId: ev.participantId,
|
|
participantName: ev.participant?.name || 'Unknown',
|
|
before,
|
|
after,
|
|
status,
|
|
});
|
|
}
|
|
|
|
return comparisons;
|
|
}
|