October 10, 2026
Methods case study · synthetic data with a planted true effect of +5 points, so every claim below is checkable against an answer key we wrote ourselves.
State A launches a job-training program in 2020. State B does nothing. We observe county employment rates in both states, every year 2018–2022. Question: did the program raise employment? This is the bread-and-butter setup: one group gets the treatment, one doesn't, and we watch both before and after.
Obvious idea 1: look at State A before vs. after. It went 61.59 → 68.83, up 7.24 points. Program works, right? Not so fast — State B also rose, 64.34 → 67.07. Employment was climbing everywhere. Crediting all of A's rise to the program confuses the program with the passing of time [confounding by time trends].
Obvious idea 2: compare A vs. B after the program: 68.83 vs. 67.07, A wins by 1.76. But they started at different levels (61.59 vs. 64.34) — maybe A was always going to differ [baseline differences / selection bias].
Each naive comparison has exactly one hole. Difference-in-differences patches both at once.
| Before (2018–19) | After (2020–22) | Change | |
|---|---|---|---|
| State A (program) | 61.59 | 68.83 | +7.24 |
| State B (no program) | 64.34 | 67.07 | +2.73 |
State A's change minus State B's change: 7.24 − 2.73 = 4.51 points. That's the estimate.
The intuition. State B's change (+2.73) is our best guess for what would have happened in State A without the program [the counterfactual]. We can't observe that world directly — it's the road not taken — so we borrow B's journey as the stand-in. Whatever is left over after subtracting it, we attribute to the program. Two differences, hence the name.
employment ~ treated_state + post + treated_state:post
Plain English: start from a baseline, add "are you in State A?", add "is it after 2020?", and then the star of the show — the interaction [Treat × Post coefficient, β₃]: "how much extra change did the treated group get, after?" That coefficient is the 4.51 from the table. The regression just packages it with standard errors: 4.51, 95% CI (3.18, 5.84), p ≈ 0.0000000001. The planted truth was 5.0 — inside the interval.
Intuition: this whole trick works only if, without the program, State A would have kept moving in parallel with State B — the gap between them would have stayed roughly constant [parallel trends assumption]. Look at the plot: 2018–2019, both lines climb about 1 point per year, side by side. That's the assumption, made visible. If A had already been accelerating before 2020, we'd be crediting the program for a trend that was already there.
Supporting assumptions (the fine print): the program didn't leak into State B [no spillover / SUTVA] — if B's workers joined A's program, B stops being a clean stand-in. The counties are the same kind of places before and after [stable composition]. And the start date is sharp — everyone knows when 2020 began.
This study earned its place in the bank: the reusable pieces live in
toolkit/did.py — did_2x2, did_regression,
placebo_did, plot_group_trends — each verified to reproduce
the numbers on this page. The working version is shown below:
import statsmodels.formula.api as smf
# the 2x2 table of means
t = df.groupby(["treated_state", "post"])["employment"].mean().unstack()
did = (t.loc[True, True] - t.loc[True, False]) \
- (t.loc[False, True] - t.loc[False, False]) # 4.51
# the regression version: the interaction IS the DiD estimate
fit = smf.ols("employment ~ treated_state + post + treated_state:post",
data=df).fit()
fit.params["treated_state[T.True]:post[T.True]"] # 4.51
# placebo: pretend treatment began in 2019, pre-period data only
pre = df[df.year <= 2019].copy()
pre["fake_post"] = pre.year >= 2019
smf.ols("employment ~ treated_state + fake_post + treated_state:fake_post",
data=pre).fit().params["treated_state[T.True]:fake_post[T.True]"]
# 0.27, p = 0.80 -> correctly finds nothing
Full script: scripts/did_case_study.py in the project folder.
Data: data/did_synthetic.csv (80 counties × 5 years, planted effect +5).
Your call. Natural follow-ups: staggered timing (states adopt in different years), the event-study plot (effect year by year instead of one number), or applying DiD to a real dataset where the answer key doesn't exist and the checks have to carry the weight.