{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Detrending, Stylized Facts and the Business Cycle\n",
    "\n",
    "In an influential article, Harvey and Jaeger (1993) described the use of unobserved components models (also known as \"structural time series models\") to derive stylized facts of the business cycle.\n",
    "\n",
    "Their paper begins:\n",
    "\n",
    "    \"Establishing the 'stylized facts' associated with a set of time series is widely considered a crucial step\n",
    "    in macroeconomic research ... For such facts to be useful they should (1) be consistent with the stochastic\n",
    "    properties of the data and (2) present meaningful information.\"\n",
    "    \n",
    "In particular, they make the argument that these goals are often better met using the unobserved components approach rather than the popular Hodrick-Prescott filter or Box-Jenkins ARIMA modeling techniques.\n",
    "\n",
    "statsmodels has the ability to perform all three types of analysis, and below we follow the steps of their paper, using a slightly updated dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\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 IPython.display import display, Latex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unobserved Components\n",
    "\n",
    "The unobserved components model available in statsmodels can be written as:\n",
    "\n",
    "$$\n",
    "y_t = \\underbrace{\\mu_{t}}_{\\text{trend}} + \\underbrace{\\gamma_{t}}_{\\text{seasonal}} + \\underbrace{c_{t}}_{\\text{cycle}} + \\sum_{j=1}^k \\underbrace{\\beta_j x_{jt}}_{\\text{explanatory}} + \\underbrace{\\varepsilon_t}_{\\text{irregular}}\n",
    "$$\n",
    "\n",
    "see Durbin and Koopman 2012, Chapter 3 for notation and additional details. Notice that different specifications for the different individual components can support a wide range of models. The specific models considered in the paper and below are specializations of this general equation.\n",
    "\n",
    "### Trend\n",
    "\n",
    "The trend component is a dynamic extension of a regression model that includes an intercept and linear time-trend.\n",
    "\n",
    "$$\n",
    "\\begin{align}\n",
    "\\underbrace{\\mu_{t+1}}_{\\text{level}} & = \\mu_t + \\nu_t + \\eta_{t+1} \\qquad & \\eta_{t+1} \\sim N(0, \\sigma_\\eta^2) \\\\\\\\\n",
    "\\underbrace{\\nu_{t+1}}_{\\text{trend}} & = \\nu_t + \\zeta_{t+1} & \\zeta_{t+1} \\sim N(0, \\sigma_\\zeta^2) \\\\\n",
    "\\end{align}\n",
    "$$\n",
    "\n",
    "where the level is a generalization of the intercept term that can dynamically vary across time, and the trend is a generalization of the time-trend such that the slope can dynamically vary across time.\n",
    "\n",
    "For both elements (level and trend), we can consider models in which:\n",
    "\n",
    "- The element is included vs excluded (if the trend is included, there must also be a level included).\n",
    "- The element is deterministic vs stochastic (i.e. whether or not the variance on the error term is confined to be zero or not)\n",
    "\n",
    "The only additional parameters to be estimated via MLE are the variances of any included stochastic components.\n",
    "\n",
    "This leads to the following specifications:\n",
    "\n",
    "|                                                                      | Level | Trend | Stochastic Level | Stochastic Trend |\n",
    "|----------------------------------------------------------------------|-------|-------|------------------|------------------|\n",
    "| Constant                                                             | ✓     |       |                  |                  |\n",
    "| Local Level <br /> (random walk)                                     | ✓     |       | ✓                |                  |\n",
    "| Deterministic trend                                                  | ✓     | ✓     |                  |                  |\n",
    "| Local level with deterministic trend <br /> (random walk with drift) | ✓     | ✓     | ✓                |                  |\n",
    "| Local linear trend                                                   | ✓     | ✓     | ✓                | ✓                |\n",
    "| Smooth trend <br /> (integrated random walk)                         | ✓     | ✓     |                  | ✓                |\n",
    "\n",
    "### Seasonal\n",
    "\n",
    "The seasonal component is written as:\n",
    "\n",
    "<span>$$\n",
    "\\gamma_t = - \\sum_{j=1}^{s-1} \\gamma_{t+1-j} + \\omega_t \\qquad \\omega_t \\sim N(0, \\sigma_\\omega^2)\n",
    "$$</span>\n",
    "\n",
    "The periodicity (number of seasons) is `s`, and the defining character is that (without the error term), the seasonal components sum to zero across one complete cycle. The inclusion of an error term allows the seasonal effects to vary over time.\n",
    "\n",
    "The variants of this model are:\n",
    "\n",
    "- The periodicity `s`\n",
    "- Whether or not to make the seasonal effects stochastic.\n",
    "\n",
    "If the seasonal effect is stochastic, then there is one additional parameter to estimate via MLE (the variance of the error term).\n",
    "\n",
    "### Cycle\n",
    "\n",
    "The cyclical component is intended to capture cyclical effects at time frames much longer than captured by the seasonal component. For example, in economics the cyclical term is often intended to capture the business cycle, and is then expected to have a period between \"1.5 and 12 years\" (see Durbin and Koopman).\n",
    "\n",
    "The cycle is written as:\n",
    "\n",
    "<span>$$\n",
    "\\begin{align}\n",
    "c_{t+1} & = c_t \\cos \\lambda_c + c_t^* \\sin \\lambda_c + \\tilde \\omega_t \\qquad & \\tilde \\omega_t \\sim N(0, \\sigma_{\\tilde \\omega}^2) \\\\\\\\\n",
    "c_{t+1}^* & = -c_t \\sin \\lambda_c + c_t^* \\cos \\lambda_c + \\tilde \\omega_t^* & \\tilde \\omega_t^* \\sim N(0, \\sigma_{\\tilde \\omega}^2)\n",
    "\\end{align}\n",
    "$$</span>\n",
    "\n",
    "The parameter $\\lambda_c$ (the frequency of the cycle) is an additional parameter to be estimated by MLE. If the seasonal effect is stochastic, then there is one another parameter to estimate (the variance of the error term - note that both of the error terms here share the same variance, but are assumed to have independent draws).\n",
    "\n",
    "### Irregular\n",
    "\n",
    "The irregular component is assumed to be a white noise error term. Its variance is a parameter to be estimated by MLE; i.e.\n",
    "\n",
    "$$\n",
    "\\varepsilon_t \\sim N(0, \\sigma_\\varepsilon^2)\n",
    "$$\n",
    "\n",
    "In some cases, we may want to generalize the irregular component to allow for autoregressive effects:\n",
    "\n",
    "$$\n",
    "\\varepsilon_t = \\rho(L) \\varepsilon_{t-1} + \\epsilon_t, \\qquad \\epsilon_t \\sim N(0, \\sigma_\\epsilon^2)\n",
    "$$\n",
    "\n",
    "In this case, the autoregressive parameters would also be estimated via MLE.\n",
    "\n",
    "### Regression effects\n",
    "\n",
    "We may want to allow for explanatory variables by including additional terms\n",
    "\n",
    "<span>$$\n",
    "\\sum_{j=1}^k \\beta_j x_{jt}\n",
    "$$</span>\n",
    "\n",
    "or for intervention effects by including\n",
    "\n",
    "<span>$$\n",
    "\\begin{align}\n",
    "\\delta w_t \\qquad \\text{where} \\qquad w_t & = 0, \\qquad t < \\tau, \\\\\\\\\n",
    "& = 1, \\qquad t \\ge \\tau\n",
    "\\end{align}\n",
    "$$</span>\n",
    "\n",
    "These additional parameters could be estimated via MLE or by including them as components of the state space formulation.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data\n",
    "\n",
    "Following Harvey and Jaeger, we will consider the following time series:\n",
    "\n",
    "- US real GNP, \"output\", ([GNPC96](https://research.stlouisfed.org/fred2/series/GNPC96))\n",
    "- US GNP implicit price deflator, \"prices\", ([GNPDEF](https://research.stlouisfed.org/fred2/series/GNPDEF))\n",
    "- US monetary base, \"money\", ([AMBSL](https://research.stlouisfed.org/fred2/series/AMBSL))\n",
    "\n",
    "The time frame in the original paper varied across series, but was broadly 1954-1989. Below we use data from the period 1948-2008 for all series. Although the unobserved components approach allows isolating a seasonal component within the model, the series considered in the paper, and here, are already seasonally adjusted.\n",
    "\n",
    "All data series considered here are taken from [Federal Reserve Economic Data (FRED)](https://research.stlouisfed.org/fred2/). Conveniently, the Python library [Pandas](https://pandas.pydata.org/) has the ability to download data from FRED directly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Datasets\n",
    "from pandas_datareader.data import DataReader\n",
    "\n",
    "# Get the raw data\n",
    "start = '1948-01'\n",
    "end = '2008-01'\n",
    "us_gnp = DataReader('GNPC96', 'fred', start=start, end=end)\n",
    "us_gnp_deflator = DataReader('GNPDEF', 'fred', start=start, end=end)\n",
    "us_monetary_base = DataReader('AMBSL', 'fred', start=start, end=end).resample('QS').mean()\n",
    "recessions = DataReader('USRECQ', 'fred', start=start, end=end).resample('QS').last().values[:,0]\n",
    "\n",
    "# Construct the dataframe\n",
    "dta = pd.concat(map(np.log, (us_gnp, us_gnp_deflator, us_monetary_base)), axis=1)\n",
    "dta.columns = ['US GNP','US Prices','US monetary base']\n",
    "dta.index.freq = dta.index.inferred_freq\n",
    "dates = dta.index._mpl_repr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a sense of these three variables over the timeframe, we can plot them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Plot the data\n",
    "ax = dta.plot(figsize=(13,3))\n",
    "ylim = ax.get_ylim()\n",
    "ax.xaxis.grid()\n",
    "ax.fill_between(dates, ylim[0]+1e-5, ylim[1]-1e-5, recessions, facecolor='k', alpha=0.1);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "Since the data is already seasonally adjusted and there are no obvious explanatory variables, the generic model considered is:\n",
    "\n",
    "$$\n",
    "y_t = \\underbrace{\\mu_{t}}_{\\text{trend}} + \\underbrace{c_{t}}_{\\text{cycle}} + \\underbrace{\\varepsilon_t}_{\\text{irregular}}\n",
    "$$\n",
    "\n",
    "The irregular will be assumed to be white noise, and the cycle will be stochastic and damped. The final modeling choice is the specification to use for the trend component. Harvey and Jaeger consider two models:\n",
    "\n",
    "1. Local linear trend (the \"unrestricted\" model)\n",
    "2. Smooth trend (the \"restricted\" model, since we are forcing $\\sigma_\\eta = 0$)\n",
    "\n",
    "Below, we construct `kwargs` dictionaries for each of these model types. Notice that rather that there are two ways to specify the models. One way is to specify components directly, as in the table above. The other way is to use string names which map to various specifications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Model specifications\n",
    "\n",
    "# Unrestricted model, using string specification\n",
    "unrestricted_model = {\n",
    "    'level': 'local linear trend', 'cycle': True, 'damped_cycle': True, 'stochastic_cycle': True\n",
    "}\n",
    "\n",
    "# Unrestricted model, setting components directly\n",
    "# This is an equivalent, but less convenient, way to specify a\n",
    "# local linear trend model with a stochastic damped cycle:\n",
    "# unrestricted_model = {\n",
    "#     'irregular': True, 'level': True, 'stochastic_level': True, 'trend': True, 'stochastic_trend': True,\n",
    "#     'cycle': True, 'damped_cycle': True, 'stochastic_cycle': True\n",
    "# }\n",
    "\n",
    "# The restricted model forces a smooth trend\n",
    "restricted_model = {\n",
    "    'level': 'smooth trend', 'cycle': True, 'damped_cycle': True, 'stochastic_cycle': True\n",
    "}\n",
    "\n",
    "# Restricted model, setting components directly\n",
    "# This is an equivalent, but less convenient, way to specify a\n",
    "# smooth trend model with a stochastic damped cycle. Notice\n",
    "# that the difference from the local linear trend model is that\n",
    "# `stochastic_level=False` here.\n",
    "# unrestricted_model = {\n",
    "#     'irregular': True, 'level': True, 'stochastic_level': False, 'trend': True, 'stochastic_trend': True,\n",
    "#     'cycle': True, 'damped_cycle': True, 'stochastic_cycle': True\n",
    "# }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now fit the following models:\n",
    "\n",
    "1. Output, unrestricted model\n",
    "2. Prices, unrestricted model\n",
    "3. Prices, restricted model\n",
    "4. Money, unrestricted model\n",
    "5. Money, restricted model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Output\n",
    "output_mod = sm.tsa.UnobservedComponents(dta['US GNP'], **unrestricted_model)\n",
    "output_res = output_mod.fit(method='powell', disp=False)\n",
    "\n",
    "# Prices\n",
    "prices_mod = sm.tsa.UnobservedComponents(dta['US Prices'], **unrestricted_model)\n",
    "prices_res = prices_mod.fit(method='powell', disp=False)\n",
    "\n",
    "prices_restricted_mod = sm.tsa.UnobservedComponents(dta['US Prices'], **restricted_model)\n",
    "prices_restricted_res = prices_restricted_mod.fit(method='powell', disp=False)\n",
    "\n",
    "# Money\n",
    "money_mod = sm.tsa.UnobservedComponents(dta['US monetary base'], **unrestricted_model)\n",
    "money_res = money_mod.fit(method='powell', disp=False)\n",
    "\n",
    "money_restricted_mod = sm.tsa.UnobservedComponents(dta['US monetary base'], **restricted_model)\n",
    "money_restricted_res = money_restricted_mod.fit(method='powell', disp=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once we have fit these models, there are a variety of ways to display the information. Looking at the model of US GNP, we can summarize the fit of the model using the `summary` method on the fit object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(output_res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For unobserved components models, and in particular when exploring stylized facts in line with point (2) from the introduction, it is often more instructive to plot the estimated unobserved components (e.g. the level, trend, and cycle) themselves to see if they provide a meaningful description of the data.\n",
    "\n",
    "The `plot_components` method of the fit object can be used to show plots and confidence intervals of each of the estimated states, as well as a plot of the observed data versus the one-step-ahead predictions of the model to assess fit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "fig = output_res.plot_components(legend_loc='lower right', figsize=(15, 9));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, Harvey and Jaeger summarize the models in another way to highlight the relative importances of the trend and cyclical components; below we replicate their Table I. The values we find are broadly consistent with, but different in the particulars from, the values from their table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Create Table I\n",
    "table_i = np.zeros((5,6))\n",
    "\n",
    "start = dta.index[0]\n",
    "end = dta.index[-1]\n",
    "time_range = '%d:%d-%d:%d' % (start.year, start.quarter, end.year, end.quarter)\n",
    "models = [\n",
    "    ('US GNP', time_range, 'None'),\n",
    "    ('US Prices', time_range, 'None'),\n",
    "    ('US Prices', time_range, r'$\\sigma_\\eta^2 = 0$'),\n",
    "    ('US monetary base', time_range, 'None'),\n",
    "    ('US monetary base', time_range, r'$\\sigma_\\eta^2 = 0$'),\n",
    "]\n",
    "index = pd.MultiIndex.from_tuples(models, names=['Series', 'Time range', 'Restrictions'])\n",
    "parameter_symbols = [\n",
    "    r'$\\sigma_\\zeta^2$', r'$\\sigma_\\eta^2$', r'$\\sigma_\\kappa^2$', r'$\\rho$',\n",
    "    r'$2 \\pi / \\lambda_c$', r'$\\sigma_\\varepsilon^2$',\n",
    "]\n",
    "\n",
    "i = 0\n",
    "for res in (output_res, prices_res, prices_restricted_res, money_res, money_restricted_res):\n",
    "    if res.model.stochastic_level:\n",
    "        (sigma_irregular, sigma_level, sigma_trend,\n",
    "         sigma_cycle, frequency_cycle, damping_cycle) = res.params\n",
    "    else:\n",
    "        (sigma_irregular, sigma_level,\n",
    "         sigma_cycle, frequency_cycle, damping_cycle) = res.params\n",
    "        sigma_trend = np.nan\n",
    "    period_cycle = 2 * np.pi / frequency_cycle\n",
    "    \n",
    "    table_i[i, :] = [\n",
    "        sigma_level*1e7, sigma_trend*1e7,\n",
    "        sigma_cycle*1e7, damping_cycle, period_cycle,\n",
    "        sigma_irregular*1e7\n",
    "    ]\n",
    "    i += 1\n",
    "    \n",
    "pd.set_option('float_format', lambda x: '%.4g' % np.round(x, 2) if not np.isnan(x) else '-')\n",
    "table_i = pd.DataFrame(table_i, index=index, columns=parameter_symbols)\n",
    "table_i"
   ]
  }
 ],
 "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.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
