Created
July 7, 2014 21:48
-
-
Save ChadFulton/a046ba1a2b91b6a0984d to your computer and use it in GitHub Desktop.
Example: Statespace ARMA(3,3)
This file contains 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": "", | |
"signature": "sha256:0dfe44a1723926d360f9bcad2ef7fa0c93b017429fb1b14915375327d1012d05" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import pykf\n", | |
"import statsmodels.api as sm" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# GDP and Unemployment, Quarterly, 1948.1 - 1995.3\n", | |
"data = pd.read_csv('GDPC1.csv')\n", | |
"dlgdp = np.log(data['VALUE']).diff()[1:]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Start params, taken from Stata\n", | |
"start_params = np.r_[\n", | |
" -1.903853, 1.36063, -.4482415,\n", | |
" 2.241022, -1.872193, .6310465,\n", | |
" .0088197**2\n", | |
"]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Statsmodels\n", | |
"mod = sm.tsa.ARMA(np.array(dlgdp), order=(3,3))\n", | |
"res = mod.fit(trend='nc', maxiter=0)\n", | |
"mod.method = 'mle'\n", | |
"sm_start_params = np.r_[start_params[3:6], start_params[:3]]\n", | |
"print mod.loglike(sm_start_params)\n", | |
"%timeit mod.loglike(sm_start_params)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"886.154615571\n", | |
"1000 loops, best of 3: 792 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# PyKF\n", | |
"mod = pykf.ARMA(dlgdp, order=(3,3))\n", | |
"print mod.loglike(start_params, True)\n", | |
"%timeit mod.loglike(start_params, True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"886.154615569\n", | |
"1000 loops, best of 3: 566 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment