CV Courseversity

Causal Inference and Counterfactual Reasoning

Introduces causal directed acyclic graphs, confounding, the do-operator for interventions, and structural counterfactuals as the formal tools for reasoning about cause and effect beyond correlation.

“A hospital deploys an AI system that notices patients who received a new treatment recover faster on average — but the sicker patients were also more likely to be given the treatment in the first place, so is the treatment actually helping, or does the correlation just reflect who got treated? And when a loan-approval model denies an applicant, can it truthfully say what would have happened had one fact about that applicant been different? Both questions require reasoning that plain statistical correlation cannot supply. This module introduces causal graphs, interventions, confounding, and counterfactuals — the formal machinery for distinguishing "what happened" from "what would have happened."”

Causal Graphs and Confounding · 15 min

Judea Pearl's 2009 survey "Causal Inference in Statistics: An Overview" frames the central idea of causal graphs plainly: in such a diagram, "arrows are drawn from (perceived) causes to their (perceived) effects, and more importantly, the absence of an arrow makes the empirical claim that Nature assigns values to one variable irrespective of another". Formally these are directed acyclic graphs (DAGs): each node is a variable, each directed edge represents a claim of direct causal influence, and the graph structure encodes a set of assumptions about which variables affect which others — assumptions that go beyond anything observed correlations alone could establish. The Stanford Encyclopedia of Philosophy's entry on causal models notes that the graph's structure connects to statistical independence through the Markov Condition and the related notion of d-separation, linking the purely causal claims encoded by the arrows to testable patterns of (conditional) independence in observed data.

A classic worked example shows why the graph matters, not just the raw correlation. Suppose a variable Z (season) causally influences both X (ice cream sales) and Y (drowning incidents) — hot weather drives both — while X does not causally influence Y at all. Observationally, X and Y will still be strongly correlated, because both rise and fall together with Z, even though buying ice cream does not cause anyone to drown. Z here is a confounder: a common cause of both the purported "treatment" and the "outcome" that creates a spurious association if ignored. Pearl's back-door criterion gives a graph-based rule for identifying which variables must be statistically adjusted for (in this case, Z) to strip out confounding and isolate whatever causal effect X may or may not actually have on Y.

This distinction is not academic hair-splitting for AI systems: any model trained on purely observational data — a hospital record, a clickstream log, a loan-approval history — risks learning confounded associations rather than genuine causal relationships, and will make systematically wrong recommendations whenever it is used to guide an intervention (recommending a treatment, a product, or a policy) rather than merely to predict an outcome that would have happened anyway regardless of the recommendation. Recognizing that "correlation is not causation" is a well-worn slogan; causal graphs are what let that slogan be turned into a precise, checkable diagram of exactly which variables must be controlled for, and why, rather than leaving the analyst to guess at which confounders might be lurking in the data. A model that merely predicts well on observational data can still be a poor guide to action, which is precisely the gap causal inference is designed to close.

Interventions and Counterfactual Reasoning · 15 min

Pearl's framework distinguishes sharply between observing a variable and intervening on it. The Stanford Encyclopedia of Philosophy's entry on causal models explains that "when intervening on a variable, one 'breaks the arrows' pointing into it," overriding whatever process normally determines that variable's value and forcing it to a chosen value instead — formalized through Pearl's do-operator, so that P(Y | do(X=x)) denotes the distribution of Y that would result from actively setting X to x, which is generally different from the merely observational P(Y | X=x). In the ice-cream/drowning example from Lesson 1, P(drownings | ice cream sales = high) is elevated because high sales are correlated with hot weather, but P(drownings | do(ice cream sales = high)) — the effect of an outside agent forcing ice cream sales up regardless of season — would show essentially no change, since forcing X does not touch the confounder Z or the causal pathway to Y.

This intervention concept underlies the average treatment effect (ATE), defined as E[Y | do(X=1)] − E[Y | do(X=0)]: the expected outcome difference from actively intervening to set treatment to "on" versus "off" for the same population, not the naive difference in observed outcomes between whoever happened to receive treatment and whoever did not. A small worked illustration: suppose four patients have treatment status and recovery scores (1=drug, score 0–10): patient 1 (treated, score 8), patient 2 (treated, score 6), patient 3 (untreated, score 4), patient 4 (untreated, score 5). The naive observed difference is 7.0 − 4.5 = 2.5, but if sicker patients were more likely to be given the drug in the first place (a confounder — severity — driving both treatment assignment and outcome), this naive gap conflates the drug's real causal effect with the fact that whoever received it differed systematically from whoever didn't, which is exactly why estimating ATE properly requires adjusting for such confounders rather than simply comparing group averages.

Counterfactual reasoning pushes one step further, asking not just about populations but about a specific individual: Pearl defines a unit-level structural counterfactual as "the value of Y in unit u, had X been x" — what would have happened to this particular patient, this particular loan applicant, had one fact about their situation been different, holding everything else about that unit fixed. The Stanford Encyclopedia entry notes that these structural counterfactuals differ from the Lewis-Stalnaker counterfactuals developed in philosophy by disallowing "backtracking" — reasoning from a changed effect back to a changed cause — since a structural counterfactual only ever propagates forward along the causal graph's arrows. This exact formalism now underlies "counterfactual explanation" methods in explainable AI, which answer a loan applicant's question "what would have to change about my application for it to be approved?" by computing precisely this kind of structural counterfactual.

Practice

Causal Graph at a Glance

Confounding: Z Causes Both X and Y Z (season) X (ice cream sales) Y (drownings) no direct arrow

Z (season) confounds the observed X–Y correlation by causing both ice cream sales and drownings, even with no direct causal arrow from X to Y.

  • In a causal DAG, the absence of an arrow is itself a claim — that one variable does not directly cause the other.
  • A confounder is a common cause of both the 'treatment' and the 'outcome,' and it can produce a strong correlation with zero true causal effect.
  • Structural counterfactuals only propagate forward along the graph's arrows, unlike some philosophical accounts of counterfactuals that allow backtracking from effect to cause.

Recall Practice

ConfoundingClick to reveal
A model observes that hospitals using a new diagnostic AI have higher patient mortality, but those hospitals also tend to treat the most severe cases. What causal-graph concept explains why this correlation might not reflect the AI's effect?
Case severity is a likely confounder — a common cause of both which hospitals adopted the AI and patient mortality — so the observed correlation may be spurious rather than reflecting a true causal effect of the AI.
do-operatorClick to reveal
How would you phrase, in do-notation, the query 'what is the effect on drownings of actively forcing ice cream sales to be high, regardless of season?'
P(drownings | do(ice cream sales = high)) — the interventional distribution of drownings under an active intervention setting ice cream sales to high, which breaks the arrow from season into ice cream sales.
ATE vs naive comparisonClick to reveal
Why can the naive difference in average outcomes between a treated and untreated group give a misleading estimate of a drug's true causal effect?
Because if treatment assignment is correlated with a confounder (such as illness severity) that also affects the outcome, the naive group difference conflates the drug's real causal effect with the systematic difference between who received treatment and who didn't; the average treatment effect must instead be estimated via the do-operator, ideally after adjusting for confounders.
Counterfactual explanationsClick to reveal
A loan-approval AI denies an applicant. How does a structural counterfactual let it answer 'what would need to change for approval'?
It computes the value the approval outcome would take for this specific applicant had one input (e.g., income) been different, holding everything else about that applicant fixed and propagating the change forward along the causal graph — exactly Pearl's definition of a unit-level structural counterfactual.

Glossary

Causal DAG
A directed acyclic graph in which each edge represents a claim of direct causal influence, and absence of an edge claims no direct causal effect.
Confounder
A variable that is a common cause of both a purported treatment and outcome, capable of producing a spurious correlation between them.
Back-Door Criterion
Pearl's graph-based rule for identifying which variables must be statistically adjusted for to remove confounding bias when estimating a causal effect.
do-operator
Notation, do(X=x), representing an active intervention that sets X to x by 'breaking' the arrows into X, as opposed to merely observing X=x.
Average Treatment Effect (ATE)
E[Y | do(X=1)] − E[Y | do(X=0)], the expected outcome difference from actively intervening to set treatment on versus off.
Structural Counterfactual
The value an outcome would have taken for a specific unit had one variable been different, propagated forward along the causal graph without backtracking.
Practical Activity

Tracing Confounding on a Supplied Causal DAG

This is a virtual, paper-based reasoning exercise using a small supplied causal DAG (three or four nodes and arrows, given as a diagram — not real patient or user data). Learners identify which variable is the confounder, state which variable(s) must be adjusted for using the back-door criterion, and reason through the difference between the observational and interventional (do-operator) queries on the supplied graph.

Ready to test yourself?

5 questions on this module.

Start Quiz