-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
33 lines (26 loc) · 1.01 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
class Simulation:
def __init__(
self, population, initially_infected, recovery_chance, transmission_rate
):
self.population = population
self.infected = initially_infected
self.unaffected = population - initially_infected
self.recovery_chance = recovery_chance
self.transmission_rate = transmission_rate
self.recovered = 0
self.history = []
def run(self):
days = 0
while self.infected > 0:
new_infections = np.random.binomial(
self.unaffected,
self.transmission_rate * self.infected / self.population,
)
new_recoveries = np.random.binomial(self.infected, self.recovery_chance)
self.unaffected -= new_infections
self.infected += new_infections - new_recoveries
self.recovered += new_recoveries
self.history.append((self.unaffected, self.infected, self.recovered))
days += 1
return days