I'm trying to make an SIR model for disease in python.
import random
import numpy as np
import math
import matplotlib.pyplot as plt
seed=random.seed()
nums=1000
fract=0.01
recovr=0.2
num_infect=1
numd=40
reps=40
data=np.full(numd,1)
sdata=np.full(numd,1)
remov=0
remove=0
for k in range(reps):
remove=0 #remove per day
P=np.full(nums,1)
n=0
while n<math.trunc(fract*nums):
if P[random.randint(0,nums-1)]!=0:
n+=1
n=0
while n<1:
if P[random.randint(0,nums-1)]==1:
P[random.randint(0,nums-1)]=2
n=1
for j in range(numd):
for i in range(nums):
if P[i]==2:
for ii in range(num_infect):
if P[random.randint(0,nums-1)]==1:
P[random.randint(0,nums-1)]=2
remov+=1
if random.random()<recovr:
if P[random.randint(0,nums-1)]==2:
P[random.randint(0,nums-1)]=0
c,s=0,0
for i in range(nums):
if P[i]==2:
c+=1
if P[i]==1:
s+=1
data[j]=(data[j]+c)
sdata[j]=(sdata[j]+s)
for i in range(numd): #average after repeating reps times
data[i]=data[i]//numd
sdata[i]=sdata[i]//numd
num_infected=remov/reps
print(num_infected)
So the maximum number of people that could be infected should be 1000 but for some reason num_infected comes out as number around 1700. Like it's very confusing because it should add 1 onto the counter (remov) for every person that gets the disease but for some reason it goes past 1000 even though I set the maximum number of people to 1000 and no way to be reinfected after becoming immune. It does remov/reps because the code repeats 40 times and that finds the average.