Skip to content
AI-grafen
DAI developerStatistics and probability· about 45 min· fundamentals that rarely change· verified 2026-10-22· EN

Correlation and causation

Be able to compute a correlation and explain why it does not show causation.

Practise in Mattegrafen ↗ · Regressionsanalys och korrelationskoeffi

See the full experiment: from hypothesis to decision

Intuition

Correlation (r between −1 and 1) measures how much two variables co-vary linearly. Ice cream sales and drownings correlate strongly — but ice cream does not cause drowning. The summer causes both: a confounder.

Other traps: reverse causation (firefighters do not cause fires, even though more firefighters = a larger fire), selection (among admitted students the entrance test correlates negatively with school grades — because both were used in the admission), chance (with 1 000 variables you always find some that correlate).

An ML model finds correlations. It knows nothing about causation. It is you who has to know that before the model is used to change anything.

Formal

The school's AI tutor: Suppose students choose between the old and new versions themselves. Forty percent of old-version users and 48% of new-version users finish a lesson. The difference is real in the observed groups, but the choice may depend on motivation. Motivation can also affect completion.

Motivation → version choice and Motivation → completion create a backdoor path. The difference of 48% minus 40% cannot therefore be attributed to the tutor alone. It measures an association between version and outcome among the students who chose each version.

To test whether the tutor causes improvement, randomly assign the version before the student uses it, use the same outcome and time window for both groups, and count students who were assigned but never started. Randomization balances possible confounders on average. You still need an interval for the observed difference; a randomized experiment can also produce an uncertain result.

Code

import numpy as np
rng = np.random.default_rng(0)

summer = rng.uniform(0, 1, 200)             # the hidden confounder
ice_cream = 100 * summer + rng.normal(0, 10, 200)
drowning = 5 * summer + rng.normal(0, 1, 200)
print(np.corrcoef(ice_cream, drowning)[0, 1])   # ≈ 0.8 — a strong correlation with no causation

# control for the summer: the residuals
res_i = ice_cream - np.polyval(np.polyfit(summer, ice_cream, 1), summer)
res_d = drowning - np.polyval(np.polyfit(summer, drowning, 1), summer)
print(np.corrcoef(res_i, res_d)[0, 1])      # ≈ 0 — the relationship disappears

To claim causation: a randomised experiment (an A/B test) or very careful control for every confounder — and even then with reservations.

Mastery means

  • Computes and interprets the Pearson correlation
  • Gives examples of a confounder and of reverse causation
  • Explains what is required to claim causation

Prerequisites

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences