Logo

Autoregressive Moving Average (ARMA): Artificial dataΒΆ

Link to Notebook GitHub

In [1]:
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 [2]:
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 [3]:
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 [4]:
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 [5]:
print(arma_res.summary())
                              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:                Sun, 01 Feb 2015   AIC                            501.773
Time:                        09:32:28   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.1792           +0.0000j           32.1792            0.5000
-----------------------------------------------------------------------------

In [6]:
y.tail()
Out[6]:
2000-06-30    0.050999
2000-07-31   -0.206404
2000-08-31   -0.170874
2000-09-30    0.257949
2000-10-31    0.245237
dtype: float64
In [7]:
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')

This Page