{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8732de12-d3f2-4a09-8c39-e5c52a5ac94a",
   "metadata": {},
   "source": [
    "# Autoregressive Distributed Lag (ARDL) models\n",
    "\n",
    "\n",
    "## ARDL Models\n",
    "\n",
    "Autoregressive Distributed Lag (ARDL) models extend Autoregressive models with lags of explanatory variables. While ARDL models are technically AR-X models, the key difference is that ARDL models focus on the exogenous variables and selecting the correct lag structure from both the endogenous variable and the exogenous variables.  ARDL models are also closely related to Vector Autoregressions, and a single ARDL is effectively one row of a VAR.  The key distinction is that an ARDL assumes that the exogenous variables are exogenous in the sense that it is not necessary to include the endogenous variable as a predictor of the exogenous variables.\n",
    "\n",
    "The full specification of ARDL models is\n",
    "\n",
    "$$\n",
    "Y_t = \\underset{\\text{Constant and Trend}}{\\underbrace{\\delta_0 + \\delta_1 t + \\ldots + \\delta_k t^k}} \n",
    "      + \\underset{\\text{Seasonal}}{\\underbrace{\\sum_{i=0}^{s-1} \\gamma_i S_i}}\n",
    "      + \\underset{\\text{Autoregressive}}{\\underbrace{\\sum_{p=1}^P \\phi_p Y_{t-p}}}\n",
    "      + \\underset{\\text{Distributed Lag}}{\\underbrace{\\sum_{k=1}^M \\sum_{j=0}^{Q_k} \\beta_{k,j} X_{k, t-j}}}\n",
    "      + \\underset{\\text{Fixed}}{\\underbrace{Z_t \\Gamma}} + \\epsilon_t\n",
    "$$\n",
    "\n",
    "The terms in the model are:\n",
    "\n",
    "* $\\delta_i$: constant and deterministic time regressors.  Set using `trend`.\n",
    "* $S_i$ are seasonal dummies which are included if `seasonal=True`.\n",
    "* $X_{k,t-j}$ are the exogenous regressors. There are a number of formats that can be used to specify which lags are included. Note that the included lag lengths do no need to be the same. If `causal=True`, then the lags start with lag 1. Otherwise lags begin with 0 so that the model included the contemporaneous relationship between $Y_t$ and $X_t$.\n",
    "* $Z_t$ are any other fixed regressors that are not part of the distributed lag specification. In practice these regressors may be included when they do no contribute to the long run-relationship between $Y_t$ and the vector of exogenous variables $X_t$.\n",
    "* $\\{\\epsilon_t\\}$ is assumed to be a White Noise process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7bb53a8-63a9-4f11-a9db-eb09280d457d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "sns.set_style(\"darkgrid\")\n",
    "sns.mpl.rc(\"figure\", figsize=(16, 6))\n",
    "sns.mpl.rc(\"font\", size=14)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2df3123-3ef9-4bc5-bc3f-c2d3f2fe945d",
   "metadata": {},
   "source": [
    "### Data\n",
    "\n",
    "This notebook makes use of money demand data from Denmark, as first used in  S. Johansen and K. Juselius (1990).  The key variables are:\n",
    "\n",
    "* `lrm`: Log of real money measured using M2\n",
    "* `lry`: Log of real income\n",
    "* `ibo`: Interest rate on bonds\n",
    "* `ide`: Interest rate of bank deposits\n",
    "\n",
    "The standard model uses `lrm` as the dependent variable and the other three as exogenous drivers.\n",
    "\n",
    "Johansen, S. and Juselius, K. (1990), Maximum Likelihood Estimation and Inference on Cointegration – with Applications to the Demand for Money, Oxford Bulletin of Economics and Statistics, 52, 2, 169–210.\n",
    "\n",
    "We start by loading the data and examining it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e835b518-53f6-45de-9c93-d397eaa08831",
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.datasets.danish_data import load\n",
    "from statsmodels.tsa.api import ARDL\n",
    "from statsmodels.tsa.ardl import ardl_select_order\n",
    "\n",
    "data = load().data\n",
    "data = data[[\"lrm\", \"lry\", \"ibo\", \"ide\"]]\n",
    "data.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32139ec2-4e69-4e53-a5b9-a6aef64edefe",
   "metadata": {},
   "source": [
    "We plot the demeaned data so that all series appear on the same scale. The `lrm` series appears to be non-stationary, as does `lry`. The stationarity of the other two is less obvious."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ca52a18-3752-4c65-9043-6c91ba543d44",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = (data - data.mean()).plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176dacd0-70c0-456a-969f-3c4ae980948f",
   "metadata": {},
   "source": [
    "### Model Selection\n",
    "\n",
    "`ardl_select_order` can be used to automatically select the order. Here we use min the minimum AIC among all modes that consider up to 3 lags of the endogenous variable and 3 lags of each exogenous variable. `trend=\"c\"` indicates that a constant should be included in the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2da134f2-5cf5-484c-833e-b70ddef76d4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res = ardl_select_order(\n",
    "    data.lrm, 3, data[[\"lry\", \"ibo\", \"ide\"]], 3, ic=\"aic\", trend=\"c\"\n",
    ")\n",
    "print(f\"The optimal order is: {sel_res.model.ardl_order}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ecf7091-5de4-4bbb-8952-326ca32b99b6",
   "metadata": {},
   "source": [
    "The optimal order is returned as the number of lags of the endogenous variable followed by each of the exogenous regressors.  The attribute `model` on `sel_res` contains the model `ARDL` specification which can be used to call `fit`. Here we look at the summary where the `L#` indicates that lag length (e.g., `L0` is no lag, i.e., $X_{k,t}$, `L2` is 2 lags, i.e., $X_{k,t-2}$)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5416d92-6b08-4b09-ac70-bccb4711dd7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = sel_res.model.fit()\n",
    "res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65f52544-1804-4266-80ea-10556b315003",
   "metadata": {},
   "source": [
    "### Global searches\n",
    "\n",
    "The selection criteria can be switched the BIC which chooses a smaller model. Here we also use the `glob=True` option to perform a global search which considers models with any subset of lags up to the maximum lag allowed (3 here).  This option lets the model selection choose non-contiguous lag specifications."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb24d882-facd-4e61-ad72-0a2ac5e362c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res = ardl_select_order(\n",
    "    data.lrm, 3, data[[\"lry\", \"ibo\", \"ide\"]], 3, ic=\"bic\", trend=\"c\", glob=True\n",
    ")\n",
    "sel_res.model.ardl_order"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39c7de3e-8370-4e9d-adbd-fa9ffcddaa8c",
   "metadata": {},
   "source": [
    "While the `ardl_order` shows the largest included lag of each variable, `ar_lags` and `dl_lags` show the specific lags included.  The AR component is regular in the sense that all 3 lags are included.  The DL component is not since `ibo` selects only lags 0 and 3 and ide selects only lags 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51c98acb-9409-4305-85a0-c62bda98807b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res.model.ar_lags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "546ec94c-6327-4d98-ad8e-a3d62961394b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res.model.dl_lags"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da5b5fcf-f2d0-41bb-8f08-5d3daff450ca",
   "metadata": {},
   "source": [
    "We can take a look at the best performing models according to the BIC which are stored in the `bic` property. `ibo` at lags 0 and 3 is consistently selected, as is `ide` at either lag 2 or 3, and `lry` at lag 0. The selected AR lags vary more, although all of the best specifications select some. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3b6f0bf-8408-466b-99c9-0bce16213fa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, val in enumerate(sel_res.bic.head(10)):\n",
    "    print(f\"{i+1}: {val}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7b4151f-4308-4b97-a7b0-1a4a4e231d9f",
   "metadata": {},
   "source": [
    "### Direct Parameterization\n",
    "\n",
    "ARDL models can be directly specified using the `ARDL` class.  The first argument is the endogenous variable ($Y_t$). The second is the AR lags. It can be a constant, in which case lags 1, 2, ..., $P$ are included, or a list of specific lags indices to include (e.g., `[1, 4]`).  The third are the exogenous variables, and the fourth is the list of lags to include.  This can be one of\n",
    "\n",
    "* An `int`: Include lags 0, 1, ..., Q\n",
    "* A dict with column names when `exog` is a `DataFrame` or numeric column locations when `exog` is a NumPy array (e.g., `{0:1, 1: 2, 2:3}`, would match the specification below if a NumPy array was used.\n",
    "* A dict with column names (DataFrames) or integers (NumPy arrays) that contains a list of specific lags to include (e.g., `{\"lry\":[0,2], \"ibo\":[1,2]}`).\n",
    "\n",
    "The specification below matches that model selected by `ardl_select_order`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edc6cca1-acda-4ff0-886a-fcb69866e077",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = ARDL(\n",
    "    data.lrm, 2, data[[\"lry\", \"ibo\", \"ide\"]], {\"lry\": 1, \"ibo\": 2, \"ide\": 3}, trend=\"c\"\n",
    ").fit()\n",
    "res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c879d6b-da6e-4fd7-b6fd-28d1f334d397",
   "metadata": {},
   "source": [
    "### NumPy Data\n",
    "\n",
    "Below we see how the specification of ARDL models differs when using NumPy arrays.  The key difference is that the keys in the dictionary are now integers which indicate the column of `x` to use. This model is identical to the previously fit model and all key value match exactly (e.g., Log Likelihood)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fcbead5-eef8-431a-83cc-6333dc1257e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.asarray(data.lrm)\n",
    "x = np.asarray(data[[\"lry\", \"ibo\", \"ide\"]])\n",
    "res = ARDL(y, 2, x, {0: 1, 1: 2, 2: 3}, trend=\"c\").fit()\n",
    "res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9c00153-e811-4482-bfdc-b319a440e27b",
   "metadata": {},
   "source": [
    "### Causal models\n",
    "\n",
    "Using the `causal=True` flag eliminates lag 0 from the DL components, so that all variables included in the model are known at time $t-1$ when modeling $Y_t$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e29bae15-3ad8-4614-a0e7-5e4ed9022d96",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = ARDL(\n",
    "    data.lrm,\n",
    "    2,\n",
    "    data[[\"lry\", \"ibo\", \"ide\"]],\n",
    "    {\"lry\": 1, \"ibo\": 2, \"ide\": 3},\n",
    "    trend=\"c\",\n",
    "    causal=True,\n",
    ").fit()\n",
    "res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ac0a3be-996b-4ade-8d96-13629e661600",
   "metadata": {},
   "source": [
    "## Unconstrained Error Correction Models (UECM)\n",
    "\n",
    "Unconstrained Error Correction Models reparameterize ARDL model to focus on the long-run component of a time series.  The reparameterized model is\n",
    "\n",
    "$$\n",
    "\\Delta Y_t = \\underset{\\text{Constant and Trend}}{\\underbrace{\\delta_0 + \\delta_1 t + \\ldots + \\delta_k t^k}} \n",
    "      + \\underset{\\text{Seasonal}}{\\underbrace{\\sum_{i=0}^{s-1} \\gamma_i S_i}}\n",
    "      + \\underset{\\text{Long-Run}}{\\underbrace{\\lambda_0 Y_{t-1} + \\sum_{b=1}^M \\lambda_i  X_{b,t-1}}}\n",
    "      + \\underset{\\text{Autoregressive}}{\\underbrace{\\sum_{p=1}^P \\phi_p \\Delta Y_{t-p}}}\n",
    "      + \\underset{\\text{Distributed Lag}}{\\underbrace{\\sum_{k=1}^M \\sum_{j=0}^{Q_k} \\beta_{k,j} \\Delta X_{k, t-j}}}\n",
    "      + \\underset{\\text{Fixed}}{\\underbrace{Z_t \\Gamma}} + \\epsilon_t\n",
    "$$\n",
    "\n",
    "\n",
    "Most of the components are the same.  The key differences are:\n",
    "\n",
    "* The levels only enter at lag 1\n",
    "* All other lags of $Y_t$ or $X_{k,t}$ are differenced\n",
    "\n",
    "Due to their structure, UECM models _do not_ support irregular lag specifications, and so lags specifications must be integers. The AR lag length must be an integer or `None`, while the DL lag specification can be an integer or a dictionary of integers.  Other options such as `trend`, `seasonal`, and `causal` are identical.\n",
    "\n",
    "Below we select a model and then using the class method `from_ardl` to construct the UECM.  The parameter estimates prefixed with `D.` are differences."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9645823c-8d25-487e-a34a-5350f0c11912",
   "metadata": {},
   "outputs": [],
   "source": [
    "from statsmodels.tsa.api import UECM\n",
    "\n",
    "sel_res = ardl_select_order(\n",
    "    data.lrm, 3, data[[\"lry\", \"ibo\", \"ide\"]], 3, ic=\"aic\", trend=\"c\"\n",
    ")\n",
    "\n",
    "ecm = UECM.from_ardl(sel_res.model)\n",
    "ecm_res = ecm.fit()\n",
    "ecm_res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ecf6cab-9477-4492-b83a-40fe92665697",
   "metadata": {},
   "source": [
    "### Cointegrating Relationships\n",
    "\n",
    "Because the focus is on the long-run relationship, the results of UECM model fits contains a number of properties that focus on the long-run relationship. These are all prefixed `ci_`, for cointegrating.  `ci_summary` contains the normalized estimates of the cointegrating relationship and associated estimated values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fed4cc4e-100c-4b6d-bea4-6ed180bda5df",
   "metadata": {},
   "outputs": [],
   "source": [
    "ecm_res.ci_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d730dc12-b3c9-427e-89da-2fcabb5deea7",
   "metadata": {},
   "source": [
    "`ci_resids` contains the long-run residual, which is the error the drives figure changes in $\\Delta Y_t$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8e12dc7-f12e-442d-b0cd-d081e8f0447b",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = ecm_res.ci_resids.plot(title=\"Cointegrating Error\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b4cd8e8-97d7-49d6-8c49-ebdd2b08f04f",
   "metadata": {},
   "source": [
    "### Seasonal Dummies\n",
    "\n",
    "Here we add seasonal terms, which appear to be statistically significant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3618d391-0136-4d59-9ee8-33eef938f2bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "ecm = UECM(data.lrm, 2, data[[\"lry\", \"ibo\", \"ide\"]], 2, seasonal=True)\n",
    "seasonal_ecm_res = ecm.fit()\n",
    "seasonal_ecm_res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e31719ac-78c4-4c8f-8fe9-81a77454ae82",
   "metadata": {},
   "source": [
    "All deterministic terms are included in the `ci_` prefixed terms.  Here we see the normalized seasonal effects in the summary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0ac53f2-990a-4379-bf1a-e031c0482150",
   "metadata": {},
   "outputs": [],
   "source": [
    "seasonal_ecm_res.ci_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09de88a7-ba21-4126-8b63-1f8d201f1412",
   "metadata": {},
   "source": [
    "The residuals are somewhat more random in appearance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb22aa49-5c7b-4e4e-8382-4b2df05f9980",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = seasonal_ecm_res.ci_resids.plot(title=\"Cointegrating Error with Seasonality\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a394c3ac-c4de-4e13-8286-c578afa22095",
   "metadata": {},
   "source": [
    "## The relationship between Consumption and Growth\n",
    "\n",
    "Here we look at an example from Greene's _Econometric analysis_ which focuses on teh long-run relationship between consumption and growth. We start by downloading the raw data.\n",
    "\n",
    "Greene, W. H. (2000). Econometric analysis 4th edition. International edition, New Jersey: Prentice Hall, 201-215."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db8d65eb-2ea5-4ced-bd72-45159dd72787",
   "metadata": {},
   "outputs": [],
   "source": [
    "greene = pd.read_fwf(\"http://www.stern.nyu.edu/~wgreene/Text/Edition7/TableF5-2.txt\")\n",
    "greene.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c3b0d83-e9b2-4f60-86b3-d7672fd73fe9",
   "metadata": {},
   "source": [
    "We then transform the index to be a pandas `DatetimeIndex` so that we can easily use seasonal terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96110bd7-6445-45c6-838b-cb091c526a9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.to_datetime(\n",
    "    greene.Year.astype(\"int\").astype(\"str\")\n",
    "    + \"Q\"\n",
    "    + greene.qtr.astype(\"int\").astype(\"str\")\n",
    ")\n",
    "greene.index = index\n",
    "greene.index.freq = greene.index.inferred_freq\n",
    "greene.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b7f7b30-72a5-4263-92f4-4365f9d0cbfb",
   "metadata": {},
   "source": [
    "We defined `g` as the log of real gdp and `c` as the log of real consumption."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78428267-7756-434f-899a-f40ae180e7e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "greene[\"c\"] = np.log(greene.realcons)\n",
    "greene[\"g\"] = np.log(greene.realgdp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9def354e-f815-40e2-a1b0-f1e3f0f89d0d",
   "metadata": {},
   "source": [
    "### Lag Length Selection\n",
    "\n",
    "The selected model contains 5 lags of consumption and 2 of growth (0 and 1). Here we include seasonal terms although these are not significant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5160337-96e1-429c-8cee-7dd096e1ee04",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res = ardl_select_order(\n",
    "    greene.c, 8, greene[[\"g\"]], 8, trend=\"c\", seasonal=True, ic=\"aic\"\n",
    ")\n",
    "ardl = sel_res.model\n",
    "ardl.ardl_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deba851b-1617-4ab2-acc5-0909d1835edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = ardl.fit(use_t=True)\n",
    "res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bc10eb0-4a92-4885-a774-91d3192b1200",
   "metadata": {},
   "source": [
    "`from_ardl` is a simple way to get the equivalent UECM specification. Here we rerun the selection without the seasonal terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4f97468-beaa-47d0-9078-e7eee3d3da64",
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_res = ardl_select_order(greene.c, 8, greene[[\"g\"]], 8, trend=\"c\", ic=\"aic\")\n",
    "\n",
    "uecm = UECM.from_ardl(sel_res.model)\n",
    "uecm_res = uecm.fit()\n",
    "uecm_res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71443176-fca6-4105-80c8-eb18261f9724",
   "metadata": {},
   "source": [
    "We see that for every % increase in consumption, we need a 1.05% increase in gdp. In other words, the saving rate is estimated to be around 5%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43a66215-0416-4c6e-8c9f-423d87944a47",
   "metadata": {},
   "outputs": [],
   "source": [
    "uecm_res.ci_summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e97dfd1-0c68-42db-b5f9-5038dd5719d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = uecm_res.ci_resids.plot(title=\"Cointegrating Error\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e8eb317-74fc-4028-8cce-c17a311dff40",
   "metadata": {},
   "source": [
    "### Direct Specification of `UECM` models\n",
    "\n",
    "`UECM` can be used to directly specify model lag lengths."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4180f7b-65e8-4389-b68a-a7567aca759e",
   "metadata": {},
   "outputs": [],
   "source": [
    "uecm = UECM(greene.c, 2, greene[[\"g\"]], 1, trend=\"c\")\n",
    "uecm_res = uecm.fit()\n",
    "uecm_res.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9259982-b1e9-4384-bd2e-f7c41a7e1165",
   "metadata": {},
   "source": [
    "The changes in the lag structure make little difference in the estimated long-run relationship."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90124be5-398c-4261-ac16-415454d7f468",
   "metadata": {},
   "outputs": [],
   "source": [
    "uecm_res.ci_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86c7dfb3-e2e6-48d6-9ae4-c9ad8404cf84",
   "metadata": {},
   "source": [
    "## Bounds Testing\n",
    "\n",
    "`UECMResults` expose the bounds test of Pesaran, Shin, and Smith (2001).  This test facilitates testing whether there is a level relationship between a set of variables without identifying which variables are I(1). This test provides two sets of critical and p-values.  If the test statistic is below the critical value for the lower bound, then there appears to be no levels relationship irrespective of the order or integration in the $X$ variables.  If it is above the upper bound, then there appears to be a levels relationship again, irrespective of the order of integration of the $X$ variables. There are 5 cases covered in the paper that include different combinations of deterministic regressors in the model or the test.\n",
    "\n",
    "\n",
    "$$\\Delta Y_{t}=\\delta_{0} + \\delta_{1}t + Z_{t-1}\\beta + \\sum_{j=0}^{P}\\Delta X_{t-j}\\Gamma + \\epsilon_{t}$$\n",
    "\n",
    "where $Z_{t-1}$ includes both $Y_{t-1}$ and $X_{t-1}$.\n",
    "\n",
    "The cases determine which deterministic terms are included in the model and which are tested as part of the test.\n",
    "\n",
    "1. No deterministic terms\n",
    "2. Constant included in both the model and the test\n",
    "3. Constant included in the model but not in the test\n",
    "4. Constant and trend included in the model, only trend included in the test\n",
    "5. Constant and trend included in the model, neither included in the test\n",
    "\n",
    "Here we run the test on the Danish money demand data set. Here we see the test statistic is above the 95% critical value for both the lower and upper.\n",
    "\n",
    "\n",
    "Pesaran, M. H., Shin, Y., & Smith, R. J. (2001). Bounds testing approaches to the analysis of level relationships. Journal of applied econometrics, 16(3), 289-326."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a05b5719-4d4c-4dbf-888a-06b2d6f08183",
   "metadata": {},
   "outputs": [],
   "source": [
    "ecm = UECM(data.lrm, 3, data[[\"lry\", \"ibo\", \"ide\"]], 3, trend=\"c\")\n",
    "ecm_fit = ecm.fit()\n",
    "bounds_test = ecm_fit.bounds_test(case=4)\n",
    "bounds_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba3abe46-ea62-4ee8-833a-d7a8acb57197",
   "metadata": {},
   "outputs": [],
   "source": [
    "bounds_test.crit_vals"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16640819-83e7-451b-8fe9-a76cc5598204",
   "metadata": {},
   "source": [
    "Case 3 also rejects the null of no levels relationship."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "717c3deb-fac2-40a2-b719-be3f8b28d747",
   "metadata": {},
   "outputs": [],
   "source": [
    "ecm = UECM(data.lrm, 3, data[[\"lry\", \"ibo\", \"ide\"]], 3, trend=\"c\")\n",
    "ecm_fit = ecm.fit()\n",
    "bounds_test = ecm_fit.bounds_test(case=3)\n",
    "bounds_test"
   ]
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}