Correlation, causation and Simpson's paradox
Before this lesson: nothing from this track; this is the first lesson. You need conditional probability (probability lesson 01), averages and correlation (foundations lesson 08), and the idea of a regression line. Time: about 70 minutes, plus the notebook.
Why this matters #
Machine learning models learn associations. A model that predicts well can still give a wrong answer to a causal question. Some examples:
- Decisions from predictions. A model of pneumonia risk learned that patients with asthma have a lower risk of death (Caruana et al. 2015). This is true in the data, because doctors send asthma patients straight to intensive care. A policy "treat asthma patients less" built on this model would kill people.
- Recommenders. A recommender shows item A and sees more clicks on it. Did showing the item cause the clicks, or did the system show it to users who would have clicked anyway?
- Medicine and policy. A hospital gives a stronger drug to sicker patients. The drug group then looks worse, even if the drug helps.
- Robustness. A model that uses a non-causal feature (a hospital's scanner type, a watermark in an image) breaks when that association changes (lesson 11).
Prediction answers "given that I see , what is ?". Causal inference answers "if I set , what happens to ?". This lesson shows why the two questions have different answers.
1. Predict first #
2. The picture #
Five reasons for one association #
Two variables and are associated (or dependent) when knowing the value of one changes what we expect of the other: depends on . A correlation is one number that measures a linear association. There are five different reasons why and can be associated:
| Source | Causal structure | Example |
|---|---|---|
| (a) causation | more fertilizer, larger crop | |
| (b) reverse causation | more crime makes a city hire more police; the data say "more police, more crime" | |
| (c) common cause (confounding) | hot weather raises ice-cream sales and drownings | |
| (d) selection on a common effect (collider, Berkson) | , we only see units with some values of | among cast actors, talent and looks are negatively related |
| (e) chance | no link | among 1,000 unrelated pairs of short series, some correlate strongly |
The arrows form a causal graph: an arrow means " is a direct cause of ". Lesson 03 makes these graphs precise. Here they are only pictures.
Switch between the five sources and press New sample a few times. All five scatter plots look alike: the correlation is about (about for selection). The small graph shows how the data were made. Arrows: aqua = causal path, magenta = a non-causal path that creates the association, violet node = common cause, yellow filled node = the variable we select on. For chance, the first sample is the strongest of 20 unrelated pairs; a new sample of the same pair loses the correlation.
Only source (a) means "changing changes ". Sources (b)–(e) give the same kind of scatter, but changing would do nothing to . The data alone cannot tell the five apart. We need knowledge about how the data were produced, or an experiment (lesson 02).
Reichenbach's common cause principle (1956) says: if and are dependent, then causes , causes , or they have a common cause. The principle assumes that the data are a fair sample of the population (no selection) and that the dependence is real (not chance). Sources (d) and (e) are exactly the cases where these two assumptions fail.
People who carry a lighter get lung cancer more often than people who do not. Which explanation fits best?
Smoking is a common cause: smokers carry lighters, and smoking causes lung cancer. So the two are associated, but taking away the lighter would not lower the risk. The association is strong and appears in every large dataset, so chance does not explain it. Reverse causation is possible in principle, but the common cause is the standard explanation.
Confounding: a common cause tilts the cloud #
Now take a common cause that affects both a "treatment" and an "outcome" , and let also have a real effect on . A small structural causal model (a list of equations, one per variable, each computed from its causes and some noise) is
with independent noises . The sign means "is computed from", not just "equals". The causal effect of on is : if we raise by 1 and keep everything else fixed, rises by . A variable such as , which affects both the treatment and the outcome, is a confounder.
Move the three sliders. The magenta line is the slope of the pooled regression of on ; the aqua dashed line has the true causal slope . Points are violet, darker for larger . Press Hold Z fixed: the yellow lines are the fitted lines at (regression of on and ), and only points in thin slices of stay bright. Set ("no causal effect"): the pooled slope stays positive. Set : the pooled slope equals .
With , , the pooled slope is about , twice the causal effect. Inside a thin slice of , the variation in no longer comes from , and the slope is the causal . In section 3 we prove the formula for the pooled slope.
Simpson's paradox with real numbers #
A famous study compared two treatments for kidney stones (Charig et al. 1986). Treatment A is open surgery; treatment B is percutaneous nephrolithotomy (PCNL), a keyhole operation. "Success" means the stone was removed. Here are the counts:
| small stones | large stones | all patients | |
|---|---|---|---|
| A (open surgery) | 81 / 87 = 93.1% | 192 / 263 = 73.0% | 273 / 350 = 78.0% |
| B (PCNL) | 234 / 270 = 86.7% | 55 / 80 = 68.8% | 289 / 350 = 82.6% |
A is better for small stones and better for large stones. Pooled, B looks better. This is Simpson's paradox: an association has one direction in every group and the opposite direction when the groups are pooled.
The reason is in the case mix. Large stones are harder to remove. Of the 350 patients who got A, 263 (75.1%) had large stones. Of the 350 patients who got B, only 80 (22.9%) had large stones. So A got the hard cases.
Move the sliders to change the share of large stones among the patients of A (blue) and B (orange). Each line joins a treatment's small-stone rate (left end) and its large-stone rate (right end). The big dot is the pooled rate: it slides along the line as the case mix changes. The aqua rings are the standardized rates: both treatments evaluated on the same stone mix. Find the share for A at which the pooled ranking flips.
The blue line lies above the orange line everywhere: for any fixed case mix, A is better. The pooled comparison puts A's dot far to the right (many hard cases) and B's dot far to the left. The aqua rings compare the two at the same mix, and A wins: 83.3% against 77.9%.
In the figure, B's patients have 23% large stones. The pooled rate of A is , where is A's share of large stones. About where does A's pooled rate fall below B's pooled rate of ?
Solve : . Above about 52% large stones, A's pooled rate is lower than B's, although A is better in both groups. At the same mix as B (23%), A wins in the pooled table too. The real study (75%) is far past the flip point.
3. Definitions and main results #
Association and causal effect #
Let be a treatment (something we could change: a drug, a discount, a feature shown to a user) and an outcome.
- and are associated if the distribution of differs between groups with different values of . For a binary , the associational difference is (or the difference in means).
- has a causal effect on if changing , with nothing else changed by us, changes the distribution of . Pearl writes the distribution of when we set to as . The average causal effect of a binary treatment is .
"Seeing " and "doing " are different. Seeing selects a subgroup of units that chose or were given , with all their other differences. Doing gives to everyone, whatever their other properties. Lesson 02 defines causal effects with potential outcomes; lesson 04 defines the do-operator. In this lesson the intuitive meaning is enough.
A confounder is a variable that causes both the treatment and the outcome (more precisely: it opens a non-causal path between them; lesson 03). Confounding is the difference between the association and the causal effect that such variables create. Confounding is a property of the causal structure. It is not a property of the numbers.
The pooled slope under confounding #
Proposition. In the linear model , with and independent , the slope of the population regression of on is
Proof.
- The slope of the least-squares line of on is (probability lesson 13).
- , because and are independent.
- .
- .
- So . Divide by step 2.
The second term, , is the confounding bias. It is zero when ( does not affect ) or ( does not affect ). With , , the pooled slope is . With it is : a positive slope with no causal effect at all.
The regression of on and has coefficient exactly on , because (the noise is independent of and ). This is "adjusting for ". It works here because is a confounder and is measured. Section 7 shows that adjusting for the wrong variable can create bias.
Simpson's paradox: why the pooled rate can flip #
Take a binary treatment ( or ), a binary outcome (success or not) and a grouping variable (small or large stone). For treatment write for the success rates in the two groups and for the share of large stones among A's patients. Define in the same way.
Lemma (pooled rate = weighted average). The pooled success rate of is
Proof. By the law of total probability, . The four factors are , , , .
So the pooled rate lies on the straight line from (all small) to (all large). This is the line in the figure. For the kidney data: and .
Proposition (no reversal with the same mix). If and , and both treatments have the same case mix , then .
Proof. . Both differences are positive and both weights are non-negative and sum to 1, so the sum is positive.
So Simpson's reversal needs a different case mix in the two groups, and the grouping variable must affect the outcome (). In the kidney data both hold: stone size affects the choice of treatment (doctors chose open surgery for large stones) and the success.
Standardization. To compare the treatments on the same mix, use the stone mix of all patients, :
If stone size is the only common cause of treatment choice and success, these standardized rates are the success rates we would see if everyone got A, or everyone got B. This formula is the adjustment formula; lesson 04 proves when it is valid (the backdoor criterion).
A table: treatment A cures 20 of 25 mild cases (80%) and 30 of 75 severe cases (40%). Treatment B cures 60 of 80 mild cases (75%) and 5 of 20 severe cases (25%). What are the pooled cure rates, and is there a Simpson's reversal?
Pooled A . Pooled B . Within groups, A is better (80% > 75%, 40% > 25%). So the direction flips: Simpson's reversal. The cause is the case mix: 75% of A's patients are severe, only 20% of B's.
The same table, two answers #
Simpson's paradox is not a puzzle about arithmetic. The arithmetic is clear. The question is which table answers the causal question: the grouped one or the pooled one? The data cannot decide. Look at two stories with exactly the same numbers:
- Story 1 (kidney stones). Stone size is known before the treatment. It affects the choice of treatment and the success: , and . Stone size is a confounder. The grouped table (or the standardized rates) answers the causal question: A is better.
- Story 2 (blood pressure, Pearl). Replace "stone size" by "blood pressure after the treatment" (low or high). The drug lowers blood pressure, and lower blood pressure helps recovery: , and . Now is on the causal path from to ; it is a mediator. Splitting by removes part of the drug's effect: we would compare patients whose blood pressure the drug has already changed. The pooled table answers the causal question: B is better.
A second classic: Berkeley admissions, 1973. In the graduate admissions of UC Berkeley in fall 1973, 44% of the men and 35% of the women who applied were admitted (Bickel, Hammel and O'Connell 1975). Within most departments, the gap was small or went the other way. The reason was that women applied more often to departments with low admission rates for everyone. So the pooled gap mainly reflects the choice of department. Two cautions: the department analysis answers the question "is there a bias at the department level?"; and it can have its own confounders (for example, if ability affects both the choice of department and admission). The causal question must be stated first.
A study of a new drug records "blood pressure one week after the start of treatment" and "recovery". The drug lowers blood pressure. To estimate the total effect of the drug on recovery, should you compare treated and untreated patients within groups of equal one-week blood pressure?
A confounder must be a cause of the treatment, so it is measured before the treatment. One-week blood pressure is measured after the treatment and is changed by it. It lies on the path drug → blood pressure → recovery. Comparing patients with the same blood pressure blocks this path, so you no longer estimate the total effect. In this story, use the pooled comparison (in a randomized trial) instead.
Simpson's paradox with continuous variables, and Lord's paradox
The same reversal happens with slopes. Suppose older people exercise more and also have higher cholesterol. Within each age group, more exercise goes with lower cholesterol (a negative slope). Pooled over all ages, the cloud of points can have a positive slope, because the older groups sit to the right (more exercise) and higher up (more cholesterol). This is the confounding picture of the widget above, with age as : the pooled slope can have a different sign from . In the widget, set , , : the pooled slope is .
Lord's paradox (Lord 1967) is a related puzzle. Two dining halls; students are weighed in September and in June. Statistician 1 compares the mean weight change in the two halls and finds no difference. Statistician 2 regresses June weight on hall and September weight and finds a difference. Both analyses are correct arithmetic; they answer different causal questions. Pearl (2016) shows that a causal graph tells which question each analysis answers.
Pearl's ladder of causation #
Pearl (Pearl and Mackenzie 2018) sorts questions into three levels, the ladder of causation:
| Rung | Activity | Question | Notation | Example from ML |
|---|---|---|---|---|
| 1. Association | seeing | What does seeing tell me about ? | a churn model ranks users by risk | |
| 2. Intervention | doing | What happens to if I set ? | an A/B test of a discount | |
| 3. Counterfactual | imagining | What would have been for this unit if had been different? | would this user have stayed without the discount she got? |
In , the unit really had and ; is its outcome in the imagined world where . A question on a higher rung cannot be answered from data of a lower rung alone: we need extra assumptions or extra data (an experiment, a causal graph, a full structural model).
Click a rung to see its question, its notation, an example from machine learning, what it needs beyond data, and the lessons of this track that teach it. Supervised learning lives on rung 1. Most of this track (lessons 02–08, 10, 11) is about rung 2; lesson 09 climbs to rung 3.
A doctor asks: "This patient took the drug and recovered. Would she have recovered without the drug?" Which rung of the ladder is this?
The question is about one unit and conditions on what really happened (drug taken, recovered), then imagines a different treatment. That is , a counterfactual. Rung 2 asks about the average result of setting the treatment in a population, without conditioning on the observed outcome.
Answers to section 1. (1) Yes. This is Simpson's paradox. In the kidney data A wins in both groups (93.1% vs 86.7%, 73.0% vs 68.8%) but B wins pooled (82.6% vs 78.0%), because 75% of A's patients had large stones. (2) Positive. Hot weather is a common cause, so the pooled slope is when heat raises both. (3) Yes, negatively. Selection on a high sum of two independent variables makes them negatively correlated among the selected (about when we keep the top half of , about for the top 10%). This is Berkson's paradox.
4. Worked example #
(a) Kidney stones, step by step. Start from the counts in section 2.
- Within-group rates. A: , . B: , . A is better in both groups: points for small stones, points for large stones.
- Case mix. Share of large stones: A , B .
- Pooled rates as weighted averages: (check: ); (check: ). B wins by points: a reversal.
- Common mix. All 700 patients: small and large, so , .
- Standardized rates: ; . A wins by points.
- Decision. If stone size is the main common cause of choice and success (story 1), recommend A. The pooled comparison mixes the effect of the treatment with the effect of stone size.
(b) The pooled slope. Take , , : , . has no effect on .
- .
- .
- Pooled slope . Causal effect . The whole slope is confounding bias: .
- Correlation: , so . A strong correlation with zero causal effect.
5. In code #
NumPy checks both parts of the worked example. The notebook for this lesson has these cells and the exercises.
import numpy as np
# Kidney stones (Charig et al. 1986): successes and patients
# small large
succ = np.array([[ 81, 192], # A: open surgery
[234, 55]]) # B: PCNL
n = np.array([[ 87, 263],
[270, 80]])
within = succ / n
pooled = succ.sum(1) / n.sum(1)
p_large = n[:, 1].sum() / n.sum() # stone mix of all 700 patients
standard = within[:, 0] * (1 - p_large) + within[:, 1] * p_large
print(within.round(3)) # [[0.931 0.73 ] [0.867 0.688]]
print(pooled.round(3)) # [0.78 0.826]
print(standard.round(3)) # [0.833 0.779]
# Confounding: Z -> T, Z -> Y, T -> Y with a true effect of 1
rng = np.random.default_rng(0)
N = 100_000
Z = rng.normal(size=N)
T = Z + rng.normal(size=N)
Y = 1.0 * T + 2.0 * Z + rng.normal(size=N)
pooled_slope = np.polyfit(T, Y, 1)[0]
X = np.column_stack([np.ones(N), T, Z])
adjusted = np.linalg.lstsq(X, Y, rcond=None)[0][1]
print(round(pooled_slope, 2), round(adjusted, 2)) # 2.0 1.0
The simulation knows the true effect (1), so we can check each estimate against the truth. Every lesson of this track works this way.
6. Where it shows up #
- Healthcare models. The pneumonia model of Caruana et al. (2015) learned "asthma lowers the risk of death" from a real association created by treatment policy. Interpretable models made the problem visible. Lesson 11 returns to such shortcut features.
- A/B tests and observational data in industry. Product teams see that "users who use feature F stay longer". Active users both use F and stay (a common cause). An experiment that turns F on at random answers the causal question (lesson 02).
- Recommenders and feedback loops. Logged clicks depend on what the system chose to show. A model trained on these logs learns the old policy's choices as well as user taste (lesson 11: off-policy evaluation).
- Selection bias in datasets. Hospital data contain only admitted patients; this can create associations between diseases that do not exist in the population (Berkson 1946). Datasets of "published papers" or "approved loans" have the same problem.
- Science. Epidemiology, economics and climate attribution all ask rung-2 questions from data that were not randomized. ML for science models (for example, emulators of physical systems) meet the same issue when they are used to predict the result of an intervention.
7. Common mistakes #
- "Correlation implies causation." A correlation can come from causation, reverse causation, a common cause, selection or chance. Check: write down the possible causal graphs before you interpret an association.
- "Correlation does not imply causation, so we can learn nothing." Also wrong. With an experiment, or with stated assumptions (a causal graph) and the right adjustment, data do answer causal questions. That is what this track teaches.
- "Always control for every variable." Splitting by a mediator (story 2) or by a common effect (Berkson) creates bias. Check: only adjust for variables measured before the treatment that are not caused by it; lessons 03–04 give the exact rule.
- "The pooled table is always right" or "the grouped table is always right". Neither. The causal story decides. Check: ask whether the grouping variable is a cause of the treatment (use groups) or an effect of it (use the pooled data).
- Forgetting selection. If your data contain only selected units (admitted, hired, published, clicked), an association may come from the selection. Check: ask how a unit got into the data.
- Forgetting multiple comparisons. Among 1,000 pairs of unrelated series of length 20, about 50 will have (the two-sided 5% critical value for ). Check: count how many pairs were tested, and test the finding again on new data.
8. Check yourself #
- Give an example of reverse causation from your own work or field.
Answer
Many answers are possible. Example: "users who see more ads buy more" when the ad system shows more ads to users who are already about to buy. Purchases (or the intent to buy) cause the ads, not the other way around. - In the model , (unit-variance, independent
), compute the pooled regression slope of on .
Answer
. The confounder hides most of the positive effect. - Treatment A: 90 successes in 100 easy cases and 30 in 100 hard cases. Treatment B: 160 successes
in 200 easy cases, and B was never used on hard cases. Compute the pooled rates. Which treatment
is better for easy cases?
Answer
Pooled A , pooled B . For easy cases A is better (90% vs 80%). B was never used on hard cases, so the data say nothing about B for hard cases: we cannot standardize B to a mix that contains hard cases. (This lack of overlap is called a positivity problem; lesson 02.) - Why can a reversal not happen when both treatments have the same case mix?
Answer
Then , a weighted average of the within-group differences with the same weights. If both differences are positive, the average is positive. - A dataset contains only students admitted to a selective university, which admits on grades plus
test score. Grades and test score are negatively correlated in the dataset. Does this mean that
good grades hurt test scores?
Answer
No. Admission is a common effect of grades and test score, and the data are selected on it (Berkson's paradox). A student with low grades was admitted only with a high test score, and the other way around. This creates a negative correlation even if the two are unrelated, or positively related, in all applicants. - Which rung of the ladder is "If we raise the price by 5% for all users, how many will cancel?"
Answer
Rung 2 (intervention): . It is about the average result of an action in the population, not about one user's past.
What is the difference between an association and a causal effect of on ?
An association means that differs between units that have different (seeing). A causal effect means that changes when we set to a different value (doing), .
Name the five sources of an association between and .
Causation , reverse causation , a common cause , selection on a common effect , and chance.
What is a confounder?
A variable that causes both the treatment and the outcome, so it creates an association between them that is not causal.
In , (unit variances), what is the pooled slope of on ?
. The second term is the confounding bias.
What is Simpson's paradox?
An association has the same direction in every subgroup but the opposite direction when the subgroups are pooled.
Why does B look better than A in the pooled kidney-stone data although A is better for both stone sizes?
A was given mostly large (hard) stones: 75% of A's patients against 23% of B's. The pooled rate is a weighted average that puts most of A's weight on the hard group.
When is the pooled table the right one to answer a causal question, even if group tables exist?
When the grouping variable is caused by the treatment (a mediator, such as blood pressure after the drug). Splitting by it removes part of the effect.
State Berkson's paradox in one sentence.
Selecting units on a common effect of two independent variables (for example, admission on grades plus test score) makes the two variables dependent, usually negatively, among the selected units.
Give the three rungs of Pearl's ladder with their notation.
Association ; intervention ; counterfactual .
What does Reichenbach's common cause principle say?
If two variables are dependent, then one causes the other or they have a common cause (assuming no selection and a real, not chance, dependence).
9. Interview angle #
- "Correlation is not causation. Explain with an example." — Give the five sources, with one example each; say that only an experiment or explicit causal assumptions separate them.
- "Explain Simpson's paradox." — Use the kidney-stone numbers. Pooled rates are weighted averages; the case mix differs; standardization fixes the comparison if the grouping variable is a confounder. Then add the blood-pressure story: the causal graph decides.
- "Users who use feature F have higher retention. Should we push F to everyone?" — Not from this data alone. Engaged users both use F and stay (common cause). Run an experiment (randomize F or an encouragement to use it), or adjust for pre-treatment engagement with care. Link interview lesson 09.
- "Your model says asthma lowers pneumonia mortality. What happened?" — The data reflect treatment policy: asthma patients get intensive care. The model is fine for prediction under the same policy and wrong for decisions.
10. Practice #
The notebook for this lesson has five exercises, each with a check cell and a folded solution:
simpson_table(succ, n): within-group, pooled and standardized rates, and a flag for a reversal; check it on the kidney-stone data.simulate(kind, n, seed)for the five sources of association; check that each gives and see which one disappears with more data.- Confounding: pooled slope, slope within thin bins of , and the regression on , against the true effect.
- Berkson: simulate talent and looks, keep the top 10% of the sum, and compute the correlation (about ).
- Multiple comparisons: 1,000 pairs of unrelated series of length 20; count how many have (about 5%).
11. Watch and read #
- Judea Pearl, Madelyn Glymour and Nicholas Jewell, Causal Inference in Statistics: A Primer (Wiley 2016), chapter 1 (Simpson's paradox, the blood-pressure story); companion site: bayes.cs.ucla.edu/PRIMER.
- Miguel Hernán and James Robins, Causal Inference: What If, free book, chapter 1.
- Judea Pearl, "Understanding Simpson's paradox" (The American Statistician, 2014), free PDF.
- Judea Pearl, "The seven tools of causal inference, with reflections on machine learning" (Communications of the ACM, 2019): the ladder of causation, free PDF.
- Brady Neal, Introduction to Causal Inference, course notes and lectures, chapter 1.
- Rich Caruana et al., "Intelligible models for healthcare" (KDD 2015): the asthma example.
- Original papers (not free): C. R. Charig et al., "Comparison of treatment of renal calculi by open surgery, percutaneous nephrolithotomy, and extracorporeal shockwave lithotripsy", BMJ 1986; P. J. Bickel, E. A. Hammel and J. W. O'Connell, "Sex bias in graduate admissions: data from Berkeley", Science 1975.
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.