08
Programming the SEIR model with Euler’s method
The SEIR model introduces an exposed compartment. A newly infected person enters \(E\) before becoming infectious, allowing the model to represent a delay between infection and infectiousness.
Biological scenario
A closed population contains 1,000 people. Initially, 990 are susceptible, 5 are exposed, 5 are infectious and none have recovered.
We use \(\beta=0.3\) per day, \(\sigma=0.2\) per day and \(\gamma=0.1\) per day. We want to simulate the epidemic for 200 days and compare the exposed and infectious peaks.
The new exposed compartment
In this basic SEIR model, exposed people are infected but are not yet infectious. They progress from \(E\) to \(I\) at rate \(\sigma E\).
Three biological flows
The progression parameter is related to the mean exposed period:
\[\text{mean exposed period}=\frac{1}{\sigma}.\]
For \(\sigma=0.2\) per day, the mean exposed period is \(1/0.2=5\) days.
Construct the four equations
For each compartment, use “flow in minus flow out.”
| Compartment | Flow in | Flow out | Rate of change |
|---|---|---|---|
| \(S\) | None | Infection | − infection flow |
| \(E\) | Infection | Progression | infection − progression |
| \(I\) | Progression | Recovery | progression − recovery |
| \(R\) | Recovery | None | + recovery flow |
SIR and SEIR are different
| SIR | SEIR |
|---|---|
| Newly infected people enter \(I\) immediately. | Newly infected people first enter \(E\). |
| The infection flow appears directly in \(dI/dt\). | The infection flow appears in \(dE/dt\); progression enters \(dI/dt\). |
| No explicit delay before infectiousness. | The exposed period creates a delay before infectiousness. |
| Three compartment arrays. | Four compartment arrays. |
Step 1: Write the SEIR rate function
def seir_rates(S, E, I, R, beta, sigma, gamma):
N = S + E + I + R
infection_flow = beta * S * I / N
progression_flow = sigma * E
recovery_flow = gamma * I
dS_dt = -infection_flow
dE_dt = infection_flow - progression_flow
dI_dt = progression_flow - recovery_flow
dR_dt = recovery_flow
return dS_dt, dE_dt, dI_dt, dR_dt
The function returns four derivatives in the same order as the compartments.
Step 2: Add exposed storage
S = np.zeros(number_of_steps + 1)
E = np.zeros(number_of_steps + 1)
I = np.zeros(number_of_steps + 1)
R = np.zeros(number_of_steps + 1)
S[0], E[0], I[0], R[0] = S0, E0, I0, R0
The arrays have matching lengths. Index \(n\) represents one common epidemic state \((S_n,E_n,I_n,R_n)\).
Step 3: Update all four compartments
As in the SIR program, calculate all derivatives from the same current state before storing any next value.
Run the complete SEIR simulation
Output
Run the code to see the result.
New outcomes in the SEIR model
| Code | Meaning |
|---|---|
np.argmax(E) | Index at which the exposed population is largest |
np.argmax(I) | Index at which the infectious population is largest |
time[infectious_peak_index] | Time corresponding to the infectious peak |
infectious_peak_time - exposed_peak_time | Numerical delay between the two peaks |
Biological interpretation
New infections increase the exposed population first. Only after progression do they increase the infectious population. Consequently, the exposed curve usually responds earlier, while the infectious curve is delayed and smoothed by the exposed period.
The parameter \(\sigma\) controls this delay. Increasing \(\sigma\) shortens the mean exposed period and moves people into \(I\) more rapidly. Decreasing \(\sigma\) lengthens the delay.
Important model and numerical conditions
- The basic model assumes exposed people do not transmit infection.
- All initial compartments and parameters must be non-negative.
- The closed population satisfies \(S+E+I+R=N\).
- \(\Delta t\) must be positive and sufficiently small.
- All four Euler derivatives must be evaluated at the same current state.
- The simulation must be long enough to include both peaks and the epidemic decline.
- The interpretation \(1/\sigma\) assumes a constant per-person progression rate.
Change the exposed period
Run the model with sigma = 0.5 and then with sigma = 0.1.
- Calculate the corresponding mean exposed periods.
- Compare the infectious peak times.
- Compare the delay between the exposed and infectious peaks.
- Explain the graphical differences biologically.