Autoregressive Moving Average (ARMA): Artificial dataΒΆ

Link to Notebook GitHub

In [ ]:
from __future__ import print_function
import numpy as np
import statsmodels.api as sm
import pandas as pd
from statsmodels.tsa.arima_process import arma_generate_sample
np.random.seed(12345)

Generate some data from an ARMA process:

In [ ]:
arparams = np.array([.75, -.25])
maparams = np.array([.65, .35])

The conventions of the arma_generate function require that we specify a 1 for the zero-lag of the AR and MA parameters and that the AR parameters be negated.

In [ ]:
arparams = np.r_[1, -arparams]
maparam = np.r_[1, maparams]
nobs = 250
y = arma_generate_sample(arparams, maparams, nobs)

Now, optionally, we can add some dates information. For this example, we'll use a pandas time series.

In [ ]:
dates = sm.tsa.datetools.dates_from_range('1980m1', length=nobs)
y = pd.TimeSeries(y, index=dates)
arma_mod = sm.tsa.ARMA(y, order=(2,2))
arma_res = arma_mod.fit(trend='nc', disp=-1)
In [ ]:
print(arma_res.summary())
In [ ]:
y.tail()
                              ARMA Model Results
==============================================================================
Dep. Variable:                      y   No. Observations:                  250
Model:                     ARMA(2, 2)   Log Likelihood                -245.887
Method:                       css-mle   S.D. of innovations              0.645
Date:                Mon, 20 Jul 2015   AIC                            501.773
Time:                        17:45:06   BIC                            519.381
Sample:                    01-31-1980   HQIC                           508.860
                         - 10-31-2000
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
ar.L1.y        0.8411      0.403      2.089      0.038         0.052     1.630
ar.L2.y       -0.2693      0.247     -1.092      0.276        -0.753     0.214
ma.L1.y        0.5352      0.412      1.299      0.195        -0.273     1.343
ma.L2.y        0.0157      0.306      0.051      0.959        -0.585     0.616
                                    Roots
=============================================================================
                 Real           Imaginary           Modulus         Frequency
-----------------------------------------------------------------------------
AR.1            1.5618           -1.1289j            1.9271           -0.0996
AR.2            1.5618           +1.1289j            1.9271            0.0996
MA.1           -1.9835           +0.0000j            1.9835            0.5000
MA.2          -32.1791           +0.0000j           32.1791            0.5000
-----------------------------------------------------------------------------
In [ ]:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,8))
fig = arma_res.plot_predict(start='1999m6', end='2001m5', ax=ax)
legend = ax.legend(loc='upper left')