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.
76 lines
2.7 KiB
Rust
76 lines
2.7 KiB
Rust
//! Nonlinear drift: the double-well process `dX = (X − X³) dt + σ dW` has a
|
||
//! CUBIC drift `x − x³` and a constant diffusion `σ²`.
|
||
//!
|
||
//! The sparse fit must recover the cubic structure — a positive linear term and
|
||
//! a negative cubic term — from a long deterministic path that the well confines
|
||
//! to a bounded interval (so the state space is well sampled).
|
||
|
||
mod common;
|
||
|
||
use common::*;
|
||
use lawsynth_sde::{BinRule, SdeConfig, discover_sde};
|
||
|
||
const SIGMA: f64 = 0.5;
|
||
|
||
fn double_well_model() -> lawsynth_sde::SdeModel {
|
||
let (time, values) = double_well(SIGMA, 0.005, 800_000, 99);
|
||
let dataset = scalar_dataset(time, values);
|
||
let config = SdeConfig::new()
|
||
.with_bins(BinRule::Count(30))
|
||
.with_min_bin_count(50)
|
||
.with_polynomial_degree(3);
|
||
discover_sde(&dataset, &config).unwrap()
|
||
}
|
||
|
||
#[test]
|
||
fn recovers_cubic_drift_structure() {
|
||
let model = double_well_model();
|
||
let drift = &model.state(&x()).unwrap().drift_law;
|
||
|
||
// Drift ≈ x − x³: positive linear term ≈ +1, negative cubic term ≈ −1.
|
||
assert!(
|
||
(drift.coefficient_of_power(1) - 1.0).abs() < 0.1,
|
||
"drift x {} not ≈ 1",
|
||
drift.coefficient_of_power(1)
|
||
);
|
||
assert!(
|
||
(drift.coefficient_of_power(3) - (-1.0)).abs() < 0.1,
|
||
"drift x^3 {} not ≈ -1",
|
||
drift.coefficient_of_power(3)
|
||
);
|
||
assert!(drift.coefficient_of_power(0).abs() < 0.05, "spurious drift constant");
|
||
assert!(drift.coefficient_of_power(2).abs() < 0.05, "spurious drift x^2");
|
||
}
|
||
|
||
#[test]
|
||
fn cubic_term_is_selected_by_the_sparse_fit() {
|
||
let model = double_well_model();
|
||
let drift = &model.state(&x()).unwrap().drift_law;
|
||
// The x³ term survives thresholding — the nonlinearity is genuinely selected.
|
||
let active_powers = drift.active_terms().map(|term| term.power).collect::<Vec<_>>();
|
||
assert!(active_powers.contains(&1), "linear term must be active");
|
||
assert!(active_powers.contains(&3), "cubic term must be active");
|
||
}
|
||
|
||
#[test]
|
||
fn recovers_constant_diffusion_under_nonlinear_drift() {
|
||
let model = double_well_model();
|
||
let diffusion = &model.state(&x()).unwrap().diffusion_law;
|
||
assert!(
|
||
(diffusion.coefficient_of_power(0) - SIGMA * SIGMA).abs() < 0.02,
|
||
"diffusion constant {} not ≈ σ²={}",
|
||
diffusion.coefficient_of_power(0),
|
||
SIGMA * SIGMA
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn drift_has_two_stable_wells() {
|
||
let model = double_well_model();
|
||
let drift = &model.state(&x()).unwrap().drift_law;
|
||
// The recovered a(x) = x − x³ pushes toward ±1: negative pull at x = 1.5,
|
||
// positive push at x = −1.5.
|
||
assert!(drift.evaluate(1.5) < 0.0);
|
||
assert!(drift.evaluate(-1.5) > 0.0);
|
||
}
|