(Live interactive piece is at https://showcase.vslashr.com/virus/)


Since the Covid19 pandemic upended billions of lives around the world, we have had some really gorgeous simulations / visualizations to explain how an infection spreads.

Being stuck at home in Singapore on a lockdown (which was just extended to another month), I wanted a project to occupy my time.

I had a different take on simulating and visualizing a virus outbreak in a town as compared to other articles out there.

Instead of people moving about randomly, I felt that giving people a routine home to work to home cycle seemed more realistic to me. Was there a way I could show this, and what insights could we draw?

So I coded up my own model for fun that people can play and explore.

Head over to the live, interactive site where you can play with and interact with the simulation! Apologies, LinkedIn does not allow code or even image embeds in the article itself.

Baseline Model

Here’s our very simplified base toy model.

  • Let’s assume that we have a small town with about 150 people that lives in it.
  • In this town, there are 50 homes and 30 workplaces. The people in town are randomly assigned to one permanent home and one permanent workplace.
  • Everybody has a schedule – they either are at work from 9am to 5pm, or they are at home.
  • The simulation starts with one sick individual on Day 0 at 12 midnight (and thus everybody is at home). Everyone is at home till 9am in the morning, at which everyone goes to work.
  • Every person is represented by a dot. If you’re healthy you’re black. Else, if you’re sick and infectious, you’re red.
  • Every hour, every sick person has a chance to infect every other non-sick person at the same location. Thus when you have more sick people at a location, the risk for healthy folks get compounded.
  • Travel from location to location is instant and no one gets infected during movement.


Our model here is rather simple, and it is just about infection. It does not take into account any resistance or anything like that. But even in this barebones state the visualization can help with intuitions about viral spread.

You might notice a few things if you run the simulation a few times:

  • You might get lucky and the number of sick is really low after several days. Perhaps you got a good randomized start, and the sick individual lived alone, and the workplace he or she worked in had very few employees.
  • Once you start having a small base of people who are sick, it likely and quickly starts to explode as people move around and spread.
  • The number of homes and workplaces gives you an idea of how spread out a population is. The more spread out a population is, the less chance they have to interact.
  • If you group a bunch of people at one same location, it is likely to become a vector of spread, and one infection will likely quickly multiply at that location and spread it all throughout the town.

The last point may be a a common sense intuition, but becomes clear when visualized.

For example, in Singapore our current lockdown situation is due to Covid19 spread in migrant workers who were squeezed into a few dormitories. For them it is extremely difficult to isolate, and thus when the virus started to circulate, it exploded.

Another example: Having one main employer in town like a meat-packing factory. If you get one case in town, it is likely to spread.

You can see these intuitions better by putting more people in less homes and workplaces when you run the simulation. One red dot quickly becomes a bunch of angry red dots at chokepoints.

A more detailed analysis on simulation runs and why having lots of people gathering in one location is a very bad thing (with graphs and all) available at the interactive site.

Stay safe people, and a shoutout to the frontline folks – doctors, nurses, grocers, handymen, etc. who do the hard work of keeping things running.