Search code examples
netlogoagent-based-modeling

How do I properly simulate a rate of infection?


I am practicing my Netlogo skills by trying to simulate viral infection rate in a closed environment. The simulation I came up with runs well, but after reviewing the results something seems off in terms of how agents infect other agents. Here is my "infection" code ("normals" are healthy people while "carriers" are the opposite):

ask carriers [
    if any? other normals-here [
    let r random 50
      if r < 50 [set virus? false]
    if r < 50 + Infectivity [
        ask normals-here [set virus? true] 
        ask normals-here [set color red]
      ]
   ]
  ]

My general idea is that, in an interaction on the same patch between a carrier and a normal agent, if a normal agent has r + infectivity (a controlled variable in the simulation, with a rate of 0-50) = a number larger than 50, then the agent gets the virus. I want each normal agent to have a random r variable of 0-50, and if that variable grows larger than 50 when the "infectivity" rate is added, that agent receives the virus. If the r remains lower than 50 upon adding the "infectivity" rate, he remains "normal".

does my code seem ok? I feel like something is off here but I cant quite see it.


Solution

  • Just addressing your specific code: (1) assuming that carriers and normals are different breeds, you don't need other because carriers would not be in normals-here anyway. (2) Your use of the random number is not doing what you think but I can't work out how to fix it because the logic doesn't really work anyway.

    First, you create a random value for r from 0 to 49. Let's say that's 20. That will always be <50 because the random number generator always returns a number 0 to 49. So the code will set the variable virus? to false for the carrier. Since Infectivity is not negative, r will also be <50 + Infectivity. So now the rest of the code will also run, which will set virus? to true for all the normals on the patch (and turn them red).

    So, I think from your description, that you want to have if r + Infectivity < 50 []. That will at least have virus? become true for some values of Infectivity. But there is another problem with this, a lower value of Infectivity will be more likely to keep the value <50, which is opposite from the natural meaning of Infectivity.

    More generally, you have separated carriers from normals. But that's not how influenza works. Someone becomes infected and is then able to infect other people, they are not different sets of people. So what you really need is something along the following lines for the design:

    1. People have a state (usual terms are susceptible, infected, removed or recovered)
    2. Most start in susceptible state (normal in your terms), but some start in infected state (carriers in your terms)
    3. You get every person who is currently in infected state to have some chance of changing the state of nearby susceptibles into infected
    4. Everyone who is infected has some chance of changing state to removed

    The other thing you need is a better way of handling probability. Say you want a 20% chance of something to happen, this is the typical way of doing it:

    let num random 100
    if num < 20 [ do something ]
    

    So you generate a random number and then do the action if that number is less than the probability you want. In your case, you can have if num < Infectivity for example.