Durbin and Koopman: Box-Jenkins Examples

See Durbin and Koopman (2012), Chapter 8.4


In [ ]:
%matplotlib inline

In [ ]:
import numpy as np
import pandas as pd
from dismalpy import ssm
import matplotlib.pyplot as plt

In [ ]:
# Get the basic series
dinternet = np.array(pd.read_csv('data/internet.csv').diff()[1:])

# Remove datapoints
missing = np.r_[6,16,26,36,46,56,66,72,73,74,75,76,86,96]-1
dinternet[missing] = np.nan

# Statespace
mod = ssm.SARIMAX(dinternet, order=(1,0,1))
res = mod.fit()
print res.summary()

In [ ]:
# In-sample one-step-ahead predictions, and out-of-sample forecasts
nforecast = 20
predict = res.get_prediction(end=mod.nobs + nforecast)
idx = np.arange(len(predict.predicted_mean))
predict_ci = predict.conf_int(alpha=0.5)

# Graph
fig, ax = plt.subplots(figsize=(12,6))
ax.xaxis.grid()
ax.plot(dinternet, 'k.')

# Plot
ax.plot(idx[:-nforecast], predict.predicted_mean[:-nforecast], 'gray')
ax.plot(idx[-nforecast:], predict.predicted_mean[-nforecast:], 'k--', linestyle='--', linewidth=2)
ax.fill_between(idx, predict_ci[:, 0], predict_ci[:, 1], alpha=0.15)

ax.set(title='Figure 8.9 - Internet series');