AI Hints
Home›Data Science Tutorial›Cause and Effect
Data Science · Lesson 24 of 101

Natural Experiments

Finding comparisons in the wild.

⏱ 2 min read▶ Video at 40:45🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

In eighteen fifty four, John Snow suspected that dirty water caused cholera. He couldn't run an experiment. But two water companies served the same streets, and one had moved its intake upstream. He compared their customers, and he proved it. That's a natural experiment, and you can use the same logic.

Natural Experiments
From the video: Natural Experiments
  • 1854: cholera in London
  • Two water companies
  • A natural experiment

Difference in differences

Oslo gets a new menu, and Bergen doesn't. Oslo's sales go up. But maybe everything rose because of the summer. So we use Bergen as a comparison. Take Oslo's change, and subtract Bergen's change. Run it. Oslo rose by one hundred and eighty, but Bergen rose by ninety anyway. So the menu's real effect is about ninety a day, half of the naive answer. This only works if both cafes would've moved in parallel without the change.

did.pypython
import pandas as pd

data = pd.DataFrame({
    "group": ["Oslo", "Oslo", "Bergen", "Bergen"],
    "period": ["Before", "After", "Before", "After"],
    "daily_sales": [1000, 1180, 900, 990],
})

t = data.pivot(index="group", columns="period", values="daily_sales")
print(t[["Before", "After"]].to_string())

change_oslo = t.loc["Oslo", "After"] - t.loc["Oslo", "Before"]
change_bergen = t.loc["Bergen", "After"] - t.loc["Bergen", "Before"]
print("Effect of the new menu:", change_oslo - change_bergen, "per day")
Output
period  Before  After
group
Bergen     900    990
Oslo      1000   1180
Effect of the new menu: 90 per day

Other natural experiments

Other designs exist. Regression discontinuity compares people just above and just below a cut-off, such as a loyalty tier. Instrumental variables use something that nudges behaviour at random. Geographic tests switch something on in some cities, and not in others. Look for these. Companies often run accidental experiments, without noticing.

Difference in differencesbefore, after, control
Regression discontinuityjust either side of a cut-off
Geo testssome cities, not others
Parallel trendsthe key assumption

Summary

When you can't experiment, find a fair comparison. You've now got the most valuable skills in data science. Next, what's coming next week? Forecasting.

Key takeaways
  • Subtract the comparison's change
  • Parallel trends assumption
  • Look for accidental experiments
Next up: Forecasting Sales — what happens next?