The world’s leading publication for data science, AI, and ML professionals.

Simple modeling of “flattening the curve” and “lifting lockdown”

A simple Python script of a fundamental epidemiological model for demonstrating the impact of social distancing.

Image: (Author-produced with Pixabay free image and programming)
Image: (Author-produced with Pixabay free image and programming)

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 or epidemiologists, and the opinions of this article should not be interpreted as professional advice. To learn more about the coronavirus pandemic, you can click here.

Introduction

Covid-19 – the greatest global crisis since World War II and the largest global pandemic since the 1918–19 Spanish Flu is upon us today. Everybody is looking at the daily rise of the death toll and the rapid, exponential spread of this novel strain of the virus.

Data scientists, like so many people from all other walks of life, may also be feeling anxious. They may also be eager to see if they can contribute somehow to the fight against this highly infectious pathogen.

Source: Pixabay (Free for commercial use)
Source: Pixabay (Free for commercial use)

There can be many avenues for data scientists and statistical modeling professionals to contribute to the cause. In almost all cases, they should be working closely with domain experts – virologists, healthcare professionals, epidemiologists. Without such active collaboration and teamwork, it is dangerous and meaningless to embark on a project of predictive modeling or forecasting the spread of the disease and mitigation efforts.

As the widely popular data modeling website Five-Thirty-Eight argues, it is freaking hard to make an accurate COVID-19 model.

Why It’s So Freaking Hard To Make A Good COVID-19 Model

However, even without venturing into actual predictive modeling, it is possible to demonstrate the efficacy of the only basic weapon that we all have against the COVID-19 virus – "social distancing" – by a simple programmatic approach.

Source: Pixabay (Free for commercial use)
Source: Pixabay (Free for commercial use)

In this article, we will show how to build such a demo in Python, following a simple yet fundamental epidemiological theory.

This is not an attempt to build any forecasting or predictive model. No actual data, other than some fictitious numbers, will be used to generate the plots and visualizations.

The goal is to show that with only a basic dynamical model, it is possible to understand key concepts such as the "flattening the curve", "herd immunity", and "lifting the lockdown too quickly". And, you can program such a model with rudimentary Python and mathematical knowledge.

However, even without venturing into actual predictive modeling, it is possible to demonstrate the efficacy of the only basic weapon that we all have against the COVID-19 virus – "social distancing" – by a simple programmatic approach.

The (epidemiological) model and the Python class

We created a Python class called SEIRclass based on the SEIR model of basic epidemiology. The script is in the SEIRclass.py file in my Github repo and the class needs to be imported for this demo.

The SEIR model

The SEIR model categorizes the entire population into four groups,

  • Susceptible: People who have never been infected and have not have developed any immunity yet.
  • Exposed: Folks who have been exposed to the virus but have not shown any symptoms or cannot be termed as ‘infected’. The virus is in the incubation stage.
  • Infected: The fraction of the population who are actively infected, showing mild or severe symptoms, and might be in need of hospitalization. This is the category that matters most for healthcare capacity planning and almost all fatalities will come from this population.
  • Recovered: The folks on the other side of the nightmare! Those who were infected and recovered from it. Note, these people are assumed to have a ‘permanent immunity‘ in this simple model i.e. they cannot fall back into the susceptible or infected population again.
Source: Wikipedia (Creative Commons license)
Source: Wikipedia (Creative Commons license)

A reference article

The idea is inspired by this article which lays down the epidemiological model and the dynamical equations clearly: Social Distancing to Slow the Coronavirus.

Social Distancing to Slow the Coronavirus

Here is the Github repo corresponding to the article above: https://github.com/dgoldman0/socialdistancing.

Dynamical equations

Almost all compartmental models of infectious disease spread are dynamical in nature i.e. they are described by a set of first-order/second-order differential equations with time as an independent variable. The basic dynamical equations are as follows,

To be noted, that the famous reproduction number R0 is the ratio of beta and gamma from this model.

All the coefficients (alpha, beta, gamma, etc.) are assumed to be constant here. In reality, the infectiousness of the pathogen may vary over time.

The first two equations change slightly with the introduction of fourth parameters 𝜌 which represents the social mixing. The higher the 𝜌 _t_he less social distancing. It can have a value from 0 to 1.

Here are a couple of articles showing how Python programming can be used to numerically solve such dynamical systems of equations.

Dynamic Simulation in Python

A Beginner’s Guide to Simulating Dynamical Systems with Python

The core Python class

