Created
August 18, 2018 00:16
-
-
Save ChadFulton/a263918798210e354917e65a17679a24 to your computer and use it in GitHub Desktop.
Statsmodels - Dynamic Prediction
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": 107, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import statsmodels.api as sm\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"# Get some data\n", | |
"macrodata = sm.datasets.macrodata.load_pandas().data\n", | |
"macrodata.index = pd.PeriodIndex(start='1959Q1', end='2009Q3', freq='Q')\n", | |
"endog = macrodata['infl'] + 0.1\n", | |
"\n", | |
"# Estimate the parameters of the model\n", | |
"mod = sm.tsa.UnobservedComponents(endog, 'llevel', seasonal=4)\n", | |
"res = mod.fit()\n", | |
"\n", | |
"np.set_printoptions(suppress=True, precision=5, linewidth=120)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Dynamic prediction" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"n_forecasts = 10\n", | |
"pred1 = np.zeros((n_forecasts, mod.nobs))\n", | |
"\n", | |
"for t in range(mod.nobs):\n", | |
" pred1[:, t] = res.predict(start=t, end=t + n_forecasts - 1, dynamic=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Multiple Kalman filters, filter from the beginning" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/Users/fulton/projects/statsmodels/statsmodels/tsa/statespace/mlemodel.py:938: RuntimeWarning: invalid value encountered in true_divide\n", | |
" (self.nobs - self.ssm.loglikelihood_burn)\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" " | |
] | |
} | |
], | |
"source": [ | |
"n_forecasts = 10\n", | |
"pred2 = np.zeros((n_forecasts, mod.nobs))\n", | |
"\n", | |
"for t in range(mod.nobs):\n", | |
" endog_fcast = [np.nan] if t == 0 else endog[:t]\n", | |
" mod_fcast = sm.tsa.UnobservedComponents(endog_fcast, 'llevel', seasonal=4)\n", | |
" res_fcast = mod_fcast.filter(res.params)\n", | |
" pred2[:, t] = res_fcast.forecast(n_forecasts)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Multiple Kalman filters, only one step filtering" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 121, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" " | |
] | |
} | |
], | |
"source": [ | |
"n_forecasts = 10\n", | |
"pred3 = np.zeros((n_forecasts, mod.nobs))\n", | |
"\n", | |
"for t in range(mod.nobs):\n", | |
" mod_fcast = sm.tsa.UnobservedComponents([], 'llevel', seasonal=4)\n", | |
" mod_fcast.ssm.initialize_known(res.predicted_state[..., t], res.predicted_state_cov[..., t])\n", | |
" res_fcast = mod_fcast.filter(res.params)\n", | |
" pred3[:, t] = res_fcast.forecast(n_forecasts)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Test for equality" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 119, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.all(pred1 == pred2))\n", | |
"print(np.all(pred2 == pred3))" | |
] | |
} | |
], | |
"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.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment