Infection Simulator

An infectious disease model simulator

by Ron Eaglin

HTML

This is a very simplified simulator of an infection spread. The model  assumes that
<br/> a certain number of people are currently infectious, 
<br/>the infection will last for a certain period of time, 
<br/>the person has contact with a certain number of people per day and 
<br/>there is a probability of causing the infection in those people. 

<br/>At the end of the time period of being infectious, the people are removed from the list. it was developed for the course COP3530 to demonstrate the concept of greater than exponential growth. Those are variables below.

<br/><br/>

Number of People Initially Infected: <input type="textbox" id="InitialInfected" value="2" /><br/>
Infection Length : <input type="textbox" id="InfectionLength" value="7" />(number of days a person is infectious)<br/>
Infection Probability : <input type="textbox" id="InfectionProbability" value="0.3" />(fraction of people you contact who will become infected) <br/>
Number of Daily Contacts : <input type="textbox" id="NumberContacts" value="3" />(number of people you have contact with daily)<br/>
<br/>
<br/>
<input type="button" value="Start Infection Simulation" onclick="StartSimulation()"/>
<br/>
<div id="output" >

</div>

JavaScript

var recovered = 0;
function StartSimulation() {
  var initialInfected = parseInt(document.getElementById("InitialInfected").value);
  var infectionLength = parseInt(document.getElementById("InfectionLength").value);
  var infectionProbability = parseFloat(document.getElementById("InfectionProbability").value);
  var numberContacts = parseInt(document.getElementById("NumberContacts").value);
  
  // Each sick person 
  var sick = [] 
  var newSick = parseInt(infectionProbability*numberContacts);
  
  for (var i=0; i < initialInfected; i++) 
  {
     sick.push({ infectedDay: 1, 
                 infectedEnd: infectionLength, 
                 numberOfContacts: numberContacts, 
                 infectionProbability: infectionProbability });
	}
  
  outputResults(0, sick);
  
  for (var i=1; i < 15; i++)
  {
     sick = CalculateDailyInfections(i, sick, infectionLength);
  } 
  
}

function CalculateDailyInfections(day, sick, il){
  var newInfections = 0;
  
  // Going through each sick person and looking at new infections
	var n = sick.length;
  for (var i=0; i < n; i++){

    if (day < sick[i].infectedEnd ){
        // Still infectious and infecting, will add more infections
        
    		 for (j=0; j < sick[i].numberOfContacts; j++){
         
         	if (Math.random() < sick[i].infectionProbability)
         	{
          debugger;
         // Each push is a newly infected person
         	 	sick.push({infectedDay: day, 
                      infectedEnd: day + il, 
                      numberOfContacts: sick[i].numberOfContacts, 
                      infectionProbability: sick[i].infectionProbability });
           }
         }
    }
    else
    {
    	// If they are better remove from list
    		sick.splice(i, 1);
        recovered++;
    }
  }
  outputResults(day, sick);
  return sick;
}

function outputResults(day, sick){

   document.getElementById("output").innerHTML += "Day " + day + " Total Sick: " + sick.length + " Recovered: " + recovered + "<br/>";
}