T-84: M1.3d — trial_ledger writer + cluster N̂-deflated Sharpe reader in @stock-bot/research (append one row per horizon, vault-fenced sample_end, PostgreSQL integration test + local benchmark)
This commit is contained in:
parent
dd05e20325
commit
744255c096
3 changed files with 134 additions and 9 deletions
|
|
@ -157,8 +157,10 @@ export interface ClusterDeflatedSharpeQuery {
|
|||
readonly dsrGate?: number;
|
||||
/**
|
||||
* Optional strictly-ascending `YYYY-MM-DD` trading calendar for the cluster's market.
|
||||
* A trial without explicit observation dates is index-aligned only when its `dailySeries`
|
||||
* length exactly matches the number of sessions in `[sampleStart, sampleEnd]`.
|
||||
* A trial without explicit observation dates is index-aligned only when `sessionDates`
|
||||
* covers the trial's full `[sampleStart, sampleEnd]` range (`sessionDates[0] <= sampleStart`
|
||||
* and `sessionDates[last] >= sampleEnd`) and its `dailySeries` length exactly matches the
|
||||
* number of sessions in `[sampleStart, sampleEnd]`.
|
||||
*/
|
||||
readonly sessionDates?: readonly string[];
|
||||
/**
|
||||
|
|
@ -1047,20 +1049,39 @@ function pearsonSliceCorrelation(
|
|||
((b.prefixSum[offsetB + len] as number) - (b.prefixSum[offsetB] as number)) / len;
|
||||
const aVals = a.values;
|
||||
const bVals = b.values;
|
||||
const firstA = aVals[offsetA] as number;
|
||||
const firstB = bVals[offsetB] as number;
|
||||
let allEqualA = true;
|
||||
let allEqualB = true;
|
||||
|
||||
let cov = 0;
|
||||
let varA = 0;
|
||||
let varB = 0;
|
||||
|
||||
for (let t = 0; t < len; t += 1) {
|
||||
const da = (aVals[offsetA + t] as number) - meanA;
|
||||
const db = (bVals[offsetB + t] as number) - meanB;
|
||||
const va = aVals[offsetA + t] as number;
|
||||
const vb = bVals[offsetB + t] as number;
|
||||
if (allEqualA && va !== firstA) {
|
||||
allEqualA = false;
|
||||
}
|
||||
if (allEqualB && vb !== firstB) {
|
||||
allEqualB = false;
|
||||
}
|
||||
const da = va - meanA;
|
||||
const db = vb - meanB;
|
||||
cov += da * db;
|
||||
varA += da * da;
|
||||
varB += db * db;
|
||||
}
|
||||
|
||||
if (!Number.isFinite(varA) || !Number.isFinite(varB) || varA <= 0 || varB <= 0) {
|
||||
if (
|
||||
allEqualA ||
|
||||
allEqualB ||
|
||||
!Number.isFinite(varA) ||
|
||||
!Number.isFinite(varB) ||
|
||||
varA <= 0 ||
|
||||
varB <= 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const denom = Math.sqrt(varA * varB);
|
||||
|
|
@ -1238,7 +1259,12 @@ function computeClusterEffectiveTrials(
|
|||
continue;
|
||||
}
|
||||
|
||||
if (sessionDates !== undefined) {
|
||||
if (
|
||||
sessionDates !== undefined &&
|
||||
sessionDates.length > 0 &&
|
||||
(sessionDates[0] as string) <= trial.sampleStart &&
|
||||
(sessionDates[sessionDates.length - 1] as string) >= trial.sampleEnd
|
||||
) {
|
||||
const startIdx = lowerBoundDate(sessionDates, trial.sampleStart);
|
||||
const endExclusive = upperBoundDate(sessionDates, trial.sampleEnd);
|
||||
if (endExclusive - startIdx === trial.dailySeries.length) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue