TExcelTAD
Home › Data Science › DS Correlation vs Causality

DS Correlation vs Causality

"Correlation does not imply causation" may be the most repeated sentence in statistics — and still the most ignored. Two variables can move together without one causing the other. Mixing the two up leads to expensive mistakes.

A puzzle from Riverside Bikes

The bike shop next to the Old Bridge station sells sunglasses. The manager notices that on days with high sunglasses sales, many more bikes are rented. Should Riverside Bikes hand out free sunglasses to boost rentals?

python · try it yourself
import numpy as np
import pandas as pd

rng = np.random.default_rng(7)
temp = rng.uniform(4, 30, 60).round(1)
# both depend on temperature, NOT on each other
sunglasses = (2 + 0.8 * temp + rng.normal(0, 3, 60)).round().clip(0).astype(int)
rides = (150 + 12 * temp + rng.normal(0, 40, 60)).round().astype(int)

df = pd.DataFrame({"temp_c": temp, "sunglasses": sunglasses, "rides": rides})
print(df.head())
print()
print("corr(sunglasses, rides) =", round(df["sunglasses"].corr(df["rides"]), 2))
   temp_c  sunglasses  rides
0    20.3          16    401
1    27.3          24    481
2    24.2          20    417
3     9.9          10    264
4    11.8           8    212

corr(sunglasses, rides) = 0.88

A strong correlation — but we built this data ourselves, and sunglasses have no effect on rides in the code. Both are driven by a third variable: temperature. Sunny, warm days sell sunglasses and bring riders out.

Confounders

A variable that influences both the supposed cause and the supposed effect is called a confounder (or lurking variable):

text
            temperature
           /           \
          v             v
   sunglasses  ~~~~~~  rides        (~~~ = correlation without causation)

One way to check for confounding is to hold the confounder roughly constant and look again. Within a narrow temperature band, does the relationship survive?

python · try it yourself
import numpy as np
import pandas as pd

rng = np.random.default_rng(7)
temp = rng.uniform(4, 30, 60).round(1)
sunglasses = (2 + 0.8 * temp + rng.normal(0, 3, 60)).round().clip(0).astype(int)
rides = (150 + 12 * temp + rng.normal(0, 40, 60)).round().astype(int)
df = pd.DataFrame({"temp_c": temp, "sunglasses": sunglasses, "rides": rides})

df["temp_band"] = pd.cut(df["temp_c"], bins=[0, 13, 21, 30], labels=["cool", "mild", "warm"])
for band, part in df.groupby("temp_band", observed=True):
    r = part["sunglasses"].corr(part["rides"])
    print(f"{band:5} ({len(part):2} days): corr(sunglasses, rides) = {r:5.2f}")
cool  (21 days): corr(sunglasses, rides) =  0.25
mild  (22 days): corr(sunglasses, rides) =  0.53
warm  (17 days): corr(sunglasses, rides) =  0.56

Inside each band the correlation drops from 0.88 to between 0.25 and 0.56. It doesn't vanish completely because temperature still varies by several degrees within each band — the narrower the band, the more it shrinks. Most of the overall correlation came from temperature. In the Multiple Regression lesson you'll do this more precisely by putting the confounder into the model.

Other reasons for correlation without causation

Reason Example
Confounding Sunglasses and rides both depend on temperature
Reverse causation More rides → more stations needed. Stations don't cause riders; rider demand caused the stations
Common trend over time Rentals and the town's coffee sales both grew during spring
Chance With enough variables, some correlate by accident (see Correlation Matrix)
Selection Analysing only members, who ride more in every weather

Shared trends: a classic trap

Any two series that both grow over time will correlate, even if they have nothing to do with each other:

python · try it yourself
import numpy as np

rng = np.random.default_rng(11)
weeks = np.arange(1, 21)
bike_members = 200 + 15 * weeks + rng.normal(0, 10, 20)       # growing app sign-ups
library_loans = 900 + 22 * weeks + rng.normal(0, 25, 20)      # unrelated, also growing

print("corr of levels:  ", round(np.corrcoef(bike_members, library_loans)[0, 1], 2))
# compare week-to-week CHANGES instead of levels
print("corr of changes: ", round(np.corrcoef(np.diff(bike_members), np.diff(library_loans))[0, 1], 2))
corr of levels:   0.99
corr of changes:  -0.06

The levels correlate almost perfectly because both rise steadily. Their week-to-week changes don't move together at all. Comparing differences is a simple first defence against trend-driven correlations.

How to establish causation

  1. Randomised experiments — the gold standard. Riverside Bikes could randomly choose half of its stations to get a discount and compare rentals. Randomisation balances confounders between the groups.
  2. Control for known confounders — include them in a regression model or compare within groups.
  3. Look for a mechanism and a time order — the cause must come before the effect, and there should be a plausible "how".
  4. Replicate — does the pattern hold in other cities, other years?

Note: Regression coefficients describe associations in the data. Calling them "effects" assumes you've dealt with confounding — state that assumption openly.

Test yourself

Sunglasses sales and bike rides are strongly correlated. What is the most likely explanation?

  1. Sunglasses make people ride
  2. Riding makes people buy sunglasses
  3. Both depend on a confounder such as sunny, warm weather
  4. It's a calculation error

What is the most reliable way to show that a discount *causes* more rentals?

  1. Compute a correlation
  2. Run a randomised experiment with and without the discount
  3. Ask the manager
  4. Plot rentals over time

Two series that both grow steadily over time have r = 0.98. A good next step is to…

  1. Conclude one drives the other
  2. Correlate their period-to-period changes instead of their levels
  3. Square r
  4. Remove the first value