Instrumental Variables in Causal ML
When confounders can't be measured
Propensity score matching and regression adjustment both assume you've observed every confounder — a strong assumption that's often false. A customer's 'motivation' or 'health-consciousness' affects both whether they adopt a product and whether they succeed with it, yet rarely shows up as a column in any dataset. When confounding is driven by something unobserved, matching and weighting are powerless; you need a strategy that doesn't require measuring the confounder at all. That's what instrumental variables (IV) provide.
What makes a valid instrument
An instrument $Z$ is a variable that satisfies three conditions with respect to treatment $T$, outcome $Y$, and unobserved confounder $U$:
1. Relevance: $Z$ is correlated with treatment $T$ (it actually shifts who gets treated).
2. Exclusion restriction: $Z$ affects $Y$ *only* through its effect on $T$ — there's no direct arrow from $Z$ to $Y$.
3. Independence (exogeneity): $Z$ is not correlated with $U$, the unobserved confounder — i.e., $Z$ is 'as good as randomly assigned' with respect to whatever is confounding $T$ and $Y$.
Classic examples: a lottery that randomly assigns military draft eligibility (instrument) used to study the effect of military service (treatment) on later earnings (outcome) — the draft lottery number has no direct effect on earnings except through whether you serve. Or distance to the nearest college as an instrument for years of education when studying its effect on income. Good instruments are rare and always require a substantive argument, not just a statistical test — the exclusion restriction can never be fully verified from data alone.
U (unobserved confounder)
\ /
v v
Z --> T ------> Y
Z (instrument) affects T directly, and only reaches Y through T.
U confounds T and Y, but Z is independent of U.
This is exactly the structure that lets IV recover the causal
effect of T on Y despite never observing U.Two-stage least squares (2SLS)
The classic estimator for linear models is two-stage least squares:
Stage 1: regress treatment $T$ on the instrument $Z$ (and any exogenous controls $X$) to get predicted treatment $\hat{T} = \hat{\alpha}_0 + \hat{\alpha}_1 Z + \hat{\alpha}_2 X$. This predicted treatment captures only the portion of variation in $T$ driven by $Z$ — the 'clean,' as-if-random part, purged of whatever correlation with $U$ the raw $T$ had.
Stage 2: regress outcome $Y$ on $\hat{T}$ (and $X$) instead of the raw $T$: $Y = \beta_0 + \beta_1 \hat{T} + \beta_2 X + \varepsilon$. The coefficient $\hat{\beta}_1$ is the IV estimate of the causal effect of $T$ on $Y$.
Intuitively: instead of asking 'how does $Y$ move when $T$ moves,' which is contaminated by $U$, 2SLS asks 'how does $Y$ move when $T$ moves *for the reason $Z$ made it move*' — and since $Z$ is independent of $U$, that particular source of variation in $T$ is confounder-free.
import pandas as pd
from linearmodels.iv import IV2SLS
df = pd.read_csv("iv_data.csv")
# columns: outcome, treatment, instrument, exogenous controls x1, x2
model = IV2SLS.from_formula(
"outcome ~ 1 + x1 + x2 + [treatment ~ instrument]",
data=df,
)
results = model.fit(cov_type="robust")
print(results.summary)
# Manual two-stage version for intuition:
import statsmodels.formula.api as smf
stage1 = smf.ols("treatment ~ instrument + x1 + x2", data=df).fit()
df["treatment_hat"] = stage1.predict(df)
stage2 = smf.ols("outcome ~ treatment_hat + x1 + x2", data=df).fit()
print(stage2.params["treatment_hat"]) # IV estimate of the causal effectWeak instruments produce unreliable, biased estimates
In causal ML, IV methods combine with flexible models
What's next?
Instrumental variables round out the core toolkit for observational causal inference: adjustment and matching for when confounders are observed, IV for when they aren't but a valid instrument exists. Together with the potential-outcomes framework and survival methods for time-to-event settings, these tools form the backbone of how modern causal machine learning moves from prediction to decision-making under real-world constraints.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.