{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Markov switching dynamic regression models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook provides an example of the use of Markov switching models in statsmodels to estimate dynamic regression models with changes in regime. It follows the examples in the Stata Markov switching documentation, which can be found at http://www.stata.com/manuals14/tsmswitch.pdf."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "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",
    "# NBER recessions\n",
    "from pandas_datareader.data import DataReader\n",
    "from datetime import datetime\n",
    "\n",
    "usrec = DataReader(\n",
    "    \"USREC\", \"fred\", start=datetime(1947, 1, 1), end=datetime(2013, 4, 1)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Federal funds rate with switching intercept\n",
    "\n",
    "The first example models the federal funds rate as noise around a constant intercept, but where the intercept changes during different regimes. The model is simply:\n",
    "\n",
    "$$r_t = \\mu_{S_t} + \\varepsilon_t \\qquad \\varepsilon_t \\sim N(0, \\sigma^2)$$\n",
    "\n",
    "where $S_t \\in \\{0, 1\\}$, and the regime transitions according to\n",
    "\n",
    "$$ P(S_t = s_t | S_{t-1} = s_{t-1}) =\n",
    "\\begin{bmatrix}\n",
    "p_{00} & p_{10} \\\\\n",
    "1 - p_{00} & 1 - p_{10}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "We will estimate the parameters of this model by maximum likelihood: $p_{00}, p_{10}, \\mu_0, \\mu_1, \\sigma^2$.\n",
    "\n",
    "The data used in this example can be found at https://www.stata-press.com/data/r14/usmacro."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Get the federal funds rate data\n",
    "from statsmodels.tsa.regime_switching.tests.test_markov_regression import fedfunds\n",
    "\n",
    "dta_fedfunds = pd.Series(\n",
    "    fedfunds, index=pd.date_range(\"1954-07-01\", \"2010-10-01\", freq=\"QS\")\n",
    ")\n",
    "\n",
    "# Plot the data\n",
    "dta_fedfunds.plot(title=\"Federal funds rate\", figsize=(12, 3))\n",
    "\n",
    "# Fit the model\n",
    "# (a switching mean is the default of the MarkovRegession model)\n",
    "mod_fedfunds = sm.tsa.MarkovRegression(dta_fedfunds, k_regimes=2)\n",
    "res_fedfunds = mod_fedfunds.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the summary output, the mean federal funds rate in the first regime (the \"low regime\") is estimated to be $3.7$ whereas in the \"high regime\" it is $9.6$. Below we plot the smoothed probabilities of being in the high regime. The model suggests that the 1980's was a time-period in which a high federal funds rate existed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds.smoothed_marginal_probabilities[1].plot(\n",
    "    title=\"Probability of being in the high regime\", figsize=(12, 3)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the estimated transition matrix we can calculate the expected duration of a low regime versus a high regime."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(res_fedfunds.expected_durations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A low regime is expected to persist for about fourteen years, whereas the high regime is expected to persist for only about five years."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Federal funds rate with switching intercept and lagged dependent variable\n",
    "\n",
    "The second example augments the previous model to include the lagged value of the federal funds rate.\n",
    "\n",
    "$$r_t = \\mu_{S_t} + r_{t-1} \\beta_{S_t} + \\varepsilon_t \\qquad \\varepsilon_t \\sim N(0, \\sigma^2)$$\n",
    "\n",
    "where $S_t \\in \\{0, 1\\}$, and the regime transitions according to\n",
    "\n",
    "$$ P(S_t = s_t | S_{t-1} = s_{t-1}) =\n",
    "\\begin{bmatrix}\n",
    "p_{00} & p_{10} \\\\\n",
    "1 - p_{00} & 1 - p_{10}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "We will estimate the parameters of this model by maximum likelihood: $p_{00}, p_{10}, \\mu_0, \\mu_1, \\beta_0, \\beta_1, \\sigma^2$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fit the model\n",
    "mod_fedfunds2 = sm.tsa.MarkovRegression(\n",
    "    dta_fedfunds.iloc[1:], k_regimes=2, exog=dta_fedfunds.iloc[:-1]\n",
    ")\n",
    "res_fedfunds2 = mod_fedfunds2.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds2.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are several things to notice from the summary output:\n",
    "\n",
    "1. The information criteria have decreased substantially, indicating that this model has a better fit than the previous model.\n",
    "2. The interpretation of the regimes, in terms of the intercept, have switched. Now the first regime has the higher intercept and the second regime has a lower intercept.\n",
    "\n",
    "Examining the smoothed probabilities of the high regime state, we now see quite a bit more variability."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds2.smoothed_marginal_probabilities[0].plot(\n",
    "    title=\"Probability of being in the high regime\", figsize=(12, 3)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the expected durations of each regime have decreased quite a bit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(res_fedfunds2.expected_durations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Taylor rule with 2 or 3 regimes\n",
    "\n",
    "We now include two additional exogenous variables - a measure of the output gap and a measure of inflation - to estimate a switching Taylor-type rule with both 2 and 3 regimes to see which fits the data better.\n",
    "\n",
    "Because the models can be often difficult to estimate, for the 3-regime model we employ a search over starting parameters to improve results, specifying 20 random search repetitions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Get the additional data\n",
    "from statsmodels.tsa.regime_switching.tests.test_markov_regression import ogap, inf\n",
    "\n",
    "dta_ogap = pd.Series(ogap, index=pd.date_range(\"1954-07-01\", \"2010-10-01\", freq=\"QS\"))\n",
    "dta_inf = pd.Series(inf, index=pd.date_range(\"1954-07-01\", \"2010-10-01\", freq=\"QS\"))\n",
    "\n",
    "exog = pd.concat((dta_fedfunds.shift(), dta_ogap, dta_inf), axis=1).iloc[4:]\n",
    "\n",
    "# Fit the 2-regime model\n",
    "mod_fedfunds3 = sm.tsa.MarkovRegression(dta_fedfunds.iloc[4:], k_regimes=2, exog=exog)\n",
    "res_fedfunds3 = mod_fedfunds3.fit()\n",
    "\n",
    "# Fit the 3-regime model\n",
    "np.random.seed(12345)\n",
    "mod_fedfunds4 = sm.tsa.MarkovRegression(dta_fedfunds.iloc[4:], k_regimes=3, exog=exog)\n",
    "res_fedfunds4 = mod_fedfunds4.fit(search_reps=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds3.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_fedfunds4.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Due to lower information criteria, we might prefer the 3-state model, with an interpretation of low-, medium-, and high-interest rate regimes. The smoothed probabilities of each regime are plotted below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(3, figsize=(10, 7))\n",
    "\n",
    "ax = axes[0]\n",
    "ax.plot(res_fedfunds4.smoothed_marginal_probabilities[0])\n",
    "ax.set(title=\"Smoothed probability of a low-interest rate regime\")\n",
    "\n",
    "ax = axes[1]\n",
    "ax.plot(res_fedfunds4.smoothed_marginal_probabilities[1])\n",
    "ax.set(title=\"Smoothed probability of a medium-interest rate regime\")\n",
    "\n",
    "ax = axes[2]\n",
    "ax.plot(res_fedfunds4.smoothed_marginal_probabilities[2])\n",
    "ax.set(title=\"Smoothed probability of a high-interest rate regime\")\n",
    "\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switching variances\n",
    "\n",
    "We can also accommodate switching variances. In particular, we consider the model\n",
    "\n",
    "$$\n",
    "y_t = \\mu_{S_t} + y_{t-1} \\beta_{S_t} + \\varepsilon_t \\quad \\varepsilon_t \\sim N(0, \\sigma_{S_t}^2)\n",
    "$$\n",
    "\n",
    "We use maximum likelihood to estimate the parameters of this model: $p_{00}, p_{10}, \\mu_0, \\mu_1, \\beta_0, \\beta_1, \\sigma_0^2, \\sigma_1^2$.\n",
    "\n",
    "The application is to absolute returns on stocks, where the data can be found at https://www.stata-press.com/data/r14/snp500."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Get the federal funds rate data\n",
    "from statsmodels.tsa.regime_switching.tests.test_markov_regression import areturns\n",
    "\n",
    "dta_areturns = pd.Series(\n",
    "    areturns, index=pd.date_range(\"2004-05-04\", \"2014-5-03\", freq=\"W\")\n",
    ")\n",
    "\n",
    "# Plot the data\n",
    "dta_areturns.plot(title=\"Absolute returns, S&P500\", figsize=(12, 3))\n",
    "\n",
    "# Fit the model\n",
    "mod_areturns = sm.tsa.MarkovRegression(\n",
    "    dta_areturns.iloc[1:],\n",
    "    k_regimes=2,\n",
    "    exog=dta_areturns.iloc[:-1],\n",
    "    switching_variance=True,\n",
    ")\n",
    "res_areturns = mod_areturns.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_areturns.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first regime is a low-variance regime and the second regime is a high-variance regime. Below we plot the probabilities of being in the low-variance regime. Between 2008 and 2012 there does not appear to be a clear indication of one regime guiding the economy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "res_areturns.smoothed_marginal_probabilities[0].plot(\n",
    "    title=\"Probability of being in a low-variance regime\", figsize=(12, 3)\n",
    ")"
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
