Reopening Safely: The Data Science Approach
Tags: sirmodel, datascience, modelling, coronavirus
Published: 2021-01-16 03:57:32

---
title: Reopening Safely: The Data Science Approach
published: true
date: 2020-11-02 03:21:24 UTC
tags: sirmodel,datascience,modelling,coronavirus
canonical_url: https://medium.com/@q9i/reopening-safely-the-data-science-approach-289fd86ef63
---

The great reopening debate attacked using infectious disease modeling

![A disease model](https://cdn-images-1.medium.com/max/960/1*ml21-N9rNe6FeIfK5VR_jA.png)

With the coronavirus raging through major cities all over the world, the question remains the same: when is it safe to reopen? The answer is not simple, so as usual, data science comes to the rescue. Using an SIR-based model in multiple experiments will give clues to how the reopening of the world might look, and whether it will last.

### The SIR Model

The SIR model is one of the most sacred disease modeling tools. Its versatility, simplicity, and effectiveness make it one of the most powerful tools in epidemiological modeling. Here is a simple breakdown of the key ideas behind the model:

#### The S, I, and R

The first step in the SIR model is to break down the population into three possible states: Susceptible (S), Infected (I), or Recovered (R). The states themselves are quite self-explanatory. However, the last state, removed, is also commonly referred to as the “Removed” state because it can contain individuals who are recovered or dead.

#### The Dynamics

As with many problems involving populations, it is much easier to describe the change of a state rather than it is to describe the state itself. Essentially, this means that differential equations are necessary.

![dS/dt = -beta * I * S/N, dI/dt = beta * I * S/N - gamma * I, dR/dt = gamma * I](https://cdn-images-1.medium.com/max/217/1*eW5KD-qu4A7HDVaoDTMQhQ.png)

The equations above might look intimidating at first glance, but when understood in the right context, they make perfect sense. In order to understand these equations, we need to define a few variables, notably:

> _Beta_ = infection rate _\*_ number of individuals a person makes contact with (essentially how many people 1 infected infects)

> _Gamma_ = _1_ / days to recover (the recovery rate)

The first equation means that the number of people exiting the susceptible compartment per timestep is beta \* I \* S/N. This makes sense because each infected infects beta people (see the definition of beta). The last part, S/N, represents the portion of the population that is susceptible because an infected or recovered person cannot be reinfected\*.

Now, since everyone who “leaves” the Susceptible compartment must go to the Infected compartment (you can’t jump to the Removed compartment without first being infected), the Infected compartment simply gets whatever population leaves the Susceptible compartment (notice the derivative of the Infected compartment is just the negated derivative of the Susceptible compartment with one other term). The extra term at the end represents the people leaving the Infected compartment after recovering (the recovery rate \* the infected population). And lastly, the same number of people leaving the Infected compartment join the Recovered compartment.

\*The idea that someone who has recovered from an illness cannot be susceptible to it again is one of the assumptions behind the SIR model which is mostly true for COVID-19

#### Setting it Up

Coding the SIR Model might seem quite intimidating at first, but luckily, there is a software library that can help — check out [epispot](https://github.com/quantum9Innovation/epispot) on GitHub. Epispot can handle the SIR model and even more complex models (where there are exposed and hospitalized states). Coding this model in epispot will be covered in the following sections. The code itself is not that important (you can always consult the documentation), but its predictions and the reasons behind them are.

If you are interested in the code, there is one other parameter that epidemiologists frequently use (and one that epispot uses). That parameter is of course R Naught. R Naught is essentially beta / gamma or the **net** gain in the infected category per infected if 100% of the population is fully susceptible. Essentially, this tells epidemiologists _how infectious_ a certain disease is (e.g. for COVID-19, this number is around 2.5).

### The Case Study

#### San Francisco

The entire code would be too much to explain for this article, but if you are interested, see the sf-case-study-covid-19.py in the [epispot repo](https://github.com/quantum9Innovation/epispot). However, the rest of the article will be mostly code snippets.

After we import the epispot library and get everything configured, we load the San Francisco case data from 3/19 to 7/11 from [DataSF](https://data.sfgov.org/stories/s/dak2-gvuj). If you want to download the data yourself, this is in the epispot repo under tests/data . Here is a sample of the data:

{% gist https://gist.github.com/quantum9Innovation/c4c96899acc2633d64cc23bc8b1e8445 %}

Now we set up the model. Again, the full code for this can be found on GitHub, but for the sake of simplicity, only an important snippet is shown. After the coronavirus data loads, the model is set up in just a few lines of code:

{% gist https://gist.github.com/quantum9Innovation/01eead02dd42f04acc71a4249c08069d %}

Before we get the predictions, there is a little bit of setup required. The parameters used in this model ( R\_0, gamma, N, delta, alpha ) have all been gathered using open data sets and a regression algorithm. This means that we have essentially _fitted_ our model to the data to get more accurate estimates. In case you are interested, the regression was performed using data from the infamous Diamond Princess cruise ship.

Lastly, the model outputs the predictions (using the epi.plots.compare method), plotted on a logarithmic scale. The predictions are from 7/11 to 8/10 (1 month ahead)\*. The predictions are calculated as if there is no social distancing at all (that is, San Francisco completely reopened). Finally, the question is answered:

\*Yes, this is not exactly happening _right now_, but it still is useful because it gives a general idea of what _could_ happen in the future. The modeling is also still necessary because we are simulating what would happen after a _complete_ reopening, so we need to predict.

![The current susceptibles and infecteds grow slowly and linearly, while after reopening, they blow up, peaking at ~100k](https://cdn-images-1.medium.com/max/1000/1*sQNNr8HHzkMhD0El6mP_xw.png)<figcaption>The infected and hospitalized cases before and after a full theoretical reopening</figcaption>

This graph can be a lot to take in at one time, especially since all the important details are stashed away in the right corner. Zooming in on days 120–140 yield the following graph:

![A zoom-in on the graph reveals a stretch of ~10 days where hospital capacity is surpassed.](https://cdn-images-1.medium.com/max/1000/1*99-g-qIePF9g2gpkDFhm8w.png)<figcaption>A zoom-in of the predicted infecteds (light blue) and predicted hospitalized (dark blue)</figcaption>

The gigantic yellow box on top is the period where San Francisco’s maximum hospital capacity is passed (i.e. not everyone can be treated). At this point, there is a clear public health disaster — so how long did San Francisco’s pretend lockdown really last?

> **7 days.**

### What Happened?

Seven days is quite a surprising number. Only one week. That is the maximum duration of a complete reopening. Reopening clearly does not mean that there will be _no_ safety precautions, but in many cases it does make some sacrifices. Aside from that, there are still many reasons why this model is an exaggeration: it is from a few months ago, the parameters are all estimated, etc. However, this model does show something important:

> _small changes in awareness, precautionary measures, or anything that is carried out on an_ even citywide _scale can have_ **_major impacts._**

Of course, several questions remain. For example, what would happen if there was just a _partial_ reopening, or what if this was in a suburb, or New York, or what happened it Italy? See you in the next blog post …

> **_This is article one of the series “Reopening Safely”_**

### While you wait …

- See the [epispot repo](https://github.com/quantum9Innovation/epispot) on GitHub
- See these [Medium posts](https://towardsdatascience.com/infectious-disease-modelling-part-i-understanding-sir-28d60e29fdfc) on infectious disease modeling
- Look at the [San Francisco case data](https://www.google.com/search?q=san+francisco+coronavirus+cases)

### Credits

Epispot is built using numpy for managing multidimensional arrays. The plots shown in this article are using epispot’s plot feature, which relies on matplotlib for the stunning graphics and annotations.