09
Programming moment equations as an ODE system
A closed moment model becomes computable when retained moments and their derivatives are placed in a consistent state-vector function.
Choose the retained state vector
\[y(t)=\begin{pmatrix}m_1(t)\\m_2(t)\end{pmatrix}.\]
Here \(m_1=\mathbb E[I]\) and \(m_2=\mathbb E[I^2]\). The third moment is supplied by the normal closure.
Closed SIS equations
\[
m_3\approx3m_1m_2-2m_1^3,
\]
\[
m_1'=(\beta-\gamma)m_1-\frac{\beta}{N}m_2,
\]
\[
m_2'=
\left[2(\beta-\gamma)-\frac{\beta}{N}\right]m_2
-\frac{2\beta}{N}m_3
+(\beta+\gamma)m_1.
\]
Function contract
An ODE solver will call a function with current time, current moment vector and parameters. The function must return derivatives in the same order as the state vector.
def sis_moment_rhs(t, y, N, beta, gamma):
m1, m2 = y
...
return [dm1, dm2]Interactive Python laboratory
This page evaluates and checks the derivative function at several valid moment states. Numerical integration belongs to lesson 10.
Interactive PythonProgramming the moment ODE right-hand side
Output
Run the code to see the result.
Programming checks
- The returned derivative order matches \([m_1,m_2]\).
- Initial moments from deterministic \(I_0\) are \(m_1(0)=I_0\) and \(m_2(0)=I_0^2\).
- A valid moment state requires \(m_2-m_1^2\ge0\).
- The closure formula is isolated in its own function so assumptions are visible.
What this lesson adds
You can now organise retained moments into a state vector, implement closure explicitly, return derivatives in solver-compatible order and test the ODE right-hand side before numerical integration.