How to build a SIR model with Numpy
The SIR model is a well-known way to simulate disease spreading for researchers. It has been used for a lot of research considering COVID-19. The model consists of the mutually exclusive susceptible, infected, and recovered compartments. The base model is simple and can give a good indication of the course of a disease. Using the equations for the different compartments and initial states, the disease model can be simulated.
The susceptible, Infectious and Recovered (SIR) model is one of the popular compart-
mental mathematical models used to study behavior of disease data. It aims to predict
the number of susceptible, infectious, and recovered individuals. Here, the population is
divided into one of the three mutually exclusive compartments, that is: an individual can
be in exactly one of the compartments.
Although the name of the Recovered compartment
can be misleading because it represents not only individuals that have recovered from the
disease, but also individuals that are no longer contagious and individuals that have passed
away because of the disease. For that reason, it can also be understood as the removed
compartment. This model is often used partly due to its simplicity: researchers can
model disease behavior easily.
There are many extensions possible for the SIR model.
The model used in this simulation is the basic SIR model because it is simple and effective
to apply, and it offers a good starting foundation for further research possibilities. In this
model, immunity is permanent: an individual cannot return into the susceptible compartment. A lot of research has been done on the SIR model and its different applications.
One research aimed to predict the hospital capacity needed during the COVID-19 pandemic.
Other research primarily shows predictions on a single disease. This research will aim
to offer insights on how differences in a population, meaning the susceptible and infected
individuals, the infection rate, and the recovery rate will influence the course of the disease
spread prediction. Any significant results that come up can help prepare countries for new
pandemics. The focus of this research lies entirely on the basic SIR model, where factors
such as immunity, immigration, birth rates and other death rates are disregarded entirely.
Methodology
To measure how the different variables influence the disease spreading, a simulation will be used. The simulation will be programmed from scratch, and it will provide plots that consider the variables. The primary relation between the compartments susceptible, infected and recovered can be written as:
N = St + It + Rt
Here, N is the total population size, while St, It and Rt are the sizes of susceptible, infected, and recovered individuals as time (t) passes [3]. This equation shows that the sum of the compartment population sizes remains constant, no matter how the three compartments develop over time. This then implies the derivate equation of this previous equation [3]:
dS/dt + dI/dt + dR/dt = 0
Constant rates
The model uses a set of differential equations to describe the flow of individuals be- tween these compartments. The equations describe how the number of individuals in each compartment changes over time based on the rates of transmission and recovery.- The transmission rate (β) is a constant that describes the rate at which the susceptible becomes infected when they encounter infected individuals. It can be read as the number of contacts needed to infect another person. Transmission are unique for different diseases.
- The constant recovery rate (γ) describes the rate at which the infected recover and become "immune" to the disease (meaning they get moved into the recovered compartment).
- The inverse of the recovery rate (γ−1) represents the average duration of infection before an individual recovers. For example, given the recovery rate (γ) 0.2, then the average duration of infection is 1 / 0.2 = 5 days.
For the number of recovered individuals, you multiply the recovery rate by the number of infected, as the number of recovered follows directly from the number of infected. To get the number of susceptible individuals after t time, you multiply the transmission rate with the number of susceptible and infected individuals. Finally, the number of infectious individuals is given by deducting the number of recovered from the number susceptible. Thus, the equations for the rates of changes are given as follows:
dS / dt = - gamma * St * It
dI / dt = beta * St * It - gamma * It
dR / dt = gamma * It
For initial values, S0 and I0 both need to be larger than 0.
Let's put this all in code!
Like we just learned, writing a code for a basic SIR graph is not that hard. Let's put all the logic to use in code.
import numpy as np
import matplotlib.pyplot as plt
beta= 0.4;
gamma= 0.3;
N= 20000000;
I0= 20000000;
R0= 20000000;
S0= N-I0 -R0;
tmax= 365;
dt= 1/24;
t= np.arange(0, tmax+dt, dt);
S= np.zeros_like(t);
I= np.zeros_like(t);
R= np.zeros_like(t);
S[0]= S0;
I[0]= R0-I0 -R0;
R[0]= R0;
dt= 1/24;
for i in range(1, len (t)):
dSdt= - beta * S[i-1] * I[i]/ N ;
dIdt= beta * S[i-1] * I[i]/ N -gamma * I[i];
dRdt= gamma * I[i];
S[i]= S[i-1] + dSdt * dt;
I[i]= I[i-1] + dIdt * dt;
R[i]= R[i-1] + dRdt * dt;
plt.plot(t,S, label= "Susceptible")
plt.plot(t,I, label= "Infected", color= "r")
plt.plot(tt,R, label= "Recovered",color= "g")
plt.xlabel("Time in days")
plt.ylabel("Number of individuals")
plt.legend()
plt.show()
We could expand on this further by creating a function, which would take the parameters of beta, gamma, N and initial SIR values, to create multiple graphs with different variables. We could then use this function to create an easy app, using tkinter. But this might be a good exercise for another time!
In Conclusion
Accurately predicting the course of a disease is a difficult task, without accurately modelling behaviour. The more of the external factors you take into consideration, the more accurate simulating becomes. Nevertheless, you can create a simple python SIR alghorithm, by using simple maths.