← Deterministic Models

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

Infection flow\[\beta\frac{SI}{N}\]
Progression flow\[\sigma E\]
Recovery flow\[\gamma I\]

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.”

Susceptible\[\frac{dS}{dt}=-\beta\frac{SI}{N}\]
Exposed\[\frac{dE}{dt}=\beta\frac{SI}{N}-\sigma E\]
Infectious\[\frac{dI}{dt}=\sigma E-\gamma I\]
Recovered\[\frac{dR}{dt}=\gamma I\]
CompartmentFlow inFlow outRate of change
\(S\)NoneInfection− infection flow
\(E\)InfectionProgressioninfection − progression
\(I\)ProgressionRecoveryprogression − recovery
\(R\)RecoveryNone+ recovery flow

SIR and SEIR are different

SIRSEIR
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

\[S_{n+1}=S_n+\left(\frac{dS}{dt}\right)_n\Delta t\]
\[E_{n+1}=E_n+\left(\frac{dE}{dt}\right)_n\Delta t\]
\[I_{n+1}=I_n+\left(\frac{dI}{dt}\right)_n\Delta t\]
\[R_{n+1}=R_n+\left(\frac{dR}{dt}\right)_n\Delta t\]

As in the SIR program, calculate all derivatives from the same current state before storing any next value.

Run the complete SEIR simulation

Interactive PythonComplete SEIR Euler simulation

Output

Run the code to see the result.

New outcomes in the SEIR model

Exposed peaklargest value reached by \(E(t)\)
Infectious peaklargest value reached by \(I(t)\)
Peak delayinfectious peak time minus exposed peak time
CodeMeaning
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_timeNumerical 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.

  1. Calculate the corresponding mean exposed periods.
  2. Compare the infectious peak times.
  3. Compare the delay between the exposed and infectious peaks.
  4. Explain the graphical differences biologically.