caelum0x 220552d530 feat: SDE discovery + discrete-time control + Python/TS reach
Batch 10 of expansion-v2. Extends discovery into stochastic dynamics, adds
discrete-time control, and completes the Python/TS reach for the analysis
commands. Deterministic/offline/std-only (Rust) or typed/tested (Python/TS).

crates/lawsynth-sde (new): stochastic law discovery via Kramers-Moyal
conditional moments. Drift a(x)=E[ΔX|x]/Δt and diffusion b²(x)=E[ΔX²|x]/Δt
estimated by binning, occupancy-gated, then sparse-fit to a polynomial library.
Recovers OU (linear drift, constant diffusion), GBM (state-dependent quadratic
diffusion), and double-well (cubic drift) from seeded Euler-Maruyama paths.
Reports raw binned table + fitted laws; honest that it's a statistical
estimator (accuracy scales with path length / bin occupancy). Closes the loop
with the existing SDE simulator. 29 tests.

crates/lawsynth-discrete (new): discrete-time DLQR + Kalman/observer. DARE by
deterministic value iteration (reused on the dual for the filter); discrete
stability = spectral radius < 1, verified via koopman::eigen. DARE residuals
1e-12..5e-14, stabilizes an open-loop-unstable plant, matches the analytic
scalar P=2+√5 case. 34 tests.

python/lawsynth analysis.py: bifurcation/sensitivity/estimate/reduce client
methods over the CLI --json (single source of truth). test_analysis 36 passed;
full SDK suite 130 passed. Honest engine constraints surfaced (pole placement
needs observability; reduce needs a diagonalizable Hurwitz linearization).

packages/world-schema + api-client: typed models + parsers for the four new
analysis-command JSON shapes, verbatim-CLI fixtures, token strings confirmed
against source. api-client 24 tests.

specs/{sde-discovery,discrete-time-control}: boundary specs with honest limits.
2026-08-18 17:21:57 +03:00

54 lines
1.8 KiB
Rust

//! Determinism: identical `(Dataset, SdeConfig)` inputs MUST yield a
//! bit-identical `SdeModel` (`f64::to_bits`), and re-simulating the same seeded
//! path MUST reproduce the same data.
mod common;
use common::*;
use lawsynth_sde::{BinRule, SdeConfig, discover_sde};
#[test]
fn identical_inputs_produce_bit_identical_models() {
let (time, values) = ornstein_uhlenbeck(1.0, 0.5, 0.01, 200_000, 42);
let dataset = scalar_dataset(time, values);
let config = SdeConfig::new().with_bins(BinRule::Count(24)).with_min_bin_count(50);
let first = discover_sde(&dataset, &config).unwrap();
let second = discover_sde(&dataset, &config).unwrap();
// Structural equality and, more strictly, bit-identical floats.
assert_eq!(first, second);
assert_eq!(model_bits(&first), model_bits(&second));
}
#[test]
fn seeded_path_is_reproducible() {
let a = ornstein_uhlenbeck(1.0, 0.5, 0.01, 50_000, 7);
let b = ornstein_uhlenbeck(1.0, 0.5, 0.01, 50_000, 7);
assert_eq!(
a.1.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
b.1.iter().map(|v| v.to_bits()).collect::<Vec<_>>()
);
}
#[test]
fn different_seeds_produce_different_paths() {
let a = ornstein_uhlenbeck(1.0, 0.5, 0.01, 50_000, 7);
let b = ornstein_uhlenbeck(1.0, 0.5, 0.01, 50_000, 8);
assert_ne!(a.1, b.1);
}
#[test]
fn ensemble_discovery_is_bit_identical() {
let (time, values) = geometric_brownian_ensemble(0.5, 0.5, 0.4, 4.0, 0.0005, 500, 200, 7);
let dataset = scalar_dataset(time, values);
let config = SdeConfig::new()
.with_bins(BinRule::Count(20))
.with_min_bin_count(50)
.with_polynomial_degree(2)
.with_trajectories(500);
let first = discover_sde(&dataset, &config).unwrap();
let second = discover_sde(&dataset, &config).unwrap();
assert_eq!(model_bits(&first), model_bits(&second));
}