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:
Studio 2026-11-28 03:43:13 -05:00
parent 9d8dfc9bc8
commit f130b239fe
2 changed files with 158 additions and 20 deletions

View file

@ -169,31 +169,53 @@ type NumericTypedArray =
| Uint8Array
| Uint8ClampedArray;
const NUMERIC_TYPED_ARRAY_TAGS = new Set<string>([
'[object Float64Array]',
'[object Float32Array]',
'[object Int32Array]',
'[object Uint32Array]',
'[object Int16Array]',
'[object Uint16Array]',
'[object Int8Array]',
'[object Uint8Array]',
'[object Uint8ClampedArray]',
]);
function isNumericTypedArray(value: unknown): value is NumericTypedArray {
return (
value instanceof Float64Array ||
value instanceof Float32Array ||
value instanceof Int32Array ||
value instanceof Uint32Array ||
value instanceof Int16Array ||
value instanceof Uint16Array ||
value instanceof Int8Array ||
value instanceof Uint8Array ||
value instanceof Uint8ClampedArray
);
if (!ArrayBuffer.isView(value)) {
return false;
}
const tag = Object.prototype.toString.call(value);
if (!NUMERIC_TYPED_ARRAY_TAGS.has(tag)) {
return false;
}
return typeof (value as { subarray?: unknown }).subarray === 'function';
}
function isSupportedVector(value: unknown): value is readonly unknown[] | NumericTypedArray {
return Array.isArray(value) || isNumericTypedArray(value);
}
function isReadonlyMapLike(value: unknown): value is ReadonlyMap<unknown, unknown> {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false;
}
const candidate = value as {
get?: unknown;
has?: unknown;
[Symbol.iterator]?: unknown;
};
return (
typeof candidate.get === 'function' &&
typeof candidate.has === 'function' &&
typeof candidate[Symbol.iterator] === 'function'
);
}
function sliceNumericSeries(series: NumericSeries, length: number): NumericSeries {
if (series.length === length) {
return series;
}
if (series instanceof Float64Array) {
return series.subarray(0, length);
}
if (Array.isArray(series)) {
return series.slice(0, length);
}
@ -630,12 +652,9 @@ export async function runEventStudy(
// Validate return series shapes against the full calendar before slicing to the pre-cutoff
// prefix so excluded Vault-tail values (e.g. non-finite returns) never leak into or break
// the pre-Vault study.
if (
typeof entityReturns !== 'object' ||
entityReturns === null ||
typeof (entityReturns as { [Symbol.iterator]?: unknown })[Symbol.iterator] !== 'function'
) {
// the pre-Vault study. Require the ReadonlyMap lookup interface (get/has/iterator) so
// serialized pair arrays with duplicate keys are rejected rather than coerced into a Map.
if (!isReadonlyMapLike(entityReturns)) {
throw new SignalStudyError(
'invalid_panel',
'request.input.entityReturns must be a ReadonlyMap of entityId to return series.'
@ -693,6 +712,12 @@ export async function runEventStudy(
);
}
if (fencedEntityReturns !== null) {
if (fencedEntityReturns.has(entityId)) {
throw new SignalStudyError(
'invalid_panel',
`Duplicate entityId "${String(entityId)}" in request.input.entityReturns.`
);
}
fencedEntityReturns.set(
entityId,
sliceNumericSeries(series as NumericSeries, preCutoffLength)