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
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:
The infectious count changes later, when a progression event occurs:
This separation introduces a delay between acquiring infection and becoming able to transmit.
The SEIR state space
For a closed population of size \(N\):
The full integer-valued state is \(X_n=(S_n,E_n,I_n,R_n)\). Its state space is:
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
Progression
Recovery
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\):
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
With \(\sigma=0.2\) per day, the mean latent period is 5 days.
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.
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:
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:
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.
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:
With initially exposed or infectious people, the size of the modelled outbreak includes them:
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
| Code | Meaning |
|---|---|
p_progression = sigma * E * dt | Calculates the probability of one exposed person becoming infectious. |
return S, E - 1, I + 1, R | Applies progression without changing the total population. |
if E == 0 and I == 0 | Stops only when neither exposed nor infectious people remain. |
initial_E + initial_I | Counts 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
- Change the seed and compare the exposed peak, infectious peak and extinction time.
- Set
S, E, I, R = 197, 3, 0, 0. Confirm that progression can create infectious people even though \(I_0=0\). - Increase
sigmato 0.5. Interpret the shorter mean latent period and changed peak timing. - Decrease
sigmato 0.1. Compare the separation between exposed and infectious curves. - Increase
dtuntil the conservative whole-state-space check rejects it. - 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.