lawsynth/docs/showcase/discovery-gallery.md
caelum0x 96e0591c88 fix(discovery): make SINDy sparsity scale-invariant
Discovery ran stlsq_standardized (thresholds in RMS-normalized feature
space) but then re-filtered the restored *original-unit* coefficients by
config.sparse.threshold. That defeated the standardization: genuine terms
whose coefficient is small purely because their state carries a large
magnitude (e.g. SIR's beta/N ~ 3e-4 with populations ~1e3) were pruned,
collapsing the law unless the user hand-tuned the threshold to the data
scale. Keep every non-zero term the standardized solver returns.

Fixing that unmasked a second bug: when a threshold prunes every remaining
active term in one step, STLSQ's loop broke on empty support and returned
the pre-prune *dense* fit, so an aggressive threshold silently produced a
denser model. Zero the coefficients when the support drains, matching the
reference STLSQ contract.

- Add regression tests: keeps_scale_small_coefficients_after_standardized_fit
  and threshold_above_every_coefficient_yields_the_empty_model.
- SIR now recovers the S*I interaction at raw population scale (thr 0.02);
  Lorenz/Lotka-Volterra/damped-pendulum recovery unchanged.
- Add a reproducible discovery gallery (examples/discovery-gallery) and a
  site showcase page (docs/showcase) with deterministic observed-vs-recovered
  SVG plots served from the docs site.
- .gitignore: local datasets, scratch worlds, and regenerable run/work dirs.

Workspace: 1653 tests pass; docs-site render stays byte-identical.
2026-08-19 00:34:05 +03:00

96 lines
3.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Discovery gallery
Each panel below is produced end-to-end by the LawSynth CLI: observations
→ `discover` (SINDy with RMS-standardized sparsity) → `simulate` the
recovered world forward from its initial condition. The **thick faint**
line is the observed trajectory; the **thin bright** line is the system
*re-simulated from the equations LawSynth inferred*. Overlap means the
recovered law reproduces the data.
Regenerate with `python3 examples/discovery-gallery/build_gallery.py`.
## Lorenz attractor (chaotic, 3-state)
![Lorenz attractor (chaotic, 3-state): observed vs. simulated from the recovered law](/showcase-lorenz.svg)
Recovers the two linear terms of the x-equation and the bilinear x·z / x·y couplings that make the system chaotic.
**Recovered laws**
```
dx/dt = 9.969275 * y + -9.96888 * x
dy/dt = -0.038285 * z + -0.787447 * y + 27.715227 * x + 0.001943 * z * z + -0.008881 * y * z + -0.020492 * y * y + -0.987527 * x * z + 0.045312 * x * y + -0.026701 * x * x
dz/dt = -2.68025 * z + 0.364585 * y + -0.547185 * x + -0.01202 * y * z + 0.016111 * x * z + 0.997025 * x * y
```
**Ground truth**
```
dx/dt = 10 (y - x)
dy/dt = 28 x - x z - y
dz/dt = x y - 2.667 z
```
## Lotka–Volterra predator–prey (2-state)
![Lotka–Volterra predator–prey (2-state): observed vs. simulated from the recovered law](/showcase-lotka-volterra.svg)
Near-exact recovery of all four rate constants from a clean oscillatory trajectory.
**Recovered laws**
```
dpredator/dt = -0.399733 * predator + 0.099944 * predator * prey
dprey/dt = 1.099223 * prey + -0.399717 * predator * prey
```
**Ground truth**
```
dprey/dt = 1.1 prey - 0.4 prey·predator
dpredator/dt = 0.1 prey·predator - 0.4 predator
```
## SIR epidemic (population-scale, 3-state)
![SIR epidemic (population-scale, 3-state): observed vs. simulated from the recovered law](/showcase-sir.svg)
The scale-invariance fix in action: with states ~O(10³) the true interaction coefficient is ~3e-4, and the recovered S·I coupling now survives sparsity at a physically meaningful threshold.
**Recovered laws**
```
dinfected/dt = 0.091526 + 0.002168 * susceptible + -0.000724 * recovered + 0.000205 * infected + -2.2681e-6 * susceptible * susceptible + -4.4417e-6 * recovered * susceptible + 7.7148e-7 * recovered * recovered + 0.000219 * infected * susceptible + -9.9107e-5 * infected * recovered + -9.9220e-5 * infected * infected
drecovered/dt = 0.896867 + 0.000307 * susceptible + 0.000146 * recovered + 0.030006 * infected + -1.2141e-6 * susceptible * susceptible + -4.6029e-6 * recovered * susceptible + -9.2664e-7 * recovered * recovered + 6.8980e-5 * infected * susceptible + 6.8553e-5 * infected * recovered + 6.9822e-5 * infected * infected
dsusceptible/dt = -0.968511 + -0.002333 * susceptible + 0.000581 * recovered + -0.03075 * infected + 3.3199e-6 * susceptible * susceptible + 8.8593e-6 * recovered * susceptible + 1.3215e-7 * recovered * recovered + -0.000287 * infected * susceptible + 3.1050e-5 * infected * recovered + 2.9917e-5 * infected * infected
```
**Ground truth**
```
dS/dt = -(β/N) S·I (β/N ≈ 3.2e-4)
dI/dt = (β/N) S·I - γ I
dR/dt = γ I (γ = 0.1)
```
## Damped pendulum (nonlinear, 2-state)
![Damped pendulum (nonlinear, 2-state): observed vs. simulated from the recovered law](/showcase-damped-pendulum.svg)
With a trigonometric library, recovery captures the sin(θ) restoring force and the linear viscous damping.
**Recovered laws**
```
domega/dt = -0.248323 * omega + -9.7726 * sin(theta)
dtheta/dt = 0.996217 * omega
```
**Ground truth**
```
dθ/dt = ω
dω/dt = -9.81 sin(θ) - 0.25 ω
```