The partial class definition is shown below. A test Jupyter notebook can be found [here](https://github.com/tirthajyoti/Covid-19-analysis/blob/master/Notebook/SEIRclass.py). The model class is here. They are released under MIT license, and therefore, you are encouraged to download and play with it, enhance it by adding other useful methods, etc.

Various methods

We define various utility methods in this class,

  • reinitialize: Re-initializes the model with new values (of the population)
  • set_params: Sets the values of the dynamical parameter
  • reset: Resets the internal lists to zero-state i.e. initial values
  • run: Runs the dynamical simulation for a given amount of time (with a given time step delta)
  • plot: Plots the basic results of the simulation on a time-axis

A simple demo

Dynamical simulation and plotting

We start by importing the class and making an object as an instance.

Then, we can run a dynamical simulation for some length of time – say 90 units of time. This can be thought of as 90 days or any other length of time.

We can plot the result of this simulation simply by,

Image: A representative simulation result of the SEIR model (author-produced).
Image: A representative simulation result of the SEIR model (author-produced).

Flattening the curve – how social distancing impacts the peak

Next, we run the simulation for various social distancing factors to examine their impact on the peak of the curves. Note that, here, a higher value means less social distancing i.e. higher social mixing/interactions.

We observe that a higher degree of social mixing leads to a higher peak for the infected population fraction, which can potentially overwhelm the healthcare system capacity.

Image: Impact of social distancing factor on the peak of the infected fraction (author-produced).
Image: Impact of social distancing factor on the peak of the infected fraction (author-produced).

Tweaking social distancing – early end of the lockdown

Now, we examine what happens with early termination of lockdown or "stay-at-home" measures, which aim to reduce social mixing i.e. increase social distancing.

We start with a certain value of the social distancing factor, let it run for a certain amount of time, and then relax the norms i.e. increase the value of the social distancing factor, and see what happens to the infected population fraction. We will observe a second peak appearing. Depending on the various social and epidemiological factors, this second peak can be higher than the first one.

For this study, we will use two SEIR models,

  • The first model will run for a certain amount of time with a low social mixing factor (strict social distancing norm)
  • The final values of the population proportion from this model will be fed into the second model.
  • The second model will run with a higher value of social mixing (relaxed social distancing norm)
Image: Does early termination of lockdown result in a second peak? (author-produced)
Image: Does early termination of lockdown result in a second peak? (author-produced)

Optimizing the lockdown periods can reduce all peaks

After the "stay-at-home" mandates end and social distancing norms are somewhat relaxed, there will inevitably be another peak of the infected cases. The question is how high that would be and whether a longer lockdown can somehow dampen that.

With this simple model, we can observe that letting the first ‘lockdown’ run for a long time can potentially reduce the absolute value of both peaks – one during the lockdown and the one which inevitably comes after the lockdown ends.

This is due to the impact of an enhanced "herd immunity" i.e. with a longer lockdown, the susceptible population gradually reduces slowly over time (without producing a sharp peak). Therefore, when the lockdown is relaxed, there is a less susceptible population for the virus to infect!

We also note that beyond 150 days, the susceptible fraction level does not go down significantly i.e. there is no visible reduction from 150 days case to the 180 days case to the 210 days case. Therefore, if the goal is to build herd immunity, the lockdown extension may be effective only up to a certain period.

Image: The susceptible fraction goes down with longer lockdown periods (author-produced)
Image: The susceptible fraction goes down with longer lockdown periods (author-produced)

The example of ‘tweaking social distancing’ above showed a second peak of as high as > 0.05 i.e. 5% of the population getting infected. Here, we show that letting the first lockdown running longer can potentially reduce both peaks. In fact, we see the first peak is higher in this experiment and it is only around 0.018 or 1.8%.

Of course, this scenario depends on the initial infection rate, disease parameters, and social dynamics. This is just one possible outcome that can be highlighted by our program.

Image: Longer lockdown may help reducing peaks in a holistic manner (author-produced)
Image: Longer lockdown may help reducing peaks in a holistic manner (author-produced)

This is a demo, not an actual model of COVID-19

Again, this work was done to show how a widely-used dynamical model of epidemiology can be programmed using a simple Python script.

Once such a programmatic model is built, many popular phrases like ‘flattening the curve‘, ‘herd immunity‘, and ‘spiking from an early end of lockdown‘ can be gauged by playing with the model parameters and simulation settings.

The goal is to show that with only a basic dynamical model, it is possible to understand key concepts such as the "flattening the curve", "herd immunity", and "lifting the lockdown too quickly". And, you can program such a model with rudimentary Python and mathematical knowledge.

The model is extremely simplified. There is no geospatial interaction, which is at the core of the most advanced infectious disease models employing graph-theoretic algorithms.

The model does not feature any statistical estimation, which is at the heart of estimating the parameters (alpha, beta, gamma, etc.) for running a realistic simulation.

So, please do not take this model as anyhow related to the actual data and predictions related to the currently unfolding situation involving COVID-19. There are many excellent resources to consult for that.

Special report: The simulations driving the world’s response to COVID-19

However, even such a simplistic programmatic approach is powerful enough to create key visualizations that drive home the messages of ‘stay-at-home’ and ‘flatten the curve’.

A great (but lengthy) piece with excellent playable JavaScript animations is here,

What Happens Next? COVID-19 Futures, Explained With Playable Simulations

Again, a test Jupyter notebook can be found [here](https://github.com/tirthajyoti/Covid-19-analysis/blob/master/Notebook/SEIRclass.py). The model class is here. They are released under MIT license, and therefore, you are encouraged to download and play with it.

Stay safe, everyone!

Note from the author: I am a semiconductor technologist, interested in applying Data Science and machine learning to various problems related to my field. I have no expertise or deep knowledge about medicine, molecular biology, epidemiology, or anything of that sort related to COVID-19. Please do not send me an email with that kind of query.


Also, you can check the author’s GitHub repositories for code, ideas, and resources in machine learning and data Science. If you are, like me, passionate about AI/machine learning/data science, please feel free to add me on LinkedIn or follow me on Twitter.

Tirthajyoti Sarkar – Sr. Principal Engineer – Semiconductor, AI, Machine Learning – ON…


Related Articles