← Discrete-Time Markov Chains

09

Programming a DTMC SEIR model

SEIR separates infection from infectiousness. A newly infected person first enters an exposed compartment and becomes infectious only after a latent period.

The new biological idea: exposure before infectiousness

Susceptible\(S\)
→
Exposed\(E\)
→
Infectious\(I\)
→
Removed\(R\)
\[ S\xrightarrow{\text{infection}}E \xrightarrow{\text{progression}}I \xrightarrow{\text{recovery}}R. \]

An exposed person is infected but not yet infectious in this basic model. Exposure and progression are therefore different biological events.

Why not move directly from \(S\) to \(I\)?

In SIR, a new infection immediately increases \(I\). In SEIR, the same infection event changes:

\[ (S,E,I,R)\longrightarrow(S-1,E+1,I,R). \]

The infectious count changes later, when a progression event occurs:

\[ (S,E,I,R)\longrightarrow(S,E-1,I+1,R). \]

This separation introduces a delay between acquiring infection and becoming able to transmit.

The SEIR state space

For a closed population of size \(N\):

\[ S_n+E_n+I_n+R_n=N. \]

The full integer-valued state is \(X_n=(S_n,E_n,I_n,R_n)\). Its state space is:

\[ \mathcal S= \left\{(s,e,i,r)\in\mathbb Z_{\ge0}^{4}: s+e+i+r=N\right\}. \]

Only three compartment counts are mathematically independent because the fourth can be obtained from conservation. We nevertheless store all four because this makes biological interpretation and validation clearer.

Three events and four outcomes

Under the one-event-per-step assumption, three biological events are possible, plus no change:

Infection

\[(s,e,i,r)\to(s-1,e+1,i,r).\]

Progression

\[(s,e,i,r)\to(s,e-1,i+1,r).\]

Recovery

\[(s,e,i,r)\to(s,e,i-1,r+1).\]

No change leaves \((s,e,i,r)\) unchanged. These four outcomes must be mutually exclusive and their probabilities must sum to 1.

Transition probabilities

Let \(\beta\) be the transmission parameter, \(\sigma\) the exposed-to-infectious progression rate and \(\gamma\) the recovery rate. For a sufficiently short \(\Delta t\):

\[ \begin{aligned} p_{\mathrm{inf}} &=\beta\frac{SI}{N}\Delta t,\\ p_{\mathrm{prog}} &=\sigma E\Delta t,\\ p_{\mathrm{rec}} &=\gamma I\Delta t,\\ p_{\mathrm{stay}} &=1-p_{\mathrm{inf}}-p_{\mathrm{prog}}-p_{\mathrm{rec}}. \end{aligned} \]

Infection still depends on susceptible and infectious people—not exposed people—because this basic SEIR model assumes exposed individuals do not transmit.

Interpret \(\sigma\) correctly

Mean latent period
\[1/\sigma.\]

With \(\sigma=0.2\) per day, the mean latent period is 5 days.

Mean infectious period
\[1/\gamma.\]

With \(\gamma=0.1\) per day, the mean infectious period is 10 days.

These are mean waiting periods implied by constant progression and recovery rates. They do not mean that every individual progresses after exactly 5 days or recovers after exactly 10 days.

The crucial extinction condition

\(I=0\) is not sufficient

If \(I=0\) but \(E>0\), exposed people can progress into the infectious compartment. The epidemic process can continue.

\(E=0\) and \(I=0\)

When both infected compartments are empty, infection, progression and recovery probabilities are all zero. The state is absorbing.

\[ \text{Extinction condition:}\qquad E=0\quad\text{and}\quad I=0. \]

This is why the simulation must use if E == 0 and I == 0, not merely if I == 0.

Probability validity now includes progression

The time-step condition becomes:

\[ p_{\mathrm{inf}}+ p_{\mathrm{prog}}+ p_{\mathrm{rec}} \le1. \]

