{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Dependence, Stationarity, and White Noise\n",
        "\n**Opening question:** How can we tell whether a time series contains predictable linear structure rather than random fluctuation?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from statsmodels.stats.diagnostic import acorr_ljungbox\n",
        "from statsmodels.tsa.stattools import acf\n",
        "\n",
        "rng = np.random.default_rng(18)\n",
        "x = rng.normal(size=500)\n",
        "print(np.round(acf(x, nlags=3, fft=True), 3))\n",
        "print(round(acorr_ljungbox(x, lags=[10], return_df=True)[\"lb_pvalue\"].iloc[0], 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The simulated white-noise series has small sample autocorrelations and does not reject joint independence at lag 10 in this run.\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(1818)\n",
        "level = np.cumsum(rng.normal(size=600))\n",
        "difference = np.diff(level)\n",
        "print(round(adfuller(level, regression=\"c\")[1], 4))\n",
        "print(f\"{adfuller(difference, regression='c')[1]:.2e}\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The random-walk level is consistent with a unit root, while its first difference is strongly stationary in the ADF diagnostic.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from statsmodels.stats.diagnostic import acorr_ljungbox\n",
        "from statsmodels.tsa.stattools import acf\n",
        "rng = np.random.default_rng(18)\n",
        "x = rng.normal(size=500)\n",
        "print(np.round(acf(x, nlags=3, fft=True), 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[1. 0.032 0.046 0.033] 0.844\n```\n\n```text\n0.8381 0.00e+00\n```\n\n```text\n[1.    0.032 0.046 0.033]\n0.844\n```\n\n```text\n0.8381\n0.00e+00\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": 37,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
