| .. | ||
| examples | ||
| src | ||
| test | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
@stock-bot/research
Browser-safe research contracts, rolling 6-month Vault holdout fencing, and append-only trial_ledger accounting (design/DATA-CONTRACTS.md, design/VALIDATION-AND-RIGOR.md).
trial_ledger Writer & Cluster N-hat-Deflated Sharpe Reader
Exported from both @stock-bot/research and @stock-bot/research/trial-ledger (libs/research/src/trial-ledger.ts). The module is dependency-free and browser-safe; PostgreSQL access is injected via TrialLedgerExecutor ({ query(text, params) }).
API
canonicalExpressionHash(input)— Returns a 64-character lowercase SHA-256 hex digest over the recursively ASCII-key-sorted compact JSON of{ expression, params, universe, sampleStart, sampleEnd, horizon }. Leading/trailing/internal whitespace inexpressionis collapsed,undefinedobject keys are omitted,-0is normalized to0, and non-JSON values (NaN,±Infinity, functions, symbols,BigInt, cycles) are rejected withTrialLedgerError.buildTrialRow(input)— Pure validator and moment builder for a singletrial_ledgerrow. Validates principal (operatororagent:<uuid>), optional UUIDs, schema enums (stage,universeMarket,horizonBucket), 64-hexexpressionHash, strictYYYY-MM-DDdates, the exclusive Vault fence, and IEEE-754REAL(float32) representability for everydailySerieselement. Computes{ nObs, sharpeUnann, skewness, kurtosis }viatrialMomentsfrom@stock-bot/calculations.recordTrials(executor, rows)— Validates every row upfront (sending zero SQL on any invalid row or batch >MAX_TRIAL_BATCH_SIZE = 4681) and appends the batch atomically in one parameterised multi-rowINSERT ... RETURNING trial_id, created_at. Multi-horizon analyses (e.g.1d,5d,20d) record one row per horizon.loadClusterTrials(executor, { universeMarket, horizonBucket, asOf? })— Loads a(universe_market, horizon_bucket)relatedness cluster ordered bytrial_id ASC(backed byidx_trial_ledger_cluster), optionally capped atcreated_at <= asOf.clusterDeflatedSharpe(executor, { trialId, asOf?, dsrGate? })— Loads the target trial and its relatedness cluster, aligns series without fabricating correlations across unalignable sample windows (sharedsampleStartuses zero-copy left alignment; sharedsampleEnduses leading-NaNright alignment; unalignable windows are placed in disjointNaN-padded blocks soeffectiveIndependentTrialssurfacespairsSkippedandmeanCorrelation: null), and computes{ trialId, clusterSize, effectiveTrials, meanCorrelation, pairsUsed, pairsSkipped, sr0, standardError, dsr, passesGate }.
Vault Holdout Fence Rule
Every row built by buildTrialRow or persisted by recordTrials requires an explicit vaultCutoffDate (YYYY-MM-DD) and delegates calendar validation and holdout enforcement to applyVaultFence in libs/research/src/market-data-store.ts. Any trial with sampleEnd >= vaultCutoffDate (or sampleStart >= vaultCutoffDate) is rejected with TrialLedgerError('VAULT_FENCE_VIOLATION') before any SQL is executed.
Local In-Memory Benchmark
Fixed-seed (20260403) in-memory benchmark covering 200 trials × 5,000 daily observations (1,000,000 observations total, 19,900 pairwise correlations) through buildTrialRow + clusterDeflatedSharpe with no PostgreSQL dependency:
- Command:
bun test libs/research/test/trial-ledger.test.ts - Workload: 200 trials × 5,000 daily observations (seed
20260403) - Measured (Bun 1.2.16):
elapsedMs = 732.35 ms,trialsPerSec = 273.1 trials/s(effectiveTrials = 171.85,dsr = 0.011891)