I am trying to implement the SIR epidemic model. Basically, the way I understand the model is that at each time step, it tells us how many nodes get infected and how many nodes are recovered. I am now trying to convert this into an discrete-event simulation and am confused at a point.
The model is generally solved using Euler’s method. Now, when I am converting it into a discrete event simulation, I am doing something like this (the numbers are used for clarity):
Initialize 100 members
At every time step t,
//Determine how many get infected
for i = 1 to 100
let i pass a message to its neighbors
When the neighbor receives the message from an infected member, it generates a random number and if it is less than beta*(infected/total), where beta is the infection rate, then the member gets infected
Update the count for infected, recovered, susceptible
//Determine how many are recovered
for i = 1 to 100
Generate a random number from a uniform distribution and check if it is less than gamma*infected. If it is, then this member is recovered.
Update the count for infected, recovered, susceptible
I was essentially wondering if the above approach is right. Any suggestions?
Looks pretty good as a start, except that for the first loop you need to remember that only susceptible individuals can become infected, and for the second one, that only infected individuals can become recovered. I also believe the probabilities of transition on each event (susceptible receiving message from infected neighbor, infected possibly recovering) are not a function of the current number of infected individuals — they’re constants (I think you’re misleading the “mass effect” probabilities as applying to each individual episode at a time step — they don’t).
A bit subtler is how you do the first loop (not obvious to me from the SIR model): I think you want to determine all “messages” first, then which ones cause transitions susceptible -> infected — i.e., two loops rather than one — because an individual that’s just gotten infected at this time step cannot infect yet others in this same time step but only in the future; also, the transition infected -> recovered is not possible for individual who just became infected at this very time step, so you’d have to arrange your loops a bit differently!
Consider modeling each individual with two “state” attributes:
as well as a fixed set of neighbors. Then:
this seems to better capture the overall flow (net of collecting and logging overall counts, which you can conceptually do as part of the last loop).