Wire the three proven hits factors pre-grade: transmission proven, gain
inconclusive THE BUG THIS NEARLY SHIPPED AS A FINDING. The first audit reported 0 factors fired on all 1,140 rows. Not a result -- my paging helper ordered by `id`, and batter_spray, team_defense, platoon_splits and statcast_aggregates have composite primary keys with NO id column. The query errored, the loop broke on error, and four fully-populated tables read as empty. hitsFactorContext.js -- the PRODUCTION loader -- had the identical defect, so live wiring would have loaded nothing and served unadjusted while logging success. Third occurrence of this class in one session. Both loaders now order by a real column and THROW rather than degrade. The Phase 2 gate is what caught it: no resolution number was quoted until transmission was proved. PHASE 1 — pipeline is now base -> FACTORS -> CALIBRATE -> GRADE. Context built in snapshotService BEFORE gradeAndCacheSlate (was line 640+, grade at 454), threaded per prop, applied to p_over before p_win is set with p_win_prefactor and a full trace retained. Hits only. Coverage 859/1140 rows (75%): 474 with all three factors, 256 two, 129 one, 281 none. PHASE 2 — TRANSMISSION PROVEN, 12/12 sign-correct, 4/4 per factor, each applied IN ISOLATION. My first table compared each factor's expected sign against the COMPOSITE change and showed 3 false failures -- with three factors firing the net can oppose any single member; that was a flaw in the test, not the wiring. Two under-side rows confirm the flip is handled: a factor raising p(over) correctly lowers p_win. Switch hitters (Bailey, Bell, Rocchio) took no spray adjustment while their other factors fired normally -- the refusal is selective, not a blanket skip. PHASE 3/4 — both maps refit on the factor-adjusted forecast; the shadow-duel baseline is VOID and restarts, since it accumulated against a different forecast. Point-in-time, 765 held-out rows: reliability 0.00795 -> 0.00828 RESOLUTION 0.00229 -> 0.00345 (variance explained 0.93% -> 1.39%) Brier 0.25398 -> 0.25305 delta -0.00093 CI [-0.00225,+0.00002] Resolution rose 51% relative. The CI TOUCHES ZERO on 4 eval dates, so the composition does NOT earn a proven keep -- three isolated passes did not grant a composed pass. INCONCLUSIVE, reported as such. The gain is far below the sum of the isolated effects, which is expected: all three run through the same pitcher-batter confrontation and share signal. PHASE 5 — 1.39% of variance is still far below what band separation needs. The pivot was correct and incomplete: the plumbing defect was real and is fixed, three proven factors reach the served number for the first time, and transmission alone did not buy grade separation. Next arc is factor STRENGTH and BREADTH, not more plumbing. PHASE 6 — rbi anomaly logged, not chased: 14.51% variance explained vs hits 1.03%, on the stat we do not serve corrected and which has no proven factors. Either the biggest lever on the board or a mirage; it deserves its own order. The byte-identical invariant INVERTED for hits by design. All 13 frozen non-hits modules verified unchanged, probabilityEstimator included -- the factors ride outside it. No new Bonferroni slot; the composed OOS claim is reported with its CI and not claimed as a pass. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W1sivYNqY2TS5ftykmHBU9
This commit is contained in:
@@ -96,7 +96,13 @@ function dedupeProps(props, limit) {
|
||||
// Grade both sides and keep the higher-confidence verdict — that's the
|
||||
// side the engine actually favors.
|
||||
async function gradeBestSide(grade, prop, sport, opts = {}) {
|
||||
// PIPELINE ORDER: the factor context must reach the engine BEFORE it grades,
|
||||
// because factors adjust the forecast the grade is read from. It was
|
||||
// previously computed downstream of the grade it should inform.
|
||||
const factorContext = typeof opts.factorContext === 'function'
|
||||
? opts.factorContext(prop, sport) : null;
|
||||
const base = {
|
||||
factor_context: factorContext,
|
||||
player: prop.player,
|
||||
stat_type: prop.stat_type,
|
||||
line: prop.line,
|
||||
|
||||
@@ -547,10 +547,33 @@ async function analyzeViaEngine1(rawProp = {}) {
|
||||
|
||||
const dir = String(prop.direction || 'over').toLowerCase();
|
||||
const est = estimateProbability({ gameLogs: meta.gameLogs, line: prop.line, statType: rawProp.stat_type, features });
|
||||
|
||||
// ── PROVEN FACTORS, PRE-GRADE ────────────────────────────────────────
|
||||
// base rate -> FACTORS -> (calibration, later) -> grade. Only the three
|
||||
// factors that passed the two-part gate, only on hits, and only where each
|
||||
// is readable — every unreadable case leaves the forecast untouched rather
|
||||
// than nudging it toward a default.
|
||||
let pOver = est.p_over;
|
||||
let factorTrace = null;
|
||||
if (String(rawProp.stat_type || '').toLowerCase() === 'hits' && rawProp.factor_context) {
|
||||
try {
|
||||
const hf = require('../model/hitsFactors');
|
||||
const adj = hf.adjustProbability(pOver, rawProp.factor_context);
|
||||
if (adj.p_adjusted != null && adj.factors_fired > 0) {
|
||||
pOver = adj.p_adjusted;
|
||||
factorTrace = { multiplier: adj.multiplier, applied: adj.applied, skipped: adj.skipped, p_before: adj.p_base };
|
||||
}
|
||||
} catch { /* a factor must never break the grade */ }
|
||||
}
|
||||
|
||||
const pWin = dir === 'under'
|
||||
? (Number.isFinite(est.p_over) ? 1 - est.p_over : null)
|
||||
: (Number.isFinite(est.p_over) ? est.p_over : null);
|
||||
? (Number.isFinite(pOver) ? 1 - pOver : null)
|
||||
: (Number.isFinite(pOver) ? pOver : null);
|
||||
if (pWin != null) legacy.p_win = Math.round(pWin * 1000) / 1000;
|
||||
if (factorTrace) {
|
||||
legacy.factor_adjustment = factorTrace;
|
||||
legacy.p_win_prefactor = Math.round((dir === 'under' ? 1 - factorTrace.p_before : factorTrace.p_before) * 1000) / 1000;
|
||||
}
|
||||
|
||||
const sideOdds = dir === 'under' ? rawProp.under_odds : rawProp.over_odds;
|
||||
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* hitsFactorContext — load the proven factors' inputs ONCE per slate.
|
||||
*
|
||||
* The three hits factors each need a database read (batter spray, team
|
||||
* positional defence, platoon splits, pitcher contact profile). Doing that per
|
||||
* prop would put four queries inside a loop that runs across the whole board, so
|
||||
* the tables are loaded once and indexed, and the per-prop lookup is a map hit.
|
||||
*
|
||||
* Loaded BEFORE grading, which is the whole point of this order — the same data
|
||||
* was previously fetched after the grade it should have informed.
|
||||
*
|
||||
* Every load is best-effort: a missing table yields an empty index, the factor
|
||||
* finds nothing readable, and the forecast is served unadjusted. A factor layer
|
||||
* must never be able to break the pipeline it rides in.
|
||||
*/
|
||||
|
||||
const { knownNumber } = require('../../utils/known');
|
||||
const { nameKey } = require('../../utils/playerName');
|
||||
|
||||
/** Rows a factor table must have before we trust it at all. */
|
||||
const MIN_ROWS = 1;
|
||||
|
||||
/**
|
||||
* Page a factor table.
|
||||
*
|
||||
* ORDERING IS PER-TABLE. These tables have COMPOSITE primary keys
|
||||
* (as_of_date, sport, player_key) and NO `id` column, so ordering by `id`
|
||||
* errors — and an error here returns an empty index, which reads exactly like
|
||||
* "this feed has no data". That is the third time in this codebase that a
|
||||
* wiring fault has worn the costume of an honest absence, so the error is now
|
||||
* surfaced rather than swallowed.
|
||||
*/
|
||||
async function page(sb, table, select, orderBy, apply) {
|
||||
const out = [];
|
||||
for (let from = 0; ; from += 1000) {
|
||||
const q = apply ? apply(sb.from(table).select(select)) : sb.from(table).select(select);
|
||||
const { data, error } = await q.order(orderBy, { ascending: true }).range(from, from + 999);
|
||||
if (error) throw new Error(`${table}: ${error.message}`);
|
||||
if (!data || data.length === 0) break;
|
||||
out.push(...data);
|
||||
if (data.length < 1000) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Keep the most recent dated row per key. */
|
||||
function latestBy(rows, keyFn, dateFn) {
|
||||
const m = new Map();
|
||||
for (const r of rows) {
|
||||
const k = keyFn(r);
|
||||
if (!k) continue;
|
||||
const prev = m.get(k);
|
||||
if (!prev || String(dateFn(r)) > String(dateFn(prev))) m.set(k, r);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {function|null} a `(prop, sport) => context` resolver, or null when
|
||||
* nothing loaded — null means "serve unadjusted", never a stub context.
|
||||
*/
|
||||
async function build(sb, opts = {}) {
|
||||
if (!sb) return null;
|
||||
let spray; let defense; let platoon; let statcast;
|
||||
try {
|
||||
[spray, defense, platoon, statcast] = await Promise.all([
|
||||
page(sb, 'batter_spray', '*', 'player_key', (q) => q.eq('sport', 'mlb')),
|
||||
page(sb, 'team_defense', '*', 'team', (q) => q.eq('sport', 'mlb')),
|
||||
page(sb, 'platoon_splits', '*', 'player_key', (q) => q.eq('sport', 'mlb')),
|
||||
page(sb, 'statcast_aggregates', 'player_key, source_id, role, bats, throws, hard_hit_pct', 'player_key', (q) => q.eq('sport', 'mlb')),
|
||||
]);
|
||||
} catch (e) {
|
||||
// Surfaced, not silent: a load failure must be distinguishable from a feed
|
||||
// that genuinely holds nothing.
|
||||
console.warn('[factors] context load FAILED (not an empty feed):', e.message);
|
||||
return null;
|
||||
}
|
||||
if (!spray.length && !defense.length && !platoon.length) return null;
|
||||
|
||||
const sprayBy = latestBy(spray, (r) => r.player_key, (r) => r.as_of_date);
|
||||
const defBy = latestBy(defense, (r) => r.team, (r) => r.as_of_date);
|
||||
const platBy = latestBy(platoon, (r) => r.player_key, (r) => r.as_of_date);
|
||||
|
||||
const batBy = new Map();
|
||||
const pitBy = new Map();
|
||||
for (const r of statcast) {
|
||||
if (!r.player_key) continue;
|
||||
if (r.role === 'pitcher') pitBy.set(r.player_key, r);
|
||||
else batBy.set(r.player_key, r);
|
||||
}
|
||||
|
||||
// statcast stores PERCENTAGES (0-100); the factor wants a fraction.
|
||||
const asFraction = (v) => {
|
||||
const n = knownNumber(v);
|
||||
if (n === null) return null;
|
||||
return n > 1 ? n / 100 : n;
|
||||
};
|
||||
|
||||
const resolver = (prop) => {
|
||||
const key = nameKey(prop && prop.player);
|
||||
if (!key) return null;
|
||||
const bat = batBy.get(key);
|
||||
const bats = bat && bat.bats ? String(bat.bats)[0] : null;
|
||||
|
||||
// The opposing team and its starter, from whatever the prop carries.
|
||||
const oppName = prop && (prop.opponent || prop.opp_team || null);
|
||||
const def = oppName ? (defBy.get(oppName) || defBy.get(String(oppName).split(' ').pop())) : null;
|
||||
|
||||
const pitKey = prop && prop.opposing_pitcher ? nameKey(prop.opposing_pitcher) : null;
|
||||
const pit = pitKey ? pitBy.get(pitKey) : null;
|
||||
|
||||
const sp = platBy.get(key);
|
||||
const splits = sp ? {
|
||||
vl: { pa: sp.vl_pa, atBats: sp.vl_ab, hits: sp.vl_hits },
|
||||
vr: { pa: sp.vr_pa, atBats: sp.vr_ab, hits: sp.vr_hits },
|
||||
} : null;
|
||||
|
||||
const ctx = {
|
||||
spray: sprayBy.get(key) || null,
|
||||
positionOaa: def && def.position_oaa ? def.position_oaa : null,
|
||||
bats,
|
||||
throws: pit && pit.throws ? String(pit.throws)[0] : null,
|
||||
pitcherHardHit: pit ? asFraction(pit.hard_hit_pct) : null,
|
||||
platoonSplits: splits,
|
||||
};
|
||||
// Nothing readable at all -> null, so the engine skips the factor block
|
||||
// entirely rather than walking an empty context.
|
||||
const anything = ctx.spray || ctx.pitcherHardHit !== null || ctx.platoonSplits;
|
||||
return anything ? ctx : null;
|
||||
};
|
||||
|
||||
resolver.__stats = {
|
||||
spray_players: sprayBy.size,
|
||||
defense_teams: defBy.size,
|
||||
platoon_players: platBy.size,
|
||||
pitcher_profiles: pitBy.size,
|
||||
batter_profiles: batBy.size,
|
||||
};
|
||||
return resolver;
|
||||
}
|
||||
|
||||
module.exports = { build, MIN_ROWS };
|
||||
@@ -0,0 +1,127 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* hitsFactors — the three PROVEN hits factors, applied to the forecast.
|
||||
*
|
||||
* These passed the two-part gate (they move the prediction AND improve
|
||||
* out-of-sample Brier) and then sat unwired: `sprayDefense.js` and
|
||||
* `platoonSeverity.js` were required by nothing in `src/`, and the served
|
||||
* `p_win` read four inputs, none of them these. They were computed downstream of
|
||||
* the grade they should inform.
|
||||
*
|
||||
* ── PIPELINE ORDER IS A CORRECTNESS PROPERTY ─────────────────────────────
|
||||
* base rate -> FACTORS -> CALIBRATE -> GRADE
|
||||
* Calibration must always correct the factor-adjusted number. Reversing it would
|
||||
* calibrate a forecast that is not the one served.
|
||||
*
|
||||
* ── EVERY UNREADABLE GUARD FROM THE ORIGINAL PROOFS SURVIVES ─────────────
|
||||
* A factor applies only where it proved. A switch hitter has no readable spray
|
||||
* side; a thin platoon split is refused rather than shrunk to a league guess; a
|
||||
* pitcher with no contact profile contributes nothing. In each case the factor
|
||||
* returns NULL and the forecast is left alone — never nudged toward a default,
|
||||
* which would be fabricating a read from an absence.
|
||||
*/
|
||||
|
||||
const sd = require('./sprayDefense');
|
||||
const pss = require('./platoonSeverity');
|
||||
const { knownNumber, knownRate } = require('../../utils/known');
|
||||
|
||||
/** League mean hard-hit rate allowed; the pitcher factor is signed off this. */
|
||||
const LEAGUE_HARD_HIT = 0.389;
|
||||
/** Bound on the pitcher-contact adjustment, as proved. */
|
||||
const PITCHER_MAX = 0.15;
|
||||
/** Bound on the composed adjustment — no stack of three may run away. */
|
||||
const COMBINED_MAX = 0.25;
|
||||
|
||||
/**
|
||||
* A contact-allowing arm concedes better contact. Null without a profile.
|
||||
*/
|
||||
function pitcherContactMultiplier(hardHitAllowed) {
|
||||
const h = knownRate(hardHitAllowed);
|
||||
if (h === null) return null;
|
||||
return 1 + Math.max(-PITCHER_MAX, Math.min(PITCHER_MAX, (h - LEAGUE_HARD_HIT) * 1.2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose the three factors for one hits prop.
|
||||
*
|
||||
* @param {object} ctx
|
||||
* spray batter_spray row (pull/straight/oppo x gb/air)
|
||||
* positionOaa opposing team's per-position OAA
|
||||
* bats 'R' | 'L' | 'S'
|
||||
* throws opposing starter's hand
|
||||
* pitcherHardHit opposing starter's hard-hit rate allowed
|
||||
* platoonSplits { vl, vr } for this hitter
|
||||
* @returns {object} { multiplier, applied[], skipped[] } — multiplier is 1 when
|
||||
* nothing is readable, which is a no-op rather than a claim.
|
||||
*/
|
||||
function hitsFactorMultiplier(ctx = {}) {
|
||||
const applied = [];
|
||||
const skipped = [];
|
||||
let mult = 1;
|
||||
|
||||
// ── defense_by_direction ──
|
||||
const spray = ctx.spray;
|
||||
const posOaa = ctx.positionOaa;
|
||||
if (!spray || !posOaa || !ctx.bats) {
|
||||
skipped.push({ factor: 'defense_by_direction', reason: 'no spray profile or positional defence' });
|
||||
} else {
|
||||
const out = sd.sprayDefenseMultiplier({ spray, bats: ctx.bats, positionOaa: posOaa });
|
||||
if (!out || !Number.isFinite(out.multiplier)) {
|
||||
// Switch hitters land here: he bats opposite by choice, so the SIDE of the
|
||||
// field his contact goes to is not determined pre-game.
|
||||
skipped.push({ factor: 'defense_by_direction', reason: 'unreadable (switch hitter or no covered zone)' });
|
||||
} else {
|
||||
mult *= out.multiplier;
|
||||
applied.push({ factor: 'defense_by_direction', multiplier: round4(out.multiplier), coverage: out.coverage });
|
||||
}
|
||||
}
|
||||
|
||||
// ── pitcher_contact_profile ──
|
||||
const pm = pitcherContactMultiplier(ctx.pitcherHardHit);
|
||||
if (pm === null) {
|
||||
skipped.push({ factor: 'pitcher_contact_profile', reason: 'no pitcher contact profile' });
|
||||
} else {
|
||||
mult *= pm;
|
||||
applied.push({ factor: 'pitcher_contact_profile', multiplier: round4(pm) });
|
||||
}
|
||||
|
||||
// ── platoon_severity ──
|
||||
if (!ctx.platoonSplits || !ctx.bats || !ctx.throws) {
|
||||
skipped.push({ factor: 'platoon_severity', reason: 'no splits or no pitcher hand' });
|
||||
} else {
|
||||
const out = pss.platoonRead({ splits: ctx.platoonSplits, bats: ctx.bats, throws: ctx.throws });
|
||||
if (!out || !out.readable || !Number.isFinite(out.multiplier)) {
|
||||
// A thin split is REFUSED, not shrunk — a heavily-shrunk severity is
|
||||
// indistinguishable from a measured league-average one.
|
||||
skipped.push({ factor: 'platoon_severity', reason: (out && out.reason) || 'unreadable split' });
|
||||
} else {
|
||||
mult *= out.multiplier;
|
||||
applied.push({ factor: 'platoon_severity', multiplier: round4(out.multiplier), split: out.observed_split });
|
||||
}
|
||||
}
|
||||
|
||||
const bounded = Math.max(1 - COMBINED_MAX, Math.min(1 + COMBINED_MAX, mult));
|
||||
return {
|
||||
multiplier: round4(bounded),
|
||||
unbounded: round4(mult),
|
||||
applied,
|
||||
skipped,
|
||||
factors_fired: applied.length,
|
||||
};
|
||||
}
|
||||
|
||||
/** Apply to a probability, clamped into a usable range. Null in, null out. */
|
||||
function adjustProbability(p, ctx = {}) {
|
||||
const raw = knownNumber(p);
|
||||
if (raw === null) return { p_adjusted: null, ...hitsFactorMultiplier(ctx) };
|
||||
const f = hitsFactorMultiplier(ctx);
|
||||
return { p_adjusted: round4(Math.max(0.01, Math.min(0.99, raw * f.multiplier))), p_base: round4(raw), ...f };
|
||||
}
|
||||
|
||||
const round4 = (v) => (v == null || !Number.isFinite(v) ? null : Math.round(v * 10000) / 10000);
|
||||
|
||||
module.exports = {
|
||||
hitsFactorMultiplier, adjustProbability, pitcherContactMultiplier,
|
||||
LEAGUE_HARD_HIT, PITCHER_MAX, COMBINED_MAX,
|
||||
};
|
||||
@@ -451,7 +451,26 @@ async function runSnapshot(sport, opts = {}) {
|
||||
// Grade the slate via the existing service; capture the envelope instead of
|
||||
// letting it write (we re-write an ENRICHED version below).
|
||||
let envelope = null;
|
||||
// ── PROVEN FACTORS, LOADED BEFORE THE GRADE ─────────────────────────────
|
||||
// This ordering IS the fix. The same inputs were previously read at line 640+,
|
||||
// downstream of the grade they should inform, so three proven factors never
|
||||
// once moved a served number. Best-effort: a failed load means the slate is
|
||||
// graded unadjusted, exactly as before.
|
||||
let factorContext = null;
|
||||
if (sp === 'mlb') {
|
||||
try {
|
||||
const ctxSvc = deps.hitsFactorContext || require('./model/hitsFactorContext');
|
||||
const sbc = require('../utils/supabase').getSupabaseServiceClient();
|
||||
factorContext = sbc ? await ctxSvc.build(sbc) : null;
|
||||
if (factorContext) console.log(`[factors] ${sp} hits context loaded — ${JSON.stringify(factorContext.__stats)}`);
|
||||
else console.log(`[factors] ${sp} — no factor context; grading unadjusted`);
|
||||
} catch (e) {
|
||||
console.warn('[factors] context skipped:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
await deps.gradeAndCacheSlate(sp, props, {
|
||||
factorContext,
|
||||
// Bisect hook (2026-08-01): lets the internal trigger run a bounded slate
|
||||
// without a prod env change, so a cap regression can be isolated by
|
||||
// measurement instead of guessed at. Omitted => gradeSlateService's own
|
||||
|
||||
Reference in New Issue
Block a user