Created
January 25, 2019 04:49
-
-
Save ChadFulton/b9b26d0362928287423b622a8bafeab4 to your computer and use it in GitHub Desktop.
Statsmodels state space: forecasting using `simulate`
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"\n", | |
"from importlib import reload\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import statsmodels.api as sm\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"from statsmodels.tsa import _ar_model, ar_model_old as ar_old\n", | |
"\n", | |
"dta = sm.datasets.macrodata.load_pandas().data\n", | |
"dta.index = pd.PeriodIndex(start='1959Q1', end='2009Q3', freq='Q')\n", | |
"endog = np.log(dta[['realgdp', 'cpi']]).diff(1).iloc[1:]\n", | |
"\n", | |
"np.set_printoptions(suppress=True, linewidth=120)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Fit the model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"mod = sm.tsa.VARMAX(endog, order=(1, 0))\n", | |
"res = mod.fit()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example: forecasting without shocks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" realgdp cpi\n", | |
"0 0.007656 0.009244\n", | |
"1 0.007840 0.009464\n", | |
"2 0.007863 0.009605\n", | |
"3 0.007850 0.009695\n", | |
"4 0.007833 0.009754\n", | |
"5 0.007820 0.009791\n", | |
"6 0.007811 0.009816\n", | |
"7 0.007805 0.009831\n", | |
"8 0.007801 0.009842\n", | |
"9 0.007799 0.009848\n", | |
"10 0.007797 0.009853\n", | |
"11 0.007796 0.009855\n", | |
"\n", | |
"fcast_simulate = fcast ? True\n" | |
] | |
} | |
], | |
"source": [ | |
"steps = 12\n", | |
"initial_state = res.predicted_state[..., -1]\n", | |
"state_shocks = np.zeros((steps, mod.k_states))\n", | |
"\n", | |
"fcast = res.forecast(steps)\n", | |
"fcast_simulate = res.simulate(steps, state_shocks=state_shocks, initial_state=initial_state)\n", | |
"\n", | |
"print(fcast_simulate)\n", | |
"print()\n", | |
"print('fcast_simulate = fcast ? ', np.all(fcast.values == fcast_simulate.values))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Forecasting with shocks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" realgdp cpi\n", | |
"0 0.007656 0.009244\n", | |
"1 0.007840 0.009464\n", | |
"2 0.007863 1.009605\n", | |
"3 0.867706 0.653800\n", | |
"4 0.170115 0.419310\n", | |
"5 -0.001913 0.272586\n", | |
"6 -0.031876 0.179143\n", | |
"7 -0.027582 0.119141\n", | |
"8 -0.017911 0.080467\n", | |
"9 -0.009651 0.055497\n", | |
"10 -0.003726 0.039363\n", | |
"11 0.000276 0.028934\n" | |
] | |
} | |
], | |
"source": [ | |
"steps = 12\n", | |
"initial_state = res.predicted_state[..., -1]\n", | |
"state_shocks = np.array([\n", | |
" [0, 0],\n", | |
" [0, 1],\n", | |
" [1, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
" [0, 0],\n", | |
"])\n", | |
"\n", | |
"print(res.simulate(steps, state_shocks=state_shocks, initial_state=initial_state))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment