{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# AR, MA, ARMA, ARIMA, and Forecasting\n",
        "\n**Opening question:** How can past values and past shocks be organized into a model that produces honest forecasts and uncertainty?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from statsmodels.tsa.arima_process import ArmaProcess\n",
        "from statsmodels.tsa.arima.model import ARIMA\n",
        "\n",
        "rng = np.random.default_rng(19)\n",
        "process = ArmaProcess(ar=[1, -0.65], ma=[1, 0.35])\n",
        "x = process.generate_sample(500, distrvs=rng.standard_normal)\n",
        "fit = ARIMA(x, order=(1, 0, 1), trend=\"c\").fit()\n",
        "print(fit.params.round(3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The estimates recover the simulated AR and MA structure approximately. Sampling variation and likelihood conventions prevent exact equality.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "forecast = fit.get_forecast(steps=5)\n",
        "mean = forecast.predicted_mean\n",
        "interval = forecast.conf_int(alpha=0.05)\n",
        "print(np.round(mean, 3))\n",
        "print(np.round(interval[0], 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The forecast returns toward the estimated mean while uncertainty expands with horizon.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "from statsmodels.tsa.arima_process import ArmaProcess\n",
        "from statsmodels.tsa.arima.model import ARIMA\n",
        "rng = np.random.default_rng(19)\n",
        "process = ArmaProcess(ar=[1, -0.65], ma=[1, 0.35])\n",
        "x = process.generate_sample(500, distrvs=rng.standard_normal)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n[-0.055 0.619 0.394 0.932]\n```\n\n```text\n[-0.603 -0.394 -0.265 -0.185 -0.136] [-2.495 1.29 ]\n```\n\n```text\n[-0.055  0.619  0.394  0.932]\n```\n\n```text\n[-0.603 -0.394 -0.265 -0.185 -0.136]\n[-2.495  1.29 ]\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": 38,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
