08
Programming a CTMC SIR model
The SIR model changes one biological assumption from SIS: recovery gives lasting removal from infection. This creates an accumulating removed compartment and a well-defined final epidemic size.
Scenario: immunity after recovery
A closed community contains 200 people. Three are initially infectious and the other 197 are susceptible. An infectious person either infects a susceptible person or recovers. Recovered people do not return to susceptibility during the modelled period.
\(S\)
\(I\)
\(R\)
State changes and event rates
Infection
One susceptible person becomes infectious. Total population is unchanged.
Recovery
One infectious person enters the removed class permanently.
The total event rate is \(a_0=a_1+a_2\). When \(I=0\), both rates are zero, so the epidemic has ended.
Why SIR needs more state information than SIS
In SIS, \(S=N-I\), so \(I\) alone determines the state. In SIR, the same infectious count can occur with different susceptible counts and therefore different infection rates.
| State | \(I\) | \(S\) | Infection rate |
|---|---|---|---|
| \((190,5,5)\) | 5 | 190 | \(\beta(190)(5)/N\) |
| \((50,5,145)\) | 5 | 50 | \(\beta(50)(5)/N\) |
Thus we store \(S\), \(I\) and \(R\), although conservation \(S+I+R=N\) means only two are mathematically independent.
Two important outcomes
Peak infectious count
The largest value of \(I\) along one random trajectory. Its time is also random.
Final epidemic size
The total number infected during the outbreak:
Here the initial removed count is zero, so final epidemic size equals final \(R\). Do not confuse the initial compartment value \(R(0)\) with the reproduction number, commonly also written \(\mathcal R_0\).
Interactive Python laboratory
Run one complete SIR epidemic. The first figure shows the compartment path; the second shows how the cumulative number ever infected grows until extinction.
Output
Run the code to see the result.
Understand the stopping rule
while I > 0:
The loop runs while at least one infectious individual can cause infection or recovery. Once \(I=0\), no infectious individual remains, both rates are zero, and the outbreak is absorbed. Unlike the preceding general Gillespie lesson, no arbitrary time horizon is needed to complete this closed SIR outbreak.
Understand the outcome code
| Code | Biological meaning |
|---|---|
final_size = N - S | Everyone who is no longer susceptible has experienced infection. |
attack_proportion = final_size / N | Proportion of the initial population infected during this outbreak. |
trajectory["I"].idxmax() | Returns the row at which the largest recorded infectious count first occurs. |
event_log.tail(8) | Displays the final eight jumps leading to extinction. |
Biological interpretation
At first, many susceptible people are available, so infection may outcompete recovery. Later, susceptible depletion reduces \(\beta SI/N\) even if infectious people remain. Eventually recoveries remove the last infectious individuals.
One trajectory does not provide the probability distribution of peak size, duration or final size. It is one possible outbreak. A later lesson will simulate many independent trajectories to estimate those distributions.
Try these experiments
- Set
I = 1andS = 199. Try several seeds and look for early extinction. - Reduce
betabelowgamma. Compare peak and final size. - Start with 50 people already removed while keeping \(N=200\). Examine how fewer susceptible people change the infection rate.
- Check the identity: number of infection events plus the initial infectious count equals final \(R\).
What is unique to this lesson?
You have programmed permanent removal, susceptible depletion, certain eventual epidemic termination, and final epidemic size. The next SEIR lesson adds a latent exposed stage: infection and infectiousness no longer begin at the same event.