{
 "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 63: LATE & Compliers (Imbens–Angrist)\n",
    "\n",
    "*Beginner to Ninja: Math for Causal Inference*\n",
    "\n",
    "When treatment effects differ across people, IV does not estimate the average effect. Under independence, exclusion and monotonicity it estimates the average effect for compliers, the people whose treatment the instrument changes. Their share, outcomes and characteristics are all identified.\n",
    "\n",
    "**Learning outcomes**\n",
    "\n",
    "- Define compliers, always-takers, never-takers and defiers with potential treatments.\n",
    "- State and prove the LATE theorem.\n",
    "- Identify the shares of each type, complier mean potential outcomes and complier characteristics.\n",
    "- Explain why different instruments give different LATEs and what monotonicity failures do."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Theory\n",
    "\n",
    "### Potential treatments\n",
    "\n",
    "Let $Z \\in \\{0, 1\\}$ be the instrument and $D(z)$ the treatment a unit would take if $Z = z$. Four types:\n",
    "\n",
    "| Type | $D(1)$ | $D(0)$ |\n",
    "|---|---|---|\n",
    "| Complier | 1 | 0 |\n",
    "| Always-taker | 1 | 1 |\n",
    "| Never-taker | 0 | 0 |\n",
    "| Defier | 0 | 1 |\n",
    "\n",
    "We observe only $D = D(Z)$, so types are not observed individually.\n",
    "\n",
    "### Assumptions\n",
    "\n",
    "1. **Independence:** $Z$ is independent of all potential outcomes $Y(d, z)$ and potential treatments $D(z)$.\n",
    "2. **Exclusion:** $Y(d, z) = Y(d)$: the instrument matters only through treatment.\n",
    "3. **Relevance:** $P(D = 1 \\mid Z = 1) \\ne P(D = 1 \\mid Z = 0)$.\n",
    "4. **Monotonicity:** $D(1) \\ge D(0)$ for everyone: no defiers.\n",
    "\n",
    "### The LATE theorem (Imbens and Angrist, 1994)\n",
    "\n",
    "Under 1–4,\n",
    "\n",
    "$$\n",
    "\\frac{E[Y \\mid Z = 1] - E[Y \\mid Z = 0]}{E[D \\mid Z = 1] - E[D \\mid Z = 0]} = E[Y(1) - Y(0) \\mid \\text{complier}] \\equiv LATE.\n",
    "$$\n",
    "\n",
    "Always-takers and never-takers have the same treatment whatever $Z$, so they contribute nothing to the reduced form; the ratio rescales the compliers' effect by their share.\n",
    "\n",
    "### What is identified\n",
    "\n",
    "- **Shares:** $\\pi_a = P(D = 1 \\mid Z = 0)$, $\\pi_n = P(D = 0 \\mid Z = 1)$, $\\pi_c = 1 - \\pi_a - \\pi_n$ (the first stage).\n",
    "- **Complier outcome means:**\n",
    "\n",
    "$$\n",
    "E[Y(1) \\mid c] = \\frac{E[YD \\mid Z = 1] - E[YD \\mid Z = 0]}{\\pi_c}, \\qquad E[Y(0) \\mid c] = \\frac{E[Y(1 - D) \\mid Z = 0] - E[Y(1 - D) \\mid Z = 1]}{\\pi_c}.\n",
    "$$\n",
    "\n",
    "- **Complier characteristics:** for any pre-treatment $X$, $E[X \\mid c] = \\big(E[XD \\mid Z = 1] - E[XD \\mid Z = 0]\\big)/\\pi_c$.\n",
    "\n",
    "### Interpretation\n",
    "\n",
    "LATE is the effect for a subpopulation defined by the instrument. Another instrument has other compliers and, with heterogeneous effects, another LATE. Whether a LATE is policy-relevant depends on whether the policy would change treatment for similar people. Profiling compliers (Exercise 12) helps judge this.\n",
    "\n",
    "## Part 1 · Warm-up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1 · Classify the types\n",
    "\n",
    "A lottery offers a place in a charter school. Classify: (a) a family that would enrol only if it wins; (b) a family that will enrol through a sibling preference whatever happens; (c) a family that would never move its child; (d) a family that would enrol only if it loses (out of spite).\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** (a) Complier: $D(1) = 1$, $D(0) = 0$.\n",
    "\n",
    "**Step 2.** (b) Always-taker.\n",
    "\n",
    "**Step 3.** (c) Never-taker.\n",
    "\n",
    "**Step 4.** (d) Defier, ruled out by monotonicity.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2 · Type shares\n",
    "\n",
    "$P(D = 1 \\mid Z = 1) = 0.7$ and $P(D = 1 \\mid Z = 0) = 0.2$. Under monotonicity, find the type shares.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Always-takers: treated even with $Z = 0$: $\\pi_a = 0.2$.\n",
    "\n",
    "**Step 2.** Never-takers: untreated even with $Z = 1$: $\\pi_n = 1 - 0.7 = 0.3$.\n",
    "\n",
    "**Step 3.** Compliers: $\\pi_c = 1 - 0.2 - 0.3 = 0.5$ (= first stage $0.7 - 0.2$).\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3 · LATE from means\n",
    "\n",
    "With Exercise 2's take-up and mean outcomes 57 ($Z = 1$) and 50.8 ($Z = 0$), compute the LATE.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $57 - 50.8 = 6.2$.\n",
    "\n",
    "**Step 2.** LATE $= 6.2/0.5 = 12.4$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 · Why defiers matter\n",
    "\n",
    "Explain in words why defiers would bias the Wald ratio.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Winning the instrument switches compliers into treatment and defiers out of it.\n",
    "\n",
    "**Step 2.** The reduced form becomes (compliers' effects) minus (defiers' effects), and the first stage becomes $\\pi_c - \\pi_d$.\n",
    "\n",
    "**Answer:** the ratio is no longer an average of anyone's effects and can even fall outside the range of all individual effects (Exercise 14).\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5 · Who complies?\n",
    "\n",
    "For the draft lottery (Day 61), describe the compliers, always-takers and never-takers.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Compliers: men who served because they were drafted but would not have volunteered.\n",
    "\n",
    "**Step 2.** Always-takers: volunteers.\n",
    "\n",
    "**Step 3.** Never-takers: men exempted (health, deferments) or who avoided service even if drafted.\n",
    "\n",
    "**Answer:** the IV estimate is the effect of service for draftees who would not have volunteered.\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 2 · Proofs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6 · The LATE theorem\n",
    "\n",
    "Prove the LATE theorem.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** By exclusion, $Y = Y(0) + D(Z)\\,(Y(1) - Y(0))$.\n",
    "\n",
    "**Step 2.** By independence, $E[Y \\mid Z = 1] - E[Y \\mid Z = 0] = E\\big[(D(1) - D(0))(Y(1) - Y(0))\\big]$.\n",
    "\n",
    "**Step 3.** By monotonicity, $D(1) - D(0) \\in \\{0, 1\\}$, equal to 1 exactly for compliers. So the reduced form is $\\pi_c\\,E[Y(1) - Y(0) \\mid c]$.\n",
    "\n",
    "**Step 4.** First stage: $E[D(1) - D(0)] = \\pi_c$.\n",
    "\n",
    "**Step 5.** Divide. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7 · Identifying the shares\n",
    "\n",
    "Prove $\\pi_a = P(D = 1 \\mid Z = 0)$ and $\\pi_n = P(D = 0 \\mid Z = 1)$ under independence and monotonicity.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** With $Z = 0$, the treated are those with $D(0) = 1$: always-takers and defiers. With no defiers, only always-takers.\n",
    "\n",
    "**Step 2.** Independence: the type distribution is the same for $Z = 0$ and $Z = 1$, so $P(D = 1 \\mid Z = 0) = \\pi_a$.\n",
    "\n",
    "**Step 3.** With $Z = 1$, the untreated are never-takers (and defiers, excluded): $P(D = 0 \\mid Z = 1) = \\pi_n$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8 · Complier mean of Y(1)\n",
    "\n",
    "Prove $E[Y(1) \\mid c] = (E[YD \\mid Z = 1] - E[YD \\mid Z = 0])/\\pi_c$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** With $Z = 1$, treated units are compliers and always-takers: $E[YD \\mid Z = 1] = \\pi_c E[Y(1) \\mid c] + \\pi_a E[Y(1) \\mid a]$.\n",
    "\n",
    "**Step 2.** With $Z = 0$, treated units are always-takers only: $E[YD \\mid Z = 0] = \\pi_a E[Y(1) \\mid a]$.\n",
    "\n",
    "**Step 3.** Subtract and divide by $\\pi_c$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9 · Wald with defiers\n",
    "\n",
    "Without monotonicity, show the Wald estimand is $\\dfrac{\\pi_c\\,LATE_c - \\pi_d\\,LATE_d}{\\pi_c - \\pi_d}$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** From Exercise 6, Step 2: the reduced form is $E[(D(1) - D(0))(Y(1) - Y(0))]$.\n",
    "\n",
    "**Step 2.** $D(1) - D(0) = 1$ for compliers, $-1$ for defiers, 0 otherwise: reduced form $= \\pi_c LATE_c - \\pi_d LATE_d$.\n",
    "\n",
    "**Step 3.** First stage $= \\pi_c - \\pi_d$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 10 · Complier characteristics\n",
    "\n",
    "For a pre-treatment covariate $X$, prove $E[X \\mid c] = (E[XD \\mid Z = 1] - E[XD \\mid Z = 0])/\\pi_c$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $X$ is pre-treatment, so it is not affected by $Z$ and, by independence, its distribution within each type does not depend on $Z$.\n",
    "\n",
    "**Step 2.** $E[XD \\mid Z = 1] = \\pi_c E[X \\mid c] + \\pi_a E[X \\mid a]$ and $E[XD \\mid Z = 0] = \\pi_a E[X \\mid a]$.\n",
    "\n",
    "**Step 3.** Subtract and divide by $\\pi_c$. $\\blacksquare$\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 3 · Applied"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 11 · A school-voucher lottery\n",
    "\n",
    "Among lottery winners ($Z = 1$), 70% use a voucher; their mean test score is 60, and non-users score 50. Among losers, 20% attend a private school anyway, scoring 62; the rest score 48. Compute the type shares, the LATE, the compliers' mean scores with and without the voucher, and compare with always-takers and never-takers.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Shares: $\\pi_a = 0.2$, $\\pi_n = 0.3$, $\\pi_c = 0.5$.\n",
    "\n",
    "**Step 2.** $E[Y(1) \\mid c] = (0.7 \\times 60 - 0.2 \\times 62)/0.5 = (42 - 12.4)/0.5 = 59.2$.\n",
    "\n",
    "**Step 3.** $E[Y(0) \\mid c] = (0.8 \\times 48 - 0.3 \\times 50)/0.5 = (38.4 - 15)/0.5 = 46.8$.\n",
    "\n",
    "**Step 4.** LATE $= 59.2 - 46.8 = 12.4$ (matches Exercise 3).\n",
    "\n",
    "**Step 5.** Always-takers' treated mean: 62; never-takers' untreated mean: 50.\n",
    "\n",
    "**Answer:** vouchers raise compliers' scores by 12.4 points. Compliers would score less than never-takers without the voucher (46.8 versus 50): they are relatively disadvantaged families for whom the voucher made private school affordable.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 12 · Who are the compliers?\n",
    "\n",
    "In the same lottery, 50% of children are girls. Among winners, girls who use the voucher make up 45% of all winners; among losers, girls in private school make up 10% of all losers. What share of compliers are girls?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $E[\\text{girl} \\cdot D \\mid Z = 1] = 0.45$ and $E[\\text{girl} \\cdot D \\mid Z = 0] = 0.10$.\n",
    "\n",
    "**Step 2.** $E[\\text{girl} \\mid c] = (0.45 - 0.10)/0.5 = 0.70$.\n",
    "\n",
    "**Answer:** 70% of compliers are girls, versus 50% overall. Generalising the LATE to boys requires care.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 13 · Two instruments, two answers\n",
    "\n",
    "Using distance to college, the return to schooling is 0.13; using a tuition subsidy, 0.07. Both are valid. Can both be right?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Each instrument has its own compliers: people for whom distance matters (often from poorer, rural families), and people for whom tuition matters.\n",
    "\n",
    "**Step 2.** With heterogeneous returns, their LATEs can differ.\n",
    "\n",
    "**Answer:** yes. The disagreement says returns vary across people, not that one instrument is invalid. An over-identification test (Day 66) would reject even if both are valid.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 14 · Defiers in a fine policy\n",
    "\n",
    "A city sends warning letters ($Z$) about a recycling fine. The letter makes 40% of households start recycling (compliers, effect on waste of 10 kg) but makes 10% stop in protest (defiers, effect 4 kg). Compute the Wald estimate.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Reduced form: $0.4 \\times 10 - 0.1 \\times 4 = 3.6$.\n",
    "\n",
    "**Step 2.** First stage: $0.4 - 0.1 = 0.3$.\n",
    "\n",
    "**Step 3.** Wald: $3.6/0.3 = 12$.\n",
    "\n",
    "**Answer:** 12, larger than every individual effect in the population (10 and 4): without monotonicity the ratio is not an average treatment effect for anyone.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 15 · A mandate versus a lottery\n",
    "\n",
    "A health insurance lottery gives a LATE of 0.4 extra doctor visits per year. A ministry asks what a universal mandate would do. Explain what extra information is needed.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** A mandate changes coverage for never-takers and (if it forces nothing on them) leaves always-takers unchanged.\n",
    "\n",
    "**Step 2.** The LATE describes compliers only; never-takers may be healthier or less inclined to use care, so their effect may be smaller.\n",
    "\n",
    "**Step 3.** Evidence needed: complier versus never-taker characteristics (Exercise 10), effect heterogeneity by observables among compliers, or a second instrument that moves never-takers.\n",
    "\n",
    "**Answer:** the LATE is informative but not the answer; extrapolation needs assumptions about how effects vary.\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(63); n = 2_000_000\n",
    "Z = rng.random(n) < .5\n",
    "typ = rng.choice([\"c\", \"a\", \"n\"], size=n, p=[.5, .2, .3])\n",
    "girl = rng.random(n) < np.select([typ == \"c\", typ == \"a\", typ == \"n\"], [.7, .3, .33])\n",
    "Y0 = np.select([typ == \"c\", typ == \"a\", typ == \"n\"], [46.8, 50, 50]) + rng.normal(0, 8, n)\n",
    "gain = np.select([typ == \"c\", typ == \"a\", typ == \"n\"], [12.4, 12.0, 5.0]) + rng.normal(0, 3, n)\n",
    "D = (typ == \"a\") | ((typ == \"c\") & Z)\n",
    "Y = Y0 + D * gain\n",
    "\n",
    "fs = D[Z].mean() - D[~Z].mean(); rf = Y[Z].mean() - Y[~Z].mean()\n",
    "print(f\"shares: always {D[~Z].mean():.3f}, never {1 - D[Z].mean():.3f}, compliers {fs:.3f}\")\n",
    "print(f\"Wald {rf / fs:.2f};  true LATE {gain[typ == 'c'].mean():.2f};  ATE {gain.mean():.2f}\")\n",
    "EY1c = ((Y * D)[Z].mean() - (Y * D)[~Z].mean()) / fs\n",
    "EY0c = ((Y * ~D)[~Z].mean() - (Y * ~D)[Z].mean()) / fs\n",
    "print(f\"complier means: Y(1) {EY1c:.2f}, Y(0) {EY0c:.2f};  share girls among compliers {((girl * D)[Z].mean() - (girl * D)[~Z].mean()) / fs:.3f}\")\n",
    "\n",
    "# Exercise 11 from the summary statistics, and Exercise 14\n",
    "print(\"voucher:\", round((.7 * 60 - .2 * 62) / .5, 2), round((.8 * 48 - .3 * 50) / .5, 2), \" defier Wald:\", round((.4 * 10 - .1 * 4) / .3, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key takeaways\n",
    "\n",
    "- Under independence, exclusion and monotonicity, IV estimates the average effect for compliers (LATE), not the ATE.\n",
    "- Type shares, complier mean potential outcomes and complier characteristics are identified from simple moments.\n",
    "- Different instruments define different compliers; defiers break the interpretation entirely."
   ]
  }
 ]
}