Files
foxhunt/crates/ml-backtesting/tests/parallel_sim_correctness.rs
jgrusewski 0c8b4b5608 refactor(per-horizon): N_HORIZONS 5→3 — ml-backtesting full propagation
Source migration:
- crates/ml-backtesting/src/lob/mod.rs:5 + policy/mod.rs:18: local
  const N_HORIZONS 5→3 via re-export from ml_alpha::heads::N_HORIZONS
  (single source of truth across crates)
- crates/ml-backtesting/src/sim/mod.rs:1751,1773,1784: [IsvKellyStateHost; 5]
  array literals → ; N_HORIZONS]
- crates/ml-backtesting/cuda/lob_state.cuh:9: #define N_HORIZONS 5→3
  (this triggers cubin rebuild of decision_policy + 4 other ml-backtesting
  kernels that #include this header)

Test migration (8 test files + 2 JSON fixtures):
- threshold_and_cost.rs, decision_floor_coldstart.rs, parallel_sim_
  correctness.rs, stop_controller.rs (37+10+1 broadcast_alpha calls),
  lob_sim_integrated_fuzz.rs, lob_sim_fixtures.rs: hardcoded [f32; 5]
  alpha-probs and [IsvKellyStateHost; 5] arrays → N_HORIZONS-sized via
  std::array::from_fn or [v; N_HORIZONS] literals
- trainer_parity.rs:34 + ring3_replay.rs:47: horizons literal
  [30,100,300,1000,6000] → ml_alpha::heads::HORIZONS
- fixtures/decision_alpha_buy_close.json + decision_program_h4_only.json:
  5-element warm_start_isv_kelly / alpha_probs / expected_isv_kelly_after
  trimmed to 3 elements; active horizon relocated to N_HORIZONS-1

Library lib-test rewrite (per pearl_tests_must_prove_not_lock_observations):
- crates/ml-backtesting/src/policy/mod.rs:197-233: lib tests
  default_strategy_has_5_horizon_leaves + ..._flattens_to_5_emits...
  renamed to N_HORIZONS-parametric form (observed-value 5 and 7
  were bug-locks).

cargo check -p ml-backtesting --all-targets: clean.
cargo test -p ml-backtesting --lib: 33 passed.
cargo test -p ml-backtesting --tests (non-CUDA, non-fixture-data): 2 passed,
53 ignored (CUDA-gated or FOXHUNT_TEST_DATA-gated).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 01:37:23 +02:00

62 lines
2.4 KiB
Rust

//! P1 regression test: per-backtest sim parameter arrays. With uniform
//! config across N backtests, all N must produce the same market_target
//! decision (proves the per-backtest indexing reduces correctly to the
//! pre-refactor scalar-broadcast semantics).
//!
//! Pair: the independence test (proving DIFFERENT per-backtest configs
//! produce different outputs) lands alongside the P2 inflight-limits
//! kernel where the read_first_inflight_arrival_ts helper exists.
use anyhow::Result;
use ml_backtesting::policy::N_HORIZONS;
use ml_backtesting::sim::{BatchedSimConfig, LobSimCuda, UniformSimParams};
use ml_core::device::MlDevice;
#[test]
#[ignore = "requires CUDA"]
fn parallel_sim_equivalence_with_uniform_config() -> Result<()> {
let dev = match MlDevice::cuda(0) {
Ok(d) => d,
Err(e) => {
eprintln!("skipping: cuda device unavailable ({e})");
return Ok(());
}
};
let mut sim = LobSimCuda::new(8, &dev)?;
sim.broadcast_alpha(&[0.8; N_HORIZONS])?;
let cfg = BatchedSimConfig::from_uniform(
8,
&UniformSimParams {
target_annual_vol_units: 50.0,
annualisation_factor: 825.0,
max_lots: 5,
latency_ns: 0,
kelly_frac_floor: 0.20,
sharpe_weight_floor: 0.10,
threshold: 0.0,
// CRT.1 C1.2: cost > 0 is required for the §4.4 conviction
// formula's eps_edge floor; 1.0 is a typical futures
// round-trip cost.
cost_per_lot_per_side: 1.0,
max_hold_ns: 0,
delta_floor: 0.0,
min_reasonable_px: 0.0,
max_reasonable_px: f32::INFINITY,
},
);
sim.step_decision_with_latency(0, &cfg)?;
let first = sim.read_market_target(0)?;
for b in 1..8 {
let got = sim.read_market_target(b)?;
assert_eq!(
got, first,
"backtest {b} differs from backtest 0 under uniform config: {got:?} vs {first:?}"
);
}
// And the uniform-config result must equal the result we'd get from
// a single-backtest sim (preserves pre-refactor behaviour).
assert_eq!(first.0, 0, "uniform config with p_h=0.8 should produce a buy; got side={}", first.0);
assert!(first.1 >= 1, "uniform-config size {} < 1", first.1);
Ok(())
}