The Brilliance of the Compartmental Models

Qlabs
4 min readJun 11, 2021

--

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. However, the dashboard isn’t really that unique. There are thousands. Nothing really makes my dashboard special. There’s this one, this one, and this one. What got me interested however was this 3b1b video, which used something called a SIR model, which helped predict a outbreak. So, I made one. Once again, this isn’t the only tool, and there are others. For example, CovaSim 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 people one infected infects

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

Equations for the SIR Model

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:

The code for the SIR model in JavaScript

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 a SEIRD model

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, 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, and import it with your HTML with:

<script src="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"> <!-- Chart.js is required. -->

See you soon!

--

--

Qlabs
Qlabs

Written by Qlabs

Computer Programmer, Musician, and hobbyist. View on GitHub -https://github.com/Quantalabs. Follow on DEV @Quantalabs