{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Unit Roots, Seasonality, and Dynamic Regression\n",
        "\n**Opening question:** How can a model distinguish persistent trend, recurring seasonality, and serially correlated errors?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "\n",
        "series = pd.Series(range(24), index=pd.date_range(\"2024-01-01\", periods=24, freq=\"MS\"))\n",
        "seasonal_difference = series.diff(12)\n",
        "print(seasonal_difference.dropna().unique().tolist())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. Each month is exactly 12 units above the same month one year earlier in this deterministic illustration.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from statsmodels.tsa.statespace.sarimax import SARIMAX\n",
        "\n",
        "rng = np.random.default_rng(39)\n",
        "dates = pd.date_range(\"2018-01-01\", periods=72, freq=\"MS\")\n",
        "seasonal = pd.Series(\n",
        "    50 + 0.2 * np.arange(72) + 4 * np.sin(2 * np.pi * np.arange(72) / 12) + rng.normal(0, 0.8, 72),\n",
        "    index=dates,\n",
        ")\n",
        "fit = SARIMAX(seasonal, order=(1, 1, 1), seasonal_order=(0, 1, 1, 12), trend=\"n\", enforce_stationarity=False).fit(disp=False)\n",
        "print(round(fit.aic, 2))\n",
        "print(fit.get_forecast(3).predicted_mean.round(2).tolist())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The exact values depend on the included processed series and estimation conventions. Residual diagnostics remain necessary.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[12.0]\n```\n\n```text\n313.71 [61.79, 63.49, 64.73]\n```\n\n```text\n313.71\n[61.79, 63.49, 64.73]\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": 39,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
