Modeling A Spread of an Infectious Disease Using Python
With the spread of the Covid 19 virus, epidemiology became an increasingly popular topic among scientific communities. In epidemiology, It is vital to understand the spread of an infectious disease like covid 19. There are lots of advanced mathematical models to predict and understand the spread of infectious diseases. Here let's discuss and implement a simple mathematical model called the “SIR model” to understand the spread of a disease through an initial population. The idea behind this article is to show you that, how mathematics(ultimately Calculus) is utilized in understanding the spread of disease.
SIR Model Intro
When it comes to epidemic modeling, Experts commonly use compartmental models. This is a general mathematical modeling technique when studying things like the spread of infectious diseases. SIR model(Kermack and McKendrick model, 1927) is a simple form of a compartmental model. When we plug initial conditions such as initial population, contact rate, etc into the SIR model, It returns the behavior of the disease over time.
The Model
Imagine an infectious disease that spreads in a population of N individuals at any given time(t). According to the SIR model, an individual can be divided into 3 compartments.
Susceptible: Number of individuals who are not being exposed to the disease
Infected: Number of individuals who are exposed to the disease
Recovered: Number of individuals who are recovered from the disease
When we are modeling real-world phenomena using mathematical models, we put various assumptions based on a particular model. SIR model is based on the assumptions below.
1.The outbreak is short so the initial population remains constant.
2.The rate of increase in infectives is proportional to contact between susceptibles and infectives(at a constant rate).
3.Recovery rate is constant.
So as I said before, this model can compute the number of infected individuals with a contagious illness in a constant initial population over time(t). SIR model uses a set of time-dependent non-linear differential equations to do that.
These very beautiful equations (fig1.0) give us the rate of change between the number of susceptible people S(t), number of infected people R(t), and number of recovered people I(t) as a function of time(t). The three equations have two parameters, β contact rate, and γ recovery rate. Now we can numerically solve these equations for any set of inputs (N, β, γ). S, I, and R values as the initial conditions of the model.
Implementing the SIR model using Python
Prerequisite Non-Developers
A computer with a good internet connection. So now you can watch tons of tutorials about how to… how to… stuffs.
Here we go!
Let’s imagine there are 1000 individuals in a population with one infected person(I0=1). If the contact rate ( β ) is 0.2 and the recovery rate ( γ ) is 1 person for 10 days. And initial recovered( R0 ) persons are 0. Let’s see the spread of the disease for the next 200 days. We assume here that only the outbreak will last for 200 days.
To numerically solve the SIR model equations under the initial conditions given, we can use a programming language like R or Python. Here let's solve the model and plot the graph using Python. Because it's a bit of a hassle to solve these equations by hand. Isn't it?
Let's write the code directly on the .py file or even you can use Jupiter notebook.
First of all, let's import our libraries. Here we import Numpy and Scipy libraries to define and solve our nonlinear time-dependent ordinary differential equations. Numpy and Scirpy libraries are commonly used in scientific computing.
Once we import our python dependencies let's define the initial condition of our model. The initial population is N, the Initial number of infected and recovered individuals are I0 and R0 respectively. And the susceptible individuals to infection initially S0. Finally the contact rate (β) and average recovery rate (γ). Also, let's define the timeframe of the outbreak using a grid of time points using the NumPy linespace function.
Now, let's add differential equations. To do so let’s define a function called deriv(you can give any name )to integrate the equations for given function parameters. In case you need to understand this step more, you can simply search on youtube “ODE solving using python”. There are plenty of good tutorials.
Finally, we can plot S(t), I(t), and R(t) curves using the Matplotlib python library.
We can modify our plots to make them more human-readable using the python Matplotlib library defaults. Look at the piece of code below.
Bottom Line
I hope this article gives you a good grasp of mathematical modeling and gives you an idea about the real-world application of calculus and computer programming.
Thank You!