{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Chapter 23: Instrumental Variables and Two-Stage Least Squares\n",
        "**Economic question:** Can we isolate useful variation in an endogenous explanatory variable?\n",
        "\nAn instrument is useful only if it moves the endogenous variable and affects the outcome through the permitted channel.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np, statsmodels.api as sm\n",
        "rng=np.random.default_rng(23)\n",
        "z=rng.normal(size=500); u=rng.normal(size=500); x=.9*z+u\n",
        "e=.7*u+rng.normal(size=500); y=1+2*x+e\n",
        "first=sm.OLS(x,sm.add_constant(z)).fit(); xhat=first.fittedvalues\n",
        "second=sm.OLS(y,sm.add_constant(xhat)).fit()\n",
        "print('first-stage slope',first.params[1]); print('educational 2SLS slope',second.params[1])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Interpretation checklist\n",
        "- State the unit of observation and units of every variable.\n",
        "- Separate association, prediction, and causation.\n",
        "- Report magnitude and uncertainty.\n",
        "- Identify the most important threat to validity.\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.13"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 23,
      "source_origin": "supplied"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
