W3cubDocs

/Statsmodels

Autoregressive Moving Average (ARMA): Sunspots data

This notebook replicates the existing ARMA notebook using the statsmodels.tsa.statespace.SARIMAX class rather than the statsmodels.tsa.ARMA class.

In [1]:
%matplotlib inline
In [2]:
from __future__ import print_function
import numpy as np
from scipy import stats
import pandas as pd
import matplotlib.pyplot as plt

import statsmodels.api as sm
In [3]:
from statsmodels.graphics.api import qqplot

Sunpots Data

In [4]:
print(sm.datasets.sunspots.NOTE)
::

    Number of Observations - 309 (Annual 1700 - 2008)
    Number of Variables - 1
    Variable name definitions::

        SUNACTIVITY - Number of sunspots for each year

    The data file contains a 'YEAR' variable that is not returned by load.

In [5]:
dta = sm.datasets.sunspots.load_pandas().data
In [6]:
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
del dta["YEAR"]
In [7]:
dta.plot(figsize=(12,4));
In [8]:
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(dta, lags=40, ax=ax2)
In [9]:
arma_mod20 = sm.tsa.statespace.SARIMAX(dta, order=(2,0,0), trend='c').fit(disp=False)
print(arma_mod20.params)
intercept     14.793947
ar.L1          1.390659
ar.L2         -0.688568
sigma2       274.761104
dtype: float64
In [10]:
arma_mod30 = sm.tsa.statespace.SARIMAX(dta, order=(3,0,0), trend='c').fit(disp=False)
In [11]:
print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic)
2622.63633814 2637.56970325 2628.60672599
In [12]:
print(arma_mod30.params)
intercept     16.762205
ar.L1          1.300810
ar.L2         -0.508122
ar.L3         -0.129612
sigma2       270.102652
dtype: float64
In [13]:
print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic)
2619.40362966 2638.07033605 2626.86661447
  • Does our model obey the theory?
In [14]:
sm.stats.durbin_watson(arma_mod30.resid)
Out[14]:
1.9564844900355425
In [15]:
fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(111)
ax = plt.plot(arma_mod30.resid)
In [16]:
resid = arma_mod30.resid
In [17]:
stats.normaltest(resid)
Out[17]:
NormaltestResult(statistic=49.847005894173549, pvalue=1.4992021638654462e-11)
In [18]:
fig = plt.figure(figsize=(12,4))
ax = fig.add_subplot(111)
fig = qqplot(resid, line='q', ax=ax, fit=True)
In [19]:
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(resid, lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(resid, lags=40, ax=ax2)
In [20]:
r,q,p = sm.tsa.acf(resid, qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
            AC          Q  Prob(>Q)
lag                                
1.0   0.009176   0.026273  0.871235
2.0   0.041820   0.573727  0.750614
3.0  -0.001342   0.574292  0.902292
4.0   0.136064   6.407488  0.170714
5.0   0.092433   9.108334  0.104820
...        ...        ...       ...
36.0 -0.119334  91.249666  0.000001
37.0 -0.036673  91.724837  0.000002
38.0 -0.046204  92.481861  0.000002
39.0 -0.017775  92.594310  0.000003
40.0 -0.006219  92.608125  0.000005

[40 rows x 3 columns]
  • This indicates a lack of fit.
  • In-sample dynamic prediction. How good does our model do?
In [21]:
predict_sunspots = arma_mod30.predict(start='1990', end='2012', dynamic=True)
In [22]:
fig, ax = plt.subplots(figsize=(12, 8))
dta.ix['1950':].plot(ax=ax)
predict_sunspots.plot(ax=ax, style='r');
In [23]:
def mean_forecast_err(y, yhat):
    return y.sub(yhat).mean()
In [24]:
mean_forecast_err(dta.SUNACTIVITY, predict_sunspots)
Out[24]:
5.6355500947126966

© 2009–2012 Statsmodels Developers
© 2006–2008 Scipy Developers
© 2006 Jonathan E. Taylor
Licensed under the 3-clause BSD License.
http://www.statsmodels.org/stable/examples/notebooks/generated/statespace_arma_0.html