Adding a compartment adds an event and therefore another contribution to the total event probability. A time step valid for an SIR model is not automatically valid after SEIR progression is added.

A simple sufficient bound over the whole state space is:

\[ \left( \frac{\beta N}{4} +\sigma N +\gamma N \right)\Delta t\le1. \]

The bound is conservative but easy to calculate and guarantees non-negative no-change probability everywhere.

Interactive Python laboratory

Click Run code to simulate one SEIR trajectory. The output reports both exposed and infectious peaks, completed outbreak size, event counts and two graphs.

Interactive PythonDTMC SEIR epidemic

Output

Run the code to see the result.

How the random intervals change

With three events, the cumulative selection rule has three boundaries:

if u < p_infection:
    event = "infection"
elif u < p_infection + p_progression:
    event = "progression"
elif u < p_infection + p_progression + p_recovery:
    event = "recovery"
else:
    event = "no change"

The order could be changed if the cumulative boundaries were changed consistently. What matters is that interval lengths equal the corresponding probabilities and that the intervals do not overlap.

Interpret the two peak times

The exposed and infectious peaks are different random outcomes. In a typical growing outbreak, the exposed peak may occur before the infectious peak because individuals must progress through \(E\) before joining \(I\).

The code must calculate both times from the trajectory. We should not assume that their difference equals exactly \(1/\sigma\). The mean latent period describes individual waiting, whereas population peaks depend on the entire changing epidemic.

Final epidemic size in SEIR

At a completed extinction state, \(E=I=0\). If \(R_0=0\), the final epidemic size is:

\[ Z=R_\infty=N-S_\infty. \]

With initially exposed or infectious people, the size of the modelled outbreak includes them:

\[ Z=E_0+I_0+(S_0-S_\infty). \]

The expression \(S_0-S_\infty\) counts new transmission infections. Adding \(E_0+I_0\) includes the infections already present at time zero.

Understand the model-specific code

CodeMeaning
p_progression = sigma * E * dtCalculates the probability of one exposed person becoming infectious.
return S, E - 1, I + 1, RApplies progression without changing the total population.
if E == 0 and I == 0Stops only when neither exposed nor infectious people remain.
initial_E + initial_ICounts infections already present at the beginning.
trajectory["E"].idxmax()Locates the first stored row attaining the exposed peak.
.reindex([...], fill_value=0)Displays every event category even if one event never occurred in this realisation.

Try these experiments

  1. Change the seed and compare the exposed peak, infectious peak and extinction time.
  2. Set S, E, I, R = 197, 3, 0, 0. Confirm that progression can create infectious people even though \(I_0=0\).
  3. Increase sigma to 0.5. Interpret the shorter mean latent period and changed peak timing.
  4. Decrease sigma to 0.1. Compare the separation between exposed and infectious curves.
  5. Increase dt until the conservative whole-state-space check rejects it.
  6. Set max_days = 20. Explain why final epidemic size is reported as incomplete when \(E>0\) or \(I>0\).

Biological interpretation

The exposed compartment delays the appearance of infectious individuals after transmission. This delay can shift the infectious peak and allows hidden infected individuals to remain even when no infectious person is currently observed.

The SEIR model is appropriate only when a distinct latent stage is biologically meaningful. If exposed individuals can also transmit, the infection probability must be modified rather than silently using the basic SEIR assumption.

What this lesson has added

  • Infection moves people from \(S\) to \(E\), not directly to \(I\).
  • Progression is a third event with probability \(\sigma E\Delta t\).
  • The latent period separates infection from infectiousness.
  • \(I=0\) is not absorbing when exposed people remain.
  • Extinction requires both \(E=0\) and \(I=0\).
  • Time-step validity must include infection, progression and recovery probabilities.
  • Exposed and infectious peaks are distinct stochastic outcomes.

Lesson 10 changes viewpoint completely: instead of selecting one random path, it represents and updates the probability distribution over all states using a transition matrix.