T-85: M1.4c — analyzeSignal / runEventStudy study runners in @stock-bot/research: analyzer math + automatic trial_ledger write + cluster N̂-deflated Sharpe (no new dependencies)
This commit is contained in:
parent
45a98d309e
commit
9d8dfc9bc8
4 changed files with 299 additions and 13 deletions
|
|
@ -470,6 +470,16 @@ describe('analyzeSignal', () => {
|
|||
),
|
||||
'invalid_option'
|
||||
);
|
||||
await expectStudyError(
|
||||
() =>
|
||||
analyzeSignal(
|
||||
executor,
|
||||
makeSignalRequest({
|
||||
ledgerSeries: null as unknown as 'rank_ic',
|
||||
})
|
||||
),
|
||||
'invalid_option'
|
||||
);
|
||||
await expectStudyError(
|
||||
() => analyzeSignal(executor, makeSignalRequest({ dsrGate: Number.NaN })),
|
||||
'invalid_option'
|
||||
|
|
@ -495,9 +505,60 @@ describe('analyzeSignal', () => {
|
|||
'invalid_panel'
|
||||
);
|
||||
|
||||
// Missing or malformed cross-section vectors must throw SignalStudyError('invalid_panel'),
|
||||
// never a raw TypeError, and issue zero SQL queries.
|
||||
const missingForwardReturnPanel = [
|
||||
{ date: '2025-03-05', ids: [1, 2, 3], signal: [1, 2, 3] },
|
||||
] as unknown as SignalCrossSection[];
|
||||
await expectStudyError(
|
||||
() => analyzeSignal(executor, makeSignalRequest({ panel: missingForwardReturnPanel })),
|
||||
'invalid_panel'
|
||||
);
|
||||
const nullSlicePanel = [null] as unknown as SignalCrossSection[];
|
||||
await expectStudyError(
|
||||
() => analyzeSignal(executor, makeSignalRequest({ panel: nullSlicePanel })),
|
||||
'invalid_panel'
|
||||
);
|
||||
|
||||
expect(calls).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('supports typed-array panel vectors and falls back conservatively to count-based DSR across cluster trials', async () => {
|
||||
const { executor, rows } = createInMemoryTrialExecutor();
|
||||
const dates = makeBusinessDates('2025-03-03', 12);
|
||||
const typedPanel: SignalCrossSection[] = makeSignalPanel(dates, 12, 111).map(slice => ({
|
||||
date: slice.date,
|
||||
ids: Int32Array.from(slice.ids as number[]),
|
||||
signal: slice.signal,
|
||||
forwardReturn: slice.forwardReturn,
|
||||
}));
|
||||
|
||||
const first = await analyzeSignal(executor, makeSignalRequest({ panel: typedPanel }));
|
||||
const second = await analyzeSignal(
|
||||
executor,
|
||||
makeSignalRequest({
|
||||
panel: typedPanel,
|
||||
expression: {
|
||||
expression: 'rank(ret_10d)',
|
||||
params: { lookback: 10 },
|
||||
universe: 'US_EQ_TOP3000',
|
||||
horizon: '5d',
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
expect(first.status).toBe('ok');
|
||||
expect(second.status).toBe('ok');
|
||||
if (first.status !== 'ok' || second.status !== 'ok') {
|
||||
return;
|
||||
}
|
||||
expect(rows).toHaveLength(2);
|
||||
expect(second.deflatedSharpe.clusterSize).toBe(2);
|
||||
expect(second.deflatedSharpe.pairsUsed).toBe(0);
|
||||
expect(second.deflatedSharpe.pairsSkipped).toBe(1);
|
||||
expect(second.deflatedSharpe.effectiveTrials).toBe(2);
|
||||
});
|
||||
|
||||
it('does not mutate frozen caller inputs', async () => {
|
||||
const { executor } = createInMemoryTrialExecutor();
|
||||
const dates = makeBusinessDates('2025-03-03', 10);
|
||||
|
|
@ -624,6 +685,40 @@ describe('runEventStudy', () => {
|
|||
Array.from(res2.study.clustered.meanCars)
|
||||
);
|
||||
expect(store1.rows[0]?.daily_series).toEqual(store2.rows[0]?.daily_series);
|
||||
|
||||
// Non-finite returns (e.g. Infinity from zero prior price) in the excluded Vault tail
|
||||
// must not fail the pre-Vault study or alter its outputs or ledger values.
|
||||
const infiniteTailReturns = new Map<number, Float64Array>();
|
||||
for (const [id, series] of baseInput.entityReturns.entries()) {
|
||||
const copy = new Float64Array(series);
|
||||
copy[cutoffIdx] = Number.POSITIVE_INFINITY;
|
||||
copy[copy.length - 1] = Number.NEGATIVE_INFINITY;
|
||||
infiniteTailReturns.set(id, copy);
|
||||
}
|
||||
const infiniteTailBenchmark = new Float64Array(baseInput.benchmarkReturns!);
|
||||
infiniteTailBenchmark[cutoffIdx] = Number.POSITIVE_INFINITY;
|
||||
const store3 = createInMemoryTrialExecutor();
|
||||
const res3 = await runEventStudy(
|
||||
store3.executor,
|
||||
makeEventStudyRequest({
|
||||
input: {
|
||||
...baseInput,
|
||||
entityReturns: infiniteTailReturns,
|
||||
benchmarkReturns: infiniteTailBenchmark,
|
||||
},
|
||||
options: { preDays: 3, postDays, minEvents: 5 },
|
||||
})
|
||||
);
|
||||
expect(res3.status).toBe('ok');
|
||||
if (res3.status !== 'ok') {
|
||||
return;
|
||||
}
|
||||
expect(res3.vaultExcludedEvents).toBe(3);
|
||||
expect(res3.trial.expressionHash).toBe(res1.trial.expressionHash);
|
||||
expect(Array.from(res3.study.clustered.meanCars)).toEqual(
|
||||
Array.from(res1.study.clustered.meanCars)
|
||||
);
|
||||
expect(store3.rows[0]?.daily_series).toEqual(store1.rows[0]?.daily_series);
|
||||
});
|
||||
|
||||
it('returns insufficient_data with zero queries for below_min_events, too_few_event_dates, and zero-variance clustered CARs', async () => {
|
||||
|
|
@ -768,6 +863,16 @@ describe('runEventStudy', () => {
|
|||
),
|
||||
'invalid_option'
|
||||
);
|
||||
await expectStudyError(
|
||||
() =>
|
||||
runEventStudy(
|
||||
executor,
|
||||
makeEventStudyRequest({
|
||||
options: { postDays: null as unknown as number },
|
||||
})
|
||||
),
|
||||
'invalid_option'
|
||||
);
|
||||
await expectStudyError(
|
||||
() =>
|
||||
runEventStudy(
|
||||
|
|
@ -782,6 +887,21 @@ describe('runEventStudy', () => {
|
|||
),
|
||||
'invalid_panel'
|
||||
);
|
||||
// Shape validation against the full calendar is preserved even when Vault dates exist.
|
||||
await expectStudyError(
|
||||
() =>
|
||||
runEventStudy(
|
||||
executor,
|
||||
makeEventStudyRequest({
|
||||
input: {
|
||||
sessionDates: ['2025-04-29', '2025-04-30', '2025-05-01'],
|
||||
entityReturns: new Map([[1, new Float64Array([0.01, 0.02])]]),
|
||||
events: [],
|
||||
},
|
||||
})
|
||||
),
|
||||
'invalid_panel'
|
||||
);
|
||||
expect(calls).toHaveLength(0);
|
||||
|
||||
// Frozen immutability test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue