Created
August 26, 2012 23:38
-
-
Save vincentarelbundock/3484395 to your computer and use it in GitHub Desktop.
Statsmodels example: Autoregressive Moving Average (ARMA) Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "ex_arma2" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Autoregressive Moving Average (ARMA) Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"import statsmodels.api as sm\n", | |
"from statsmodels.tsa.arima_process import arma_generate_sample\n", | |
"np.random.seed(12345)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"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": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"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": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"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": [ | |
"import pandas\n", | |
"dates = sm.tsa.datetools.dates_from_range('1980m1', length=nobs)\n", | |
"y = pandas.TimeSeries(y, index=dates)\n", | |
"arma_mod = sm.tsa.ARMA(y, freq='M')\n", | |
"arma_res = arma_mod.fit(order=(2,2), trend='nc', disp=-1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stderr", | |
"text": [ | |
"/usr/local/lib/python2.7/dist-packages/statsmodels-0.5.0-py2.7-linux-x86_64.egg/statsmodels/tsa/arima_model.py:226: FutureWarning: In the next release order will not be optional in the model constructor.\n", | |
" \"in the model constructor.\", FutureWarning)\n", | |
"/usr/local/lib/python2.7/dist-packages/statsmodels-0.5.0-py2.7-linux-x86_64.egg/statsmodels/tsa/arima_model.py:657: FutureWarning: The order argument to fit is deprecated. Please use the model constructor argument order. This will overwrite any order given in the model constructor.\n", | |
" \"constructor.\", FutureWarning)\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment