Causal Inference Primer
Data science interview prep. Chapter order follows Ruiz de Villa, Causal Inference for Data Science (Manning, 2024). Companies are composites. All numbers are simulated.Rahul's SiteSeeing is not doing
prediction vs causation · A/B tests · confoundingMusic appUsers who make a playlist in week one are 3x as likely to still be listening at month three. Should the app push everyone to make one?
The gap we see mixes two things: what playlists do, and who makes them
Heavy listeners make playlists, and they stay anyway
A coin flip fixes this. Often you can't flip one.
Five steps for any causal question
First principleA random coin flip is the only thing that makes two groups differ in exactly one way. Without one, you have to argue that they do.
Compare like with like
confounders · Simpson's paradox · adjustment formula · positivityFood deliverySupport tickets go to a chatbot or a human agent. Bot tickets end with happier customers. Should more tickets go to the bot?
Humans win in every group, and lose overall
Hard issues go to humans, and hard issues end unhappy
You can only compare what was tried
Can answer. Both handled both kinds.
Can't answer. The bot never got a hard ticket.
No data on how the bot does with hard tickets means no answer for them. Restrict the question to simple tickets, or run a test.
First principleSplit by the shared cause. Compare inside each slice. Add the slices back up in their real proportions.
Draw the causes before touching the data
causal graphs (DAGs) · assumptions · cyclesStreamingThe Top 10 row is ranked from yesterday's plays. How many extra plays does being in the row add?
Build the drawing in five steps
A loop becomes a chain once you add time
The cost of unrolling: you need the data for every day, not one snapshot.
First principleThe drawing holds your assumptions. Each arrow you leave out is a claim that someone can check.
A great predictor can give the wrong answer
supervised learning vs causal inference · S-learner · cross-fittingHome rentalsThe marketplace wants to tell hosts what a $10 price rise does to bookings. Can it read the answer off its booking model?
Busy nights have higher prices and more bookings
3,000 simulated listing-nights. True effect of +$10: −3.0 points of occupancy.
The model with the lowest error gives the worst price effect
First-week bookings are part of the outcome. They predict it well and absorb the price effect.
Any prediction model can estimate an effect: change one input, predict againS-learner
Flexible models memorize. Fit on one half, predict the other, then swap.cross-fitting
First principlePrediction assumes the world keeps working as it did. A decision changes the world. Judge the model by the drawing, not by its error.
Compare people who were equally likely to get it
propensity scores · inverse probability weighting · overlapProfessional networkMembers who buy Premium are hired more often within 90 days. Does Premium help people get hired?
Score each member by their chance of buying, then compare within the same score
20,000 simulated members. Chance of buying fitted from job search status, seniority and connections. True effect: +4.0 points.
First principleSqueeze every feature into one number: how likely this person was to get the treatment. Fair comparisons exist only where both groups share that number.
Effects travel along paths
linear regression · direct and indirect effects · mediatorsFood deliveryCoupons bring users back to the app, and app visits turn into orders. New users get bigger coupons. How many orders does $1 of coupon buy?
The variables you add to a regression decide which question it answers
40,000 simulated users. Weekly orders and app visits. Coupon in dollars. Tenure in years.
Multiply along each path, then add the paths
First principleHold fixed what causes both the coupon and the orders. Hold fixed a step in between only if you want the effect that skips it.
Some controls make it worse
d-separation · back-door criterion · good and bad controls · collidersApp storeAmong featured apps, the ones with bigger marketing budgets have lower quality. Does marketing make apps worse?
Three shapes decide what holding a variable fixed does
Looking only at featured apps creates a link that isn't there
1,500 simulated apps. Quality and budget are unrelated. An app is featured when quality + budget is high.
Good, neutral and bad controls for "did the ad cause the purchase?"
The rule. Block every path into the ad through an arrow pointing at it. Don't hold fixed anything the ad causes, or anything both cause.back-door criterion
First principleHolding a variable fixed can cut a link or create one. The drawing tells you which.
Two models are safer than one
double machine learning · bootstrap · doubly robust (AIPW)Video appEngaged users see more ads and watch more. Engagement acts in a curve, not a line. How much does one more ad per hour cut weekly watch time?
Remove what engagement explains from both sides, then compare what's left
How sure are we? Redo it on 300 resampled datasetsbootstrap
Model who gets treated and model the outcome. One right model is enough.doubly robust
Predict the outcome, then fix its average mistake with weights from the treatment model. If either model is right, the fix lands on the truth.
First principlePredict the treatment and the outcome from the shared causes. The parts neither prediction explains are as good as random. Compare those.
Borrow a random nudge
instrumental variables · two-stage least squares · weak instrumentsWorkplace chatTeams that use huddles stay longer. Engaged teams use huddles and stay anyway, and engagement isn't measured. An old random banner test can still answer it.
The banner was random, and it works only through usage
Divide the banner's effect on staying by its effect on usage
| Teams | Use huddles | Stay a year | |
|---|---|---|---|
| Got the banner | 20,000 | 35.0% | 80.9% |
| No banner | 20,000 | 20.0% | 80.0% |
| Banner's effect | +15.0 pts | +0.9 pts |
A weak nudge gives a wild answer
95% range with 40,000 teams. The banner's effect on staying is measured to about ±0.8 points. Dividing by a small number blows that up.
First principleIf a coin flip moved the treatment a little, its effect on the outcome is that same treatment effect, shrunk by how much the flip moved the treatment. Undo the shrink.
Every user has two futures
potential outcomes · no spillover (SUTVA) · counterfactuals · ladder of causationRide-hailingSome riders got a $5 promo. What would each rider have done without it?
Trips next month, with and without the promo
Riders share drivers, so one rider's promo changes another's trips
Fix: randomize whole cities or time slots, not single riders.
Three kinds of questions, each harder than the last
First principleAn effect compares two futures for the same people. Data only ever shows one of them, so every method is a way to fill in the other.
When something starts at a line
regression discontinuity · synthetic control · difference-in-differencesA badge, a price change, a launch. Each starts at a known cutoff or date, and that line is your experiment.
Home rentalsHosts get a badge at a 4.80 rating. Just above vs just below is close to a coin flip.regression discontinuity
6,000 simulated listings. Dots are averages per 0.01 of rating. True jump: +2.5 bookings a month. Breaks if hosts can push their rating over the line.
StreamingA price rise in Canada only. Build a stand-in Canada from countries that tracked it before.synthetic control
Weekly sign-ups, indexed. Weights fitted on weeks 1 to 36 only, non-negative and summing to 100%.
Music appAudiobooks launch in the UK, not in Ireland. Subtract Ireland's change from the UK's.difference-in-differences
Monthly listening hours per user. Works only if both countries moved in parallel before the launch.
First principleA sharp line in time or score splits units that were alike a moment before. Compare across the line.
Pick a method
cheat sheetEvery method answers one question: how do you get the future you didn't see?
| Method | The idea | Use it when | Breaks when | Here |
|---|---|---|---|---|
| A/B testrandomized experiment | A coin flip picks who gets it. | You can randomize. | Users affect each other. | 1 10 |
| Compare within slicesadjustment, regression | Hold the shared causes fixed. | You measured every shared cause. | One shared cause is missing. | 2 6 |
| Predict both worldsS-learner, double ML, AIPW | Model the outcome, flip the treatment, predict again. | Many shared causes, curved effects. | A missing cause, or no cross-fitting. | 4 8 |
| Equal chancespropensity score | Compare people equally likely to get it. | Yes/no treatment, many features. | No overlap between groups. | 5 |
| Random nudgeinstrumental variable | Scale up a random nudge's effect. | Shared causes aren't measured, but a random nudge exists. | The nudge is weak, or acts on its own. | 9 |
| Just above vs just belowregression discontinuity | A cutoff splits look-alikes. | A score cutoff decides who gets it. | People can game the score. | 11 |
| Build a stand-insynthetic control | Blend untouched units that tracked it before. | One unit treated, many untouched. | No blend matches the past. | 11 |
| Subtract the twin's changedifference-in-differences | Before vs after, minus a control's before vs after. | Both moved in parallel before. | Something else changed at the same time. | 11 |