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.
92 lines
3.4 KiB
Rust
92 lines
3.4 KiB
Rust
//! Typed-error boundary tests for the discrete designs.
|
||
|
||
mod common;
|
||
|
||
use common::matrix;
|
||
use lawsynth_discrete::{DiscreteError, Matrix, discrete_kalman, dlqr};
|
||
|
||
#[test]
|
||
fn dlqr_rejects_indefinite_r() {
|
||
let a = matrix(vec![vec![1.0, 0.1], vec![0.0, 1.0]]);
|
||
let b = matrix(vec![vec![0.0], vec![0.1]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![-1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::NotPositiveDefinite);
|
||
}
|
||
|
||
#[test]
|
||
fn dlqr_rejects_non_symmetric_r() {
|
||
let a = matrix(vec![vec![1.0, 0.1], vec![0.0, 1.0]]);
|
||
let b = matrix(vec![vec![0.0, 0.0], vec![0.1, 0.1]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![1.0, 2.0], vec![0.0, 1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::NotSymmetric);
|
||
}
|
||
|
||
#[test]
|
||
fn dlqr_rejects_negative_definite_q() {
|
||
let a = matrix(vec![vec![1.0, 0.1], vec![0.0, 1.0]]);
|
||
let b = matrix(vec![vec![0.0], vec![0.1]]);
|
||
let q = matrix(vec![vec![-1.0, 0.0], vec![0.0, -1.0]]);
|
||
let r = matrix(vec![vec![1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::NotPositiveSemidefinite);
|
||
}
|
||
|
||
#[test]
|
||
fn dlqr_rejects_non_square_a() {
|
||
let a = matrix(vec![vec![1.0, 0.1, 0.0], vec![0.0, 1.0, 0.0]]);
|
||
let b = matrix(vec![vec![0.0], vec![0.1]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::NonSquare);
|
||
}
|
||
|
||
#[test]
|
||
fn dlqr_rejects_shape_mismatch() {
|
||
let a = matrix(vec![vec![1.0, 0.1], vec![0.0, 1.0]]);
|
||
let b = matrix(vec![vec![0.0], vec![0.1], vec![0.0]]); // 3 rows, A is order 2
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::ShapeMismatch);
|
||
}
|
||
|
||
#[test]
|
||
fn dlqr_reports_non_convergence_for_uncontrollable_unstable_mode() {
|
||
// State 1 is unstable (1.5) and entirely unactuated (B row is zero) and
|
||
// decoupled — no gain can stabilize it, so the DARE iterate diverges.
|
||
let a = matrix(vec![vec![0.5, 0.0], vec![0.0, 1.5]]);
|
||
let b = matrix(vec![vec![1.0], vec![0.0]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![1.0]]);
|
||
assert_eq!(dlqr(&a, &b, &q, &r).unwrap_err(), DiscreteError::NotConvergent);
|
||
}
|
||
|
||
#[test]
|
||
fn kalman_rejects_indefinite_r() {
|
||
let a = matrix(vec![vec![0.9, 0.1], vec![0.0, 0.8]]);
|
||
let c = matrix(vec![vec![1.0, 0.0]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![-0.5]]);
|
||
assert_eq!(discrete_kalman(&a, &c, &q, &r).unwrap_err(), DiscreteError::NotPositiveDefinite);
|
||
}
|
||
|
||
#[test]
|
||
fn kalman_rejects_shape_mismatch() {
|
||
let a = matrix(vec![vec![0.9, 0.1], vec![0.0, 0.8]]);
|
||
let c = matrix(vec![vec![1.0, 0.0]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![0.1, 0.0], vec![0.0, 0.1]]); // 2×2 but one output
|
||
assert_eq!(discrete_kalman(&a, &c, &q, &r).unwrap_err(), DiscreteError::ShapeMismatch);
|
||
}
|
||
|
||
#[test]
|
||
fn kalman_reports_non_convergence_for_undetectable_unstable_mode() {
|
||
// State 1 is unstable (1.5) and unmeasured (C sees only state 0) and
|
||
// decoupled — undetectable, so the dual DARE iterate diverges.
|
||
let a = matrix(vec![vec![0.5, 0.0], vec![0.0, 1.5]]);
|
||
let c = matrix(vec![vec![1.0, 0.0]]);
|
||
let q = Matrix::identity(2);
|
||
let r = matrix(vec![vec![0.1]]);
|
||
assert_eq!(discrete_kalman(&a, &c, &q, &r).unwrap_err(), DiscreteError::NotConvergent);
|
||
}
|