{
 "metadata": {
  "name": "",
  "signature": "sha256:fdc85b7fc5fa0dbe79fab572ae79c45c49bf2d964d3dae03dbb5e8bb85143785"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Dates in timeseries models"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import print_function\n",
      "import statsmodels.api as sm\n",
      "import numpy as np\n",
      "import pandas as pd"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Getting started"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = sm.datasets.sunspots.load()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Right now an annual date series must be datetimes at the end of the year."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from datetime import datetime\n",
      "dates = sm.tsa.datetools.dates_from_range('1700', length=len(data.endog))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Using Pandas\n",
      "\n",
      "Make a pandas TimeSeries or DataFrame"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "endog = pd.TimeSeries(data.endog, index=dates)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Instantiate the model"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ar_model = sm.tsa.AR(endog, freq='A')\n",
      "pandas_ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Out-of-sample prediction"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pred = pandas_ar_res.predict(start='2005', end='2015')\n",
      "print(pred)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Using explicit dates"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ar_model = sm.tsa.AR(data.endog, dates=dates, freq='A')\n",
      "ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)\n",
      "pred = ar_res.predict(start='2005', end='2015')\n",
      "print(pred)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This just returns a regular array, but since the model has date information attached, you can get the prediction dates in a roundabout way."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(ar_res.data.predict_dates)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Note: This attribute only exists if predict has been called. It holds the dates associated with the last call to predict."
     ]
    }
   ],
   "metadata": {}
  }
 ]
}