-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
400 lines (305 loc) · 11.7 KB
/
core.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
## Créditos
# Esse trabalho é inspirado [neste material da Loft](https://loft.science/).
# Que por sua vez é inspirado [neste material do Kevin Systrom](https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb).
# general imports
import numpy as np
import pandas as pd
import datetime as dt
# matplotlib
from matplotlib import pyplot as plt
from matplotlib.dates import date2num, num2date
from matplotlib import dates as mdates
from matplotlib import ticker
from matplotlib.colors import ListedColormap
from matplotlib.patches import Patch
# scipy specifics
from scipy import stats as sps
from scipy.interpolate import interp1d
# urls
CITY_DATA_URL = 'https://raw.githubusercontent.com/wcota/covid19br/master/cases-brazil-cities-time.csv'
STATE_DATA_URL = 'https://raw.githubusercontent.com/wcota/covid19br/master/cases-brazil-cities-time.csv'
# We create an array for every possible value of Rt
R_T_MAX = 12
r_t_range = np.linspace(0, R_T_MAX, R_T_MAX*100+1)
# best sigma for Brazil (prior hyperparameters)
#OPTIMAL_SIGMA = 0.35 # through Kevin's Optimization
OPTIMAL_SIGMA = 0.01
# Gamma is 1/serial interval
# https://wwwnc.cdc.gov/eid/article/26/7/20-0282_article
# https://www.nejm.org/doi/full/10.1056/NEJMoa2001316
GAMMA = 1/7
def load_data():
"""
Loads state and city data from wcota repository
Returns
----------
city_df: city data
state_df: state data (Brazil)
"""
city_df = (
pd.read_csv(CITY_DATA_URL, parse_dates=['date'])
.rename(columns={'totalCases':'confirmed_total',
'newCases': 'confirmed_new',
'deaths': 'deaths_total',
'newDeaths': 'deaths_new'})
.drop(['ibgeID','country','state'], axis=1)
.assign(city = lambda x: x['city'].replace('TOTAL', 'Brazil'))
.groupby(['city','date']).sum()
)
state_df = (
pd.read_csv(STATE_DATA_URL, parse_dates=['date'])
.rename(columns={'totalCases':'confirmed_total',
'newCases': 'confirmed_new',
'deaths': 'deaths_total',
'newDeaths': 'deaths_new'})
.drop(['ibgeID','country','city'], axis=1)
.assign(state = lambda x: x['state'].replace('TOTAL', 'Brazil'))
.groupby(['state','date']).sum()
)
return city_df, state_df
def smooth_new_cases(new_cases):
"""
Function to apply gaussian smoothing to cases
Arguments
----------
new_cases: time series of new cases
Returns
----------
smoothed_cases: cases after gaussian smoothing
See also
----------
This code is heavily based on Realtime R0
by Kevin Systrom
https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
"""
smoothed_cases = new_cases.rolling(7,
win_type='gaussian',
min_periods=1,
center=True).mean(std=2).round()
zeros = smoothed_cases.index[smoothed_cases.eq(0)]
if len(zeros) == 0:
idx_start = 0
else:
last_zero = zeros.max()
idx_start = smoothed_cases.index.get_loc(last_zero) + 1
smoothed_cases = smoothed_cases.iloc[idx_start:]
original = new_cases.loc[smoothed_cases.index]
return original, smoothed_cases
def calculate_posteriors(sr, sigma=0.15):
"""
Function to calculate posteriors of Rt over time
Arguments
----------
sr: smoothed time series of new cases
sigma: gaussian noise applied to prior so we can "forget" past observations
works like exponential weighting
Returns
----------
posteriors: posterior distributions
log_likelihood: log likelihood given data
See also
----------
This code is heavily based on Realtime R0
by Kevin Systrom
https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
"""
# (1) Calculate Lambda
lam = sr[:-1].values * np.exp(GAMMA * (r_t_range[:, None] - 1))
# (2) Calculate each day's likelihood
likelihoods = pd.DataFrame(
data = sps.poisson.pmf(sr[1:].values, lam),
index = r_t_range,
columns = sr.index[1:])
# (3) Create the Gaussian Matrix
process_matrix = sps.norm(loc=r_t_range,
scale=sigma
).pdf(r_t_range[:, None])
# (3a) Normalize all rows to sum to 1
process_matrix /= process_matrix.sum(axis=0)
# (4) Calculate the initial prior
prior0 = sps.gamma(a=4).pdf(r_t_range)
prior0 /= prior0.sum()
# Create a DataFrame that will hold our posteriors for each day
# Insert our prior as the first posterior.
posteriors = pd.DataFrame(
index=r_t_range,
columns=sr.index,
data={sr.index[0]: prior0}
)
# We said we'd keep track of the sum of the log of the probability
# of the data for maximum likelihood calculation.
log_likelihood = 0.0
# (5) Iteratively apply Bayes' rule
for previous_day, current_day in zip(sr.index[:-1], sr.index[1:]):
#(5a) Calculate the new prior
current_prior = process_matrix @ posteriors[previous_day]
#(5b) Calculate the numerator of Bayes' Rule: P(k|R_t)P(R_t)
numerator = likelihoods[current_day] * current_prior
#(5c) Calcluate the denominator of Bayes' Rule P(k)
denominator = np.sum(numerator)
# Execute full Bayes' Rule
posteriors[current_day] = numerator/denominator
# Add to the running sum of log likelihoods
log_likelihood += np.log(denominator)
return posteriors, log_likelihood
def highest_density_interval(pmf, p=.9):
"""
Function to calculate highest density interval
from posteriors of Rt over time
Arguments
----------
pmf: posterior distribution of Rt
p: mass of high density interval
Returns
----------
interval: expected value and density interval
See also
----------
This code is heavily based on Realtime R0
by Kevin Systrom
https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
"""
# If we pass a DataFrame, just call this recursively on the columns
if(isinstance(pmf, pd.DataFrame)):
return pd.DataFrame([highest_density_interval(pmf[col], p=p) for col in pmf],
index=pmf.columns)
cumsum = np.cumsum(pmf.values)
best = None
for i, value in enumerate(cumsum):
for j, high_value in enumerate(cumsum[i+1:]):
if (high_value-value > p) and (not best or j<best[1]-best[0]):
best = (i, i+j+1)
break
low = pmf.index[best[0]]
high = pmf.index[best[1]]
most_likely = pmf.idxmax()
interval = pd.Series([most_likely, low, high], index=['ML',f'Low_{p*100:.0f}', f'High_{p*100:.0f}'])
return interval
def plot_rt(result, ax, state_name):
"""
Function to plot Rt
Arguments
----------
result: expected value and HDI of posterior
ax: matplotlib axes
state_name: state to be considered
See also
----------
This code is heavily based on Realtime R0
by Kevin Systrom
https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
"""
ax.set_title(f"{state_name}")
# Colors
ABOVE = [1,0,0]
MIDDLE = [1,1,1]
BELOW = [0,0,0]
cmap = ListedColormap(np.r_[
np.linspace(BELOW,MIDDLE,25),
np.linspace(MIDDLE,ABOVE,25)
])
color_mapped = lambda y: np.clip(y, .5, 1.5)-.5
index = result['ML'].index.get_level_values('date')
values = result['ML'].values
# Plot dots and line
ax.plot(index, values, c='k', zorder=1, alpha=.25)
ax.scatter(index,
values,
s=40,
lw=.5,
c=cmap(color_mapped(values)),
edgecolors='k', zorder=2)
# Aesthetically, extrapolate credible interval by 1 day either side
lowfn = interp1d(date2num(index),
result['Low_90'].values,
bounds_error=False,
fill_value='extrapolate')
highfn = interp1d(date2num(index),
result['High_90'].values,
bounds_error=False,
fill_value='extrapolate')
extended = pd.date_range(start=pd.Timestamp('2020-03-01'),
end=index[-1]+pd.Timedelta(days=1))
ax.fill_between(extended,
lowfn(date2num(extended)),
highfn(date2num(extended)),
color='k',
alpha=.1,
lw=0,
zorder=3)
ax.axhline(1.0, c='k', lw=1, label='$R_t=1.0$', alpha=.25);
# Formatting
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
ax.xaxis.set_minor_locator(mdates.DayLocator())
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1f}"))
ax.yaxis.tick_right()
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.margins(0)
ax.grid(which='major', axis='y', c='k', alpha=.1, zorder=-2)
ax.margins(0)
ax.set_ylim(0.0, 5.0)
ax.set_xlim(pd.Timestamp('2020-03-01'), result.index.get_level_values('date')[-1]+pd.Timedelta(days=1))
def plot_standings(mr, figsize=None, title='Most Recent $R_t$ by location'):
"""
Function to plot standings
Arguments
----------
mr: results by state
See also
----------
This code is heavily based on Realtime R0
by Kevin Systrom
https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
"""
if not figsize:
figsize = ((15.9/50)*len(mr)+.1,2.5)
fig, ax = plt.subplots(figsize=figsize, dpi=150)
ax.set_title(title)
err = mr[['Low_90', 'High_90']].sub(mr['ML'], axis=0).abs()
bars = ax.bar(mr.index,
mr['ML'],
width=.825,
color=[.7,.7,.7],
ecolor=[.3,.3,.3],
capsize=2,
error_kw={'alpha':.5, 'lw':1},
yerr=err.values.T)
#for bar, state_name in zip(bars, mr.index):
# if state_name in no_lockdown:
# bar.set_color(NONE_COLOR)
# if state_name in partial_lockdown:
# bar.set_color(PARTIAL_COLOR)
labels = mr.index.to_series().replace({'District of Columbia':'DC'})
ax.set_xticklabels(labels, rotation=90, fontsize=11)
ax.margins(0)
ax.set_ylim(0,4.)
ax.axhline(1.0, linestyle=':', color='k', lw=1)
#leg = ax.legend(handles=[
# Patch(label='Full', color=FULL_COLOR),
# Patch(label='Partial', color=PARTIAL_COLOR),
# Patch(label='None', color=NONE_COLOR)
# ],
# title='Lockdown',
# ncol=3,
# loc='upper left',
# columnspacing=.75,
# handletextpad=.5,
# handlelength=1)
#leg._legend_box.align = "left"
fig.set_facecolor('w')
return fig, ax
def run_full_model(cases, sigma=OPTIMAL_SIGMA):
# initializing result dict
result = pd.Series()
# smoothing series
new, smoothed = smooth_new_cases(cases)
if smoothed.empty == False:
# calculating posteriors
posteriors, log_likelihood = calculate_posteriors(smoothed, sigma=sigma)
# calculating HDI
result = highest_density_interval(posteriors, p=.9)
return result