T-73: M1.1h — Pass 4: pure in-memory cross-sectional price cross-check (25 stratified sample dates, price ratio ≈ 1.0 or ≈ USD/CAD, return correlation)
This commit is contained in:
parent
8f76d99046
commit
db284d7760
3 changed files with 169 additions and 6 deletions
|
|
@ -395,6 +395,115 @@ describe('security master Pass 4 cross-sectional price cross-check', () => {
|
|||
expect(result.conflicts[0]?.sourceId).toBe('OLD:US');
|
||||
});
|
||||
|
||||
test('treats default ratio tolerance bounds (0.98 and 1.02) as inclusive and rejects values just outside', () => {
|
||||
// Exact integer multiples avoid floating-point drift on left/right so medianRatio is
|
||||
// strictly 1.02, 0.98, 1.0201, and 0.9799.
|
||||
const basePrices = [100, 150, 200, 125, 250, 175, 300, 225];
|
||||
const cases = [
|
||||
{ id: 'AT_UPPER', factor: 1.02, expectedVerdict: 'confirmed' as const },
|
||||
{ id: 'AT_LOWER', factor: 0.98, expectedVerdict: 'confirmed' as const },
|
||||
{ id: 'OVER_UPPER', factor: 1.0201, expectedVerdict: 'ratio_mismatch' as const },
|
||||
{ id: 'UNDER_LOWER', factor: 0.9799, expectedVerdict: 'ratio_mismatch' as const },
|
||||
];
|
||||
|
||||
const pairs: PriceCrossCheckPair[] = [];
|
||||
const closes: SampleCloseRecord[] = [];
|
||||
for (const c of cases) {
|
||||
pairs.push({
|
||||
left: { source: 'qm', sourceId: `${c.id}:US`, currency: 'USD' },
|
||||
right: { source: 'eod', sourceId: `${c.id}.US`, currency: 'USD' },
|
||||
});
|
||||
for (let i = 0; i < SAMPLE_8_DATES.length; i++) {
|
||||
const date = SAMPLE_8_DATES[i] ?? '';
|
||||
const p = basePrices[i] ?? 100;
|
||||
closes.push({ source: 'qm', sourceId: `${c.id}:US`, date, close: p * c.factor });
|
||||
closes.push({ source: 'eod', sourceId: `${c.id}.US`, date, close: p });
|
||||
}
|
||||
}
|
||||
|
||||
const result = crossCheckPrices({ pairs, closes, usdCadByDate: [] });
|
||||
assertReconciliationAndContracts(result, pairs.length);
|
||||
expect(result.stats.invalidPairs).toBe(0);
|
||||
|
||||
const byId = new Map(result.results.map((r) => [r.left.sourceId, r]));
|
||||
expect(byId.get('AT_UPPER:US')?.medianRatio).toBe(1.02);
|
||||
expect(byId.get('AT_UPPER:US')?.verdict).toBe('confirmed');
|
||||
expect(byId.get('AT_LOWER:US')?.medianRatio).toBe(0.98);
|
||||
expect(byId.get('AT_LOWER:US')?.verdict).toBe('confirmed');
|
||||
expect(byId.get('OVER_UPPER:US')?.verdict).toBe('ratio_mismatch');
|
||||
expect(byId.get('UNDER_LOWER:US')?.verdict).toBe('ratio_mismatch');
|
||||
|
||||
expect(result.conflicts.map((c) => c.sourceId)).toEqual(['OVER_UPPER:US', 'UNDER_LOWER:US']);
|
||||
expect(result.conflicts.every((c) => c.conflictKind === 'price_ratio_mismatch')).toBe(true);
|
||||
});
|
||||
|
||||
test('records deterministic non-confirming results for malformed candidates without aborting valid ones', () => {
|
||||
const basePrices = [100, 112, 105, 118, 124, 119, 130, 136];
|
||||
const closes: SampleCloseRecord[] = [];
|
||||
for (let i = 0; i < SAMPLE_8_DATES.length; i++) {
|
||||
const date = SAMPLE_8_DATES[i] ?? '';
|
||||
const p = basePrices[i] ?? 100;
|
||||
closes.push({ source: 'qm', sourceId: 'GOOD:US', date, close: p });
|
||||
closes.push({ source: 'eod', sourceId: 'GOOD.US', date, close: p });
|
||||
}
|
||||
|
||||
const validPair: PriceCrossCheckPair = {
|
||||
left: { source: 'qm', sourceId: 'GOOD:US', currency: 'USD' },
|
||||
right: { source: 'eod', sourceId: 'GOOD.US', currency: 'USD' },
|
||||
};
|
||||
|
||||
// Include: valid pair, null pair, undefined pair, sparse array hole, null right endpoint,
|
||||
// missing left endpoint, and empty/non-string source with valid USD currencies.
|
||||
const malformedPairs = new Array<PriceCrossCheckPair>(7);
|
||||
malformedPairs[0] = validPair;
|
||||
malformedPairs[1] = null as unknown as PriceCrossCheckPair;
|
||||
malformedPairs[2] = undefined as unknown as PriceCrossCheckPair;
|
||||
// index 3 intentionally left as a sparse hole
|
||||
malformedPairs[4] = {
|
||||
left: { source: 'qm', sourceId: 'NULL_RIGHT:US', currency: 'USD' },
|
||||
right: null as unknown as PriceCrossCheckPair['right'],
|
||||
};
|
||||
malformedPairs[5] = {
|
||||
left: undefined as unknown as PriceCrossCheckPair['left'],
|
||||
right: { source: 'eod', sourceId: 'NULL_LEFT.US', currency: 'USD' },
|
||||
};
|
||||
malformedPairs[6] = {
|
||||
left: { source: '', sourceId: 'EMPTY_SRC:US', currency: 'USD' },
|
||||
right: { source: 'eod', sourceId: 'EMPTY_SRC.US', currency: 'USD' },
|
||||
};
|
||||
|
||||
const result = crossCheckPrices({
|
||||
pairs: malformedPairs,
|
||||
closes,
|
||||
usdCadByDate: [],
|
||||
});
|
||||
|
||||
assertReconciliationAndContracts(result, malformedPairs.length);
|
||||
expect(result.stats.pairsIn).toBe(7);
|
||||
expect(result.stats.invalidPairs).toBe(6);
|
||||
expect(result.stats.byVerdict.confirmed).toBe(1);
|
||||
expect(result.stats.byVerdict.unsupported_currency_pair).toBe(5);
|
||||
expect(result.stats.byVerdict.insufficient_data).toBe(1);
|
||||
expect(result.conflicts).toEqual([]);
|
||||
|
||||
const confirmed = result.results.filter((r) => r.verdict === 'confirmed');
|
||||
expect(confirmed.length).toBe(1);
|
||||
expect(confirmed[0]?.left.sourceId).toBe('GOOD:US');
|
||||
expect(confirmed[0]?.priceCorr).toBe(1);
|
||||
|
||||
// Reversing the dense equivalent produces the exact same sorted output and stats.
|
||||
const denseReversed = [
|
||||
malformedPairs[6],
|
||||
malformedPairs[5],
|
||||
malformedPairs[4],
|
||||
undefined,
|
||||
malformedPairs[2],
|
||||
malformedPairs[1],
|
||||
malformedPairs[0],
|
||||
] as unknown as PriceCrossCheckPair[];
|
||||
expect(crossCheckPrices({ pairs: denseReversed, closes, usdCadByDate: [] })).toEqual(result);
|
||||
});
|
||||
|
||||
test('flags low_correlation when median ratio ~ 1.0 but returns do not co-move', () => {
|
||||
// Both hover around 100 (median ratio ~ 1.0) but move in opposite directions.
|
||||
const leftPrices = [100, 101.2, 99.1, 101.5, 98.8, 101.4, 99.2, 101.1];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue