spb/anomaly-atlas Public License
Systematic discovery & rigorous validation of statistical anomalies in open HF market data (hfmarketdata.io) — pre-registered, artifact-null-driven, fully reproducible. Live atlas: www.anomaly-atlas.io
Python 61.4%
JavaScript 28.7%
CSS 8.6%
Shell 0.7%
Makefile 0.5%
1# =============================================================================2# Project : anomaly-atlas3# File : benchmarks/synthetic/test_gate_expd.py4# Purpose : §8.1 gate for expD: grids, LOCF masks, both-fresh de-artifacting5# Author : Simon-Pierre Boucher6# Contact : contact@spboucher.ai7# Data src : hfmarketdata.io (sole data source)8# Created : 2026-08-129# Modified : 2026-08-1210# Platform : macOS / Apple Silicon (arm64)11# License : All rights reserved (research code)12# =============================================================================13"""Gate the expD alignment primitives before real data:1415 * grid building from bar dicts is exact (slots, NaN gaps, RTH filter);16 * LOCF returns carry correct fresh-masks; missing days never leak;17 * the both-fresh treatment REMOVES the planted non-synchronicity artifact18 that the raw LOCF join manufactures (the core expD claim).19"""2021from __future__ import annotations2223import sys24from pathlib import Path2526import numpy as np2728sys.path.insert(0, str(Path(__file__).resolve().parent))2930from generators import correlated_pair # noqa: E4023132from anomaly_atlas.data.cleaning import (33 both_fresh,34 locf_day_returns,35 nan_xcorr,36 rth_day_grids,37)3839SEEDS = [1, 2, 3]404142def bars_for_day(day: str, prices: dict[str, float]) -> list[dict]:43 return [{"datetime": f"{day} {hhmm}:00", "close": p} for hhmm, p in prices.items()]444546def test_grid_building_slots_and_rth_filter():47 bars = bars_for_day("2024-01-02", {"09:30": 100.0, "09:32": 101.0, "15:59": 102.0})48 bars += bars_for_day("2024-01-02", {"04:00": 99.0, "16:00": 103.0}) # outside RTH49 days = rth_day_grids(bars)50 g = days["2024-01-02"]51 assert np.isfinite(g[[0, 2, 389]]).all()52 assert np.isnan(g[1]) and np.isnan(g[3])53 assert np.isfinite(g).sum() == 3 # extended-hours bars excluded545556def test_locf_masks_and_missing_days():57 bars = bars_for_day("2024-01-02", {"09:30": 100.0, "09:32": 101.0, "09:33": 101.5})58 days = rth_day_grids(bars)59 r, fresh = locf_day_returns(days, ["2024-01-02", "2024-01-03"])60 assert len(r) == 389 * 261 assert r[0] == 0.0 and not fresh[0] # 09:31 carried forward62 # 09:31->09:32 return ends fresh but STARTS on a carried value -> not fresh63 assert abs(r[1]) > 0 and not fresh[1]64 # 09:32->09:33: both endpoints fresh -> a genuine 1-minute return65 assert abs(r[2]) > 0 and fresh[2]66 assert np.isnan(r[389:]).all() # absent day never leaks67 assert not fresh[389:].any()686970def synthetic_pair_day_grids(n_days: int, rho: float, p_obs: float, seed: int):71 """Correlated 1min walks; y observed sparsely. Returns (days_x, days_y, day_list)."""72 rng = np.random.default_rng(seed)73 px, py = correlated_pair(n_days * 390, rho=rho, sigma=0.001, seed=seed)74 days_x, days_y, day_list = {}, {}, []75 for d in range(n_days):76 day = f"2024-02-{d + 1:02d}"77 day_list.append(day)78 gx = px[d * 390 : (d + 1) * 390].copy()79 gy = py[d * 390 : (d + 1) * 390].copy()80 mask = rng.random(390) < p_obs81 mask[0] = True82 gy[~mask] = np.nan83 days_x[day] = gx84 days_y[day] = gy85 return days_x, days_y, day_list868788def test_both_fresh_removes_planted_nonsync_artifact():89 for seed in SEEDS:90 dx, dy, dl = synthetic_pair_day_grids(25, rho=0.7, p_obs=0.3, seed=seed)91 rx, fx = locf_day_returns(dx, dl)92 ry, fy = locf_day_returns(dy, dl)93 raw = nan_xcorr(rx, ry, 2)94 assert raw[1] > 0.10 # artifact present in the raw LOCF join95 bx, by = both_fresh(rx, fx, ry, fy)96 sync = nan_xcorr(bx, by, 2)97 assert abs(sync[1]) < 0.05 # and gone on the synchronized subsample98 assert sync[0] > 0.6 # true contemporaneous corr (rho=0.7) recovered99