Created
April 10, 2020 19:04
-
-
Save fredericojordan/1e4c1cea5da4f5a581fc0f57118c195b to your computer and use it in GitHub Desktop.
Graphing the "Coronavirus curve" with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
from scipy.integrate import odeint | |
MAX_TIME = 20.0 | |
infected_start = 0.01 | |
infection_rate = 2.5 | |
recovery_rate = 0.3 | |
susceptible_start = 1.0 - infected_start | |
recovered_start = 0.0 | |
initial_state = [susceptible_start, infected_start, recovered_start] | |
state_change_rate = lambda state, _time: [ | |
-state[0] * state[1] * infection_rate, | |
state[0] * state[1] * infection_rate - state[1] * recovery_rate, | |
state[1] * recovery_rate, | |
] | |
time_series = np.linspace(0, MAX_TIME, 100) | |
state_series = odeint(state_change_rate, initial_state, time_series) | |
susceptible_series = state_series[:, 0] | |
infected_series = state_series[:, 1] | |
recovered_series = state_series[:, 2] | |
plt.plot(time_series, susceptible_series, label="Susceptible") | |
plt.plot(time_series, infected_series, label="Infected") | |
plt.plot(time_series, recovered_series, label="Recovered") | |
plt.xlabel("Time") | |
plt.ylabel("Population") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment