{
 "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 51: Interventions & the do-Operator\n",
    "\n",
    "*Beginner to Ninja: Math for Causal Inference*\n",
    "\n",
    "Seeing is not doing. An intervention do(X = x) replaces X's structural equation, cutting the arrows into X. The truncated factorisation turns this into a formula for the interventional distribution, which differs from ordinary conditioning whenever X has open back-door paths.\n",
    "\n",
    "**Learning outcomes**\n",
    "\n",
    "- Draw the mutilated graph for an intervention and write the truncated factorisation.\n",
    "- Compute P(y | do(x)) by adjusting for the parents of X, and compare it with P(y | x).\n",
    "- Prove that interventions leave non-descendants unchanged and that conditioning equals intervening when X has no parents.\n",
    "- Evaluate deterministic and stochastic policy interventions in health and economics."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Theory\n",
    "\n",
    "### Intervening on a model\n",
    "\n",
    "An **intervention** $\\operatorname{do}(X = x)$ replaces the structural equation $X := f_X(\\operatorname{pa}(X), U_X)$ by the constant $X := x$, leaving every other equation unchanged. The modified model is $\\mathcal M_x$; its graph $G_{\\overline X}$ is the **mutilated graph**, with all arrows *into* $X$ removed.\n",
    "\n",
    "The distribution of $Y$ in $\\mathcal M_x$ is written $P(y \\mid \\operatorname{do}(x))$. It answers \"what would happen to $Y$ if we *set* $X$ to $x$ for everyone?\"\n",
    "\n",
    "### Seeing versus doing\n",
    "\n",
    "$P(y \\mid x)$ describes $Y$ among units that *happen* to have $X = x$. Those units may differ in other ways (they have different parents of $X$). $P(y \\mid \\operatorname{do}(x))$ describes the whole population when $X$ is *set* to $x$. They coincide only when nothing that affects $Y$ also affects $X$.\n",
    "\n",
    "### The truncated factorisation\n",
    "\n",
    "For a Markovian model, delete the factor for $X$ and plug in $X = x$ everywhere else:\n",
    "\n",
    "$$\n",
    "P(\\mathbf v \\mid \\operatorname{do}(x)) = \\prod_{i: V_i \\ne X} P\\big(v_i \\mid \\operatorname{pa}(v_i)\\big)\\Big|_{X = x} \\quad \\text{for } \\mathbf v \\text{ consistent with } x.\n",
    "$$\n",
    "\n",
    "### Adjusting for the parents\n",
    "\n",
    "Summing the truncated factorisation over everything except $Y$ and the parents $\\mathbf{PA} = \\operatorname{pa}(X)$ gives the **adjustment formula**\n",
    "\n",
    "$$\n",
    "P(y \\mid \\operatorname{do}(x)) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})\\,P(\\mathbf{pa}).\n",
    "$$\n",
    "\n",
    "Compare with conditioning, which weights by $P(\\mathbf{pa} \\mid x)$ instead of $P(\\mathbf{pa})$:\n",
    "\n",
    "$$\n",
    "P(y \\mid x) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})\\,P(\\mathbf{pa} \\mid x).\n",
    "$$\n",
    "\n",
    "The difference is exactly confounding. (When the parents are unobserved, Days 52–56 look for other sets.)\n",
    "\n",
    "### Causal effects\n",
    "\n",
    "The **average causal effect** of a binary $X$ is $E[Y \\mid \\operatorname{do}(X = 1)] - E[Y \\mid \\operatorname{do}(X = 0)]$. It is the ATE of Phase 3: in an SCM, the potential outcome is $Y_i(x) = Y$ computed in $\\mathcal M_x$ with unit $i$'s disturbances (Day 59).\n",
    "\n",
    "### Stochastic (policy) interventions\n",
    "\n",
    "Real policies rarely force $X$ on everyone; they shift its distribution, for example a subsidy that raises vaccination rates. Replacing $P(x \\mid \\operatorname{pa})$ by a new $q(x \\mid \\operatorname{pa})$ gives\n",
    "\n",
    "$$\n",
    "P_q(y) = \\sum_{\\mathbf{pa}, x} P(y \\mid x, \\mathbf{pa})\\,q(x \\mid \\mathbf{pa})\\,P(\\mathbf{pa}).\n",
    "$$\n",
    "\n",
    "## Part 1 · Warm-up"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1 · Mutilated graphs\n",
    "\n",
    "For $Z \\to X$, $Z \\to Y$, $X \\to M \\to Y$, $W \\to X$, draw $G_{\\overline X}$ and $G_{\\overline M}$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $G_{\\overline X}$ removes arrows into $X$ ($Z \\to X$, $W \\to X$): left with $Z \\to Y$, $X \\to M \\to Y$, and $W$ isolated.\n",
    "\n",
    "**Step 2.** $G_{\\overline M}$ removes $X \\to M$: left with $Z \\to X$, $W \\to X$, $Z \\to Y$, $M \\to Y$.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2 · Truncated factorisation\n",
    "\n",
    "Write $P(z, m, y \\mid \\operatorname{do}(x))$ for the graph in Exercise 1 (ignore $W$).\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Full factorisation: $P(z)P(x \\mid z)P(m \\mid x)P(y \\mid z, m)$.\n",
    "\n",
    "**Step 2.** Drop $P(x \\mid z)$ and fix $X = x$:\n",
    "\n",
    "$$\n",
    "P(z, m, y \\mid \\operatorname{do}(x)) = P(z)\\,P(m \\mid x)\\,P(y \\mid z, m).\n",
    "$$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3 · Linear seeing versus doing\n",
    "\n",
    "$Z \\sim N(0, 1)$, $X := Z + U_X$ with $\\operatorname{Var}(U_X) = 1$, $Y := 2X + 3Z + U_Y$. Find $E[Y \\mid \\operatorname{do}(x)]$ and $E[Y \\mid X = x]$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Doing: $Z$'s equation is untouched, so $E[Y \\mid \\operatorname{do}(x)] = 2x + 3E[Z] = 2x$.\n",
    "\n",
    "**Step 2.** Seeing: $E[Z \\mid X = x] = \\dfrac{\\operatorname{Cov}(Z, X)}{\\operatorname{Var}(X)}x = \\dfrac{1}{2}x$.\n",
    "\n",
    "**Step 3.** $E[Y \\mid X = x] = 2x + 3 \\cdot \\tfrac12 x = 3.5x$.\n",
    "\n",
    "**Answer:** slope 2 for doing, 3.5 for seeing.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4 · No parents, no difference\n",
    "\n",
    "If $X$ has no parents (a coin flip), show $P(y \\mid \\operatorname{do}(x)) = P(y \\mid x)$ using the adjustment formula.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** With $\\operatorname{pa}(X) = \\varnothing$ the sum has one term: $P(y \\mid \\operatorname{do}(x)) = P(y \\mid x)$.\n",
    "\n",
    "**Answer:** randomisation makes $X$ parentless; that is why experiments identify causal effects.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 5 · Upstream variables do not move\n",
    "\n",
    "In the graph of Exercise 1, is $P(z \\mid \\operatorname{do}(x)) = P(z)$? Is $P(z \\mid x) = P(z)$?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Doing: $Z$ is not a descendant of $X$, and the truncated factorisation keeps $P(z)$. Summing out $m, y$ gives $P(z \\mid \\operatorname{do}(x)) = P(z)$.\n",
    "\n",
    "**Step 2.** Seeing: $Z \\to X$, so observing $X$ is informative about $Z$: in general $P(z \\mid x) \\ne P(z)$.\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 2 · Proofs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 6 · Truncated factorisation from the SCM\n",
    "\n",
    "Prove the truncated factorisation for a Markovian SCM.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\mathcal M_x$ is again a Markovian SCM: same equations and disturbances for all $V_i \\ne X$, and $X := x$ (a constant with no parents).\n",
    "\n",
    "**Step 2.** By Day 50 (Exercise 6), its distribution factorises over its graph $G_{\\overline X}$: $\\prod_{V_i \\ne X} P_{\\mathcal M_x}(v_i \\mid \\operatorname{pa}(v_i)) \\times \\mathbf 1[X = x]$.\n",
    "\n",
    "**Step 3.** For $V_i \\ne X$, the conditional distribution of $V_i$ given its parents comes from the same $f_i$ and $U_i$ as in $\\mathcal M$, so $P_{\\mathcal M_x}(v_i \\mid \\operatorname{pa}) = P(v_i \\mid \\operatorname{pa})$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 7 · The parent adjustment formula\n",
    "\n",
    "Derive $P(y \\mid \\operatorname{do}(x)) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})P(\\mathbf{pa})$ from the truncated factorisation.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Multiply and divide the truncated product by $P(x \\mid \\mathbf{pa})$: $P(\\mathbf v \\mid \\operatorname{do}(x)) = \\dfrac{P(\\mathbf v)}{P(x \\mid \\mathbf{pa})}$ for $\\mathbf v$ consistent with $x$.\n",
    "\n",
    "**Step 2.** Write $P(\\mathbf v) = P(\\mathbf{pa})P(x \\mid \\mathbf{pa})P(\\mathbf r \\mid x, \\mathbf{pa})$, where $\\mathbf R$ is all remaining variables, including $Y$.\n",
    "\n",
    "**Step 3.** So $P(\\mathbf v \\mid \\operatorname{do}(x)) = P(\\mathbf{pa})P(\\mathbf r \\mid x, \\mathbf{pa})$.\n",
    "\n",
    "**Step 4.** Sum over $\\mathbf{pa}$ and over all components of $\\mathbf r$ other than $y$: $P(y \\mid \\operatorname{do}(x)) = \\sum_{\\mathbf{pa}} P(\\mathbf{pa})P(y \\mid x, \\mathbf{pa})$. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 8 · Non-descendants are unaffected\n",
    "\n",
    "Prove that for any set $\\mathbf N$ of non-descendants of $X$, $P(\\mathbf n \\mid \\operatorname{do}(x)) = P(\\mathbf n)$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Order the variables topologically with all non-descendants of $X$ first. This is possible: no non-descendant has a descendant of $X$ as an ancestor.\n",
    "\n",
    "**Step 2.** The equations of non-descendants involve only other non-descendants and their own disturbances.\n",
    "\n",
    "**Step 3.** The intervention changes only $X$'s equation, and $X$ is not an input to any non-descendant's equation (directly or indirectly).\n",
    "\n",
    "**Step 4.** So the non-descendants are the same functions of the same disturbances in $\\mathcal M$ and $\\mathcal M_x$: their joint distribution is unchanged. $\\blacksquare$\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 9 · Conditional ignorability from the SCM\n",
    "\n",
    "In a Markovian SCM, prove that the potential outcome $Y(x)$ is independent of $X$ given $\\operatorname{pa}(X)$.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** In $\\mathcal M_x$, $X$'s equation is gone, so $Y(x)$ is a function of the disturbances other than $U_X$: $Y(x) = g(\\mathbf U_{-X})$.\n",
    "\n",
    "**Step 2.** $\\operatorname{pa}(X)$ is a function of the disturbances of its ancestors, also in $\\mathbf U_{-X}$.\n",
    "\n",
    "**Step 3.** $X = f_X(\\operatorname{pa}(X), U_X)$. Given $\\operatorname{pa}(X) = \\mathbf p$, $X = f_X(\\mathbf p, U_X)$ depends only on $U_X$.\n",
    "\n",
    "**Step 4.** $U_X$ is independent of $\\mathbf U_{-X}$, also after conditioning on an event defined by $\\mathbf U_{-X}$ (the event $\\operatorname{pa}(X) = \\mathbf p$).\n",
    "\n",
    "**Step 5.** So given $\\operatorname{pa}(X)$, $X$ is independent of $Y(x)$. $\\blacksquare$ This is Phase 3's unconfoundedness, derived from a graph.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 10 · Conditioning is a weighted adjustment\n",
    "\n",
    "Show that $P(y \\mid x) - P(y \\mid \\operatorname{do}(x)) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})\\big[P(\\mathbf{pa} \\mid x) - P(\\mathbf{pa})\\big]$, and give two conditions under which it is zero.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** By the law of total probability, $P(y \\mid x) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})P(\\mathbf{pa} \\mid x)$.\n",
    "\n",
    "**Step 2.** Subtract the adjustment formula term by term.\n",
    "\n",
    "**Step 3.** Zero if $P(\\mathbf{pa} \\mid x) = P(\\mathbf{pa})$ for all values ($X$ independent of its parents, as under randomisation).\n",
    "\n",
    "**Step 4.** Zero if $P(y \\mid x, \\mathbf{pa})$ does not depend on $\\mathbf{pa}$ (the parents do not affect $Y$ except through $X$). $\\blacksquare$\n",
    "\n",
    "</details>\n",
    "\n",
    "## Part 3 · Applied"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 11 · Vaccination: seeing versus doing\n",
    "\n",
    "Use Day 47's flu model: 30% are older; $P(V = 1 \\mid \\text{old}) = 0.8$, $P(V = 1 \\mid \\text{young}) = 0.4$; infection risks young unvaccinated 0.10, young vaccinated 0.04, old unvaccinated 0.30, old vaccinated 0.12. Compute $P(I = 1 \\mid \\operatorname{do}(V = 1))$ and $P(I = 1 \\mid \\operatorname{do}(V = 0))$ and compare with the observed 0.077 and 0.125.\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Age is the only parent of $V$. Adjust: $P(I = 1 \\mid \\operatorname{do}(V = 1)) = 0.7(0.04) + 0.3(0.12) = 0.028 + 0.036 = 0.064$.\n",
    "\n",
    "**Step 2.** $P(I = 1 \\mid \\operatorname{do}(V = 0)) = 0.7(0.10) + 0.3(0.30) = 0.07 + 0.09 = 0.16$.\n",
    "\n",
    "**Step 3.** Causal risk ratio $0.064/0.16 = 0.40$; risk difference $-0.096$.\n",
    "\n",
    "**Answer:** universal vaccination would cut the infection rate from 16% to 6.4%. The observed comparison (7.7% vs 12.5%, ratio 0.62) understates the benefit, because the vaccinated are older.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 12 · A vaccination subsidy\n",
    "\n",
    "A subsidy raises vaccination among the young from 40% to 70%, leaving the old at 80%. Using Exercise 11's model, what infection rate does the policy produce, compared with 10% now?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** Stochastic intervention: replace $P(V = 1 \\mid \\text{young}) = 0.4$ by $q = 0.7$.\n",
    "\n",
    "**Step 2.** Young: $0.7 \\times [0.7(0.04) + 0.3(0.10)] = 0.7 \\times 0.058 = 0.0406$.\n",
    "\n",
    "**Step 3.** Old (unchanged): $0.3 \\times [0.8(0.12) + 0.2(0.30)] = 0.3 \\times 0.156 = 0.0468$.\n",
    "\n",
    "**Step 4.** Total: $0.0406 + 0.0468 = 0.0874$.\n",
    "\n",
    "**Answer:** about 8.7% instead of 10%, a 1.3-point reduction. The policy effect depends on whom the subsidy reaches, which a single ATE does not tell you.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 13 · Fertiliser and yields\n",
    "\n",
    "On farms, rainfall $R \\sim N(0, 1)$ affects both fertiliser use $F := 0.5R + U_F$ ($\\operatorname{Var}(U_F) = 0.75$) and yields $Y := 2F + 3R + U_Y$. A regression of yields on fertiliser alone finds what slope? What is the effect of a fertiliser subsidy that raises $F$ by one unit on every farm?\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $\\operatorname{Var}(F) = 0.25 + 0.75 = 1$ and $\\operatorname{Cov}(F, R) = 0.5$.\n",
    "\n",
    "**Step 2.** Seeing: $E[Y \\mid F = f] = 2f + 3E[R \\mid F = f] = 2f + 3(0.5f) = 3.5f$. Slope 3.5.\n",
    "\n",
    "**Step 3.** Doing: $E[Y \\mid \\operatorname{do}(F = f + 1)] - E[Y \\mid \\operatorname{do}(F = f)] = 2$.\n",
    "\n",
    "**Answer:** the regression overstates the yield response by 75%, because farmers use more fertiliser in wet years, which are also good years.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 14 · Should hospitals close intensive care units?\n",
    "\n",
    "20% of patients are severe. $P(\\text{ICU} \\mid \\text{severe}) = 0.9$ and $P(\\text{ICU} \\mid \\text{mild}) = 0.1$. Death risks: mild without ICU 0.02, mild in ICU 0.01, severe without ICU 0.40, severe in ICU 0.25. Compute observed mortality with and without ICU care, and the mortality under do(ICU) and do(no ICU).\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** $P(\\text{ICU}) = 0.8(0.1) + 0.2(0.9) = 0.26$.\n",
    "\n",
    "**Step 2.** Seeing, ICU: $\\dfrac{0.8(0.1)(0.01) + 0.2(0.9)(0.25)}{0.26} = \\dfrac{0.0008 + 0.045}{0.26} \\approx 0.176$.\n",
    "\n",
    "**Step 3.** Seeing, no ICU: $\\dfrac{0.8(0.9)(0.02) + 0.2(0.1)(0.40)}{0.74} = \\dfrac{0.0144 + 0.008}{0.74} \\approx 0.030$.\n",
    "\n",
    "**Step 4.** Doing: $P(D \\mid \\operatorname{do}(\\text{ICU})) = 0.8(0.01) + 0.2(0.25) = 0.058$; $P(D \\mid \\operatorname{do}(\\text{no ICU})) = 0.8(0.02) + 0.2(0.40) = 0.096$.\n",
    "\n",
    "**Answer:** ICU patients die six times as often (17.6% vs 3.0%), yet giving everyone ICU care would cut mortality from 9.6% to 5.8%. Severity is a parent of both admission and death.\n",
    "\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Your work:*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 15 · Seeing or doing?\n",
    "\n",
    "Classify each question as a conditional (seeing) or interventional (doing) query: (a) \"What share of microfinance borrowers start a business?\"; (b) \"How much would business creation rise if every household got a loan?\"; (c) \"Do workers who join a union earn more?\"; (d) \"What would happen to wages if union membership became mandatory?\"; (e) \"Which patients with a positive test actually have the disease?\"\n",
    "\n",
    "<details><summary>Show solution</summary>\n",
    "\n",
    "**Step 1.** (a) Seeing: $P(B \\mid L = 1)$.\n",
    "\n",
    "**Step 2.** (b) Doing: $P(B \\mid \\operatorname{do}(L = 1)) - P(B)$.\n",
    "\n",
    "**Step 3.** (c) Seeing: $E[W \\mid U = 1] - E[W \\mid U = 0]$.\n",
    "\n",
    "**Step 4.** (d) Doing: $E[W \\mid \\operatorname{do}(U = 1)]$.\n",
    "\n",
    "**Step 5.** (e) Seeing: $P(D \\mid T = +)$, a diagnostic question answered by Bayes' rule (Day 2); no intervention is involved.\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",
    "# Exercises 11-12: vaccination\n",
    "pOld = .3; pV = {\"young\": .4, \"old\": .8}\n",
    "risk = {(\"young\", 0): .10, (\"young\", 1): .04, (\"old\", 0): .30, (\"old\", 1): .12}\n",
    "w = {\"young\": 1 - pOld, \"old\": pOld}\n",
    "do = {v: sum(w[a] * risk[a, v] for a in w) for v in (0, 1)}\n",
    "see = {v: sum(w[a] * (pV[a] if v else 1 - pV[a]) * risk[a, v] for a in w) / sum(w[a] * (pV[a] if v else 1 - pV[a]) for a in w) for v in (0, 1)}\n",
    "print(\"do:\", {k: round(x, 4) for k, x in do.items()}, \" see:\", {k: round(x, 4) for k, x in see.items()})\n",
    "q = {\"young\": .7, \"old\": .8}\n",
    "print(\"infection with subsidy:\", round(sum(w[a] * (q[a] * risk[a, 1] + (1 - q[a]) * risk[a, 0]) for a in w), 4))\n",
    "\n",
    "# Exercise 13: fertiliser, by simulation of the SCM and of the intervened SCM\n",
    "rng = np.random.default_rng(51); n = 1_000_000\n",
    "R = rng.normal(size=n); F = .5 * R + np.sqrt(.75) * rng.normal(size=n); Y = 2 * F + 3 * R + rng.normal(size=n)\n",
    "print(\"observational slope:\", round(np.polyfit(F, Y, 1)[0], 3))\n",
    "Y1 = 2 * (F + 1) + 3 * R + rng.normal(size=n)       # do(F := F + 1) for every farm\n",
    "print(\"effect of +1 unit:\", round((Y1 - Y).mean(), 3))\n",
    "\n",
    "# Exercise 14: ICU\n",
    "pS = .2; pICU = {0: .1, 1: .9}; d = {(0, 0): .02, (0, 1): .01, (1, 0): .40, (1, 1): .25}   # key (severe, icu)\n",
    "ws = {0: 1 - pS, 1: pS}\n",
    "for icu in (0, 1):\n",
    "    num = sum(ws[s] * (pICU[s] if icu else 1 - pICU[s]) * d[s, icu] for s in (0, 1))\n",
    "    den = sum(ws[s] * (pICU[s] if icu else 1 - pICU[s]) for s in (0, 1))\n",
    "    print(f\"ICU={icu}: seeing {num / den:.4f}  doing {sum(ws[s] * d[s, icu] for s in (0, 1)):.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key takeaways\n",
    "\n",
    "- $\\operatorname{do}(X = x)$ deletes $X$'s equation and the arrows into $X$; everything else stays the same.\n",
    "- With observed parents, $P(y \\mid \\operatorname{do}(x)) = \\sum_{\\mathbf{pa}} P(y \\mid x, \\mathbf{pa})P(\\mathbf{pa})$; conditioning instead weights by $P(\\mathbf{pa} \\mid x)$.\n",
    "- Policies are often stochastic interventions: they shift $P(x \\mid \\operatorname{pa})$ rather than fix $x$."
   ]
  }
 ]
}