Simulate an Infectious Disease with Python

I will present a simple yet powerful infectious disease simulator in Python. I will then analyse the output data and discuss how it can be used for better decision making to fight pandemics

Nadav Loebl
Analytics Vidhya
5 min readApr 19, 2020

--

image credit: blog.equinix.com

Nowadays, because of the Coronavirus, there is a lot of hype around mathematical modelling of infectious diseases. The most widely known is the SIR Model. Despite being over simplfied for describing reality, the result indeed “behaves” like a real pandemic. In this article I won’t discuss any existing mathematical models but suggest another way for simulating an infectious disease in a population. Such a tool can be used by goverments for investigating different strategies to handle a disease, and in addition, try to predict the properties of the disease itself (I will discuss this important part at the end of the article).

Lets dive in. The aim of the simulator presented is to simulate the behavior of an infectious disease in a population. Similar to mathematical models for the prediction of an infectious disease spread, it is a simplified model of reality. As you will see, I argue that it is encapsulating a more complex behaviour.

Basic assumptions of the simulator

Time step — you can think of it as an hour, a day, a month — it does not matter as long as it is a one time unit. The simulation is progressing in steps.

Randomness — this is an important part. As you will see, there are some parts in the simultaor that are random (you are able to determine the mu property of the normal ditribution. You are welcome to choose other distribution, of course). This is important because as in reality itself, there are random events.

A citizen who became sick and removed (the infected citizen is either cured or dead) can not get sick again — this is the basic assumption in the majority of the models.

The meaning of a connection between citizens is that they meet physically and therefore can infect each other. The connections between every two of the simulated entities (or citizens) are one of the two — they are either not connected (won’t see each other physically) or they are. If they are connected, there are two options:
They belong to same Clique — meaning they are part of the same group of citizens that all of the physical connections of each individual within this group is only to other individual in the group (you can think about this as a quarantine) or they are connected in a “regular” way (i.e. the connection formed randomly in our simulator, and every party can be connected to other citizens)

You can find more assumptions (or hardcoded probabilities) in the code.

The Infectious Disease

In our simulation, the population is struggling with an infectious disease. The disease in our simulator has 4 parameters:

infection_prob : The probability of a healthy citizen to get infected by a sick citizen as a result of a physical meeting
average_life_span : The average time (number of steps) that the disease is infectious before removed
is_sick : By default, the citizen is not infected
counter : count the number of steps (time)

The Citizen In The Community

We need to simulate a citizen in a the simulated community. The main parameters of our citizen are:

community : The community that the citizen belongs to
_id : The citizen’s id
disease : The disease that the citizen may or may not have
removed : If True it indicates that the citizen is either cured or dead

The Community

In our simulator, the community is basically aimed to describe connections between citizens. Parameters:
num_of_citizens : The number of citizens in the community
average_physical_connections : The average of physical connection that a citizen within the community has (the disease can be spread only by an
infected citizen making a physical connection with another citizen)
disease : The disease
num_of_infected : The initial number of infected citizens in the community
cliqe_prob : The probability that the physical connections of a citizen
in the community is in a closed clique

The Simulator

The Simulator class is pretty straightforward, it is mainly a holder of a set of communities:

Case Study

Lets examine an example with the following parameters:

output:

orange: number of active cases, blue: number of total cases

The number of total cases in the population is 5502, which is 27.51% percent of the population.

Little Difference - BIG Impact!

Let us see what happens when we tweak only one parameter — clique_prob. Just a reminder — clique_prob is eqvivalent to “percentage of the population in quarantine”. So, clique_prob = 0.9, leets change it to clique_prob = 0.8 (80% instead of 90%) and see what happens:

output:

The general shape of the curve may look similar to the former in the first glance, however, there is a BIG difference. The absolute numbers are much higher: 10% change of the population being in quarantine, 80% instead of 90%, resulted in 51.26% of the population got infected (!).

This is just one example for how this simulator can be used to investigate how important a specific parameter (or “behavior”) is to fight the spreading of the disease. Governments around the world can leverage this ability in order to plan better strategies for the disease combat.

What’s Next?

As presented, the simulator can be used in order to create synthetic data. In my next article, I will try to make use of synthetic data that was generated from the simulator in order to use neural networks and try to predict the parameters of the simulator — more specificly the infection_prob (how much of the population is hygienic) and clique_prod (how much of the population is obeying the quarantine).

--

--