Simulating Compartmental Models in Epidemiology using Python & Jupyter Widgets

Implement and simulate SEIR and SEIRD models in Jupyter Notebooks.

Abhay Shukla
Towards Data Science

--

Note from the editors: Towards Data Science is a Medium publication primarily based on the study of data science and machine learning. We are not health professionals and the opinions of this article should not be interpreted as professional advice.

In this post, first, we will understand the basics of compartmental models used in Epidemiology and then we will use python to simulate two such models SEIR and SEIRD. In the end, you should be able to simulate the effect of changing different parameters, like the one shown in the animation below.

The following animation shows the effect of reduction in infection rate while holding other parameters constant and assuming the entire population is always susceptible to the disease.

Simulation of SEIRD Compartmental Model
Simulating the effect of reduction in Infection rate, holding all other parameters constant and assuming that the entire population is always susceptible to the disease

Note that this is purely a programming and data science exercise, so refrain from drawing conclusions based on these simulations. Leave that to the experts in epidemiology and medicine.

If you are familiar with the field and would instead like to jump directly into code, you can use check the SEIR and SEIRD Jupyter notebooks on my GitHub.

Let's start with the basics now.

Compartmental Models

Compartmental models are mechanistic models which try to explain the observed data by incorporating the mechanisms involved in the dynamics of the problem into the model.

Compartmental models are based on a system of ordinary differential equations which express the dynamics between different epidemiological states of a population. They are useful for short and long-term forecast of spread of a phenomenon, e.g. a disease, and can also be used to study the effect of different interventions (Chowell, 2017).

The word ‘compartment’ signifies the division the population into mutually exclusive groups called ‘compartments’. For example, in the popular SIR model, the three compartments are Susceptible, Infectious and Recovered (or Deceased). SIR model is used for diseases in which recovery leads to lasting resistance from the disease, such as in case of measles (Allen et al.). Its extremely important to understand the assumptions of these models and their validity for a particular disease, therefore, best left in the hands of experts :)

Building upon SIR model let’s see what are SEIR and SEIRD models and the dynamics between their compartments.

SEIR Models

SEIR stands for Susceptible Exposed Infectious and Recovered (or Deceased). The progression between these 4 epidemiological states are shown in figure 1.

SEIR Model
Figure 1: SEIR Model

The dynamics of the model are expressed in the system of differential equations shown in Figure 2,

Figure 2: SEIR model’s system of ODE

Refer to Figure 5 for description of parameters used in the equations.

SEIRD Model

SEIRD stands for Susceptible Exposed Infectious Recovered and Dead. Here the dynamics of dead individuals is modeled with a separate compartment. In some diseases, like Ebola, dead can still be infectious and so the dynamics of SEIRD model may also study the interactions between D and E individuals (Weitz et al.). For simplicity, we will assume that D are not infectious, as a result, progression between these 5 epidemiological states are shown in Figure 3.

SEIRD Model
Figure 3: SEIRD Model

The dynamics of the model are expressed in the system of differential equations shown in Figure 4.

Figure 4: SEIR model’s system of ODE
Figure 5: Notation

Having understood the basics, let's move to the simulation now.

Simulation of SEIR and SEIRD Models

The result of this implementation exercise will be a widget which will enable us to modify different parameters of the model and observe its effect on the curves of SEIR and D. We will be using Jupyter Widgets for interactivity and the final widget will be similar to the one shown in Figure 6.

SEIRD Simulator
Figure 6: SEIRD Simulator Widget

Implementation of simulation will require us to define a system of Ordinary Differential Equation (ODE), solve it using a solver and forecast for the time span of interest and finally tie it all together with Jupyter Widgets which will enable interactivity.

I am going to heavily use the super awesome tutorials Solve Differential Equations in Python, Computational Statistics in Python and Ordinary Differential Equations (ODE) with Python and Jupyter for implementation for these steps. Let's take SEIR model and go step by step for clarity of implementation.

Simulation of SEIR Model

Define ODEs

Here we will code the ODEs in Figure 2 in a python function and return the system of ODE in a list. Let's see how,

System of ODEs for SEIR

Define Solver

Next, define a function which will take as inputs, time span, initial conditions for solving the system of ODE and model parameters and returns the resulting predictions after solving the system of ODEs. Note that the initial conditions are required for solving differential equations to estimate the constants of integration. We will be using scipy’s odeint as solver. Let's look at the code now,

Function for solving ODEs of SEIR Model

Jupyter Widgets for Interactivity

Now, let us tie it all together using widgets in ipywidgets library to enable interactivity and help us in simulation.

Following the tutorial Ordinary Differential Equations (ODE) with Python and Jupyter for implementation, we will write the main function where we will call the helper functions defined above. Let's see it in code…

main function required for interact method in ipywidgets.widgets

The final piece of the puzzle is to call interact function with arguments as main function implemented above and values of parameters required by the main function, as read by the corresponding parameter widgets. The widgets used are IntSlider and FloatSlider depending on the type of input. Let's see this final piece of code which makes everything work!

interact method for creation of widget

You can find the complete code in the Jupyter notebook Simulation of SEIR Model. Once you run the notebook, you will get a widget similar to the one shown in Figure 6.

Simulation of SEIRD Model

The corresponding ode_model, ode_solver, main and interact function for SEIRD model are,

Define ODEs

System of ODEs for SEIRD

Define Solver

Function for solving ODEs of SEIRD Model

Jupyter Widgets for Interactivity

main function required for interact method in ipywidgets.widgets

and finally,

interact method for creation of widget

Again, you can find the complete code in the Jupyter notebook Simulation of SEIRD Model.

Conclusion

In this post we learned about,

  1. Compartmental models in Epidemiology
  2. SIR, SEIR, SEIRD models — their compartments and dynamics expressed as a system of ODEs.
  3. Code for solving ODE’s in python
  4. Code for simulating the compartmental models using Juputer widgets in python.

Hope you have fun working with these models.

References

[1] Computational Statistics in Python (https://people.duke.edu/~ccc14/sta-663/CalibratingODEs.html)

[2] Solve Differential Equations in Python (https://www.youtube.com/watch?v=VV3BnroVjZo)

[3] SEIR and SEIRS models (https://www.idmod.org/docs/hiv/model-seir.html)

[4] Compartmental models in epidemiology (https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SEIR_model)

[5] Ordinary Differential Equations (ODE) with Python and Jupyter (https://elc.github.io/posts/ordinary-differential-equations-with-python/)

[6] SEIRS+ Model (https://github.com/ryansmcgee/seirsplus)

[7] R Gupta , G Pandey , P Chaudhary , S Pal, SEIR and Regression Model based COVID-19 outbreak predictions in India (2020), medRxiv preprint

[8] G Chowell, Fitting dynamic models to epidemic outbreaks with quantified uncertainty: A primer for parameter uncertainty, identifiability, and forecasts (2017), Infectious Disease Modelling Volume 2, Issue 3, August 2017, Pages 379–398

[9] LJ Allen, MA Jones, CF Martin, A discrete-time model with vaccination for a measles epidemic (1991), Mathematical Biosciences Volume 105, Issue 1, June 1991, Pages 111–131

[10] JS Weitz, J Dushoff, Modeling Post-death Transmission of Ebola: Challenges for Inference and Opportunities for Control (2015), Scientific Reports volume 5, Article number: 8751 (2015)

--

--

Data Science @ Meesho, Ex- Airtel, Swiggy, [24]7.ai https://www.linkedin.com/in/shuklaabhay/ #DataScience #ML #AI #Statistics #Reading #Music #Running