{
 "metadata": {
  "name": "",
  "signature": "sha256:f9294c816d94669d5f3c50eabcbc5c81a09e3a4737399ea424f3e6dabb76ce8d"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Autoregressive Moving Average (ARMA): Artificial data"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "import numpy as np\n",
      "import statsmodels.api as sm\n",
      "import pandas as pd\n",
      "from statsmodels.tsa.arima_process import arma_generate_sample\n",
      "np.random.seed(12345)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Generate some data from an ARMA process:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arparams = np.array([.75, -.25])\n",
      "maparams = np.array([.65, .35])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "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."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arparams = np.r_[1, -arparams]\n",
      "maparam = np.r_[1, maparams]\n",
      "nobs = 250\n",
      "y = arma_generate_sample(arparams, maparams, nobs)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      " Now, optionally, we can add some dates information. For this example, we'll use a pandas time series."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dates = sm.tsa.datetools.dates_from_range('1980m1', length=nobs)\n",
      "y = pd.TimeSeries(y, index=dates)\n",
      "arma_mod = sm.tsa.ARMA(y, order=(2,2))\n",
      "arma_res = arma_mod.fit(trend='nc', disp=-1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(arma_res.summary())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "y.tail()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import matplotlib.pyplot as plt\n",
      "fig, ax = plt.subplots(figsize=(10,8))\n",
      "fig = arma_res.plot_predict(start='1999m6', end='2001m5', ax=ax)\n",
      "legend = ax.legend(loc='upper left')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}