source

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 Site
1

Seeing is not doing

prediction vs causation · A/B tests · confounding

Music 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.

People choose itUsers decide when to upgrade their phone's OS. It would hurt usersTurning off fraud checks for half of all accounts. It already happenedLast year's price change. It costs too muchHolding half of users back from a ranking model that earns more.

Five steps for any causal question

1What data do you have? A test, or logs of what happened?
2Learn how the product works from the people who built it.
3Draw what causes what.
4Show the drawing to the team and agree on it.
5Only now pick a method and estimate.

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.

2

Compare like with like

confounders · Simpson's paradox · adjustment formula · positivity

Food 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.

Bot
Human
Simple
8,000
2,000
Hard
1,000
9,000

Can't answer. The bot never got a hard ticket.

Bot
Human
Simple
9,000
1,000
Hard
0  ?
10,000

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.

3

Draw the causes before touching the data

causal graphs (DAGs) · assumptions · cycles

StreamingThe 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.

4

A great predictor can give the wrong answer

supervised learning vs causal inference · S-learner · cross-fitting

Home 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.

5

Compare people who were equally likely to get it

propensity scores · inverse probability weighting · overlap

Professional 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

Weight1 / chance of getting what they got A non-buyer with a 0.80 chance counts 5x. They stand in for the many look-alikes who bought.

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.

6

Effects travel along paths

linear regression · direct and indirect effects · mediators

Food 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.

7

Some controls make it worse

d-separation · back-door criterion · good and bad controls · colliders

App 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.

8

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

Outcome model right
Outcome model wrong
Treatment model right
Right
Right
Treatment model wrong
Right
Wrong

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.

9

Borrow a random nudge

instrumental variables · two-stage least squares · weak instruments

Workplace 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

Moves usageThe banner changes who uses huddles.RandomNothing else decides who saw it.One roadIt changes staying only through usage.

Divide the banner's effect on staying by its effect on usage

TeamsUse huddlesStay a year
Got the banner20,00035.0%80.9%
No banner20,00020.0%80.0%
Banner's effect+15.0 pts+0.9 pts
+6.0 pts0.9 / 0.15 effect of using huddles, for teams the banner moved+13.0 pts90.0% − 77.0% simple comparison of users vs non-users

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.

10

Every user has two futures

potential outcomes · no spillover (SUTVA) · counterfactuals · ladder of causation

Ride-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

+1.3trips per rider, A/B test gap
+0.6trips per rider, real gain at full launch

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.

11

When something starts at a line

regression discontinuity · synthetic control · difference-in-differences

A 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 sheet

Every method answers one question: how do you get the future you didn't see?

MethodThe ideaUse it whenBreaks whenHere
A/B testrandomized experimentA coin flip picks who gets it.You can randomize.Users affect each other.1 10
Compare within slicesadjustment, regressionHold the shared causes fixed.You measured every shared cause.One shared cause is missing.2 6
Predict both worldsS-learner, double ML, AIPWModel the outcome, flip the treatment, predict again.Many shared causes, curved effects.A missing cause, or no cross-fitting.4 8
Equal chancespropensity scoreCompare people equally likely to get it.Yes/no treatment, many features.No overlap between groups.5
Random nudgeinstrumental variableScale 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 discontinuityA cutoff splits look-alikes.A score cutoff decides who gets it.People can game the score.11
Build a stand-insynthetic controlBlend untouched units that tracked it before.One unit treated, many untouched.No blend matches the past.11
Subtract the twin's changedifference-in-differencesBefore vs after, minus a control's before vs after.Both moved in parallel before.Something else changed at the same time.11