-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActiveFromDeath4.py
More file actions
164 lines (111 loc) · 5.4 KB
/
ActiveFromDeath4.py
File metadata and controls
164 lines (111 loc) · 5.4 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score
# TODO:
# parameters: CFR (IFR preferable), offset, death_factor
# /////////////////////////////////////////////////////////////////////////// FUNCTIONS LIST:
# //////////////////// Curves
def exponential(x,c0,c1,c2):
return c0 + c1 * np.exp( c2 * x )
def logistic(x,p,k,r,b):
return p*k*np.exp(r*(x-b))/((k-p) + p*np.exp(r*(x-b)))
# //////////////////// Calculates active cases from Deaths Reported
def calcActive(deaths, death_fact, ifr, offset): # back-calculates active from reported deaths
actual_infected = []
for i in range(offset, len(deaths)):
actual_infected.append(deaths[i] * death_fact * (100/ifr))
actual_infected = np.array(actual_infected)
return actual_infected
# //////////////////// Functions for curve-fitting
def fit_exp(x, y, offset):
# optimum parameters for curve-fitting
p0 = [0.01, 0.08, 0.02] # initial parameter guess - exponential
c, cov = curve_fit(exponential, x, y, p0) # curve-fitting - exponential
print(f"Optimum Parameters are {c}")
yp = exponential(x, c[0], c[1], c[2]) # calculate predictions of curve-fit
print(f"Rsq. of curve fit is {r2_score(y, yp)}") # Rsq calc.
# plot
plt.figure()
plt.title("Actual Infected Numbers")
plt.plot(x, y, "r", label="Predicted")
plt.plot(x, yp, "b", label="Exponential")
plt.xlabel("Days")
plt.ylabel("Actual Infected")
plt.legend()
plt.show()
new_vals = []
for i in range(len(x), len(x)+offset):
new_vals.append(exponential(i, c[0], c[1], c[2]))
return np.append(actual, new_vals)
def fit_logistic(x, y, offset):
# optimum parameters for curve-fitting
p0 = [.05, 0.1, 0.001, .02] # initial parameter guess - logistic
c, cov = curve_fit(logistic, x, y, p0) # curve-fitting - logistic
print(f"Optimum Parameters are {c}")
yp = logistic(x, c[0], c[1], c[2], c[3]) # calculate predictions of curve-fit
print(f"Rsq. of curve fit is {r2_score(y, yp)}") # Rsq calc.
# plot
plt.figure()
plt.title("Actual Infected Numbers")
plt.plot(x, y, "r", label="Predicted")
plt.plot(x, yp, "b", label="Logistic Growth")
plt.xlabel("Days")
plt.ylabel("Actual Infected")
plt.legend()
plt.show()
new_vals = []
for i in range(len(x), len(x)+offset):
new_vals.append(logistic(i, c[0], c[1], c[2], c[3]))
return np.append(actual, new_vals)
# ///////////////////////////////////////////////////////////////////////////
ifr = 1.04 # current working estimate for Infection Fatality Rate (1.04 - global ifr estimate)
offset = 23 # from The Lancet (17.8) - to be modified with age structuring, 26 from INDSCI-SIM + 5 day incubation
death_fact = 3.5 # assumption to correlate reported deaths with actual deaths
min_death_fact = 2
max_death_fact = 5
data = pd.read_csv("owid-covid-data.csv")
india_combined = data.loc[data["iso_code"]=="IND",("date","total_cases","total_deaths")]
cases = india_combined["total_cases"].values
deaths = india_combined["total_deaths"].values
# Starts from 1st death reported
for i in range(0,len(deaths)):
if deaths[i]!=0:
start_index = i
break
cases = cases[start_index:]
deaths = deaths[start_index:]
actual = calcActive(deaths, death_fact, ifr, offset)
# ///////////////////////////////////////////////////////////////////////////
# Short Term Forecasting with Curve-Fitting
#prediction = fit_exp(np.array(range(len(actual))), actual, offset)
prediction = fit_logistic(np.array(range(len(actual))), actual, offset)
# /////////////////////////////////////////////////////////////////////////// PLOTS
# Logarithmic Plot
plt.figure()
plt.title("COVID-19 National Projections")
plt.plot(np.array(range(len(cases))), deaths, 'r--', label="Reported Deaths")
plt.plot(np.array(range(len(cases))), cases, label="Confirmed Cases")
plt.plot(np.array(range(len(cases))), deaths * death_fact, color='red', label="Expected Deaths")
plt.fill_between(np.array(range(len(cases))), deaths * max_death_fact, deaths * min_death_fact, alpha = 0.2, color='red', label="Confidence of Expected Deaths")
plt.plot(np.array(range(len(cases))), prediction, color='green', label="Predicted Infections")
plt.fill_between(np.array(range(len(cases))), prediction * (max_death_fact/death_fact), prediction * (min_death_fact/death_fact), alpha = 0.2, color='green', label="Confidence of Predicted Infections")
plt.yscale("log")
plt.xlabel("Day Number")
plt.ylabel("Population (Logarithmic)")
plt.legend()
plt.show()
# Linear Plot
plt.figure()
plt.title("COVID-19 National Projections")
plt.plot(np.array(range(len(cases))), deaths, 'r--', label="Reported Deaths")
plt.plot(np.array(range(len(cases))), cases, label="Confirmed Cases")
plt.plot(np.array(range(len(cases))), deaths * death_fact, color='red', label="Expected Deaths")
plt.fill_between(np.array(range(len(cases))), deaths * max_death_fact, deaths * min_death_fact, alpha = 0.2, color='red', label="Confidence of Expected Deaths")
plt.plot(np.array(range(len(cases))), prediction, color='green', label="Predicted Infections")
plt.fill_between(np.array(range(len(cases))), prediction * (max_death_fact/death_fact), prediction * (min_death_fact/death_fact), alpha = 0.2, color='green', label="Confidence of Predicted Infections")
plt.xlabel("Day Number")
plt.ylabel("Population (Linear)")
plt.legend()
plt.show()