← Continuous-Time Markov Chains

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.

Susceptible
\(S\)
infection →
Infectious
\(I\)
recovery →
Removed
\(R\)
Key consequence: every infection permanently moves one person out of \(S\), and every recovery permanently moves one person into \(R\). Hence \(R\) never decreases and the epidemic cannot persist indefinitely through reinfection.

State changes and event rates

Infection

\[(S,I,R)\to(S-1,I+1,R),\qquad a_1=\beta\frac{SI}{N}.\]

One susceptible person becomes infectious. Total population is unchanged.

Recovery

\[(S,I,R)\to(S,I-1,R+1),\qquad a_2=\gamma I.\]

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)\)5190\(\beta(190)(5)/N\)
\((50,5,145)\)550\(\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:

\[N-S_{\mathrm{final}}=R_{\mathrm{final}}-R_0^{\mathrm{initial}}.\]

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.

Interactive PythonCTMC SIR model

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

CodeBiological meaning
final_size = N - SEveryone who is no longer susceptible has experienced infection.
attack_proportion = final_size / NProportion 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

  1. Set I = 1 and S = 199. Try several seeds and look for early extinction.
  2. Reduce beta below gamma. Compare peak and final size.
  3. Start with 50 people already removed while keeping \(N=200\). Examine how fewer susceptible people change the infection rate.
  4. 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.