Causal Inference and Confounding

Every summer, two numbers rise together: ice-cream sales and drownings. Plot one against the other and you get a gorgeous, strong correlation. Should we ban ice cream to save lives? Obviously not — a third thing, hot weather, drives both. Ice cream and drowning don't cause each other at all; they merely share a hidden common cause.

This is the deepest trap in all of data science. Models are built on correlations, but decisions require causation — and the two come apart constantly. Causal inference is the set of tools for telling them apart.

The confounder, drawn

The picture below is a causal diagram (a directed acyclic graph, or DAG): an arrow A \to B means "A has a causal effect on B." Reveal it step by step.

The weather Z is a confounder: a common cause of both the thing we're looking at X (ice-cream sales) and the outcome Y (drownings). It opens a back-door path X \leftarrow Z \to Y that makes X and Y move together even though neither causes the other. The dashed line is the correlation we observe; it is real, and it is not causal.

Two ways to break the confounding

1. Adjust for it (condition). If you compare ice-cream sales and drownings within days of the same temperature — all the 30 °C days together, all the 10 °C days together — the spurious link vanishes. Statistically this is what a regression does when you add the confounder as a control variable, or what stratifying the data by that variable does. You can only adjust for confounders you have measured — which is why the unmeasured ones are so dangerous.

2. Randomise it (intervene). If you could assign ice-cream consumption at random — coin-flip who gets ice cream regardless of the weather — then by construction the weather no longer influences X, the arrow Z \to X is severed, and any remaining association with Y must be causal. This is why the randomised controlled experiment is the gold standard: randomisation breaks every back-door path at once, even through confounders you never thought to measure.

In 1973 Berkeley was accused of admitting men at a higher rate than women. Overall, it was true — 44% of men admitted versus 35% of women. But when statisticians looked department by department, most departments actually favoured women slightly. How? Women applied in larger numbers to the most competitive departments, which admitted few of anyone. The department was a confounder, and pooling across it reversed the apparent effect. That's Simpson's paradox: an association can point one way in aggregate and the opposite way inside every subgroup. The lesson is not "always disaggregate" — it's that which variables you adjust for is a causal question you cannot answer from the numbers alone.

It is tempting to throw every available variable into a regression as a control. This is wrong, and it can create bias rather than remove it. Adjusting for a collider — a variable that is a common effect of X and Y, X \to C \leftarrow Y — opens a spurious path that was previously closed. Adjusting for a mediator (a variable on the causal path X \to M \to Y) throws away the very effect you wanted to measure. Deciding what to adjust for requires a causal diagram, not a correlation matrix. The graph comes first; the statistics follow.

Why this matters for a data scientist

Predictive models are perfectly happy with confounded correlations — a spam filter doesn't care why a word predicts spam. But the moment someone asks "what happens if we change this?" — raise the price, send the email, prescribe the drug — you have left prediction and entered causation, where correlations mislead. Recognising a confounder in a causal diagram, and knowing to reach for adjustment or randomisation, is what separates a real recommendation from a dangerous one.

Learn this on Kaggle

Kaggle Learn's Intro to AI Ethics course explores how confounding and biased data lead models to unfair and wrong conclusions, and the broader Kaggle community hosts many notebooks on A/B testing and causal analysis in Python — good places to see these ideas applied to real datasets.