{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "colab": {
   "provenance": []
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day 61: Endogeneity & the Instrumental-Variables Idea\n",
    "\n",
    "*Beginner to Ninja: Math for Causal Inference*\n",
    "\n",
    "When the regressor is correlated with the error, OLS is biased whatever the sample size. Omitted variables, measurement error and simultaneity are the three classic causes. An instrument moves the regressor but touches the outcome only through it, and the ratio of two covariances recovers the causal effect.\n",
    "\n",
    "**Learning outcomes**\n",
    "\n",
    "- Diagnose omitted-variable bias, attenuation from measurement error and simultaneity bias, and derive each probability limit.\n",
    "- State the instrumental-variable conditions (relevance, independence, exclusion) and draw them as a DAG.\n",
    "- Compute the IV estimate as reduced form divided by first stage.\n",
    "- Judge candidate instruments in labour, health and industrial-organisation settings."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Theory\n",
    "\n",
    "### Endogeneity\n",
    "\n",
    "In $Y = \\alpha + \\beta X + \\varepsilon$, OLS estimates $\\beta$ consistently only if $\\operatorname{Cov}(X, \\varepsilon) = 0$. Otherwise $X$ is **endogenous** and\n",
    "\n",
    "$$\n",
    "\\operatorname{plim}\\hat\\beta_{OLS} = \\beta + \\frac{\\operatorname{Cov}(X, \\varepsilon)}{\\operatorname{Var}(X)}.\n",
    "$$\n",
    "\n",
    "More data do not help: the bias does not shrink with $n$.\n",
    "\n",
    "### Three sources\n",
    "\n",
    "1. **Omitted variables.** If $\\varepsilon = \\gamma A + u$ with $A$ unobserved, the bias is $\\gamma\\operatorname{Cov}(X, A)/\\operatorname{Var}(X)$ (Day 50).\n",
    "2. **Measurement error.** If we observe $X = X^* + e$ with classical error $e$, OLS is **attenuated**: $\\operatorname{plim}\\hat\\beta = \\lambda\\beta$, with reliability $\\lambda = \\operatorname{Var}(X^*)/(\\operatorname{Var}(X^*) + \\operatorname{Var}(e))$.\n",
    "3. **Simultaneity.** Price and quantity are set jointly by supply and demand; the OLS slope mixes the two curves.\n",
    "\n",
    "### Instruments\n",
    "\n",
    "A variable $Z$ is a valid **instrument** for $X$ if\n",
    "\n",
    "1. **Relevance:** $\\operatorname{Cov}(Z, X) \\ne 0$;\n",
    "2. **Independence:** $Z$ is as good as randomly assigned (no common cause with $Y$);\n",
    "3. **Exclusion:** $Z$ affects $Y$ only through $X$.\n",
    "\n",
    "Conditions 2 and 3 together give $\\operatorname{Cov}(Z, \\varepsilon) = 0$. As a DAG: $Z \\to X \\to Y$, $U \\to X$, $U \\to Y$, with no arrow from $Z$ to $Y$ or from $U$ to $Z$.\n",
    "\n",
    "### The IV estimand\n",
    "\n",
    "Take covariances of $Y = \\alpha + \\beta X + \\varepsilon$ with $Z$: $\\operatorname{Cov}(Z, Y) = \\beta\\operatorname{Cov}(Z, X)$. So\n",
    "\n",
    "$$\n",
    "\\beta_{IV} = \\frac{\\operatorname{Cov}(Z, Y)}{\\operatorname{Cov}(Z, X)} = \\frac{\\text{reduced form}}{\\text{first stage}},\n",
    "$$\n",
    "\n",
    "where the **first stage** is the slope of $X$ on $Z$ and the **reduced form** the slope of $Y$ on $Z$. Intuition: the instrument shifts $X$ by the first stage and, through $X$ alone, shifts $Y$ by the reduced form; the ratio is the effect per unit of $X$.\n",
    "\n",
    "### What linearity buys\n",
    "\n",
    "Day 56 showed that the instrument DAG does not identify $P(y \\mid \\operatorname{do}(x))$ nonparametrically. With a constant linear effect it identifies $\\beta$. With heterogeneous effects and a binary treatment it identifies a local effect for compliers (Day 63).\n",
    "\n",
    "### Classic instruments\n",
    "\n",
    "Quarter of birth for schooling (compulsory-schooling laws); distance to a college; draft lottery numbers for military service; weather for agricultural supply; judge leniency for incarceration; lotteries for school places or health insurance.\n",
    "\n",
    "## Part 1 · Warm-up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1 · Name the endogeneity\n",
    "\n",
    "Which source of endogeneity dominates? (a) Regressing wages on years of schooling without ability. (b) Regressing health on self-reported income. (c) Regressing quantity of coffee sold on its price across months.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** (a) Omitted variable: ability raises both schooling and wages.\n",
    "\n",
    "**Step 2.** (b) Measurement error: reported income is noisy (also reverse causality from health to income).\n",
    "\n",
    "**Step 3.** (c) Simultaneity: price and quantity are jointly determined by supply and demand.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2 · Attenuation\n",
    "\n",
    "True $\\operatorname{Var}(X^*) = 4$, measurement-error variance 1, true slope 0.5. What does OLS estimate?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reliability: $\\lambda = 4/(4 + 1) = 0.8$.\n",
    "\n",
    "**Step 2.** $\\operatorname{plim}\\hat\\beta = 0.8 \\times 0.5 = 0.4$.\n",
    "\n",
    "**Answer:** 0.4, a 20% underestimate.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3 · The covariance ratio\n",
    "\n",
    "$\\operatorname{Cov}(Z, Y) = 0.6$ and $\\operatorname{Cov}(Z, X) = 0.3$. Compute $\\beta_{IV}$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\beta_{IV} = 0.6/0.3 = 2.0$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 · Reduced form over first stage\n",
    "\n",
    "A regression of $Y$ on $Z$ gives 0.06; of $X$ on $Z$ gives 0.4. Compute the IV estimate.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Both slopes share the denominator $\\operatorname{Var}(Z)$, which cancels in the ratio.\n",
    "\n",
    "**Step 2.** $\\beta_{IV} = 0.06/0.4 = 0.15$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5 · Check the conditions\n",
    "\n",
    "For each candidate instrument, which condition is most doubtful? (a) Rainfall as an instrument for farm income in a regression of child health on income. (b) Number of siblings for schooling. (c) A randomised offer of a training voucher for training participation.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** (a) Exclusion: rainfall also affects health directly (malaria, water quality).\n",
    "\n",
    "**Step 2.** (b) Independence: family size is chosen and correlated with parental resources.\n",
    "\n",
    "**Step 3.** (c) Relevance is testable, independence is guaranteed by randomisation; exclusion is plausible if the offer itself does not change behaviour except through training.\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 2 · Proofs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6 · Omitted-variable bias\n",
    "\n",
    "Let $Y = \\beta X + \\gamma A + u$ with $u$ uncorrelated with $(X, A)$. Prove $\\operatorname{plim}\\hat\\beta_{OLS} = \\beta + \\gamma\\operatorname{Cov}(X, A)/\\operatorname{Var}(X)$ for the short regression.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\hat\\beta_{OLS} \\to \\operatorname{Cov}(X, Y)/\\operatorname{Var}(X)$ by the LLN.\n",
    "\n",
    "**Step 2.** $\\operatorname{Cov}(X, Y) = \\beta\\operatorname{Var}(X) + \\gamma\\operatorname{Cov}(X, A) + 0$.\n",
    "\n",
    "**Step 3.** Divide by $\\operatorname{Var}(X)$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7 · Attenuation bias\n",
    "\n",
    "Let $Y = \\beta X^* + u$ and $X = X^* + e$, with $e$ independent of $(X^*, u)$ and mean 0. Prove $\\operatorname{plim}\\hat\\beta = \\lambda\\beta$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\operatorname{Cov}(X, Y) = \\operatorname{Cov}(X^* + e, \\beta X^* + u) = \\beta\\operatorname{Var}(X^*)$.\n",
    "\n",
    "**Step 2.** $\\operatorname{Var}(X) = \\operatorname{Var}(X^*) + \\operatorname{Var}(e)$.\n",
    "\n",
    "**Step 3.** Ratio: $\\beta\\operatorname{Var}(X^*)/(\\operatorname{Var}(X^*) + \\operatorname{Var}(e)) = \\lambda\\beta$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8 · Simultaneity bias\n",
    "\n",
    "Demand $Q = -bP + u_d$ and supply $Q = sP + u_s$ ($b, s > 0$), with independent shocks of variances $\\sigma_d^2, \\sigma_s^2$. Prove the OLS slope of $Q$ on $P$ converges to $(s\\sigma_d^2 - b\\sigma_s^2)/(\\sigma_d^2 + \\sigma_s^2)$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Equilibrium: $-bP + u_d = sP + u_s$, so $P = (u_d - u_s)/(b + s)$.\n",
    "\n",
    "**Step 2.** $Q = sP + u_s = (s u_d + b u_s)/(b + s)$.\n",
    "\n",
    "**Step 3.** $\\operatorname{Cov}(P, Q) = (s\\sigma_d^2 - b\\sigma_s^2)/(b + s)^2$ and $\\operatorname{Var}(P) = (\\sigma_d^2 + \\sigma_s^2)/(b + s)^2$.\n",
    "\n",
    "**Step 4.** Ratio as stated. $\\blacksquare$ OLS is a variance-weighted mix of the supply slope $s$ and the demand slope $-b$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9 · IV is consistent\n",
    "\n",
    "With $Y = \\alpha + \\beta X + \\varepsilon$, $\\operatorname{Cov}(Z, \\varepsilon) = 0$ and $\\operatorname{Cov}(Z, X) \\ne 0$, prove $\\hat\\beta_{IV} = \\widehat{\\operatorname{Cov}}(Z, Y)/\\widehat{\\operatorname{Cov}}(Z, X) \\to \\beta$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Sample covariances converge to population ones (LLN).\n",
    "\n",
    "**Step 2.** $\\operatorname{Cov}(Z, Y) = \\beta\\operatorname{Cov}(Z, X) + \\operatorname{Cov}(Z, \\varepsilon) = \\beta\\operatorname{Cov}(Z, X)$.\n",
    "\n",
    "**Step 3.** By the continuous mapping theorem (denominator non-zero), the ratio converges to $\\beta$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 10 · A second measurement as an instrument\n",
    "\n",
    "Two independent noisy measurements $X_1 = X^* + e_1$ and $X_2 = X^* + e_2$ are available. Prove that IV with $X_1$ as the regressor and $X_2$ as the instrument is consistent for $\\beta$ in $Y = \\beta X^* + u$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Write $Y = \\beta X_1 + (u - \\beta e_1)$: the error includes $-\\beta e_1$.\n",
    "\n",
    "**Step 2.** $\\operatorname{Cov}(X_2, u - \\beta e_1) = 0$, since $e_2, X^*$ are independent of $u$ and $e_1$.\n",
    "\n",
    "**Step 3.** $\\operatorname{Cov}(X_2, X_1) = \\operatorname{Var}(X^*) \\ne 0$.\n",
    "\n",
    "**Step 4.** By Exercise 9, IV converges to $\\beta$. $\\blacksquare$\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 3 · Applied"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 11 · College proximity and the return to schooling\n",
    "\n",
    "In a stylised version of Card's study, growing up near a four-year college raises schooling by 0.32 years (first stage) and log wages by 0.042 (reduced form). OLS gives 0.075. Compute the IV return and interpret the gap.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\hat\\beta_{IV} = 0.042/0.32 \\approx 0.131$.\n",
    "\n",
    "**Step 2.** IV exceeds OLS (13.1% versus 7.5% per year).\n",
    "\n",
    "**Step 3.** Possible reasons: measurement error attenuates OLS; the compliers (people whose schooling depends on proximity) may have high returns but face high costs (Day 63).\n",
    "\n",
    "**Answer:** about 13% per year. A larger IV than OLS is common in this literature and does not by itself mean ability bias is negative.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 12 · The demand for fish\n",
    "\n",
    "At a wholesale fish market, stormy weather at sea reduces the catch (a supply shifter) but does not affect buyers' demand. On stormy days log price is 0.30 higher and log quantity 0.36 lower than on calm days. Estimate the price elasticity of demand. Why not regress quantity on price?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Weather shifts supply along a fixed demand curve, tracing out its slope.\n",
    "\n",
    "**Step 2.** $\\hat\\beta_{IV} = -0.36/0.30 = -1.2$.\n",
    "\n",
    "**Step 3.** OLS on price mixes supply and demand responses (Exercise 8).\n",
    "\n",
    "**Answer:** demand elasticity about −1.2: a 1% price rise lowers the quantity demanded by 1.2%.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 13 · Self-reported calories\n",
    "\n",
    "A nutrition survey regresses weight gain on self-reported daily calories. True intake has variance 160,000 kcal², reporting error variance 90,000 kcal². If each extra kcal per day truly adds 0.002 kg per month, what does OLS find? Suggest a fix.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\lambda = 160{,}000/(160{,}000 + 90{,}000) = 0.64$.\n",
    "\n",
    "**Step 2.** $\\operatorname{plim}\\hat\\beta = 0.64 \\times 0.002 = 0.00128$ kg per kcal per month.\n",
    "\n",
    "**Step 3.** Fix: a second, independent measure (food diary or doubly labelled water) as an instrument (Exercise 10).\n",
    "\n",
    "**Answer:** OLS understates the effect by 36%.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 14 · Rainfall, income and child health\n",
    "\n",
    "An analyst instruments household income with rainfall in a regression of child height on income. Suppose rainfall also improves child health directly (cleaner water) by 0.02 sd per unit, and raises income by 0.1 units per unit of rainfall. By how much is the IV estimate biased?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** With a direct effect $\\delta$ of $Z$ on $Y$: $\\operatorname{Cov}(Z, Y) = \\beta\\operatorname{Cov}(Z, X) + \\delta\\operatorname{Var}(Z)$.\n",
    "\n",
    "**Step 2.** So $\\beta_{IV} = \\beta + \\delta/\\pi$, where $\\pi$ is the first-stage slope.\n",
    "\n",
    "**Step 3.** Bias $= 0.02/0.1 = 0.2$ sd per unit of income.\n",
    "\n",
    "**Answer:** a small exclusion violation becomes a large bias when the first stage is weak.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 15 · The Vietnam draft lottery\n",
    "\n",
    "Draft eligibility was determined by a lottery on birth dates. Evaluate the three IV conditions for estimating the effect of military service on later earnings.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Relevance: eligible men served more often (testable: the first stage is about 15 points).\n",
    "\n",
    "**Step 2.** Independence: the lottery was random.\n",
    "\n",
    "**Step 3.** Exclusion: eligibility could affect earnings other than through service, for example by encouraging men to stay in school to obtain deferments.\n",
    "\n",
    "**Answer:** relevance and independence hold; exclusion needs argument and checks (e.g. schooling of eligible versus ineligible men).\n",
    "\n",
    "</details>\n",
    "\n",
    "## Verify in Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "rng = np.random.default_rng(61); n = 1_000_000\n",
    "slope = lambda y, x: np.cov(x, y)[0, 1] / np.var(x, ddof=1)\n",
    "iv = lambda y, x, z: np.cov(z, y)[0, 1] / np.cov(z, x)[0, 1]\n",
    "\n",
    "# Omitted ability, with an instrument (true effect 0.08)\n",
    "A = rng.normal(size=n); Z = rng.normal(size=n)\n",
    "S = 0.3 * Z + 2 * A + rng.normal(0, 2, n)\n",
    "W = 0.08 * S + 0.10 * A + rng.normal(0, .3, n)\n",
    "print(f\"schooling: OLS {slope(W, S):.4f}  IV {iv(W, S, Z):.4f}  (truth 0.08)\")\n",
    "\n",
    "# Measurement error: Exercise 2 and a second measurement as instrument\n",
    "Xs = rng.normal(0, 2, n); Y = .5 * Xs + rng.normal(size=n)\n",
    "X1 = Xs + rng.normal(size=n); X2 = Xs + rng.normal(size=n)\n",
    "print(f\"attenuation: OLS {slope(Y, X1):.3f} (0.4)  IV with second measure {iv(Y, X1, X2):.3f} (0.5)\")\n",
    "\n",
    "# Simultaneity: Exercise 8 with b = 1.2, s = 0.8, sd_d = sd_s = 1, and a supply shifter (weather)\n",
    "b, s = 1.2, .8\n",
    "ud = rng.normal(size=n); storm = rng.normal(size=n); us = rng.normal(size=n) - 0.5 * storm\n",
    "P = (ud - us) / (b + s); Q = -b * P + ud\n",
    "print(f\"market: OLS {slope(Q, P):.3f} (formula {(s * 1 - b * 1.25) / (1 + 1.25):.3f})  IV demand slope {iv(Q, P, storm):.3f} (truth -1.2)\")\n",
    "\n",
    "# Exercises 11-14\n",
    "print(\"college IV:\", round(.042 / .32, 3), \" fish elasticity:\", -.36 / .30, \" calories:\", .64 * .002, \" rainfall bias:\", round(.02 / .1, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key takeaways\n",
    "\n",
    "- Endogeneity ($\\operatorname{Cov}(X, \\varepsilon) \\ne 0$) comes from omitted variables, measurement error or simultaneity, and does not vanish with more data.\n",
    "- An instrument must move the treatment (relevance), be as good as random (independence) and act only through the treatment (exclusion).\n",
    "- $\\beta_{IV} = \\operatorname{Cov}(Z, Y)/\\operatorname{Cov}(Z, X)$ = reduced form ÷ first stage; small exclusion violations are magnified by weak first stages."
   ]
  }
 ]
}