This file contains hidden or 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
weekly = pm.auto_arima(training.y, start_p=1, start_q=1, | |
max_p=7, max_q=7, # maximum p and q | |
m=7, # frequency of series | |
d=0, # order of differencing | |
seasonal=True, # Yes Seasonality | |
error_action='ignore', | |
suppress_warnings=True, | |
) |
This file contains hidden or 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
# 7,0,7 ARIMA Model | |
model = ARIMA(training.y, order=(7,0,7)) | |
model_fit = model.fit(disp=0) |
This file contains hidden or 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
# ACF plot of 1st differenced series | |
fig, axes = plt.subplots(1, 2, sharex=True) | |
axes[0].plot(df['y'].diff()); axes[0].set_title('1st Differencing') | |
axes[1].set(ylim=(0,2),xlim=(-1,50)) | |
plot_acf((df['y'].diff()).dropna(), ax=axes[1]) | |
plt.show() |
This file contains hidden or 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
# PACF plot of differenced series | |
plt.rcParams.update({'figure.figsize':(9,3), 'figure.dpi':120}) | |
fig, axes = plt.subplots(1, 2, sharex=True) | |
axes[0].plot(df['y'].diff()); axes[0].set_title('1st Differencing') | |
axes[1].set(ylim=(0,2),xlim=(-1,40)) | |
plot_pacf((df['y'].diff()).dropna(), ax=axes[1]) | |
plt.show() |
This file contains hidden or 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
adf_stats = adfuller(df['y']) | |
'ADF Statistic: %f' % adf_stats[0] | |
'p-value: %f' % adf_stats[1] |
This file contains hidden or 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
def dh_plotter(training,testing,forecast,title): | |
# Add testing/training tables | |
train_table =createTableFromData({'Date':training.ds,'Training':training.y, | |
'Testing':[0.0],'Forecast':[0.0]}, columns=None, convertUnknownToString=False) | |
test_table = createTableFromData({'Date':testing.ds,'Training':[0.0],'Testing' | |
:testing.y,'Forecast':forecast}, columns=None, convertUnknownToString=False) | |
# Merge together to create output table | |
output_table = ttools.merge(train_table,test_table)\ |
This file contains hidden or 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
manning_table = createTableFromData({'Date':df.ds,'Search Frequency':df.y},\ | |
columns=None, convertUnknownToString=False)\ | |
.update('Date=convertDateTime(Date+`T16:30 NY`)') | |
manning_plot = Plot.plot("Search Frequency", manning_table, 'Date','Search_Frequency')\ | |
.chartTitle("Peyton Manning Search Frequency")\ | |
.xLabel('Date')\ | |
.yLabel('Search Frequency')\ | |
.figure | |
This file contains hidden or 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
# specifying model conditions | |
step = pm.NUTS(target_accept=.99) | |
start = pm.find_MAP() | |
# execute sampling | |
modelTrace pm.sample(draws=500, tune=500, step=step, start=start, chains=5) |
This file contains hidden or 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
# create variance prior | |
sigma = pm.HalfCauchy('sigma', beta=2) | |
# create likelihood with modelled counts and observed counts | |
obsDeath = pm.TruncatedNormal('obsDeath', mu=sirDeath, sigma=sigma, lower=0, upper=pop, | |
observed=dataDeath) |
This file contains hidden or 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
# create number of removed based on analytic solution and above parameters | |
sirRem = pm.Deterministic('sirRem', | |
pop - ((s0 + i0)**(beta/(beta - gamma)))* | |
(s0 + i0*tt.exp(time*(beta - gamma)))**(-gamma/(beta - gamma))) | |
# create number of deaths as a proportion of the number of removed | |
sirDeath = pm.Deterministic('sirDeath', rho*sirRem) |