← Moment Equations

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

\[m_1(0)=I_0,\qquad m_2(0)=I_0^2,\qquad v(0)=0.\]

If the initial state were random, its first two moments would need to be supplied instead.

Solver inputs

InputPurpose
funMoment derivative function.
t_spanStart and final time.
y0Initial retained moments.
t_evalTimes at which results are returned.
rtol, atolRelative and absolute local error controls.

Interactive Python laboratory

Interactive PythonSolving closed moment equations

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.