With a contagious infection present in a dynamic population of individuals (where everyone is either contagious, unsusceptible, or susceptible), the susceptible are vulnerable to infection by the infected, whoare contagious, and the unsusceptible are neither able to contract the disease or spread it. Herd immunity is the concept that the unsusceptible (either by vaccine or previous infection) confer some degree of protection to the sterile group in the population. I wrote a program in python to simulate the process of infection spread across a population with these criteria in order to see how effective herd immunity might be.

The simulation contains a population of 500 individuals each assigned to a random coordinate in a 500 x 500 grid. In the first graph, the population randomly is assigned a coordinate, 50 are randomly infected & contagious, 400 are unsusceptible (vaccinated) to infection and incapable of spreading the disease, and the remaining 50 individuals are susceptible and capable of contracting the infection and then spreading it to others. At each turn or step every individual has an equal probability of moving in any direction from 0 – 3 units of distance (but not out of the 500×500 grid). I chose 3 units because of the time scale chosen. After each turn the program checks to see if any susceptible individuals are at the same coordinate as a contagious individual, AKA a collision, and if there is a collision there is a probability the susceptible individual in the collision will become infected. The probability depends on the infectivity which was set at .1 or 10% for the run shown in the first graph and in the GIF.

Next I did a total of 552 iterations (separate simulations/runs) where the number of initially infected went from 50 to 300 in intervals of 50 and the number of initially unsusceptible individuals went from 0 to up to a possible 420 by intervals of 40. This number was sometimes limited by the initial number infected as there were always only 500 individuals in the population and of those at least 30 were susceptible. The iterations were repeated over 4 different infectivity levels: 0.1,0.2,0.3 and 0.4 listed on top of each graph. The graphs show the fraction of the population that is initially unsusceptible to infection and incapable of spreading the infection for a run on the x-axis. The y-axis is the fraction of initially sterile individuals that became infected at the end of a run. The color of each point indicates the number of initially infected individuals for a run. The line indicates a path that fits the data automatically generated by R.

Notes: This program simulates a very mixed population which isn’t necessarily the way things work as people tend to have networks of groups they interact with frequently with some overlap. The time scale was chosen to allow me to run the program without it taking weeks, which translates to there being 2400 steps in a “day”. This isn’t necessarily accurate and my goal wasn’t to make it completely true to reality, rather just to see the effect an initial vaccination might have.

In the program, there are actually 4 different ways an individual can be: infected, infected & contagious, susceptible and unsusceptible. If someone is infected & contagious, they can spread the infection. If they are only infected they cannot spread the infection an infected individual will eventually become immune (unsusceptible). I added this, probably unnecessary, complication because in reality an individual is not generally contagious for the duration of their infection. The infection and contagious stage of the infection lasted a certain duration chosen randomly from a normal distribution. The average length of infection was 4 days and an infected individual did not become contagious until a “day” into their infection and remained contagious for the duration.

The graphs were created in R and the simulation program was created with Python.

Update: Here is the code for the python script on GitHub. I’m new to GitHub so if I made any obvious errors don’t hesitate to let me know.