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.
78 lines
3.0 KiB
Rust
78 lines
3.0 KiB
Rust
//! State-dependent diffusion: geometric Brownian motion
|
||
//! `dX = μ X dt + σ X dW` has drift `μ x` and diffusion `b²(x) = σ² x²`.
|
||
//!
|
||
//! Recovering the QUADRATIC diffusion `σ² x²` is the headline — it proves the
|
||
//! estimator handles state-dependent (multiplicative) diffusion, not just an
|
||
//! additive constant. GBM has no stationary distribution, so a single long path
|
||
//! either explodes or collapses; we therefore sample an ENSEMBLE of short
|
||
//! trajectories launched from initial conditions spread across the state space
|
||
//! (see `geometric_brownian_ensemble`) and pass it with
|
||
//! `SdeConfig::with_trajectories`, which forms increments only within a path.
|
||
|
||
mod common;
|
||
|
||
use common::*;
|
||
use lawsynth_sde::{BinRule, SdeConfig, discover_sde};
|
||
|
||
const MU: f64 = 0.5;
|
||
const SIGMA: f64 = 0.5;
|
||
const TRAJECTORIES: usize = 2000;
|
||
const SEGMENT_LEN: usize = 250;
|
||
|
||
fn gbm_model() -> lawsynth_sde::SdeModel {
|
||
let (time, values) =
|
||
geometric_brownian_ensemble(MU, SIGMA, 0.4, 4.0, 0.0005, TRAJECTORIES, SEGMENT_LEN, 7);
|
||
let dataset = scalar_dataset(time, values);
|
||
// Degree 2 is the right library: drift is `μ x` and diffusion is `σ² x²`, so
|
||
// no cubic term is needed and dropping it avoids over-fitting a noisy tail.
|
||
let config = SdeConfig::new()
|
||
.with_bins(BinRule::Count(24))
|
||
.with_min_bin_count(50)
|
||
.with_polynomial_degree(2)
|
||
.with_trajectories(TRAJECTORIES);
|
||
discover_sde(&dataset, &config).unwrap()
|
||
}
|
||
|
||
#[test]
|
||
fn recovers_quadratic_state_dependent_diffusion() {
|
||
let model = gbm_model();
|
||
let diffusion = &model.state(&x()).unwrap().diffusion_law;
|
||
let sigma_squared = SIGMA * SIGMA;
|
||
|
||
// b²(x) ≈ σ² x²: the x² term carries σ², with negligible constant / linear.
|
||
assert!(
|
||
(diffusion.coefficient_of_power(2) - sigma_squared).abs() < 0.04,
|
||
"diffusion x^2 {} not ≈ σ²={}",
|
||
diffusion.coefficient_of_power(2),
|
||
sigma_squared
|
||
);
|
||
assert!(diffusion.coefficient_of_power(0).abs() < 0.06, "spurious diffusion constant");
|
||
assert!(diffusion.coefficient_of_power(1).abs() < 0.06, "spurious diffusion x");
|
||
}
|
||
|
||
#[test]
|
||
fn recovers_linear_drift() {
|
||
let model = gbm_model();
|
||
let drift = &model.state(&x()).unwrap().drift_law;
|
||
|
||
// Drift ≈ μ x. GBM drift is a small signal buried under multiplicative noise,
|
||
// so the tolerance is looser than the (much cleaner) diffusion recovery.
|
||
assert!(
|
||
(drift.coefficient_of_power(1) - MU).abs() < 0.12,
|
||
"drift slope {} not ≈ μ={}",
|
||
drift.coefficient_of_power(1),
|
||
MU
|
||
);
|
||
assert!(drift.coefficient_of_power(0).abs() < 0.15, "spurious drift constant");
|
||
assert!(drift.coefficient_of_power(2).abs() < 0.05, "spurious drift x^2");
|
||
}
|
||
|
||
#[test]
|
||
fn diffusion_grows_with_state() {
|
||
let model = gbm_model();
|
||
let diffusion = &model.state(&x()).unwrap().diffusion_law;
|
||
// A genuinely state-dependent diffusion: larger |x| ⇒ larger b²(x).
|
||
assert!(diffusion.evaluate(3.0) > diffusion.evaluate(1.0));
|
||
assert!(diffusion.evaluate(1.0) > diffusion.evaluate(0.5));
|
||
}
|