{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## ARMA Modeling: Model Selection\n", "\n", "**Functions**\n", "\n", "`sm.tsa.SARIMAX`\n", "\n", "### Exercise 68\n", "Perform a model selection exercise on the term premium using\n", "\n", "1. General-to-Specific\n", "2. Specific-to-General\n", "3. Minimizing an Information Criteria\n", "\n", "**Note**: Some models, especially if not well specified, may show various warnings. These can be ignored" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:18.192654Z", "iopub.status.busy": "2021-09-22T10:06:18.192654Z", "iopub.status.idle": "2021-09-22T10:06:18.864655Z", "shell.execute_reply": "2021-09-22T10:06:18.864655Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "data = pd.read_hdf(\"data/term-premium.h5\", \"term_premium\")\n", "term = data[\"TERM\"]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:18.868655Z", "iopub.status.busy": "2021-09-22T10:06:18.868655Z", "iopub.status.idle": "2021-09-22T10:06:33.897816Z", "shell.execute_reply": "2021-09-22T10:06:33.897816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AR: 0, MA: 0\n", "AR: 0, MA: 1\n", "AR: 0, MA: 2\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 0, MA: 3\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 0, MA: 4\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 1, MA: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 1, MA: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 1, MA: 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 1, MA: 3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 1, MA: 4\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 2, MA: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 2, MA: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 2, MA: 2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 2, MA: 3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 2, MA: 4\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 3, MA: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 3, MA: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 3, MA: 2\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:966: UserWarning: Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.\n", " warn('Non-stationary starting autoregressive parameters'\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 3, MA: 3\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:966: UserWarning: Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.\n", " warn('Non-stationary starting autoregressive parameters'\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 3, MA: 4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 4, MA: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 4, MA: 1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 4, MA: 2\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:966: UserWarning: Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.\n", " warn('Non-stationary starting autoregressive parameters'\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:966: UserWarning: Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.\n", " warn('Non-stationary starting autoregressive parameters'\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 4, MA: 3\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "AR: 4, MA: 4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AICBIC
ARMA
002446.5024242455.923470
11459.6584181473.789987
2892.995533911.837626
3544.132966567.685581
4378.778704407.041842
1084.50633498.637903
1-21.530719-2.688626
2-24.091152-0.538537
3-24.1322064.130932
4-25.3284777.645185
203.09952321.941616
1-25.747284-2.194669
2-25.6238892.639250
3-23.8939539.079709
4-23.53554514.148640
30-12.83229010.720326
1-25.7371912.525948
2-23.7849299.188733
3-31.1366026.547583
4-31.14857511.246133
40-14.47142813.791711
1-23.8545859.119077
2-21.93369215.750493
3-31.00190511.392803
4-23.74602323.359208
\n", "
" ], "text/plain": [ " AIC BIC\n", "AR MA \n", "0 0 2446.502424 2455.923470\n", " 1 1459.658418 1473.789987\n", " 2 892.995533 911.837626\n", " 3 544.132966 567.685581\n", " 4 378.778704 407.041842\n", "1 0 84.506334 98.637903\n", " 1 -21.530719 -2.688626\n", " 2 -24.091152 -0.538537\n", " 3 -24.132206 4.130932\n", " 4 -25.328477 7.645185\n", "2 0 3.099523 21.941616\n", " 1 -25.747284 -2.194669\n", " 2 -25.623889 2.639250\n", " 3 -23.893953 9.079709\n", " 4 -23.535545 14.148640\n", "3 0 -12.832290 10.720326\n", " 1 -25.737191 2.525948\n", " 2 -23.784929 9.188733\n", " 3 -31.136602 6.547583\n", " 4 -31.148575 11.246133\n", "4 0 -14.471428 13.791711\n", " 1 -23.854585 9.119077\n", " 2 -21.933692 15.750493\n", " 3 -31.001905 11.392803\n", " 4 -23.746023 23.359208" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statsmodels.tsa.api as tsa\n", "\n", "ic = {}\n", "for ar in range(5):\n", " for ma in range(5):\n", " print(f\"AR: {ar}, MA: {ma}\")\n", " mod = tsa.SARIMAX(term, order=(ar, 0, ma), trend=\"c\")\n", " res = mod.fit()\n", " ic[(ar, ma)] = [res.aic, res.bic]\n", "\n", "ic = pd.DataFrame(ic, index=[\"AIC\", \"BIC\"]).T\n", "ic.index = ic.index.set_names([\"AR\", \"MA\"])\n", "ic" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:33.901814Z", "iopub.status.busy": "2021-09-22T10:06:33.901814Z", "iopub.status.idle": "2021-09-22T10:06:33.913815Z", "shell.execute_reply": "2021-09-22T10:06:33.913815Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AIC selects AR 3, MA 4\n", "BIC selects AR 1, MA 1\n" ] } ], "source": [ "aic = ic.sort_values(\"AIC\")\n", "aic_ar, aic_ma = aic.index[0]\n", "print(f\"AIC selects AR {aic_ar}, MA {aic_ma}\")\n", "\n", "bic = ic.sort_values(\"BIC\")\n", "bic_ar, bic_ma = bic.index[0]\n", "print(f\"BIC selects AR {bic_ar}, MA {bic_ma}\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:33.916816Z", "iopub.status.busy": "2021-09-22T10:06:33.915814Z", "iopub.status.idle": "2021-09-22T10:06:35.237816Z", "shell.execute_reply": "2021-09-22T10:06:35.237816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "data": { "text/plain": [ "intercept 3.169315\n", "ar.L1 3.502662\n", "ar.L2 -2.196129\n", "ar.L3 1.793205\n", "ar.L4 3.267782\n", "ma.L1 4.951384\n", "ma.L2 6.300074\n", "ma.L3 4.604737\n", "ma.L4 0.968661\n", "sigma2 37.269149\n", "dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(4, 0, 4), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:35.241814Z", "iopub.status.busy": "2021-09-22T10:06:35.241814Z", "iopub.status.idle": "2021-09-22T10:06:36.570817Z", "shell.execute_reply": "2021-09-22T10:06:36.570817Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:966: UserWarning: Non-stationary starting autoregressive parameters found. Using zeros as starting parameters.\n", " warn('Non-stationary starting autoregressive parameters'\n", "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\base\\model.py:582: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n", " warnings.warn(\"Maximum Likelihood optimization failed to \"\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
SARIMAX Results
Dep. Variable: TERM No. Observations: 821
Model: SARIMAX(4, 0, 3) Log Likelihood 24.501
Date: Wed, 22 Sep 2021 AIC -31.002
Time: 11:06:36 BIC 11.393
Sample: 04-01-1953 HQIC -14.736
- 08-01-2021
Covariance Type: opg
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err z P>|z| [0.025 0.975]
intercept 0.1427 0.039 3.668 0.000 0.066 0.219
ar.L1 -0.1950 0.045 -4.377 0.000 -0.282 -0.108
ar.L2 0.0228 0.045 0.511 0.609 -0.065 0.110
ar.L3 0.8256 0.043 19.204 0.000 0.741 0.910
ar.L4 0.2075 0.053 3.947 0.000 0.104 0.310
ma.L1 1.5455 0.042 36.477 0.000 1.462 1.629
ma.L2 1.4873 0.062 24.077 0.000 1.366 1.608
ma.L3 0.6098 0.047 12.838 0.000 0.517 0.703
sigma2 0.0547 0.001 39.262 0.000 0.052 0.057
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Ljung-Box (L1) (Q): 0.00 Jarque-Bera (JB): 3550.80
Prob(Q): 0.95 Prob(JB): 0.00
Heteroskedasticity (H): 0.68 Skew: 0.69
Prob(H) (two-sided): 0.00 Kurtosis: 13.09


Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step)." ], "text/plain": [ "\n", "\"\"\"\n", " SARIMAX Results \n", "==============================================================================\n", "Dep. Variable: TERM No. Observations: 821\n", "Model: SARIMAX(4, 0, 3) Log Likelihood 24.501\n", "Date: Wed, 22 Sep 2021 AIC -31.002\n", "Time: 11:06:36 BIC 11.393\n", "Sample: 04-01-1953 HQIC -14.736\n", " - 08-01-2021 \n", "Covariance Type: opg \n", "==============================================================================\n", " coef std err z P>|z| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "intercept 0.1427 0.039 3.668 0.000 0.066 0.219\n", "ar.L1 -0.1950 0.045 -4.377 0.000 -0.282 -0.108\n", "ar.L2 0.0228 0.045 0.511 0.609 -0.065 0.110\n", "ar.L3 0.8256 0.043 19.204 0.000 0.741 0.910\n", "ar.L4 0.2075 0.053 3.947 0.000 0.104 0.310\n", "ma.L1 1.5455 0.042 36.477 0.000 1.462 1.629\n", "ma.L2 1.4873 0.062 24.077 0.000 1.366 1.608\n", "ma.L3 0.6098 0.047 12.838 0.000 0.517 0.703\n", "sigma2 0.0547 0.001 39.262 0.000 0.052 0.057\n", "===================================================================================\n", "Ljung-Box (L1) (Q): 0.00 Jarque-Bera (JB): 3550.80\n", "Prob(Q): 0.95 Prob(JB): 0.00\n", "Heteroskedasticity (H): 0.68 Skew: 0.69\n", "Prob(H) (two-sided): 0.00 Kurtosis: 13.09\n", "===================================================================================\n", "\n", "Warnings:\n", "[1] Covariance matrix calculated using the outer product of gradients (complex-step).\n", "\"\"\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gts_res = tsa.SARIMAX(term, order=(4, 0, 3), trend=\"c\").fit()\n", "gts_res.summary()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:36.574814Z", "iopub.status.busy": "2021-09-22T10:06:36.574814Z", "iopub.status.idle": "2021-09-22T10:06:36.790816Z", "shell.execute_reply": "2021-09-22T10:06:36.790816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 3.096112\n", "ar.L1 154.969395\n", "sigma2 58.220475\n", "dtype: float64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(1, 0, 0), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:36.794814Z", "iopub.status.busy": "2021-09-22T10:06:36.793814Z", "iopub.status.idle": "2021-09-22T10:06:36.917815Z", "shell.execute_reply": "2021-09-22T10:06:36.917815Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\git\\statsmodels\\statsmodels\\tsa\\statespace\\sarimax.py:978: UserWarning: Non-invertible starting MA parameters found. Using zeros as starting parameters.\n", " warn('Non-invertible starting MA parameters found.'\n" ] }, { "data": { "text/plain": [ "intercept 24.389775\n", "ma.L1 74.017777\n", "sigma2 21.999551\n", "dtype: float64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(0, 0, 1), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:36.921814Z", "iopub.status.busy": "2021-09-22T10:06:36.921814Z", "iopub.status.idle": "2021-09-22T10:06:37.357816Z", "shell.execute_reply": "2021-09-22T10:06:37.357816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 4.244909\n", "ar.L1 93.891822\n", "ar.L2 -22.426362\n", "sigma2 50.391595\n", "dtype: float64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(2, 0, 0), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:37.361817Z", "iopub.status.busy": "2021-09-22T10:06:37.360817Z", "iopub.status.idle": "2021-09-22T10:06:37.596816Z", "shell.execute_reply": "2021-09-22T10:06:37.596816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 4.053603\n", "ar.L1 117.546963\n", "ma.L1 33.983406\n", "sigma2 43.174806\n", "dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(1, 0, 1), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:37.600814Z", "iopub.status.busy": "2021-09-22T10:06:37.599814Z", "iopub.status.idle": "2021-09-22T10:06:38.052816Z", "shell.execute_reply": "2021-09-22T10:06:38.052816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 3.752052\n", "ar.L1 18.512311\n", "ar.L2 6.003319\n", "ma.L1 16.754859\n", "sigma2 38.684049\n", "dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(2, 0, 1), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:38.056814Z", "iopub.status.busy": "2021-09-22T10:06:38.056814Z", "iopub.status.idle": "2021-09-22T10:06:38.385816Z", "shell.execute_reply": "2021-09-22T10:06:38.385816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 3.724566\n", "ar.L1 117.277030\n", "ma.L1 26.188197\n", "ma.L2 -3.595584\n", "sigma2 37.913572\n", "dtype: float64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(1, 0, 2), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:38.389814Z", "iopub.status.busy": "2021-09-22T10:06:38.388814Z", "iopub.status.idle": "2021-09-22T10:06:38.874816Z", "shell.execute_reply": "2021-09-22T10:06:38.874816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 3.798727\n", "ar.L1 11.830394\n", "ar.L2 4.810068\n", "ar.L3 -1.934410\n", "ma.L1 12.735244\n", "sigma2 37.999059\n", "dtype: float64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(3, 0, 1), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:38.877814Z", "iopub.status.busy": "2021-09-22T10:06:38.877814Z", "iopub.status.idle": "2021-09-22T10:06:39.588817Z", "shell.execute_reply": "2021-09-22T10:06:39.588817Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "intercept 3.245250\n", "ar.L1 2.111979\n", "ar.L2 3.056135\n", "ma.L1 4.948013\n", "ma.L2 1.668112\n", "sigma2 38.191631\n", "dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = tsa.SARIMAX(term, order=(2, 0, 2), trend=\"c\").fit()\n", "res.tvalues" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2021-09-22T10:06:39.592814Z", "iopub.status.busy": "2021-09-22T10:06:39.591814Z", "iopub.status.idle": "2021-09-22T10:06:40.047816Z", "shell.execute_reply": "2021-09-22T10:06:40.047816Z" }, "pycharm": { "is_executing": false, "name": "#%%\n" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
SARIMAX Results
Dep. Variable: TERM No. Observations: 821
Model: SARIMAX(2, 0, 1) Log Likelihood 17.874
Date: Wed, 22 Sep 2021 AIC -25.747
Time: 11:06:40 BIC -2.195
Sample: 04-01-1953 HQIC -16.711
- 08-01-2021
Covariance Type: opg
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err z P>|z| [0.025 0.975]
intercept 0.0510 0.014 3.752 0.000 0.024 0.078
ar.L1 0.7160 0.039 18.512 0.000 0.640 0.792
ar.L2 0.2307 0.038 6.003 0.000 0.155 0.306
ma.L1 0.6209 0.037 16.755 0.000 0.548 0.694
sigma2 0.0558 0.001 38.684 0.000 0.053 0.059
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Ljung-Box (L1) (Q): 0.06 Jarque-Bera (JB): 3194.19
Prob(Q): 0.81 Prob(JB): 0.00
Heteroskedasticity (H): 0.70 Skew: 0.58
Prob(H) (two-sided): 0.00 Kurtosis: 12.59


Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step)." ], "text/plain": [ "\n", "\"\"\"\n", " SARIMAX Results \n", "==============================================================================\n", "Dep. Variable: TERM No. Observations: 821\n", "Model: SARIMAX(2, 0, 1) Log Likelihood 17.874\n", "Date: Wed, 22 Sep 2021 AIC -25.747\n", "Time: 11:06:40 BIC -2.195\n", "Sample: 04-01-1953 HQIC -16.711\n", " - 08-01-2021 \n", "Covariance Type: opg \n", "==============================================================================\n", " coef std err z P>|z| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "intercept 0.0510 0.014 3.752 0.000 0.024 0.078\n", "ar.L1 0.7160 0.039 18.512 0.000 0.640 0.792\n", "ar.L2 0.2307 0.038 6.003 0.000 0.155 0.306\n", "ma.L1 0.6209 0.037 16.755 0.000 0.548 0.694\n", "sigma2 0.0558 0.001 38.684 0.000 0.053 0.059\n", "===================================================================================\n", "Ljung-Box (L1) (Q): 0.06 Jarque-Bera (JB): 3194.19\n", "Prob(Q): 0.81 Prob(JB): 0.00\n", "Heteroskedasticity (H): 0.70 Skew: 0.58\n", "Prob(H) (two-sided): 0.00 Kurtosis: 12.59\n", "===================================================================================\n", "\n", "Warnings:\n", "[1] Covariance matrix calculated using the outer product of gradients (complex-step).\n", "\"\"\"" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stg_res = tsa.SARIMAX(term, order=(2, 0, 1), trend=\"c\").fit()\n", "stg_res.summary()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }