{
 "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 62: The Wald Estimator\n",
    "\n",
    "*Beginner to Ninja: Math for Causal Inference*\n",
    "\n",
    "With a binary instrument, IV is a ratio of two differences in means. The Wald estimator, its grouped-data generalisation, its delta-method standard error, and the special case of one-sided non-compliance where it recovers the effect on the treated.\n",
    "\n",
    "**Learning outcomes**\n",
    "\n",
    "- Compute the Wald estimator from group means and relate it to the covariance formula.\n",
    "- Read IV graphically as the slope through group means.\n",
    "- Derive a delta-method standard error for a ratio of differences.\n",
    "- Prove that with one-sided non-compliance the Wald estimator equals the effect of treatment on the treated."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Theory\n",
    "\n",
    "### Binary instrument\n",
    "\n",
    "Let $Z \\in \\{0, 1\\}$ (won a lottery, born in an eligible year, offered a voucher). The **Wald estimator** is\n",
    "\n",
    "$$\n",
    "\\hat\\beta_{W} = \\frac{\\bar Y_1 - \\bar Y_0}{\\bar X_1 - \\bar X_0},\n",
    "$$\n",
    "\n",
    "where $\\bar Y_z, \\bar X_z$ are means among units with $Z = z$. The numerator is the **reduced form** (intention-to-treat effect on $Y$), the denominator the **first stage** (effect of $Z$ on $X$; for binary $X$, the difference in take-up rates).\n",
    "\n",
    "### Same as IV\n",
    "\n",
    "For binary $Z$, $\\operatorname{Cov}(Z, Y) = p(1 - p)(\\bar Y_1 - \\bar Y_0)$ with $p = P(Z = 1)$, and similarly for $X$. The factor $p(1 - p)$ cancels, so the Wald estimator is the IV estimator.\n",
    "\n",
    "### The picture\n",
    "\n",
    "Plot the two group means $(\\bar X_0, \\bar Y_0)$ and $(\\bar X_1, \\bar Y_1)$: the Wald estimate is the slope of the line through them. With several groups (quarters of birth, lottery cohorts), fitting a line through the group means, weighted by group size, is **2SLS with group dummies as instruments** (Angrist's grouping estimator).\n",
    "\n",
    "### Standard error\n",
    "\n",
    "By the delta method, with reduced form $\\hat\\rho$, first stage $\\hat\\pi$ and $\\hat\\beta = \\hat\\rho/\\hat\\pi$,\n",
    "\n",
    "$$\n",
    "\\operatorname{Var}(\\hat\\beta) \\approx \\frac{\\operatorname{Var}(\\hat\\rho) + \\beta^2\\operatorname{Var}(\\hat\\pi) - 2\\beta\\operatorname{Cov}(\\hat\\rho, \\hat\\pi)}{\\pi^2}.\n",
    "$$\n",
    "\n",
    "A small first stage $\\pi$ inflates the variance quadratically (Day 65).\n",
    "\n",
    "### One-sided non-compliance\n",
    "\n",
    "If nobody with $Z = 0$ can get treated (a programme only lottery winners can join), then under exclusion the Wald estimator equals the **effect of treatment on the treated** (Bloom's result). Phase 3, Day 41, called the same ratio the CACE; Day 63 develops the general case.\n",
    "\n",
    "## Part 1 · Warm-up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1 · A first Wald estimate\n",
    "\n",
    "Mean outcomes are 52 ($Z = 1$) and 50 ($Z = 0$); treatment rates 0.6 and 0.2. Compute the Wald estimate.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $52 - 50 = 2$.\n",
    "\n",
    "**Step 2.** First stage: $0.6 - 0.2 = 0.4$.\n",
    "\n",
    "**Step 3.** $\\hat\\beta_W = 2/0.4 = 5$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2 · Slope through two points\n",
    "\n",
    "Show that the line through $(\\bar X_0, \\bar Y_0) = (0.2, 50)$ and $(\\bar X_1, \\bar Y_1) = (0.6, 52)$ has slope 5.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Slope $= (52 - 50)/(0.6 - 0.2) = 2/0.4 = 5$, the Wald estimate.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3 · Grouped data\n",
    "\n",
    "Three cohorts have mean schooling and mean wage (10, 20), (12, 23), (14, 26), with equal sizes. What is the grouping estimator?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** The points lie on a line: each 2 extra years add 3 to the wage.\n",
    "\n",
    "**Step 2.** Slope $= 3/2 = 1.5$.\n",
    "\n",
    "**Answer:** 1.5 per year of schooling.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 · A standard error\n",
    "\n",
    "Reduced form 2 (SE 0.5), first stage 0.4 (SE 0.02), estimates uncorrelated. Compute the delta-method SE of $\\hat\\beta = 5$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Numerator: $0.5^2 + 5^2(0.02)^2 = 0.25 + 0.01 = 0.26$.\n",
    "\n",
    "**Step 2.** $\\operatorname{SE} \\approx \\sqrt{0.26}/0.4 \\approx 0.51/0.4 \\approx 1.27$.\n",
    "\n",
    "**Answer:** about 1.27; most of the uncertainty comes from the reduced form.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5 · Signs\n",
    "\n",
    "The reduced form is +3 and the first stage −0.5. What is the Wald estimate, and what does the negative first stage mean?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\hat\\beta_W = 3/(-0.5) = -6$.\n",
    "\n",
    "**Step 2.** The instrument lowers take-up, and the outcome rises: so the treatment lowers the outcome.\n",
    "\n",
    "**Answer:** −6. Recoding $Z$ as $1 - Z$ flips both signs and leaves the estimate unchanged.\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 2 · Proofs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6 · Wald equals IV\n",
    "\n",
    "For binary $Z$ with $p = P(Z = 1)$, prove $\\operatorname{Cov}(Z, Y) = p(1 - p)(E[Y \\mid Z = 1] - E[Y \\mid Z = 0])$, and conclude that the Wald estimand equals $\\operatorname{Cov}(Z, Y)/\\operatorname{Cov}(Z, X)$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\operatorname{Cov}(Z, Y) = E[ZY] - E[Z]E[Y] = pE[Y \\mid Z = 1] - p\\big(pE[Y \\mid Z = 1] + (1 - p)E[Y \\mid Z = 0]\\big)$.\n",
    "\n",
    "**Step 2.** $= p(1 - p)E[Y \\mid Z = 1] - p(1 - p)E[Y \\mid Z = 0]$.\n",
    "\n",
    "**Step 3.** The same holds for $X$; the factor $p(1 - p)$ cancels in the ratio. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7 · Delta method for a ratio\n",
    "\n",
    "Let $g(\\rho, \\pi) = \\rho/\\pi$. Derive the delta-method variance in the theory.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Gradient: $\\partial g/\\partial\\rho = 1/\\pi$, $\\partial g/\\partial\\pi = -\\rho/\\pi^2 = -\\beta/\\pi$.\n",
    "\n",
    "**Step 2.** $\\operatorname{Var}(g) \\approx \\nabla g^\\top\\boldsymbol\\Sigma\\nabla g = \\frac{1}{\\pi^2}\\operatorname{Var}(\\hat\\rho) + \\frac{\\beta^2}{\\pi^2}\\operatorname{Var}(\\hat\\pi) - \\frac{2\\beta}{\\pi^2}\\operatorname{Cov}(\\hat\\rho, \\hat\\pi)$.\n",
    "\n",
    "**Step 3.** Factor out $1/\\pi^2$. $\\blacksquare$ (Day 12.)\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8 · One-sided non-compliance gives the ATT\n",
    "\n",
    "Let $Z$ be random, $D \\in \\{0, 1\\}$ with $D = 0$ whenever $Z = 0$, and exclusion: $Y = Y(D)$ does not depend on $Z$ otherwise. Prove the Wald estimand equals $E[Y(1) - Y(0) \\mid D = 1]$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $E[Y \\mid Z = 1] - E[Y \\mid Z = 0] = E[Y(0) + D(Y(1) - Y(0)) \\mid Z = 1] - E[Y(0) \\mid Z = 0]$.\n",
    "\n",
    "**Step 2.** Randomisation: $E[Y(0) \\mid Z = 1] = E[Y(0) \\mid Z = 0]$, so the reduced form is $E[D(Y(1) - Y(0)) \\mid Z = 1]$.\n",
    "\n",
    "**Step 3.** $= P(D = 1 \\mid Z = 1)\\,E[Y(1) - Y(0) \\mid D = 1, Z = 1]$.\n",
    "\n",
    "**Step 4.** First stage: $P(D = 1 \\mid Z = 1) - 0$. Ratio: $E[Y(1) - Y(0) \\mid D = 1, Z = 1]$.\n",
    "\n",
    "**Step 5.** Everyone with $D = 1$ has $Z = 1$, so this is the ATT. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9 · Grouping estimator with two groups\n",
    "\n",
    "Prove that the IV estimator with a binary instrument equals the slope of the line through the two sample group means.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Exercise 6 holds in the sample too: $\\widehat{\\operatorname{Cov}}(Z, Y) = \\hat p(1 - \\hat p)(\\bar Y_1 - \\bar Y_0)$ (with the same divisor for both covariances).\n",
    "\n",
    "**Step 2.** The ratio is $(\\bar Y_1 - \\bar Y_0)/(\\bar X_1 - \\bar X_0)$, which is the slope through the two points. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 10 · A direct effect of the instrument\n",
    "\n",
    "If $Z$ also affects $Y$ directly by $\\delta$ (exclusion fails), show the Wald estimand is $\\beta + \\delta/\\pi$ in the constant-effect model $Y = \\alpha + \\beta X + \\delta Z + \\varepsilon$, $E[\\varepsilon \\mid Z] = 0$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $E[Y \\mid Z = 1] - E[Y \\mid Z = 0] = \\beta(E[X \\mid Z = 1] - E[X \\mid Z = 0]) + \\delta = \\beta\\pi + \\delta$.\n",
    "\n",
    "**Step 2.** Divide by $\\pi$: $\\beta + \\delta/\\pi$. $\\blacksquare$\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 3 · Applied"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 11 · Draft lottery and earnings\n",
    "\n",
    "In a stylised version of Angrist (1990), men with draft-eligible lottery numbers earn \\$15,600 a year later in life versus \\$16,050 for ineligible men. Their veteran rates are 0.35 and 0.19. Estimate the earnings effect of military service.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $15{,}600 - 16{,}050 = -450$.\n",
    "\n",
    "**Step 2.** First stage: $0.35 - 0.19 = 0.16$.\n",
    "\n",
    "**Step 3.** $\\hat\\beta_W = -450/0.16 = -2{,}812.5$.\n",
    "\n",
    "**Answer:** serving lowers annual earnings by about \\$2,800 for men whose service depended on the lottery.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 12 · Health insurance lottery\n",
    "\n",
    "In a stylised Oregon-style lottery, winners average 1.10 emergency-room visits a year and losers 1.00; insurance coverage is 43% versus 18%. Estimate the effect of insurance on ER visits.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $1.10 - 1.00 = 0.10$ visits.\n",
    "\n",
    "**Step 2.** First stage: $0.43 - 0.18 = 0.25$.\n",
    "\n",
    "**Step 3.** $\\hat\\beta_W = 0.10/0.25 = 0.40$.\n",
    "\n",
    "**Answer:** insurance raises ER use by 0.4 visits a year among people who took it up because they won, contradicting the hope that coverage would reduce ER use.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 13 · Quarter of birth\n",
    "\n",
    "Mean schooling and log weekly wages by quarter of birth (equal cohort sizes): Q1 (12.70, 5.892), Q2 (12.80, 5.900), Q3 (12.85, 5.904), Q4 (12.90, 5.908). Estimate the return to schooling by the grouping estimator.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Means: $\\bar x = 12.8125$, $\\bar y = 5.901$.\n",
    "\n",
    "**Step 2.** Deviations of $x$: −0.1125, −0.0125, 0.0375, 0.0875; of $y$: −0.009, −0.001, 0.003, 0.007.\n",
    "\n",
    "**Step 3.** $\\sum dx\\,dy = 0.0010125 + 0.0000125 + 0.0001125 + 0.0006125 = 0.00175$; $\\sum dx^2 = 0.021875$.\n",
    "\n",
    "**Step 4.** Slope $= 0.00175/0.021875 = 0.08$.\n",
    "\n",
    "**Answer:** 8% per year. Tiny differences in group means identify the effect, which is why such designs need very large samples (Day 65).\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 14 · A training voucher only winners could use\n",
    "\n",
    "Only lottery winners could use a training voucher; 60% of winners used it. Winners earn \\$300 a year more than losers. Estimate the effect on those trained.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** One-sided non-compliance: first stage $0.6 - 0 = 0.6$.\n",
    "\n",
    "**Step 2.** $\\hat\\beta_W = 300/0.6 = 500$.\n",
    "\n",
    "**Answer:** \\$500 a year, the ATT (Exercise 8).\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 15 · A confidence interval for the draft effect\n",
    "\n",
    "In Exercise 11, the reduced form has SE \\$200 and the first stage SE 0.01, uncorrelated. Compute a 95% confidence interval for the effect of service.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Numerator: $200^2 + (-2{,}812.5)^2(0.01)^2 = 40{,}000 + 791 = 40{,}791$.\n",
    "\n",
    "**Step 2.** SE $\\approx \\sqrt{40{,}791}/0.16 \\approx 202/0.16 \\approx 1{,}262$.\n",
    "\n",
    "**Step 3.** CI: $-2{,}812.5 \\pm 1.96 \\times 1{,}262 \\approx [-5{,}287,\\; -338]$.\n",
    "\n",
    "**Answer:** negative but imprecise. With a weak first stage the delta method can be unreliable; Day 65 gives better intervals.\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(62)\n",
    "# A simulated lottery: Z random, compliers/always/never takers, effect -2800 for those who serve\n",
    "n = 400_000\n",
    "Z = rng.random(n) < .3\n",
    "kind = rng.choice([\"c\", \"a\", \"n\"], size=n, p=[.16, .19, .65])\n",
    "D = (kind == \"a\") | ((kind == \"c\") & Z)\n",
    "Y0 = rng.normal(16_000 + 600 * (kind == \"a\"), 5_000, n)\n",
    "Y = Y0 - 2_800 * D\n",
    "rf = Y[Z].mean() - Y[~Z].mean(); fs = D[Z].mean() - D[~Z].mean()\n",
    "iv = np.cov(Z, Y)[0, 1] / np.cov(Z, D)[0, 1]\n",
    "print(f\"reduced form {rf:.0f}, first stage {fs:.3f}, Wald {rf / fs:.0f}, covariance IV {iv:.0f}\")\n",
    "\n",
    "# Delta-method SE versus the bootstrap\n",
    "var_rf = Y[Z].var() / Z.sum() + Y[~Z].var() / (~Z).sum()\n",
    "var_fs = D[Z].var() / Z.sum() + D[~Z].var() / (~Z).sum()\n",
    "b = rf / fs\n",
    "cov = np.cov(Y[Z], D[Z])[0, 1] / Z.sum() + np.cov(Y[~Z], D[~Z])[0, 1] / (~Z).sum()\n",
    "print(f\"delta-method SE {np.sqrt(var_rf + b**2 * var_fs - 2 * b * cov) / abs(fs):.0f}\")\n",
    "boot = []\n",
    "for _ in range(200):\n",
    "    i = rng.integers(0, n, n); z, d, y = Z[i], D[i], Y[i]\n",
    "    boot.append((y[z].mean() - y[~z].mean()) / (d[z].mean() - d[~z].mean()))\n",
    "print(f\"bootstrap SE {np.std(boot):.0f}\")\n",
    "\n",
    "# Exercises 11, 13, 15\n",
    "b = -450 / .16\n",
    "print(\"draft:\", b, \" SE\", round(np.sqrt(200**2 + b**2 * .01**2) / .16), \" CI\", np.round([b - 1.96 * 1262.3, b + 1.96 * 1262.3]))\n",
    "x = np.array([12.7, 12.8, 12.85, 12.9]); y = np.array([5.892, 5.900, 5.904, 5.908])\n",
    "print(\"quarter-of-birth slope:\", round(np.polyfit(x, y, 1)[0], 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key takeaways\n",
    "\n",
    "- With a binary instrument, IV is the Wald ratio: difference in outcomes over difference in treatment between instrument groups.\n",
    "- Graphically, it is the slope through group means; with many groups, a size-weighted line through the means is 2SLS.\n",
    "- Its delta-method variance is inflated by $1/\\pi^2$; with one-sided non-compliance it estimates the effect on the treated."
   ]
  }
 ]
}