{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Multiple Time Series\n",
        "\n**Opening question:** When several series move together, which relationships are short-run predictive and which are long-run equilibrating?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from statsmodels.tsa.api import VAR\n",
        "\n",
        "rng = np.random.default_rng(26)\n",
        "y = np.zeros((400, 2))\n",
        "for t in range(1, 400):\n",
        "    y[t] = [0.55*y[t-1,0] + 0.20*y[t-1,1], -0.10*y[t-1,0] + 0.45*y[t-1,1]] + rng.normal(scale=0.6, size=2)\n",
        "df = pd.DataFrame(y, columns=[\"x\", \"z\"])\n",
        "fit = VAR(df).fit(1)\n",
        "print(fit.coefs[0].round(2))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The estimated lag matrix is close to the simulated system, with sampling variation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from statsmodels.tsa.stattools import adfuller\n",
        "\n",
        "rng = np.random.default_rng(2601)\n",
        "common = np.cumsum(rng.normal(size=600))\n",
        "x = common + rng.normal(scale=0.5, size=600)\n",
        "y = 1.2*common + rng.normal(scale=0.5, size=600)\n",
        "spread = y - 1.2*x\n",
        "print(f\"{adfuller(spread)[1]:.2e}\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The constructed spread is stationary even though the two levels inherit a shared stochastic trend.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from statsmodels.tsa.api import VAR\n",
        "rng = np.random.default_rng(26)\n",
        "y = np.zeros((400, 2))\n",
        "df = pd.DataFrame(y, columns=[\"x\", \"z\"])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[[ 0.55 0.15] [-0.13 0.47]]\n```\n\n```text\n0.00e+00\n```\n\n```text\n[[ 0.55  0.15]\n [-0.13  0.47]]\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 45,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
