Batch 9 of expansion-v2. Makes the analysis layer user-reachable, adds two
model-use capabilities, and threads an analysis job through the hosted platform.
Deterministic/offline/std-only.
crates/lawsynth-mpc (new): successive-linearization LQR-MPC. Relinearizes each
step (analytic A=∂f/∂x, B=∂f/∂u), solves a local LQR (lawsynth-feedback),
applies the first clamped move, advances the nonlinear plant by RK4. Regulates a
forced pendulum to <1e-3, respects saturation, stabilizes a Van der Pol limit
cycle; per-step gain bit-identical to direct LQR on a linear plant. Honest that
it's LQR-MPC, not constrained QP-MPC. 18 tests.
crates/lawsynth-propagate (new): coefficient→forecast uncertainty. Delta method
Cov(x(t))≈S·Cov(θ)·Sᵀ via forward sensitivities, plus seeded Monte-Carlo over the
bootstrap ensemble. Delta matches the (t·x0·e^-θt)²·s² closed form to <1e-9;
delta≈MC at small σ and honestly diverges (asymmetric MC band) at large σ.
covariance_from_ensemble closes the bootstrap→bands pipeline. 34 tests.
crates/lawsynth-cli: new analysis subcommands bifurcation/sensitivity/estimate/
reduce (shared linearize-at-fixed-point helper derives A/C/B from a world), with
honest guardrails (bifurcation needs a parameterized law; reduce needs a Hurwitz
point; estimate pole placement is single-output). 115 lib + 7 integration tests.
services/worker + scheduler: Job::AnalyzeStability + JobOutput::Stability threaded
through every worker match site (job/execute/artifacts/errors/http_error) and
proven first-class in the scheduler via an end-to-end submit→lease→execute test.
Gateway HTTP→Job decode noted as an honest follow-up. Worker 64 + scheduler tests.
specs/{model-predictive-control,forecast-uncertainty}: boundary specs.
Forecast uncertainty boundary (delta method + seeded Monte-Carlo)
This directory specifies deterministic propagation of coefficient uncertainty
into forecast intervals — mapping a parameter covariance Cov(θ) onto
trajectory prediction bands for a discovered model, implemented in
crates/lawsynth-propagate (delta_forecast, monte_carlo_forecast,
covariance_from_ensemble). It is a boundary specification in the house
style: it states what a conforming implementation MUST do, and — crucially —
what a reported band is and is not allowed to claim.
Motivation
A discovered dynamical law ẋ = f(x; θ) is only half the story. The coefficients
θ are themselves estimates: the deterministic bootstrap of
crates/lawsynth-uncertainty returns an ensemble of re-fit coefficient vectors,
whose spread is the parameter covariance Cov(θ). A forecast that ignores that
spread is dishonest — it draws a single line where the data support a band.
This crate closes the loop: it propagates parameter uncertainty into
trajectory uncertainty, so x(t) arrives with a prediction band. It offers
two methods that MUST agree in the small-uncertainty limit and are allowed —
indeed required — to diverge honestly outside it:
- Delta method (analytic, first-order). The forward sensitivities
S(t) = ∂x(t)/∂θ(fromcrates/lawsynth-sensitivity) are the linear map from parameter space to state space. To first order,Cov(x(t)) ≈ S(t)·Cov(θ)·S(t)ᵀ; the per-state variance is that product's diagonal and the band isx(t) ± z·sqrt(variance). - Monte-Carlo (seeded ensemble). Draw
Mparameter vectors from the ensemble, simulate each with the same fixed-step RK4 the sensitivities use, and take the per-time empirical mean and percentile band.
LawSynth is deterministic and offline. The bands are computed with analytic sensitivities and a seeded pseudo-random generator — never a wall-clock sampler. Identical inputs MUST yield bit-identical bands.
What a band IS
A band here is a quantified image of Cov(θ) under the model, never a
coverage proof. The contract is:
- Delta = first-order linearization via sensitivities. The delta variance is
exactly
diag(S(t)·Cov(θ)·S(t)ᵀ)withS(t)the forward-integrated sensitivities of the reported discrete trajectory (same RK4, same step). The bandx(t) ± z·sqrt(diag)is a symmetric Gaussian band about the nominal trajectoryx(t)integrated at the suppliedθ. - Monte-Carlo = seeded ensemble RK4.
Mparameter vectors are drawn either by resampling the raw bootstrap replicate coefficient vectors with replacement, or by samplingN(mean, Cov(θ))via a deterministic SplitMix64 + Box–Muller draw shaped by the Cholesky factor ofCov(θ). Each vector is simulated with the same fixed-step RK4, and the band is the per-time empirical mean, unbiased variance, and two-sided percentile interval. Cov(θ)comes from the bootstrap replicates.covariance_from_ensembleis the unbiased (B − 1denominator) sample covariance of the replicate coefficient vectors, matching the unbiased per-termstandard_errorthe uncertainty crate already reports. This is the intended pipeline: bootstrap → covariance → band.- Honest reporting, no fabrication. The propagator reports mean, variance, and band endpoints as computed. It never invents a distribution, never silently regularizes a bad covariance, and never suppresses a legitimate zero (a parameter absent from the fields has zero sensitivity, hence contributes zero variance — reported as such).
The two maps
For state x ∈ Rⁿ, parameters θ ∈ Rᵖ, covariance Σ_θ = Cov(θ), and forward
sensitivities S(t) = ∂x(t)/∂θ (an n × p matrix):
Delta: Cov(x(t)) ≈ S(t)·Σ_θ·S(t)ᵀ
Var(x_i(t)) = Σ_j Σ_l S_ij(t)·Σ_θ[j][l]·S_il(t)
band_i(t) = x_i(t) ± z·sqrt(Var(x_i(t)))
Monte-Carlo: θ^(m) ~ ensemble (m = 1 … M, seeded by (seed, m))
x^(m)(t) = RK4-simulate(f, x0, θ^(m))
mean_i(t) = (1/M) Σ_m x_i^(m)(t)
band_i(t) = [ Q_{α/2}, Q_{1−α/2} ] of { x_i^(m)(t) }_m
with α = 1 − confidence and quantiles by the R type-7 rule (the same rule as
lawsynth-uncertainty::percentile). The two maps coincide as Σ_θ → 0.
Requirements
-
Delta from analytic sensitivities. A conforming implementation MUST obtain
S(t)fromcrates/lawsynth-sensitivity'sforward_sensitivities(analytic Jacobian and analytic parameter partials, one shared fixed-step RK4). The nominal trajectory reported as the band centre MUST be the state block of that same integration, so the sensitivities are the sensitivities of the reported trajectory. It MUST NOT finite-difference the field. -
Monte-Carlo mirrors the same integrator. Each drawn parameter vector MUST be simulated with the same fixed-step RK4 (same
t0,dt,steps) as the sensitivities, so that asΣ_θ → 0the Monte-Carlo mean converges to the delta nominal trajectory. The two methods MUST share the time grid. -
Cov(θ)from the bootstrap replicates.covariance_from_ensembleMUST be the unbiased sample covariance (B − 1denominator) of the replicate coefficient vectors, so it is consistent with the crate's per-termstandard_error. Callers MAY also supply a covariance directly (delta and the Gaussian Monte-Carlo source) or the raw replicates (the resampling Monte-Carlo source). -
Determinism. All arithmetic (the quadratic form
S·Σ·Sᵀ, the Cholesky factor, the RNG, the empirical aggregation) MUST be accumulated in a fixed order. The Monte-Carlo generator MUST be seeded per sample from(seed, m)using the same SplitMix64 family as the coefficient bootstrap, so the ensemble is independent of iteration order and bit-reproducible. Identical inputs MUST produce a bit-identicalForecastBands(verified tof64::to_bitsvia a canonical-string fingerprint). -
Totality and typed errors. The implementation MUST reject, with distinct typed errors and never a fabricated or silently dropped band:
- a covariance that is not square, or whose dimension differs from the parameter count;
- a non-finite covariance, mean, or replicate value, or a non-finite band
multiplier
z; - an indefinite covariance — both a Cholesky failure (Monte-Carlo Gaussian
draw) and a meaningfully negative delta variance MUST surface as
"not positive semi-definite" rather than a
NaNor complex result; - a Monte-Carlo request for zero samples, a confidence outside
(0, 1), an empty replicate ensemble, or a replicate of the wrong width; - any structural or numeric failure surfaced by the underlying sensitivity integration (unknown symbol, dimension mismatch, invalid config, …), passed through verbatim. A tiny negative delta variance produced by roundoff from a legitimately positive-semi-definite covariance is clamped to zero, not reported as an error.
-
Query surface.
ForecastBandsMUST expose the shared time grid, the state ordering, and the four[state][time]matrices (mean, variance, lower, upper), plus a band-width helper (out-of-range indices returnNone, never panic) and a canonical-string fingerprint for determinism checks. -
Honest verification. Any claim that the bands are correct MUST be backed by reproducible cross-checks: (a) an analytic check — for
ẋ = −θ·xwithVar(θ) = s², the delta variance is exactly(t·x0·e^{−θt})²·s², checked to a tight tolerance; (b) delta ≈ Monte-Carlo for small uncertainty on a nonlinear model (logistic), agreeing to a stated relative tolerance, together with a demonstration that they diverge under large uncertainty (the honest first-order limitation); (c) monotonicity (bands widen with time for a growing sensitivity, and with largerCov(θ)); (d) coverage sanity (the nominal trajectory lies inside the Monte-Carlo band; higher confidence widens it); (e) an end-to-end run from a real bootstrap ensemble throughcovariance_from_ensembleto bands; and (f) determinism (bit-identical bands). The reference suite pins the analytic check to1e-9and the delta/Monte-Carlo variance agreement to 10% relative at smallσ.
Public API
delta_forecast(
&[(Identifier, Expr)], // fields ẋ_i = f_i(x; θ)
&[Identifier], // states (output ordering)
&[Identifier], // parameters
&[f64], // initial state x(t0)
&[f64], // nominal parameter values θ
&[Vec<f64>], // Cov(θ), p × p
&SensitivityConfig,
f64, // band multiplier z
) -> Result<ForecastBands, PropagateError>
monte_carlo_forecast(
&[(Identifier, Expr)], &[Identifier], &[Identifier], &[f64],
EnsembleSource, // Gaussian { mean, covariance } | Replicates { draws }
&SensitivityConfig,
usize, // samples M
u64, // seed
f64, // confidence in (0, 1)
) -> Result<ForecastBands, PropagateError>
covariance_from_ensemble(&CoefficientEnsemble) -> Vec<Vec<f64>> // unbiased sample Cov(θ)
z_for_confidence(f64) -> Result<f64, PropagateError> // two-sided normal multiplier
ForecastBands::times() -> &[f64]
ForecastBands::states() -> &[Identifier]
ForecastBands::mean() / variance() / lower() / upper() -> &[Vec<f64>] // [state][time]
ForecastBands::band_width(state, time) -> Option<f64>
ForecastBands::to_canonical_string() -> String // determinism fingerprint
z_for_confidence maps a two-sided confidence level to the delta multiplier via
the standard normal quantile (Acklam's rational approximation), so the delta band
and a Monte-Carlo percentile band can be compared on the same footing.
Honest scope & limits
- The delta method is FIRST-ORDER.
S(t)·Cov(θ)·S(t)ᵀis the exact covariance of the linearized flow. Under strong model nonlinearity or large parameter uncertainty the truex(t)distribution is skewed, so the symmetric delta band undercovers and drifts from the Monte-Carlo band. The reference suite exercises exactly this divergence — it is a property, not a bug. - Monte-Carlo coverage is only as good as the ensemble. The
Replicatessource inherits every limitation of the bootstrap (percentile intervals can undercover under misspecification; case resampling assumes exchangeable rows). TheGaussiansource additionally assumes the coefficients are approximately normal with the given mean and covariance — a modelling choice, not a theorem. - Gaussian bands assume approximate normality of
x(t). The delta band reportsmean ± z·sd; readingzas a coverage level assumesx(t)is approximately normal, which the nonlinear flow only guarantees in the small-uncertainty limit. - Both inherit upstream biases. The sensitivities and the RK4 re-simulations
carry the fixed-step integrator's
O(dt⁴)error and drift over long horizons; and neither method corrects for the finite-differenceẋbias baked into the original coefficient fit. A band is honest about the discretisation and the covariance it was handed, not about the true continuous flow or the true data. Cov(θ)must be positive semi-definite. An indefinite covariance has no Cholesky factor and yields negative variances; the propagator reports this rather than regularizing it away. A near-singular covariance (e.g. a parameter absent from the fields, giving a zero row) is valid for the delta method but not for the Gaussian Monte-Carlo draw — use theReplicatessource or the delta method there.
Non-goals
- No second-order (Hessian) delta correction, no unscented transform, and no adjoint/reverse propagation — a single first-order forward map only.
- No adaptive sampling, importance sampling, or variance reduction — a flat, seeded ensemble only.
- No coverage calibration or conformal band — the bands are the honest image of
Cov(θ), not a guaranteed-coverage interval. - No uncertainty in the initial condition, the model structure, or the time grid; only coefficient uncertainty is propagated.
- No stochastic (wall-clock-seeded) sampling, no network, no external crates.