SHSLib is a Lean 4 / Mathlib formalization of stochastic hybrid systems (SHS): processes with a discrete mode and a continuous state, which follow an ordinary differential equation between random jumps that switch the mode and reset the state. The core follows Hespanha (2005) and rests on Davis’s theory of piecewise-deterministic Markov processes (PDMPs). The library contains no sorry and no axiom: every theorem depends only on propext, Classical.choice and Quot.sound.
- API documentation: every module, definition and theorem, with search and links to the source. It is generated by doc-gen4, the tool behind the Mathlib documentation, and rebuilt on every push. Names from Mathlib and Lean link to the Mathlib documentation.
- The root module: the full list of modules.
- Module notes: what each group of modules proves, and how it maps onto the papers.
- Release notes and source code.
What is formalized
Alongside the Hespanha/Davis core, the library formalizes three further treatments of the same circle of ideas:
- Bect’s (2010) unifying Fokker–Planck–Kolmogorov equation for general stochastic hybrid systems;
- Bujorianu and Lygeros’s (2006) general theory of stochastic hybrid systems;
- the textbook derivations of the differential Chapman–Kolmogorov equation by Gardiner and by Ullah and Wolkenhauer, together with Gardiner’s Itô calculus, stochastic differential equations and approximation methods.
A further layer, SHSLib.Category, relates these frameworks through one category of generator presentations. Each framework enters as objects and morphisms, and Dynkin’s formula, the forward equation and the martingale problem transfer along every morphism. Composing morphisms gives cross-framework results, for example Bect’s generalized Dynkin formula for a Bujorianu–Lygeros system that satisfies Dynkin’s formula.
The extended generator
An SHS evolves in a mode \(q \in Q\) and a continuous state \(x \in \mathbb{R}^n\). Between jumps it flows by \(\dot{x} = f(q, x, t)\). Jumps of type \(\ell = 1, \dots, m\) occur with intensity \(\lambda_\ell(q, x, t)\) and send the system to mode \(\varphi_q^\ell(q, x, t)\) and state \(\varphi_x^\ell(q, x, t)\). For a test function \(\psi(q, x, t)\), the extended generator is
\[(L\psi)(q,x,t) = \frac{\partial \psi}{\partial x} \cdot f(q,x,t) + \frac{\partial \psi}{\partial t} + \sum_{\ell=1}^{m} \Big(\psi\big(\varphi_q^\ell(q,x,t), \varphi_x^\ell(q,x,t), t\big) - \psi(q,x,t)\Big)\, \lambda_\ell(q,x,t).\]The first two terms are the drift along the flow; the sum is the expected contribution of the jumps. Along the sample paths of the SHS,
\[\mathbb{E}\big[\psi(q(t), x(t), t)\big] = \psi(q_0, x_0, t_0) + \mathbb{E}\left[\int_{t_0}^{t} (L\psi)(q(s), x(s), s)\, ds\right],\]which is SHS.theorem1_generator_formula. The differential form, \(\frac{d}{dt}\mathbb{E}[\psi] = \mathbb{E}[L\psi]\), is SHS.eq10_differential_generator_formula. These identities are the basis for moment equations and for the Fokker–Planck equation of the state density.
Forward equations
Three further layers formalize the evolution of the law of the process.
The differential Chapman–Kolmogorov equation (Gardiner §3.4). For a Markov process on \(\mathbb{R}^n\) with drift \(A_i\), diffusion \(B_{ij}\) and jump rate \(W(z \mid x)\), the transition density satisfies
\[\frac{\partial p}{\partial t}(z) = -\sum_i \frac{\partial}{\partial z_i}\Big[A_i(z)\,p(z)\Big] + \frac{1}{2}\sum_{i,j} \frac{\partial^2}{\partial z_i \partial z_j}\Big[B_{ij}(z)\,p(z)\Big] + \int \Big( W(z \mid x)\,p(x) - W(x \mid z)\,p(z) \Big)\,dx .\]This is Gardiner (3.4.22), proved as Gardiner.differentialCK from the weak form (3.4.16), Gardiner.differentialCK_weak. Dropping terms gives the Fokker–Planck, Master and Liouville equations (SHSLib.Gardiner.Special). The third and all higher conditional moments are proved to vanish (Gardiner.CondThird.eq_zero, Gardiner.CondHigher.eq_zero), which makes the truncation exact rather than an approximation.
The same equation without integration by parts (Ullah and Wolkenhauer §8.2). Ullah.differentialCK reaches the identical right-hand side by splitting the gain–loss flux into a near and a far region. The two derivations are formally connected: Ullah.condDrift_iff_mom and Ullah.condDiffusion_iff_mom prove that that text’s conditions are Gardiner’s.
The generalized FPK equation for hybrid systems (Bect 2010). On a hybrid state space the density may not exist, so the equation is stated for the law \(\mu_t\) as a measure. Writing \(\langle \mu, \varphi \rangle = \int \varphi \, d\mu\),
\[\frac{d}{dt}\,\langle \mu_t, \varphi \rangle = \langle \mu_t,\, L\varphi \rangle + \big\langle r_t,\, (K - I)\varphi \big\rangle ,\]where \(L\) is the generator, \(K\) the reset kernel and \(r_t\) the mean jump intensity. This is Definition 7 / Eq. (14), GSHS.FPKData.IsWeakFPKSolution; its integrated form, the generalized Dynkin formula (13), is GSHS.FPKData.WeakDynkin. One equation covers diffusions, Markov chains, spontaneous jumps and forced jumps at the boundary, and Bect’s Example 18 specializes it back to Hespanha’s SHS.
Layers
| Layer | What it formalizes | Start here |
|---|---|---|
SHSLib.Core |
Hespanha’s SHS: structure, flow and extended generator; the bridge to Davis PDPs; reset compensators and their martingales; global existence of the flow | Core.Basic, Core.Generator |
SHSLib.Foundations |
Davis Ch. 1–2: measure theory, martingales, PDMP theory and the strong Markov property; special functions: Stirling, Beta, polygamma, Clopper–Pearson, order statistics | Foundations.DavisCh2 |
SHSLib.FPK |
Bect (2010): hybrid state space, Lévy systems, the generalized Dynkin formula, probability current, boundary conditions, worked solutions | FPK |
SHSLib.Gardiner |
Gardiner: the dCKE (§3.4–3.6), Itô calculus and SDEs (Ch. 4), approximation methods and adiabatic elimination (Ch. 6–7), escape problems (Ch. 9), SDE numerics (Ch. 10) | Gardiner |
SHSLib.Ullah |
Ullah and Wolkenhauer §8.2: the dCKE by near/far splitting | Ullah |
SHSLib.Bujorianu2006 |
Bujorianu and Lygeros (2006): hybrid state space, Markov strings, jump measures, resolvents, the strong generator and Dynkin’s formula | Bujorianu2006.HybridSpace |
SHSLib.Category |
The category of generator presentations that relates all of the above | Category |
A note on faithfulness
Formalizing a paper sometimes shows a stated result to be false as literally written. Where that happened, the library keeps the original statement in a comment, proves a counterexample certificate next to it, and then proves a corrected form. Hespanha’s Eq. (35), for example, is certified false by SHS.lemma1_linear_growth_bound_false and corrected in SHSLib.Core.GronwallGrowthBound. Nothing is quietly weakened.
Using SHSLib
Add it to your project’s lakefile.toml, and use the same Lean version as SHSLib’s lean-toolchain:
[[require]]
name = "SHSLib"
git = "https://github.com/cavargar/SHSLib"
rev = "main" # or a release tag, such as "v2.4.0"
References
The API documentation has a bibliography page, linked from the docstrings that cite these sources.
- M. H. A. Davis, Markov Models and Optimization, Chapman & Hall, 1993.
- J. P. Hespanha, “A model for stochastic hybrid systems with application to communication networks,” Nonlinear Analysis 62 (2005).
- J. Bect, “A unifying formulation of the Fokker–Planck–Kolmogorov equation for general stochastic hybrid systems,” Nonlinear Analysis: Hybrid Systems 4 (2010) 357–370.
- C. W. Gardiner, Handbook of Stochastic Methods for Physics, Chemistry and the Natural Sciences, Springer.
- M. Ullah and O. Wolkenhauer, Stochastic Approaches for Systems Biology, Springer, 2011.
- M. L. Bujorianu and J. Lygeros, “Toward a general theory of stochastic hybrid systems,” in Stochastic Hybrid Systems, LNCIS 337, Springer, 2006.
- R. Degenne, D. Ledvinka, E. Marion and P. Pfaffelhuber, Formalization of Brownian motion in Lean, arXiv:2511.20118 (2025).