Building An Agent Based Covid-19 Contagion Simulator With Excel VBA

Zhijing Eu
8 min readAug 6, 2020

--

https://www.dezeen.com/2020/05/14/covid-19-images-coronavirus-cdc-medical-illustrator-dan-higgins/

BOILERPLATE WARNING : I am neither an epidemiologist nor a statistician and have built the analytical model more for educational/hobbyist pursuits so please take the findings/conclusions with a pinch (maybe a heap?) of salt….

(Note- This Medium article is an edited re-post of other articles I wrote over at LinkedIn in April 2020 https://www.linkedin.com/pulse/covid-19-contagion-modelling-excel-vba-eu-zhijing/ and https://www.linkedin.com/pulse/covid19-contagion-modelling-excel-round-2-eu-zhijing )

1.Introduction & Motivation

Much like everyone else affected by lock-down/movement restrictions , I had some time on my hands so I figured I’d put it to use with a “covid19 themed” programming self challenge.

Inspired by some of the great youtube vids and my limited understanding of the SIR Model of Epidemiology , I built my own “toy” simulator … but instead of Python , I used ….. EXCEL(+Some VBA) !

So yes, hard core programmers will scoff at using Excel but I thought I’d give myself a break and see if I could still do Excel

The excel model sets up a 32x32 grid (i.e ~1000 squares) populated with colored dots representing people who move around randomly at each time step and can get sick and spread disease.

Users can vary things like population density , travel distance per step , contagion level (i.e disease infection range , chance of contagion ), how long the disease lasts, and see the outcome play out when running the simulation.

The model also can simulate a scenario of total lockdown of entire population (where travel distance per step becomes ZERO for a certain duration) and quarantine of infected persons(where travel distance for ONLY infected individuals becomes ZERO until they recover).

But before presenting the modelling results , I’d like to cover some of the basic theory first…

2.How Compartment Models Of Contagion Work

2.1 SIR Model

In a SIR Model individuals start as Susceptible but contract the disease from other Infected individuals and once the course of the disease has run become Recovered and immune to further re-infection.

2.2 SEIR Model

The key difference in SEIR vs SIR is that there is an incubation period where individuals have caught the disease but are not yet contagious.

2.3 SEIR Model With Chance Of Mortality When Infected Total Exceeds Hospital Capacity

In this version, the infected individuals have a certain chance of death if are infected during the period where the hospitalisation capacity is overloaded.

As a test, I set a risk of fatality at an INDIVIDUAL level if you fall ill during the hospitalization overload to 0.25% (Arbitrary ! Not based on research). However what is worrying is that it still leads to relatively high TOTAL POPULATION level fatality rates (~2%!) which is easily x ~10 which isn’t intuitively obvious.

2.4 SEIR With Chance Of Mortality & Potential Loss Of Immunity

Lastly the SEIR can be converted to a SEI(R)(S) model as there is an option to set the scenario where recovered individuals can be re-infected (I.e Immunity is not permanent). As you can see in the example below where I’ve set a 5% chance of relapse of recovered individuals, this leads to a prolonged outbreak that when coupled with an overloaded hospital system, leads to a dramatic mortality rate of 10 % !

From a modelling perspective, what I found most interesting is that although most SIR or SEIR models are usually developed by solving ordinary differential equations (because these compartmental models focus on understanding the rate of change between compartments) , this “agent based” probabilistic approach I’ve used in the Excel file produces similar results as the interaction between simple rules related to contagion conditions and agent movement eventually lead to complex emergent patterns.

3 Using The Model For Scenario Simulation & Policy Impact Insights

Using the model above, I set up a number of different scenarios initialized under the same starting conditions (i.e No of and starting position of the agents ) to showing the progression of the spread over time if different policies are enacted.

3.1 Base Case : Moderately Populated Grid, Disease Duration = 14 days with Movement Allowed

This is a simple base case to compare against the other cases below.

3.2 Limited Travel Restriction Case : Shorter Travel Distances

A prolonged limited travel restriction movement (where people in the model are allowed to move but only within shorter distances) only has a moderate effect on the peak no of cases (~25% reduction) but drags out the duration by ~40%

3.3 Lockdown Cases : Zero Movement Imposed Early vs Late Implementation & Prematurely Lifted

A lock down only works if it is implemented early enough and is enforced long enough. However even though an effective lock down cuts the peak cases by ~50%, it also effectively doubles the duration (Which I supposed is still good because a lower peak means hospitals aren’t overloaded)

3.4 Widespread Testing Case : Allowing Isolation & Early Quarantine Of Infectees

Widespread testing (i.e Quarantining the infected early before they can spread the disease) seems to be the most effective measure as it more than halves the peak cases.

3.5 Initial Lockdown Case: Followed By Surge In Movement Post Lockdown + Influx Of More People

(EDITED UPDATE 02 APR 20'— After someone just sent me this article https://www.thestar.com.my/news/nation/2020/04/02/who-expects-malaysia039s-covid-19-cases-to-peak-in-mid-april about how the WHO estimated that the peak in Malaysia was probably in mid Apr and the MCO (movement control order) was effective , I decided to model a scenario where :

There is an initial lock down which appears to work in “flattening” the peak BUT immediately after the lock down is released, you get TWO effects

  • First — there is an influx of “new people” travelling into the region and there are “fresh” infectees with these newcomers
  • Second — everyone starts to travel even more (i.e further distances) than before with their ‘newfound freedom’ post lockdown)

What happens then is that the recently ‘released from lockdown’ healthy people quickly get infected and cause a new spike. So I guess this is the worst of all scenarios then as the population gets to “enjoy” being under a lock down but still end up with a (relatively) bad peak no of cases later AND also an overall prolonged duration.

3.6 Potential Reinfection (I.e No Immunity Post Recovery) Case

( [Another] EDITED Update 02 APR 20'— After reading this article on Al Jazeera “Can You Catch Coronavirus Twice ?” https://www.aljazeera.com/indepth/features/doctor-note-catch-coronavirus-200402144702217.html and the general depression that has sunk in , I thought I’d have a look at a bleak scenario. I’ve set up the same conditions as the base case but with ONE KEY DIFFERENCE — There is now a 1% chance that recovered people who would normally be completely immune to falling sick again can be re-infected (i.e either immunity is not 100% or the virus mutates enough over time that immunity to one strain doesn’t confer immunity to another)

The results are as follows — as you can see, after the first big wave, there continue to be seasonal waves that follow periodically. In the model the no of infected never goes down to zero because there is always enough of people losing their immunity to re-trigger smaller scale outbreaks. Although the peak never hits the same high , it does mean that disease never goes away which underscores just how important it will be in the long term to develop reliable and efficient testing methods and that the simple personal hygiene habits we picked up during this crisis will have to stick.

Conclusion

Although this is still a simplistic model meant purely for educational purposes , I hope that others can use it to gain a good visual idea of some key principles/concepts used in more complex disease modelling techniques

Notwithstanding the morbidity of the subject matter, this has been an interesting hobby project for me as it’s not too far different from my past professional experience where I developed similar probabilistic models (i.e Monte Carlo simulations) but for very different purposes to analyze project cost and schedule uncertainty.

While the excel file is a TOY MODEL but it seems to indicate that with the combination of lockdown / travel restrictions in place across various countries, this crisis will likely drag out for not just next 2~3 months but easily another 6–9 months till Oct-Jan 2021.[Yet Another] EDITED Update 05 AUG 2020 — 5 months later after this article was initially written in April 2020 , daily new cases have fallen to almost single digits and the restriction movements have been eased but Malaysia is still on alert https://www.freemalaysiatoday.com/category/nation/2020/08/03/2-new-cases-but-4-recoveries-recorded ]

Hopefully Malaysia gets the wide spread testing equipment/infrastructure up and running soon as that should really help even if it implemented late (However this assumes the sick people obey the quarantine)

For anyone wants to play around with it , the source Excel file available at this link: https://www.dropbox.com/s/fnk5wbj2xf8sc2l/AgentBasedEpiSimRev03.1_SEIRModel.xlsm?dl=0

In meantime, stay safe — wash your hands frequently, limit travel outside & practice social distancing and please observe whatever guidelines the government/health officials have been issuing

Resources

For those of you who are interested in going deeper into the subject, there are a whole lot of better built web-based disease models out there in the public domain:-

https://alhill.shinyapps.io/COVID19seir/

http://covid19forecast.science.unimelb.edu.au/

https://covid19-scenarios.org/

…and if you are into the fundamental theory , there is this rather good set of Epidemiology training resources from the CDC

https://www.cdc.gov/eis/request-services/epiresources.html

#covid19 #excelvba #programming

--

--

Zhijing Eu
Zhijing Eu

Written by Zhijing Eu

Hi ! I’m “Z”. I am big on sci-fi, tech and digital trends.

Responses (1)