Created
February 21, 2017 00:28
-
-
Save KenRoytman/2fcd6af572c4281e2d03c74479407ba3 to your computer and use it in GitHub Desktop.
ARCH exogenous variable example
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4.0\n" | |
] | |
} | |
], | |
"source": [ | |
"import arch\n", | |
"print arch.__version__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import datetime as dt\n", | |
"\n", | |
"import numpy as np\n", | |
"\n", | |
"import pandas_datareader.data as web\n", | |
"\n", | |
"from arch import arch_model\n", | |
"\n", | |
"start = dt.datetime(2000, 1, 1)\n", | |
"end = dt.datetime(2014, 1, 1)\n", | |
"sp500 = web.DataReader('^GSPC', 'yahoo', start=start, end=end)\n", | |
"returns = 100 * sp500['Adj Close'].pct_change().dropna()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"mondays = np.array([1 if i.weekday() == 0 else 0 for i in sp500.index][1:])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Iteration: 1, Func. Count: 6, Neg. LLF: 5159.58323938\n", | |
"Iteration: 2, Func. Count: 16, Neg. LLF: 5156.09760149\n", | |
"Iteration: 3, Func. Count: 24, Neg. LLF: 5152.29989336\n", | |
"Iteration: 4, Func. Count: 31, Neg. LLF: 5146.47531817\n", | |
"Iteration: 5, Func. Count: 38, Neg. LLF: 5143.86337547\n", | |
"Iteration: 6, Func. Count: 45, Neg. LLF: 5143.02096168\n", | |
"Iteration: 7, Func. Count: 52, Neg. LLF: 5142.24105149\n", | |
"Iteration: 8, Func. Count: 60, Neg. LLF: 5142.07138947\n", | |
"Iteration: 9, Func. Count: 67, Neg. LLF: 5141.41665305\n", | |
"Iteration: 10, Func. Count: 73, Neg. LLF: 5141.39212285\n", | |
"Iteration: 11, Func. Count: 79, Neg. LLF: 5141.39023885\n", | |
"Iteration: 12, Func. Count: 85, Neg. LLF: 5141.39023359\n", | |
"Optimization terminated successfully. (Exit mode 0)\n", | |
" Current function value: 5141.39023359\n", | |
" Iterations: 12\n", | |
" Function evaluations: 85\n", | |
" Gradient evaluations: 12\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"<table class=\"simpletable\">\n", | |
"<caption>Constant Mean - GARCH Model Results</caption>\n", | |
"<tr>\n", | |
" <th>Dep. Variable:</th> <td>Adj Close</td> <th> R-squared: </th> <td> -0.001</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Mean Model:</th> <td>Constant Mean</td> <th> Adj. R-squared: </th> <td> -0.001</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Vol Model:</th> <td>GARCH</td> <th> Log-Likelihood: </th> <td> -5141.39</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Distribution:</th> <td>Normal</td> <th> AIC: </th> <td> 10290.8</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Method:</th> <td>Maximum Likelihood</td> <th> BIC: </th> <td> 10315.4</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th></th> <td></td> <th> No. Observations: </th> <td>3520</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Date:</th> <td>Mon, Feb 20 2017</td> <th> Df Residuals: </th> <td>3516</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Time:</th> <td>18:22:11</td> <th> Df Model: </th> <td>4</td> \n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<caption>Mean Model</caption>\n", | |
"<tr>\n", | |
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>95.0% Conf. Int.</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>mu</th> <td> 0.0531</td> <td>1.487e-02</td> <td> 3.569</td> <td>3.581e-04</td> <td>[2.392e-02,8.220e-02]</td>\n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<caption>Volatility Model</caption>\n", | |
"<tr>\n", | |
" <td></td> <th>coef</th> <th>std err</th> <th>t</th> <th>P>|t|</th> <th>95.0% Conf. Int.</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>omega</th> <td> 0.0156</td> <td>4.932e-03</td> <td> 3.155</td> <td>1.606e-03</td> <td>[5.892e-03,2.523e-02]</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>alpha[1]</th> <td> 0.0879</td> <td>1.140e-02</td> <td> 7.710</td> <td>1.260e-14</td> <td>[6.554e-02, 0.110]</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>beta[1]</th> <td> 0.9014</td> <td>1.183e-02</td> <td> 76.163</td> <td>0.000</td> <td>[ 0.878, 0.925]</td> \n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<class 'statsmodels.iolib.summary.Summary'>\n", | |
"\"\"\"\n", | |
" Constant Mean - GARCH Model Results \n", | |
"==============================================================================\n", | |
"Dep. Variable: Adj Close R-squared: -0.001\n", | |
"Mean Model: Constant Mean Adj. R-squared: -0.001\n", | |
"Vol Model: GARCH Log-Likelihood: -5141.39\n", | |
"Distribution: Normal AIC: 10290.8\n", | |
"Method: Maximum Likelihood BIC: 10315.4\n", | |
" No. Observations: 3520\n", | |
"Date: Mon, Feb 20 2017 Df Residuals: 3516\n", | |
"Time: 18:22:11 Df Model: 4\n", | |
" Mean Model \n", | |
"==============================================================================\n", | |
" coef std err t P>|t| 95.0% Conf. Int.\n", | |
"------------------------------------------------------------------------------\n", | |
"mu 0.0531 1.487e-02 3.569 3.581e-04 [2.392e-02,8.220e-02]\n", | |
" Volatility Model \n", | |
"==============================================================================\n", | |
" coef std err t P>|t| 95.0% Conf. Int.\n", | |
"------------------------------------------------------------------------------\n", | |
"omega 0.0156 4.932e-03 3.155 1.606e-03 [5.892e-03,2.523e-02]\n", | |
"alpha[1] 0.0879 1.140e-02 7.710 1.260e-14 [6.554e-02, 0.110]\n", | |
"beta[1] 0.9014 1.183e-02 76.163 0.000 [ 0.878, 0.925]\n", | |
"==============================================================================\n", | |
"\n", | |
"Covariance estimator: robust\n", | |
"\"\"\"" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arch_model(returns, x=mondays).fit().summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "ValueError", | |
"evalue": "x must be nobs by n, where nobs is the same as the number of elements in y", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-31-f6829c130ccd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0march_model\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mreturns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmondays\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmean\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'HARX'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msummary\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32mC:\\Program Files\\Miniconda2\\envs\\booth-econometrics2\\lib\\site-packages\\arch\\univariate\\mean.pyc\u001b[0m in \u001b[0;36march_model\u001b[0;34m(y, x, mean, lags, vol, p, o, q, power, dist, hold_back)\u001b[0m\n\u001b[1;32m 1159\u001b[0m \u001b[0mam\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mConstantMean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhold_back\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mhold_back\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 1160\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mmean\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'harx'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1161\u001b[0;31m \u001b[0mam\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mHARX\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlags\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhold_back\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mhold_back\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1162\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mmean\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'har'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 1163\u001b[0m \u001b[0mam\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mHARX\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlags\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhold_back\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mhold_back\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32mC:\\Program Files\\Miniconda2\\envs\\booth-econometrics2\\lib\\site-packages\\arch\\univariate\\mean.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, y, x, lags, constant, use_rotated, hold_back, volatility, distribution)\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_hold_back\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmax_lags\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 169\u001b[0;31m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_init_model\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 170\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 171\u001b[0m \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32mC:\\Program Files\\Miniconda2\\envs\\booth-econometrics2\\lib\\site-packages\\arch\\univariate\\mean.pyc\u001b[0m in \u001b[0;36m_init_model\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 463\u001b[0m \u001b[1;34m\"\"\"Should be called whenever the model is initialized or changed\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 464\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_reformat_lags\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 465\u001b[0;31m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_check_specification\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 466\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 467\u001b[0m \u001b[0mnobs_orig\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_y\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32mC:\\Program Files\\Miniconda2\\envs\\booth-econometrics2\\lib\\site-packages\\arch\\univariate\\mean.pyc\u001b[0m in \u001b[0;36m_check_specification\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_x\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_x\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_y\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 392\u001b[0m raise ValueError(\n\u001b[0;32m--> 393\u001b[0;31m \u001b[1;34m'x must be nobs by n, where nobs is the same as '\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 394\u001b[0m 'the number of elements in y')\n\u001b[1;32m 395\u001b[0m \u001b[0mdef_names\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'x'\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_x\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mValueError\u001b[0m: x must be nobs by n, where nobs is the same as the number of elements in y" | |
] | |
} | |
], | |
"source": [ | |
"arch_model(returns, x=mondays, mean='HARX').fit().summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment