import { type EventObservation, type EventStudyExchangeCalendars, buildEventStudyInput, } from '../src/event-panel'; import type { BarRecord } from '../src/market-data-store'; const SEED = 20261129; const SESSIONS = 5_040; const LISTINGS = 3_000; const EVENT_STRIDE_SESSIONS = 63; // 5040 / 63 = 80 filings/listing -> 240,000 total events const PRE_CUTOFF_SESSIONS = 5_020; function createMulberry32(seed: number): () => number { let state = seed >>> 0; return () => { state = (state + 0x6d2b79f5) >>> 0; let t = Math.imul(state ^ (state >>> 15), 1 | state); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296; }; } function isLeapYear(year: number): boolean { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } const DAYS_IN_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] as const; function generateBusinessCalendar(count: number, startYear: number): { dates: string[]; closeTs: Float64Array; afterCloseTs: Float64Array; } { const dates: string[] = new Array(count); const closeTs = new Float64Array(count); const afterCloseTs = new Float64Array(count); let year = startYear; let month = 1; let day = 1; // 2006-01-01 was a Sunday (dow = 0) let dow = 0; let produced = 0; while (produced < count) { if (dow !== 0 && dow !== 6) { const yyyy = String(year).padStart(4, '0'); const mm = String(month).padStart(2, '0'); const dd = String(day).padStart(2, '0'); dates[produced] = `${yyyy}-${mm}-${dd}`; closeTs[produced] = Date.UTC(year, month - 1, day, 21, 0, 0, 0); // 16:00 ET afterCloseTs[produced] = Date.UTC(year, month - 1, day, 22, 0, 0, 0); // 17:00 ET produced += 1; } dow = (dow + 1) % 7; day += 1; const baseDays = DAYS_IN_MONTH[month] ?? 31; const maxDay = month === 2 && isLeapYear(year) ? 29 : baseDays; if (day > maxDay) { day = 1; month += 1; if (month > 12) { month = 1; year += 1; } } } return { dates, closeTs, afterCloseTs }; } function fnv1aByte(hash: number, byte: number): number { return Math.imul((hash ^ (byte & 0xff)) >>> 0, 0x01000193) >>> 0; } function fnv1aUint32(hash: number, value: number): number { const v = value >>> 0; let h = fnv1aByte(hash, v & 0xff); h = fnv1aByte(h, (v >>> 8) & 0xff); h = fnv1aByte(h, (v >>> 16) & 0xff); h = fnv1aByte(h, (v >>> 24) & 0xff); return h; } function fnv1aBytes(hash: number, bytes: Uint8Array): number { let h = hash >>> 0; for (let i = 0; i < bytes.length; i += 1) { h = Math.imul((h ^ (bytes[i] as number)) >>> 0, 0x01000193) >>> 0; } return h; } const rng = createMulberry32(SEED); const calendar = generateBusinessCalendar(SESSIONS, 2006); const vaultCutoffDate = calendar.dates[PRE_CUTOFF_SESSIONS] as string; const totalBars = SESSIONS * LISTINGS; const bars: BarRecord[] = new Array(totalBars); const eventsPerListing = Math.ceil(SESSIONS / EVENT_STRIDE_SESSIONS); const totalEvents = LISTINGS * eventsPerListing; const events: EventObservation[] = new Array(totalEvents); let barIdx = 0; let evIdx = 0; for (let l = 0; l < LISTINGS; l += 1) { const listingId = l + 1; const subtype = l % 2 === 0 ? '10-q' : 'interim_financial_statements'; let price = 10 + (l % 200) * 0.5; for (let t = 0; t < SESSIONS; t += 1) { const ts = calendar.closeTs[t] as number; price = Math.max(1, price * (1 + (rng() - 0.4995) * 0.02)); bars[barIdx++] = { listingId, ts, open: price, high: price, low: price, close: price, volume: 10_000, vwap: price, knownAt: ts, }; if (t % EVENT_STRIDE_SESSIONS === 0) { events[evIdx++] = { listingId, knownAt: calendar.afterCloseTs[t] as number, kind: 'filing', subtype, }; } } } const sessionCloses = calendar.dates.map((date, idx) => ({ date, closeTs: calendar.closeTs[idx] as number, })); const listingCalendarId = new Map(); for (let l = 0; l < LISTINGS; l += 1) { listingCalendarId.set(l + 1, l % 2 === 0 ? 'XNYS' : 'XTSE'); } const exchangeCalendars: EventStudyExchangeCalendars = { calendars: new Map([ ['XNYS', sessionCloses], ['XTSE', sessionCloses], ]), listingCalendarId, }; // Timed section: pure buildEventStudyInput execution (no fixture generation or checksum). const started = performance.now(); const result = buildEventStudyInput({ bars, events, vaultCutoffDate, benchmark: 'equal_weight', exchangeCalendars, }); const elapsedMs = performance.now() - started; // Untimed verification pass: FNV-1a 32-bit checksum over lengths, diagnostics, returns, and events. let checksum = 0x811c9dc5; checksum = fnv1aUint32(checksum, result.input.sessionDates.length); checksum = fnv1aUint32(checksum, result.input.entityReturns.size); checksum = fnv1aUint32(checksum, result.input.events.length); checksum = fnv1aUint32(checksum, result.diagnostics.vaultExcludedDates); checksum = fnv1aUint32(checksum, result.diagnostics.vaultExcludedEvents); checksum = fnv1aUint32(checksum, result.diagnostics.droppedUnknownListing); checksum = fnv1aUint32(checksum, result.diagnostics.droppedAfterLastSession); checksum = fnv1aUint32(checksum, result.diagnostics.droppedMissingCalendarSession); checksum = fnv1aUint32(checksum, result.diagnostics.duplicateEvents); checksum = fnv1aUint32(checksum, result.diagnostics.duplicateBars); checksum = fnv1aUint32(checksum, result.diagnostics.nonPositiveCloseReturns); if (result.input.benchmarkReturns !== undefined) { const bench = result.input.benchmarkReturns as Float64Array; checksum = fnv1aBytes(checksum, new Uint8Array(bench.buffer, bench.byteOffset, bench.byteLength)); } for (const [entityId, series] of result.input.entityReturns) { checksum = fnv1aUint32(checksum, Number(entityId)); const arr = series as Float64Array; checksum = fnv1aBytes(checksum, new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)); } for (let i = 0; i < result.input.events.length; i += 1) { const ev = result.input.events[i]; if (ev === undefined) { continue; } checksum = fnv1aUint32(checksum, ev.dayIndex); checksum = fnv1aUint32(checksum, Number(ev.entityId)); } const checksumHex = checksum.toString(16).padStart(8, '0'); const EXPECTED_CHECKSUM = '01b356d3'; if (checksumHex !== EXPECTED_CHECKSUM) { throw new Error( `Benchmark checksum mismatch: expected ${EXPECTED_CHECKSUM}, got ${checksumHex}.` ); } const elapsedSec = elapsedMs / 1000; const barsPerSec = Math.round(totalBars / elapsedSec); const eventsPerSec = Math.round(totalEvents / elapsedSec); console.log( JSON.stringify( { seed: SEED, sessions: SESSIONS, listings: LISTINGS, vaultCutoffDate, inputBars: totalBars, inputEvents: totalEvents, sessionDates: result.input.sessionDates.length, anchoredEvents: result.input.events.length, elapsedMs: Number(elapsedMs.toFixed(2)), barsPerSec, eventsPerSec, diagnostics: result.diagnostics, checksum: checksumHex, }, null, 2 ) );