{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Regression and Econometrics with Python\n",
        "\n**Opening question:** What does a regression coefficient mean, and which assumptions are needed before it can support an economic claim?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import statsmodels.formula.api as smf\n",
        "\n",
        "rng = np.random.default_rng(15)\n",
        "x = np.linspace(0, 10, 120)\n",
        "y = 1.5 + 0.8*x + rng.normal(0, 0.4 + 0.08*x)\n",
        "df = pd.DataFrame({\"y\": y, \"x\": x})\n",
        "model = smf.ols(\"y ~ x\", data=df).fit(cov_type=\"HC3\")\n",
        "print(round(model.params[\"x\"], 3), round(model.bse[\"x\"], 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The slope is close to the data-generating value. HC3 standard errors account for heteroskedasticity in a large-sample approximation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "df[\"group\"] = (df[\"x\"] > 5).astype(int)\n",
        "interaction = smf.ols(\"y ~ x * group\", data=df).fit(cov_type=\"HC3\")\n",
        "print(interaction.params.round(3).to_dict())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The interaction allows the slope to differ after x exceeds five. Here the estimated difference is small because the simulated process has one common slope.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import statsmodels.formula.api as smf\n",
        "rng = np.random.default_rng(15)\n",
        "x = np.linspace(0, 10, 120)\n",
        "y = 1.5 + 0.8*x + rng.normal(0, 0.4 + 0.08*x)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n0.848 0.028\n```\n\n```text\n{'Intercept': 1.503, 'x': 0.809, 'group': -0.565, 'x:group': 0.099}\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": 15,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
