T-83: M1.4b — Pure event-study math in @stock-bot/calculations: abnormal-return fan (AAR/CAAR) around events, date-clustered Newey-West t-stat and pre/post-2023 split (no I/O)

This commit is contained in:
Studio 2026-11-28 02:23:06 -05:00
parent 1a2396916e
commit c4c99d6e8c
2 changed files with 168 additions and 37 deletions

View file

@ -149,7 +149,16 @@ export interface EventStudyRecencyGate {
readonly ratio: number | null;
}
/** Pre/post recency split summaries and sign-preserving gate decision on date-clustered CARs. */
/**
* Pre/post recency split summaries and sign-preserving gate decision on date-clustered CARs.
*
* Cohort assignment follows the event session date (`sessionDates[dayIndex] < splitDate` vs
* `>= splitDate`): each event's complete `CAR[0..postDays]` belongs to its event-date cohort.
* Consequently, a late pre-split event whose post-event window extends into the post-split
* calendar period contributes those returns to `pre`. Callers requiring strictly disjoint
* calendar return periods should exclude pre-split events whose `dayIndex + postDays` session
* falls on or after `splitDate`.
*/
export interface EventStudyRecencyResult {
readonly splitDate: string;
readonly minRatio: number;
@ -1010,6 +1019,9 @@ export function eventStudy(
};
// Sign-preserving recency gate reusing T-82's evaluateRecencyGate.
// Cohorts are split by event session date (`clusteredDates[i] < recencySplitDate` vs `>=`);
// each complete CAR[0..postDays] stays with its event date. Callers needing strictly disjoint
// return periods must pre-filter pre-split events whose post-event window reaches the split.
const rawRecency = evaluateRecencyGate(clusteredDates, clusteredMeanCars, {
recencySplitDate,
recencyMinRatio,

View file

@ -437,16 +437,28 @@ describe('eventStudy hand-computed golden fixture', () => {
const expectedCaar = [0.025, 0.0575, 0.0925, 0.125];
const expectedCaarZero = [null, 0.0325, 0.0675, 0.1];
// Per-event running CAR[-1..k] at k = +2:
// E1 (A,2): 0.02+0.03+0.04+0.05 = 0.14
// E2 (B,2): 0.01+0.02+0.01+0.03 = 0.07
// E3 (C,7): 0.03+0.02+0.01+0.04 = 0.10
// E4 (A,8): 0.04+0.06+0.08+0.01 = 0.19
// Per-event CAR[0..+2]:
// E1: 0.03+0.04+0.05 = 0.12
// E2: 0.02+0.01+0.03 = 0.06
// E3: 0.02+0.01+0.04 = 0.07
// E4: 0.06+0.08+0.01 = 0.15
// Per-event running CAR[-1..k]:
// k = -1: [0.02, 0.01, 0.03, 0.04] -> sorted [0.01, 0.02, 0.03, 0.04]
// k = 0: [0.05, 0.03, 0.05, 0.10] -> sorted [0.03, 0.05, 0.05, 0.10]
// k = +1: [0.09, 0.04, 0.06, 0.18] -> sorted [0.04, 0.06, 0.09, 0.18]
// k = +2: [0.14, 0.07, 0.10, 0.19] -> sorted [0.07, 0.10, 0.14, 0.19]
const expectedCarPercentiles = [
{ p5: 0.0115, p25: 0.0175, p50: 0.025, p75: 0.0325, p95: 0.0385 },
{ p5: 0.033, p25: 0.045, p50: 0.05, p75: 0.0625, p95: 0.0925 },
{ p5: 0.043, p25: 0.055, p50: 0.075, p75: 0.1125, p95: 0.1665 },
{ p5: 0.0745, p25: 0.0925, p50: 0.12, p75: 0.1525, p95: 0.1825 },
];
// Per-event running post-event CAR[0..k]:
// k = 0: [0.03, 0.02, 0.02, 0.06] -> sorted [0.02, 0.02, 0.03, 0.06]
// k = +1: [0.07, 0.03, 0.03, 0.14] -> sorted [0.03, 0.03, 0.07, 0.14]
// k = +2: [0.12, 0.06, 0.07, 0.15] -> sorted [0.06, 0.07, 0.12, 0.15]
const expectedPostCarPercentiles = [
null,
{ p5: 0.02, p25: 0.02, p50: 0.025, p75: 0.0375, p95: 0.0555 },
{ p5: 0.03, p25: 0.03, p50: 0.05, p75: 0.0875, p95: 0.1295 },
{ p5: 0.0615, p25: 0.0675, p50: 0.095, p75: 0.1275, p95: 0.1455 },
];
for (let w = 0; w < 4; w++) {
const off = res.offsets[w]!;
expect(off.offset).toBe(w - 1);
@ -454,23 +466,30 @@ describe('eventStudy hand-computed golden fixture', () => {
expect(Math.abs(off.aar - expectedAar[w]!)).toBeLessThan(1e-12);
expect(Math.abs(off.standardError - expectedSe[w]!)).toBeLessThan(1e-12);
expect(Math.abs(off.caar - expectedCaar[w]!)).toBeLessThan(1e-12);
if (expectedCaarZero[w] === null) {
const expCarP = expectedCarPercentiles[w]!;
expect(Math.abs(off.carPercentiles.p5 - expCarP.p5)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p25 - expCarP.p25)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p50 - expCarP.p50)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p75 - expCarP.p75)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p95 - expCarP.p95)).toBeLessThan(1e-12);
const expPostP = expectedPostCarPercentiles[w]!;
if (expectedCaarZero[w] === null || expPostP === null) {
expect(off.caarFromZero).toBeNull();
expect(off.postCarPercentiles).toBeNull();
} else {
expect(Math.abs((off.caarFromZero as number) - expectedCaarZero[w]!)).toBeLessThan(1e-12);
expect(off.postCarPercentiles).not.toBeNull();
const postP = off.postCarPercentiles!;
expect(Math.abs(postP.p5 - expPostP.p5)).toBeLessThan(1e-12);
expect(Math.abs(postP.p25 - expPostP.p25)).toBeLessThan(1e-12);
expect(Math.abs(postP.p50 - expPostP.p50)).toBeLessThan(1e-12);
expect(Math.abs(postP.p75 - expPostP.p75)).toBeLessThan(1e-12);
expect(Math.abs(postP.p95 - expPostP.p95)).toBeLessThan(1e-12);
}
}
// Check final offset percentiles on sorted full CARs [0.07, 0.10, 0.14, 0.19]
const lastFullP = res.offsets[3]!.carPercentiles;
expect(Math.abs(lastFullP.p5 - (0.07 + 0.15 * 0.03))).toBeLessThan(1e-12);
expect(Math.abs(lastFullP.p25 - (0.07 + 0.75 * 0.03))).toBeLessThan(1e-12);
expect(Math.abs(lastFullP.p50 - 0.12)).toBeLessThan(1e-12);
expect(Math.abs(lastFullP.p75 - (0.14 + 0.25 * 0.05))).toBeLessThan(1e-12);
expect(Math.abs(lastFullP.p95 - (0.14 + 0.85 * 0.05))).toBeLessThan(1e-12);
// Per-event CAR[0..+2] in canonical (dayIndex ASC, entityId ASC) order
expect(res.eventCars.entityIds).toEqual(['A', 'B', 'C', 'A']);
expect(Array.from(res.eventCars.dayIndices)).toEqual([2, 2, 7, 8]);
@ -507,7 +526,7 @@ describe('eventStudy hand-computed golden fixture', () => {
assertNoNaNDeep(res);
});
test('matches hand-computed AAR, CAAR, and per-event CAR with benchmark to 1e-12', () => {
test('matches hand-computed AAR, SE, CAAR, percentiles, CAR, and clustered stats with benchmark to 1e-12', () => {
const res = eventStudy(
{ sessionDates, entityReturns, benchmarkReturns: bench, events },
{ preDays: 1, postDays: 2, minEvents: 4, neweyWestLag: 0 }
@ -519,34 +538,73 @@ describe('eventStudy hand-computed golden fixture', () => {
}
expect(res.hasBenchmark).toBe(true);
expect(res.offsets.length).toBe(4);
// Benchmark values at each event's offsets:
// Offset -1: bench[1]=0.005, bench[1]=0.005, bench[6]=0.005, bench[7]=0.01 -> mean bench = 0.00625
// Offset 0: bench[2]=0.01, bench[2]=0.01, bench[7]=0.01, bench[8]=0.01 -> mean bench = 0.01
// Offset +1: bench[3]=0.01, bench[3]=0.01, bench[8]=0.01, bench[9]=0.01 -> mean bench = 0.01
// Offset +2: bench[4]=0.01, bench[4]=0.01, bench[9]=0.01, bench[10]=0.005 -> mean bench = 0.00875
const expectedAar = [0.025 - 0.00625, 0.0325 - 0.01, 0.035 - 0.01, 0.0325 - 0.00875];
const expectedCaar = [
expectedAar[0]!,
expectedAar[0]! + expectedAar[1]!,
expectedAar[0]! + expectedAar[1]! + expectedAar[2]!,
expectedAar[0]! + expectedAar[1]! + expectedAar[2]! + expectedAar[3]!,
// Abnormal returns AR = r - bench at each offset:
// Offset -1: [0.015, 0.005, 0.025, 0.030] -> mean = 0.01875, sumSq = 0.00036875
// Offset 0: [0.020, 0.010, 0.010, 0.050] -> mean = 0.0225, sumSq = 0.001075
// Offset +1: [0.030, 0.000, 0.000, 0.070] -> mean = 0.025, sumSq = 0.0033
// Offset +2: [0.040, 0.020, 0.030, 0.005] -> mean = 0.02375, sumSq = 0.00066875
const expectedAar = [0.01875, 0.0225, 0.025, 0.02375];
const expectedSe = [
Math.sqrt(0.00036875 / 12),
Math.sqrt(0.001075 / 12),
Math.sqrt(0.0033 / 12),
Math.sqrt(0.00066875 / 12),
];
const expectedCaarZero = [
const expectedCaar = [0.01875, 0.04125, 0.06625, 0.09];
const expectedCaarZero = [null, 0.0225, 0.0475, 0.07125];
// Per-event running benchmark-adjusted CAR[-1..k]:
// k = -1: [0.015, 0.005, 0.025, 0.030] -> sorted [0.005, 0.015, 0.025, 0.030]
// k = 0: [0.035, 0.015, 0.035, 0.080] -> sorted [0.015, 0.035, 0.035, 0.080]
// k = +1: [0.065, 0.015, 0.035, 0.150] -> sorted [0.015, 0.035, 0.065, 0.150]
// k = +2: [0.105, 0.035, 0.065, 0.155] -> sorted [0.035, 0.065, 0.105, 0.155]
const expectedCarPercentiles = [
{ p5: 0.0065, p25: 0.0125, p50: 0.02, p75: 0.02625, p95: 0.02925 },
{ p5: 0.018, p25: 0.03, p50: 0.035, p75: 0.04625, p95: 0.07325 },
{ p5: 0.018, p25: 0.03, p50: 0.05, p75: 0.08625, p95: 0.13725 },
{ p5: 0.0395, p25: 0.0575, p50: 0.085, p75: 0.1175, p95: 0.1475 },
];
// Per-event running benchmark-adjusted post-event CAR[0..k]:
// k = 0: [0.02, 0.01, 0.01, 0.05] -> sorted [0.01, 0.01, 0.02, 0.05]
// k = +1: [0.05, 0.01, 0.01, 0.12] -> sorted [0.01, 0.01, 0.05, 0.12]
// k = +2: [0.09, 0.03, 0.04, 0.125] -> sorted [0.03, 0.04, 0.09, 0.125]
const expectedPostCarPercentiles = [
null,
expectedAar[1]!,
expectedAar[1]! + expectedAar[2]!,
expectedAar[1]! + expectedAar[2]! + expectedAar[3]!,
{ p5: 0.01, p25: 0.01, p50: 0.015, p75: 0.0275, p95: 0.0455 },
{ p5: 0.01, p25: 0.01, p50: 0.03, p75: 0.0675, p95: 0.1095 },
{ p5: 0.0315, p25: 0.0375, p50: 0.065, p75: 0.09875, p95: 0.11975 },
];
for (let w = 0; w < 4; w++) {
const off = res.offsets[w]!;
expect(off.offset).toBe(w - 1);
expect(off.n).toBe(4);
expect(Math.abs(off.aar - expectedAar[w]!)).toBeLessThan(1e-12);
expect(Math.abs(off.standardError - expectedSe[w]!)).toBeLessThan(1e-12);
expect(Math.abs(off.caar - expectedCaar[w]!)).toBeLessThan(1e-12);
if (expectedCaarZero[w] === null) {
const expCarP = expectedCarPercentiles[w]!;
expect(Math.abs(off.carPercentiles.p5 - expCarP.p5)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p25 - expCarP.p25)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p50 - expCarP.p50)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p75 - expCarP.p75)).toBeLessThan(1e-12);
expect(Math.abs(off.carPercentiles.p95 - expCarP.p95)).toBeLessThan(1e-12);
const expPostP = expectedPostCarPercentiles[w]!;
if (expectedCaarZero[w] === null || expPostP === null) {
expect(off.caarFromZero).toBeNull();
expect(off.postCarPercentiles).toBeNull();
} else {
expect(Math.abs((off.caarFromZero as number) - expectedCaarZero[w]!)).toBeLessThan(1e-12);
expect(off.postCarPercentiles).not.toBeNull();
const postP = off.postCarPercentiles!;
expect(Math.abs(postP.p5 - expPostP.p5)).toBeLessThan(1e-12);
expect(Math.abs(postP.p25 - expPostP.p25)).toBeLessThan(1e-12);
expect(Math.abs(postP.p50 - expPostP.p50)).toBeLessThan(1e-12);
expect(Math.abs(postP.p75 - expPostP.p75)).toBeLessThan(1e-12);
expect(Math.abs(postP.p95 - expPostP.p95)).toBeLessThan(1e-12);
}
}
@ -561,10 +619,27 @@ describe('eventStudy hand-computed golden fixture', () => {
}
// Clustered: day 2 -> mean(0.09, 0.03) = 0.06, day 7 -> 0.04, day 8 -> 0.125
expect(res.clustered.nDates).toBe(3);
expect(res.clustered.dates).toEqual(['2022-01-05', '2023-01-04', '2023-01-05']);
expect(Array.from(res.clustered.eventCounts)).toEqual([2, 1, 1]);
const expectedClustered = [0.06, 0.04, 0.125];
for (let i = 0; i < 3; i++) {
expect(Math.abs(res.clustered.meanCars[i]! - expectedClustered[i]!)).toBeLessThan(1e-12);
}
expect(Math.abs(res.clustered.carMean - 0.075)).toBeLessThan(1e-12);
const expectedNw = neweyWestMeanTest(expectedClustered, 0);
expect(res.clustered.neweyWest.status).toBe('ok');
expect(expectedNw.status).toBe('ok');
if (res.clustered.neweyWest.status === 'ok' && expectedNw.status === 'ok') {
expect(res.clustered.neweyWest.lag).toBe(expectedNw.lag);
expect(
Math.abs(res.clustered.neweyWest.longRunVariance - expectedNw.longRunVariance)
).toBeLessThan(1e-12);
expect(
Math.abs(res.clustered.neweyWest.standardError - expectedNw.standardError)
).toBeLessThan(1e-12);
expect(Math.abs(res.clustered.neweyWest.tStat - expectedNw.tStat)).toBeLessThan(1e-12);
}
assertNoNaNDeep(res);
});
});
@ -833,6 +908,50 @@ describe('eventStudy recency gate', () => {
expect(emptyPost.gate.pass).toBe(false);
expect(emptyPost.gate.reason).toBe('empty_post_window');
});
test('assigns complete CAR[0..postDays] to its event-date cohort across the split boundary', () => {
// Late-2022 event on 2022-12-30 has postDays=2 spanning 2023-01-03 and 2023-01-04.
// Its full CAR[0..2] (0.01 + 0.02 + 0.03 = 0.06) is assigned to the pre-2023 cohort by event date.
const sessionDates = [
'2022-12-29',
'2022-12-30',
'2023-01-03',
'2023-01-04',
'2023-01-05',
'2023-01-06',
];
const series = new Float64Array([0.0, 0.01, 0.02, 0.03, 0.01, 0.0]);
const res = eventStudy(
{
sessionDates,
entityReturns: new Map([[1, series]]),
events: [
{ entityId: 1, dayIndex: 1 }, // 2022-12-30 (< 2023-01-01): CAR[0..2] = 0.01+0.02+0.03 = 0.06
{ entityId: 1, dayIndex: 2 }, // 2023-01-03 (>= 2023-01-01): CAR[0..2] = 0.02+0.03+0.01 = 0.06
],
},
{
preDays: 0,
postDays: 2,
minEvents: 2,
recencySplitDate: RECENCY_SPLIT_DATE,
recencyMinRatio: RECENCY_MIN_RATIO,
}
);
expect(res.status).toBe('ok');
if (res.status !== 'ok') {
return;
}
expect(res.recency.pre.count).toBe(1);
expect(res.recency.post.count).toBe(1);
expect(Math.abs((res.recency.pre.mean as number) - 0.06)).toBeLessThan(1e-12);
expect(Math.abs((res.recency.post.mean as number) - 0.06)).toBeLessThan(1e-12);
expect(res.recency.gate.pass).toBe(true);
expect(res.recency.gate.reason).toBe('passed');
expect(Math.abs((res.recency.gate.ratio as number) - 1.0)).toBeLessThan(1e-12);
});
});
describe('eventStudy insufficient data, zero variance, and validation errors', () => {