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.
38 lines
1.4 KiB
Rust
38 lines
1.4 KiB
Rust
//! Statistical honesty: the Kramers–Moyal estimate is noisy on finite data, and
|
||
//! a LONGER path tightens it. This test demonstrates that increasing the number
|
||
//! of samples reduces the error of the recovered OU drift slope — documenting
|
||
//! that the tolerances used elsewhere are a function of path length, not a claim
|
||
//! of machine-precision recovery.
|
||
|
||
mod common;
|
||
|
||
use common::*;
|
||
use lawsynth_sde::{BinRule, SdeConfig, discover_sde};
|
||
|
||
const THETA: f64 = 1.0;
|
||
const SIGMA: f64 = 0.5;
|
||
|
||
fn drift_slope_error(steps: usize) -> f64 {
|
||
let (time, values) = ornstein_uhlenbeck(THETA, SIGMA, 0.01, steps, 2024);
|
||
let dataset = scalar_dataset(time, values);
|
||
let config = SdeConfig::new()
|
||
.with_bins(BinRule::Count(20))
|
||
.with_min_bin_count(20)
|
||
.with_polynomial_degree(3);
|
||
let model = discover_sde(&dataset, &config).unwrap();
|
||
let slope = model.state(&x()).unwrap().drift_law.coefficient_of_power(1);
|
||
(slope - (-THETA)).abs()
|
||
}
|
||
|
||
#[test]
|
||
fn more_samples_reduce_the_drift_error() {
|
||
let short = drift_slope_error(20_000);
|
||
let long = drift_slope_error(400_000);
|
||
assert!(
|
||
long < short,
|
||
"expected the longer path (err={long}) to beat the shorter path (err={short})"
|
||
);
|
||
// The long path should land the slope comfortably close to the truth.
|
||
assert!(long < 0.05, "long-path drift error {long} unexpectedly large");
|
||
}
|