10
Solving moment equations numerically
A numerical solver advances the retained moments through time; solver checks and reconstructed-moment checks are both necessary.
Initial moments
If the initial infectious count is known exactly as \(I(0)=I_0\), then
If the initial state were random, its first two moments would need to be supplied instead.
Solver inputs
| Input | Purpose |
|---|---|
fun | Moment derivative function. |
t_span | Start and final time. |
y0 | Initial retained moments. |
t_eval | Times at which results are returned. |
rtol, atol | Relative and absolute local error controls. |
Interactive Python laboratory
Output
Run the code to see the result.
Why tolerate a tiny negative residual?
Variance is mathematically non-negative. A computed value such as \(-10^{-12}\) can arise from floating-point subtraction of nearly equal numbers. The code rejects materially negative variance below \(-10^{-7}\) and clips only smaller numerical residuals to zero. This is different from hiding a failed closure or solver.
Solver accuracy is not closure accuracy
Tight tolerances reduce numerical integration error in the closed ODEs. They do not make the normal closure exact. Comparison with stochastic simulations is still required.
What this lesson adds
You can now supply initial moments, solve a closed system with solve_ivp, inspect solver success, reconstruct variance and separate numerical tolerance from closure error.