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

Correlation and Causation

The question that matters most.

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

Every important business question is about cause. Will this discount increase sales? Did that advert work? Would closing this cafe help? A chart can show you what happened. It can't tell you what would have happened otherwise. And that gap is where the most expensive mistakes live.

Correlation and Causation
From the video: Correlation and Causation
  • Every important question is causal
  • Charts show what happened
  • Not what would have happened

Simpson's paradox

Here's something that seems impossible. Cafe B has a better upsell rate overall. But split by day, cafe A is better on weekdays, and better on weekends too! How can A win both, yet lose overall? Run it. The reason is that B does most of its trade on weekends, when everyone upsells more. That's Simpson's paradox. The hidden variable, the type of day, flips the conclusion. So always ask what you might be missing.

simpson.pypython
import pandas as pd

df = pd.DataFrame({
    "cafe": ["A"] * 2 + ["B"] * 2,
    "weekday": ["Mon-Fri", "Weekend", "Mon-Fri", "Weekend"],
    "orders": [800, 200, 200, 800],
    "upsold": [160, 80, 30, 280],
})

overall = df.groupby("cafe")[["upsold", "orders"]].sum()
print((overall["upsold"] / overall["orders"]).round(3).to_string())
print()
print(df.assign(rate=lambda d: (d["upsold"] / d["orders"]).round(3))
        .pivot(index="cafe", columns="weekday", values="rate").to_string())
Output
cafe
A    0.24
B    0.31

weekday  Mon-Fri  Weekend
cafe
A           0.20     0.40
B           0.15     0.35

Three ways to answer a causal question

There are three main routes. Best of all is a randomised experiment. If you can't do that, control for confounders, using regression. Or look for a natural experiment, where something changed for one group, but not for another. Each step down needs stronger assumptions, so say those assumptions out loud.

Experimentthe gold standard
Controlsregression adjusts for confounders
Natural experimentlook for a lucky comparison
State assumptionsalways

Summary

Hidden variables can reverse any conclusion. And there are ways to deal with that. The most common tool in the business is regression. Next, regression as an explanation, not just a prediction.

Key takeaways
  • Causal questions are about what-if
  • Simpson's paradox flips results
  • Experiment, controls, natural experiment
Next up: Regression for Understanding — explaining, not just predicting