The Brilliance of the Compartmental Models
Tags: epidemiology, javascript, covid19
Published: 2021-06-11 18:17:32

---
title: The Brilliance of the Compartmental Models
published: true
date: 2021-06-11 17:57:33 UTC
tags: epidemiology,javascript,covid19
canonical_url: https://qlabs.medium.com/the-brilliance-of-the-compartmental-models-e4a5245db0c8
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41t17uauuqbxttyk0zrz.jpg
series: COVID-19 Modelling
---

We’re stuck in the middle of a pandemic and a lockdown, and being a programmer, one of the first things that came to mind was to create a dashboard for people to use. [And so I did.](https://github.com/Quantalabs/NCOVDashboard) However, the dashboard isn’t really that unique. There are thousands. Nothing really makes my dashboard special. There’s [this one](https://ncov2019.live/), [this one](https://covid19.who.int/), and [this one](https://federico-dondi.github.io/COVID-19-dashboard/#/). What got me interested however was this [3b1b video](https://www.youtube.com/watch?v=gxAaO2rsdIs), which used something called a SIR model, which helped predict a outbreak. So, I [made one](https://github.com/Quantalabs/EpiJS). Once again, this isn’t the only tool, and there are others. For example, [CovaSim](http://app.covasim.org/) is made specifically for COVID-19. So, how does these models work?

All of these models are build on top of the SIR model, which stands for Susceptible, Infected, and _Removed._ You might see the R as Recovered, but for this case, we’ll use Removed, because this better represents an outbreak. The R could include both dead and recovered, where we assume people can’t be re-infected. Now, the susceptible class are people who don’t have the disease, and can be infected. The infected class are people who have the disease, and spread it. The Removed class are people who no longer have the disease and cannot catch it. Pretty straightforward, right? Now let’s get into the math.

Let’s define some variables:

- γ - The recovery rate
- β - The infection rate

In addition to this, we have other parameters which will come in handy later on:

- R0 - The basic reproduction number, in other words, the amount of one infected infects

With this, we can form the basic equations for the model below:

![Equations for the SIR model](https://cdn-images-1.medium.com/max/213/1*SZm_qqZG8uIIaW0tkw88aQ.png)<figcaption>Equations for the SIR Model</figcaption>

If you are confused by the equations above, don’t fret! It’s quite simple! This states that for the susceptible population, it loses β×_S_×_I_ people each day. The infected population gains that many people each day, however loses γ×_I_ people each day, which the recovered population gains.

With this in mind, we can start converting this into JavaScript code:

{% gist https://gist.github.com/Quantalabs/54a4c293ab7923af28e319e5985b9a91 %}

Notice, instead of having the user pass in the infection rate, we ask them to give us R0 and the recovery/removed rate, which when we multiply together, we get our infection rate.

So, now that we’ve got our JS code, let’s look at how we can expand this.

The brilliance of the SIR model is that it might be extremely simple on its own, but it’s almost built to be expanded. Here are some of the things we can expand it with:

- Exposed, people who have the disease but can’t spread it. Like the incubation period in COVID-19. They do eventually become infected.
- Dead, people who are dead.

Let’s see how we can turn these into equations:

![Equations for the SEIRD model](https://cdn-images-1.medium.com/max/259/1*ar7MIZJqmAnuWcCJiNBPYw.png)<figcaption>Equations for a SEIRD model</figcaption>

This forms a SEIRD model, with the Susceptible, Exposed, Infected, Recovered, and Dead compartments. We now no longer have removed, it’s split up into the dead and recovered. However, as you might have noticed, we brought in some other parameters:

- θ - The death rate.
- α - The rate at which people in the exposed move to the . This is most likely 1/_incubation period_

I won’t get into the JS code for this, but, let’s think of other ways to expand this:

- Hospitalized, people with the disease in a hospital.
- Critical, people in critical condition, perhaps in an ICU.

We can continue on and on and on, expanding this model, to better represent the world, which is the true brilliance of compartmental models.

Stay tuned for my next article, where I’ll discuss how to use these equations to help model COVID-19, with data. In the meanwhile, you can play around with data on my [EpiJS playground](https://epi.js.org/demo), which allows you to create a SEIRD model with your own customized parameters. If you want a SEIHCRD model, or other models, you can use the library itself in your HTML. You can view the docs [here](https://epi.js.org), and import it with your HTML with:

```html
<script src="[https://cdn.jsdelivr.net/gh/Quantalabs/EpiJS/web/pre.min.js](https://cdn.jsdelivr.net/gh/Quantalabs/EpiJS/web/pre.min.js)"></script>
<script src="[https://cdn.jsdelivr.net/npm/chart.js@3.0.2/dist/chart.min.js](https://cdn.jsdelivr.net/npm/chart.js@3.0.2/dist/chart.min.js)"> <!-- Chart.js is required. -->
```


See you soon!