def testModelFit(arma_mod30, dta):
# does our model fit the theory?
residuals = arma_mod30.resid
sm.stats.durbin_watson(residuals.values)
# NOTE: Durbin Watson Test Statistic approximately equal to 2*(1-r)
# where r is the sample autocorrelation of the residuals.
# Thus, for r == 0, indicating no serial correlation,
# the test statistic equals 2. This statistic will always be
# between 0 and 4. The closer to 0 the statistic, the more evidence
# for positive serial correlation. The closer to 4, the more evidence
# for negative serial correlation.
# plot the residuals so we can see if there are any areas in time which
# are poorly explained.
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
ax = arma_mod30.resid.plot(ax=ax);
plt.savefig(FIG_DIR+'residualsVsTime.png', bbox_inches='tight')
# plt.show()
# tests if samples are different from normal dist.
k2, p = stats.normaltest(residuals)
print ("residuals skew (k2):" + str(k2) +
" fit w/ normal dist (p-value): " + str(p))
# plot residuals
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(211)
fig = qqplot(residuals, line='q', ax=ax, fit=True)
ax2 = fig.add_subplot(212)
# resid_dev = residuals.resid_deviance.copy()
# resid_std = (resid_dev - resid_dev.mean()) / resid_dev.std()
plt.hist(residuals, bins=25);
plt.title('Histogram of standardized deviance residuals');
plt.savefig(FIG_DIR+'residualsNormality.png', bbox_inches='tight')
# plot ACF/PACF for residuals
plotACFAndPACF(residuals, 'residualsACFAndPACF.png')
r,q,p = sm.tsa.acf(residuals.values.squeeze(), qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pandas.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
# sameple data indicates a lack of fit.
评论列表
文章目录