-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseir_model.py
59 lines (51 loc) · 1.48 KB
/
seir_model.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import matplotlib.pyplot as plt
# SEIR Model function
def seir_model(S, E, I, R, beta, sigma, gamma, N, days):
"""
Parameters:
S: Initial susceptible population
E: Initial exposed population
I: Initial infected population
R: Initial recovered population
beta: Infection rate
sigma: Incubation rate (1/incubation period)
gamma: Recovery rate (1/infection period)
N: Total population
days: Number of days to simulate
Returns:
A dictionary of daily values for S, E, I, R
"""
S_vals, E_vals, I_vals, R_vals = [S], [E], [I], [R]
for _ in range(days):
dS = -beta * S * I / N
dE = beta * S * I / N - sigma * E
dI = sigma * E - gamma * I
dR = gamma * I
S += dS
E += dE
I += dI
R += dR
S_vals.append(S)
E_vals.append(E)
I_vals.append(I)
R_vals.append(R)
return {
"Susceptible": S_vals,
"Exposed": E_vals,
"Infected": I_vals,
"Recovered": R_vals,
}
# Plot results
def plot_seir(data, days):
plt.figure(figsize=(10, 6))
plt.plot(data["Susceptible"], label="Susceptible")
plt.plot(data["Exposed"], label="Exposed")
plt.plot(data["Infected"], label="Infected")
plt.plot(data["Recovered"], label="Recovered")
plt.xlabel("Days")
plt.ylabel("Population")
plt.title("SEIR Model Simulation")
plt.legend()
plt.grid()
plt.show()