Layer 3 Step 4: derived park factors, composable for weather
PHASE 0 GATE — the answer is BOTH, and the important half was already here. A STATIC FanGraphs park-factor table has existed since Session 15 (src/data/parkFactors.js) and computeFeatures already consumes it, so park is not a new idea in this codebase. What was missing is OUR derivation. I nearly built a second source of truth before finding it; the new service lives at src/services/parkFactors.js and the two are deliberately distinct. That discovery changes the point of this order rather than just its scope. If the champion already sees a park factor, adding one to the challenger risks double-counting — which is exactly the redundancy the Session-72 harness exists to catch. So park ships as a NOMINATED CHALLENGER whose job is to be tested for marginal contribution, not as an assumed improvement. Checked and worth noting: the static table reaches computeFeatures but NOT probabilityEstimator, so it does not currently touch p_win at all. DERIVATION, not ingestion. statsapi gives every game with venue, linescore and scoringPlays in one call per date range — and since every home run scores at least the batter, HR totals are fully recoverable from scoring plays. Derived from 5,055 real games across 2022-2025: Coors tops the run environment at 1.099, Dodger Stadium tops home runs at 1.106, Oracle Park and PNC suppress them at 0.923 and 0.917. Eighteen parks cleared the floor, eighteen did not and are honestly absent. COMPOSABLE BY CONSTRUCTION — the architectural point. Park emits a multiplier around 1.0, never an additive nudge, because weather has to modulate it next order: effective = park_base x weather_mod. Additive terms do not compose correctly (a 5% park and an 8% wind are 1.05 x 1.08, not +13%), and the challenger converts the multiplier to log-odds so stacking stays correct. A test multiplies a placeholder weather term onto the park base to prove the shape composes with no rearchitecting. DIRECTIONAL BY PROP-OWNER: home_runs and home_runs_allowed both key off hr_base in the same direction, because the sign lives in the STAT, not the park. Coors inflates the hitter's home run prop and the pitcher's home-runs-allowed prop identically. THREE HONEST STATES, deliberately distinct. Absent (thin sample, adjust nothing), present (adjust), and weather_na for domes — where the park factor STILL APPLIES because a dome has a real run environment, and the flag exists so next order's weather modulation correctly does nothing there. N/A is not absent; conflating them would either drop a valid park factor or apply wind indoors. Structural breaks: a season deviating past the threshold starts a new regime only if the FOLLOWING season confirms it — one odd year is noise, two consecutive years on the same side is a rebuilt park. Only post-break seasons are used, so a humidor or moved wall cannot be diluted by the stadium that preceded it. Factors regress toward neutral by sample size, so a two-season park cannot assert a Coors-sized coefficient, and fine conditioning stays unavailable until its own larger floor. Tests 3669 passed / 297 suites, web build exit 0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCNgGSt5qvcLxaeQqa7Zpj
This commit is contained in:
@@ -100,25 +100,71 @@ function num(v) {
|
||||
* `adjustments` is empty — the challenger is the champion on those rows, by
|
||||
* construction, and the comparison stays clean.
|
||||
*/
|
||||
function adjust({ pWin, direction, statType, classification } = {}) {
|
||||
/**
|
||||
* Compose an ENVIRONMENT multiplier into the same log-odds space the archetype
|
||||
* nudges use. A multiplier of 1.0 is a no-op; >1 leans toward the over.
|
||||
*
|
||||
* Park is the BASE environment; weather will multiply onto it next order
|
||||
* (`env = park_base × weather_mod × …`) with no change here — that is why the
|
||||
* park layer emits a coefficient rather than a nudge.
|
||||
*
|
||||
* `Math.log(env)` converts a multiplicative environment into an additive
|
||||
* log-odds term, which is the correct composition: two independent 5% effects
|
||||
* become 1.05 × 1.05, not +5% +5%.
|
||||
*/
|
||||
const ENV_SCALE = Number(process.env.ENV_NUDGE_SCALE) || 1.0;
|
||||
const MAX_ENV_NUDGE = 0.30;
|
||||
function envNudge(env, dirSign) {
|
||||
const e = num(env);
|
||||
if (e == null || e <= 0 || e === 1) return 0;
|
||||
const raw = Math.log(e) * ENV_SCALE * dirSign;
|
||||
return clamp(raw, -MAX_ENV_NUDGE, MAX_ENV_NUDGE);
|
||||
}
|
||||
|
||||
function adjust({ pWin, direction, statType, classification, environment } = {}) {
|
||||
const p = num(pWin);
|
||||
const identical = (reason) => ({
|
||||
p_win_challenger: p, delta: 0, adjustments: [], reason, version: CHALLENGER_VERSION,
|
||||
});
|
||||
|
||||
if (p == null || p <= 0 || p >= 1) return identical('no_champion_probability');
|
||||
if (!classification || !classification.sufficient) return identical('archetype_absent_or_thin');
|
||||
const envPresent = num(environment && environment.multiplier) != null
|
||||
&& num(environment.multiplier) !== 1;
|
||||
if ((!classification || !classification.sufficient) && !envPresent) {
|
||||
return identical('archetype_absent_or_thin');
|
||||
}
|
||||
|
||||
const vector = classification.vector || {};
|
||||
const vector = (classification && classification.vector) || {};
|
||||
const stat = String(statType || '').toLowerCase();
|
||||
const map = (classification.role === 'pitcher' ? PITCHER_MAP : BATTER_MAP)[stat];
|
||||
if (!map) return identical('stat_not_mapped');
|
||||
const role = classification && classification.role === 'pitcher' ? 'pitcher' : 'batter';
|
||||
const map = ((classification && classification.sufficient)
|
||||
? (role === 'pitcher' ? PITCHER_MAP : BATTER_MAP)[stat]
|
||||
: null) || {};
|
||||
if (!Object.keys(map).length && !envPresent) return identical('stat_not_mapped');
|
||||
|
||||
// Direction: a trait that raises the stat raises P(over) and lowers P(under).
|
||||
const dirSign = String(direction || 'over').toLowerCase() === 'under' ? -1 : 1;
|
||||
|
||||
const adjustments = [];
|
||||
let total = 0;
|
||||
|
||||
// ── ENVIRONMENT (park now; weather composes onto it next order) ────────
|
||||
// Applied even when no archetype axis fires: a park effect is real whether or
|
||||
// not the player is distinctive. `environment` is the COMPOSED multiplier.
|
||||
const envMult = num(environment && environment.multiplier);
|
||||
if (envMult != null && envMult !== 1) {
|
||||
const n = envNudge(envMult, dirSign);
|
||||
if (n) {
|
||||
total += n;
|
||||
adjustments.push({
|
||||
axis: 'environment', label: (environment.label || 'PARK'),
|
||||
tier: 'env', nudge: Math.round(n * 1000) / 1000,
|
||||
multiplier: Math.round(envMult * 1000) / 1000,
|
||||
venue: environment.venue || null,
|
||||
weather_na: environment.weather_na ?? null,
|
||||
});
|
||||
}
|
||||
}
|
||||
for (const [axisKey, sign] of Object.entries(map)) {
|
||||
if (!sign) continue;
|
||||
const hit = vector[axisKey];
|
||||
@@ -132,7 +178,7 @@ function adjust({ pWin, direction, statType, classification } = {}) {
|
||||
adjustments.push({ axis: axisKey, label: hit.label, tier: hit.tier, nudge: Math.round(signed * 1000) / 1000 });
|
||||
}
|
||||
|
||||
if (!adjustments.length) return identical('no_distinctive_axis_for_stat');
|
||||
if (!adjustments.length) return identical('no_distinctive_axis_or_environment');
|
||||
|
||||
const capped = clamp(total, -MAX_TOTAL_NUDGE, MAX_TOTAL_NUDGE);
|
||||
const challenger = clamp(fromLogOdds(toLogOdds(p) + capped), 0.01, 0.99);
|
||||
|
||||
Reference in New Issue
Block a user