SIR Epidemiological Model¶
A deterministic compartmental model with dissmodel¶
The SIR model tracks the spread of an infectious disease through a population divided into three compartments:
- Susceptible — healthy individuals who can be infected
- Infected — individuals who carry and spread the disease
- Recovered — individuals who have recovered and gained immunity
How it works¶
At each time step, the model computes:
| Variable | Formula | Description |
|---|---|---|
| α | contacts × probability |
Force of infection |
| ΔI | I × α × (S / N) |
New infections |
| ΔR | I / duration |
New recoveries |
Where N = S + I + R is the total population.
Imports¶
In [2]:
Copied!
from dissmodel.core import Environment
from dissmodel.models.sysdyn import SIR
from dissmodel.visualization import Chart
from dissmodel.core import Environment
from dissmodel.models.sysdyn import SIR
from dissmodel.visualization import Chart
⚠️ Important: instantiation order¶
The Environment must always be created before any model (SIR, Chart).
Models connect to the active environment automatically when instantiated —
if no environment exists yet, the connection fails.
Environment → SIR → Chart
↑ ↑ ↑
first second third
In [3]:
Copied!
# 1. Environment must be created first
env = Environment()
# 1. Environment must be created first
env = Environment()
Setting up the model¶
The SIR model accepts the following parameters:
| Parameter | Description | Default |
|---|---|---|
susceptible |
Initial susceptible population | 9998 |
infected |
Initial infected population | 2 |
recovered |
Initial recovered population | 0 |
duration |
Steps an individual remains infectious | 2 |
contacts |
Average contacts per individual per step | 6 |
probability |
Transmission probability per contact | 0.25 |
In [4]:
Copied!
# 2. Model connects to the active environment automatically
SIR(
susceptible=9998,
infected=2,
recovered=0,
duration=2,
contacts=6,
probability=0.25,
)
# 2. Model connects to the active environment automatically
SIR(
susceptible=9998,
infected=2,
recovered=0,
duration=2,
contacts=6,
probability=0.25,
)
Out[4]:
SIR (sir.0)
Visualization¶
The Chart model redraws the time series at every simulation step.
It automatically tracks susceptible, infected, and recovered
because the SIR class is decorated with @track_plot — no extra
configuration needed.
In [5]:
Copied!
# 3. Chart also connects to the active environment automatically
Chart(show_legend=True, show_grid=True, title="SIR Model")
# 3. Chart also connects to the active environment automatically
Chart(show_legend=True, show_grid=True, title="SIR Model")
Out[5]:
Chart (chart.0)
Running the simulation¶
In [6]:
Copied!
env.run(30)
env.run(30)
Try it yourself¶
Go back and experiment:
- Increase
contactsto simulate a more contagious disease - Increase
durationto make the disease last longer - Decrease
probabilityto simulate a less transmissible disease - Try starting with more
infectedindividuals - Change
env.run(30)to run for more steps