diff --git a/libs/calculations/src/analytics/signal-analysis.ts b/libs/calculations/src/analytics/signal-analysis.ts index dae0c60c..fa7ab4f3 100644 --- a/libs/calculations/src/analytics/signal-analysis.ts +++ b/libs/calculations/src/analytics/signal-analysis.ts @@ -37,6 +37,7 @@ export type SignalAnalysisErrorCode = | 'UNSORTED_DATES' | 'DUPLICATE_DATE' | 'DUPLICATE_ID' + | 'INVALID_ID' | 'INVALID_DATE' | 'INVALID_OPTION'; @@ -982,18 +983,22 @@ function buildRecencySplitResult( const pre = preStats.summary; const post = postStats.summary; + const preTol = zeroToleranceForMean(preStats.absSum, pre.count); + const postTol = zeroToleranceForMean(postStats.absSum, post.count); + let gate: RecencyGateResult; if (pre.count === 0 || pre.mean === null) { gate = { pass: false, reason: 'empty_pre_window', ratio: null }; } else if (post.count === 0 || post.mean === null) { gate = { pass: false, reason: 'empty_post_window', ratio: null }; - } else if (pre.mean <= zeroToleranceForMean(preStats.absSum, pre.count)) { + } else if (pre.mean <= preTol) { gate = { pass: false, reason: 'non_positive_pre_mean', ratio: null }; } else { const ratio = post.mean / pre.mean; - if (post.mean <= zeroToleranceForMean(postStats.absSum, post.count)) { + const ratioThresholdTol = postTol + minRatio * preTol; + if (post.mean <= postTol) { gate = { pass: false, reason: 'non_positive_post_mean', ratio }; - } else if (post.mean < minRatio * pre.mean) { + } else if (post.mean < minRatio * pre.mean - ratioThresholdTol) { gate = { pass: false, reason: 'post_below_ratio_threshold', ratio }; } else { gate = { pass: true, reason: 'passed', ratio }; @@ -1256,6 +1261,12 @@ export function analyzeSignalPanel( } seenEpoch[id] = idEpoch; } else { + if (typeof id === 'number' && Number.isNaN(id)) { + throw new SignalAnalysisError( + 'INVALID_ID', + `Entity id must not be NaN on ${slice.date} at index ${i}` + ); + } if (seenIds.has(id)) { throw new SignalAnalysisError( 'DUPLICATE_ID', diff --git a/libs/calculations/test/signal-analysis.test.ts b/libs/calculations/test/signal-analysis.test.ts index 0ca83b1b..5fcfd64e 100644 --- a/libs/calculations/test/signal-analysis.test.ts +++ b/libs/calculations/test/signal-analysis.test.ts @@ -580,6 +580,43 @@ describe('evaluateRecencyGate and vaultCutoffDate fence', () => { expect(resZeroPost.recency.gate.reason).toBe('non_positive_post_mean'); }); + test('passes exact 50% recency boundary despite floating-point summation rounding and rejects clearly below', () => { + // 5-name cross-sections with exact Spearman rank-ICs: + // [1, 4, 5, 2, 3] -> 0.2, [1, 2, 3, 5, 4] -> 0.9, [4, 5, 2, 3, 1] -> -0.8, [2, 5, 1, 4, 3] -> 0.1 + // Pre mean = 0.2; exact post mean = (0.2 + 0.9 - 0.8) / 3 = 0.1 (exact 50% of pre), + // even though compensated float arithmetic evaluates post mean to 0.09999999999999999. + const ids = [1, 2, 3, 4, 5]; + const signal = [1, 2, 3, 4, 5]; + const panelExactHalf: SignalCrossSection[] = [ + { date: '2022-12-30', ids, signal, forwardReturn: [1, 4, 5, 2, 3] }, + { date: '2023-01-03', ids, signal, forwardReturn: [1, 4, 5, 2, 3] }, + { date: '2023-01-04', ids, signal, forwardReturn: [1, 2, 3, 5, 4] }, + { date: '2023-01-05', ids, signal, forwardReturn: [4, 5, 2, 3, 1] }, + ]; + + const resExact = analyzeSignalPanel(panelExactHalf); + expect(Math.abs((resExact.rankIcSeries[0] as number) - 0.2)).toBeLessThan(1e-12); + expect(Math.abs((resExact.rankIcSeries[1] as number) - 0.2)).toBeLessThan(1e-12); + expect(Math.abs((resExact.rankIcSeries[2] as number) - 0.9)).toBeLessThan(1e-12); + expect(Math.abs((resExact.rankIcSeries[3] as number) - -0.8)).toBeLessThan(1e-12); + expect(resExact.recency.gate.pass).toBe(true); + expect(resExact.recency.gate.reason).toBe('passed'); + expect(Math.abs((resExact.recency.gate.ratio as number) - 0.5)).toBeLessThan(1e-12); + + // Clearly below 50%: pre mean = 0.2, post mean = (0.1 + 0.9 - 0.8) / 3 = 0.2 / 3 (~33.3%) + const panelBelowHalf: SignalCrossSection[] = [ + { date: '2022-12-30', ids, signal, forwardReturn: [1, 4, 5, 2, 3] }, + { date: '2023-01-03', ids, signal, forwardReturn: [2, 5, 1, 4, 3] }, + { date: '2023-01-04', ids, signal, forwardReturn: [1, 2, 3, 5, 4] }, + { date: '2023-01-05', ids, signal, forwardReturn: [4, 5, 2, 3, 1] }, + ]; + + const resBelow = analyzeSignalPanel(panelBelowHalf); + expect(resBelow.recency.gate.pass).toBe(false); + expect(resBelow.recency.gate.reason).toBe('post_below_ratio_threshold'); + expect(Math.abs((resBelow.recency.gate.ratio as number) - 1 / 3)).toBeLessThan(1e-12); + }); + test('vaultCutoffDate excludes dates >= cutoff, counts them, and isolates non-vault metrics', () => { const basePanel: SignalCrossSection[] = [ { @@ -786,6 +823,24 @@ describe('input validation errors', () => { expect(resWithDroppedDup.diagnostics.droppedPairs).toBe(1); }); + test('throws INVALID_ID on NaN numeric entity IDs in a tied cross-section', () => { + expectErrorCode( + () => + analyzeSignalPanel( + [ + { + date: '2023-01-03', + ids: [1, Number.NaN, 2, 3, 4], + signal: [1, 1, 1, 2, 3], + forwardReturn: [0.01, 0.05, -0.02, 0.03, 0.04], + }, + ], + { quantiles: 5 } + ), + 'INVALID_ID' + ); + }); + test('throws INVALID_OPTION on invalid quantiles, lag, minCrossSection, or recencyMinRatio', () => { expectErrorCode(() => analyzeSignalPanel([validSlice], { quantiles: 1 }), 'INVALID_OPTION'); expectErrorCode(() => analyzeSignalPanel([validSlice], { quantiles: 2.5 }), 'INVALID_OPTION');