Files
vyndr/src/services/model/hitsFactorContext.js
T
builtbykev 43f65d30cb 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
2026-08-07 02:53:48 -04:00

145 lines
5.4 KiB
JavaScript

'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 };