Day 67. Difference-in-Differences: the 2×2 Case
Compare how outcomes changed in a treated group with how they changed in an untreated group. Under parallel trends, the difference in changes is the effect of treatment on the treated. The estimand, its regression form, and the assumptions that make or break it.
- ●Compute the 2×2 difference-in-differences estimate from four means and from a regression with an interaction.
- ●Prove that DiD identifies the ATT under parallel trends and no anticipation.
- ●Show that parallel trends depend on the scale of the outcome (levels versus logs).
- ●Apply DiD to minimum wages, health insurance expansions and training programmes, and spot failures such as the Ashenfelter dip.
Theory
Set-up
Two groups ( treated, comparison) and two periods ( before, after). Only the treated group in period 1 receives treatment. The target is the ATT in the post period:
where is the potential outcome in period . The problem: , what would have happened to the treated without treatment, is unobserved.
Two naive comparisons
- Before–after in the treated group: , biased by anything else that changed over time.
- Treated–comparison after: , biased by permanent group differences.
Parallel trends
Assumption (parallel trends): without treatment, the average outcome would have changed by the same amount in both groups:
No anticipation: for the treated (treatment does not affect pre-period outcomes).
The DiD estimand
Under both assumptions,
The comparison group's change is the counterfactual change for the treated.
Regression form
The four cell means are , , , ; so is exactly the DiD of sample means. With panel data, regressing on gives the same number. Covariates can be added (for instance pre-treatment characteristics interacted with ), and standard errors should be clustered at the level at which treatment is assigned.
Scale matters
Parallel trends in levels and in logs cannot both hold unless baseline levels are equal or there is no trend. The choice of scale is a substantive assumption, not a technicality.
Threats
Different shocks to the two groups in the post period; selection into treatment based on transitory outcomes (the Ashenfelter dip: people join training after a temporary earnings drop, which would have recovered anyway); compositional changes in repeated cross-sections; spillovers to the comparison group.
Part 1 · Warm-up
Exercise 1 · Four means
Treated: 40 before, 52 after. Comparison: 35 before, 41 after. Compute the DiD.
Show solution
Step 1. Treated change: .
Step 2. Comparison change: .
Step 3. DiD: .
Exercise 2 · Regression coefficients
Using Exercise 1's means, give in the regression form.
Show solution
Step 1. (comparison, before).
Step 2. (group gap before).
Step 3. (comparison trend).
Step 4. , the DiD.
Exercise 3 · The naive comparisons
With Exercise 1's means, compute the before–after and post-period cross-sectional comparisons, and say what biases each.
Show solution
Step 1. Before–after: 12; includes the common trend of 6.
Step 2. Cross-section after: ; includes the permanent gap of 5.
Answer: both overstate the effect (6); DiD removes both biases under parallel trends.
Exercise 4 · Levels or logs?
Comparison: 10 → 12. Treated: 20 → 25. Compute DiD in levels and in logs. If the treated would have grown by the comparison group's percentage, what is the effect in levels?
Show solution
Step 1. Levels: .
Step 2. Logs: .
Step 3. Under proportional (log) parallel trends, the treated counterfactual is , so the effect is .
Answer: 3 under level trends, 1 under proportional trends: the scale assumption triples the estimate.
Exercise 5 · Anticipation
A tax cut announced a year ahead is evaluated with DiD using the announcement year as "before". Why might this be a problem?
Show solution
Step 1. Firms may change behaviour once the cut is announced (for example, delaying investment until it takes effect).
Step 2. Then the "before" outcome is already affected: no anticipation fails.
Answer: use a baseline before the announcement.
Part 2 · Proofs
Exercise 6 · DiD identifies the ATT
Prove that under parallel trends and no anticipation the DiD estimand equals the ATT.
Show solution
Step 1. Observed outcomes: ; (no anticipation); comparison outcomes are untreated potential outcomes.
Step 2. DiD .
Step 3. By parallel trends, replace the second term by .
Step 4. DiD .
Exercise 7 · The interaction coefficient
Prove that OLS of the saturated regression gives , with the cell means.
Show solution
Step 1. The regression has four parameters and four cells: it is saturated, so fitted values equal cell means.
Step 2. Fitted values: , , , .
Step 3. Solve: .
Exercise 8 · First differences
With a balanced panel, show that regressing on a constant and gives the DiD estimate.
Show solution
Step 1. A regression on a constant and a binary variable gives the difference in group means of the outcome.
Step 2. .
Exercise 9 · Bias from non-parallel trends
Without parallel trends, show DiD , where .
Show solution
Step 1. From Exercise 6, Step 2: DiD .
Step 2. Add and subtract : DiD .
Exercise 10 · Scale dependence
Suppose untreated outcomes grow by the same proportion in both groups and baseline means differ (). Show that parallel trends in levels fails.
Show solution
Step 1. Untreated change for group : .
Step 2. Difference between groups: .
Answer: level trends differ by , which is exactly the level-DiD bias.
Part 3 · Applied
Exercise 11 · The New Jersey minimum wage
In Card and Krueger's study, average full-time-equivalent employment per fast-food restaurant was 20.44 before and 21.03 after New Jersey raised its minimum wage, and 23.33 and 21.17 in neighbouring eastern Pennsylvania. Compute the DiD and state the identifying assumption.
Show solution
Step 1. New Jersey change: .
Step 2. Pennsylvania change: .
Step 3. DiD: .
Answer: about +2.75 FTE workers per restaurant, no evidence of job loss. Identifying assumption: without the increase, New Jersey restaurants would have followed Pennsylvania's employment trend.
Exercise 12 · Medicaid expansion and mortality
Adult mortality per 100,000 fell from 320 to 310 in states that expanded Medicaid and from 330 to 328 in states that did not. Estimate the effect and give one threat to parallel trends.
Show solution
Step 1. Expansion change: −10. Non-expansion change: −2.
Step 2. DiD: deaths per 100,000.
Step 3. Threat: expansion states may have been on a different health trajectory (different opioid epidemics, economic recoveries).
Answer: −8 per 100,000, credible only if pre-expansion mortality trends were similar (Day 68 checks this).
Exercise 13 · A pre-period placebo for bed nets
Malaria cases per 1,000 children in districts that received a bed-net campaign in 2020: 2018: 150, 2019: 140, 2021: 100. In comparison districts: 2018: 160, 2019: 155, 2021: 140. Compute a placebo DiD for 2018–2019 and the main DiD for 2019–2021. Interpret.
Show solution
Step 1. Placebo: treated change ; comparison ; placebo DiD .
Step 2. Main: treated ; comparison ; DiD .
Step 3. The placebo shows the treated districts were already improving 5 cases faster per year.
Answer: −25 per 1,000, but with a pre-trend; if the extra −5 per year continued over two years, a trend-adjusted estimate would be about (Day 68 formalises this).
Exercise 14 · Inference with two states
The New Jersey study compares restaurants in two states. Why are conventional standard errors (treating restaurants as independent) too optimistic, and what can be done?
Show solution
Step 1. Any state-specific shock in the post period hits all restaurants in that state together: errors are correlated within state.
Step 2. With one treated and one comparison cluster, cluster-robust SEs cannot be computed reliably.
Answer: use more comparison units (many states or counties), randomisation inference across placebo states, or synthetic control methods (Day 85); be explicit that with two clusters the uncertainty is understated.
Exercise 15 · The Ashenfelter dip
Trainees' earnings were $20,000 two years before training, $15,000 the year before (after job losses) and $19,000 the year after. Comparison workers earned $20,000, $19,800 and $20,000. Compute DiD using the year before as baseline, then using two years before. Which is more credible?
Show solution
Step 1. Baseline = year before: treated ; comparison ; DiD .
Step 2. Baseline = two years before: treated ; comparison ; DiD .
Step 3. The dip just before training is transitory: trainees enrolled because their earnings had temporarily fallen, and much of the rebound would have happened anyway.
Answer: the year-before baseline overstates the effect; analyses should use earlier baselines, model the dip, or compare with workers who had similar dips.
Verify in Python
import numpy as np
did = lambda t0, t1, c0, c1: (t1 - t0) - (c1 - c0)
print("Ex 1:", did(40, 52, 35, 41), " NJ:", round(did(20.44, 21.03, 23.33, 21.17), 2), " Medicaid:", did(320, 310, 330, 328))
print("logs vs levels:", did(20, 25, 10, 12), round(np.log(25 / 20) - np.log(12 / 10), 4))
print("bed nets placebo:", did(150, 140, 160, 155), " main:", did(140, 100, 155, 140))
print("Ashenfelter:", did(15_000, 19_000, 19_800, 20_000), did(20_000, 19_000, 20_000, 20_000))
# Regression form with simulated restaurants: the interaction equals the DiD of means
rng = np.random.default_rng(67)
n = 400; G = np.repeat([0, 1], n // 2)
y0 = 23 - 3 * G + rng.normal(0, 8, n) # before
y1 = y0 - 2 + 2.75 * G + rng.normal(0, 4, n) # after: common trend -2, effect 2.75
Y = np.r_[y0, y1]; T = np.r_[np.zeros(n), np.ones(n)]; GG = np.r_[G, G]
X = np.column_stack([np.ones(2 * n), GG, T, GG * T])
b = np.linalg.lstsq(X, Y, rcond=None)[0]
means = did(y0[G == 1].mean(), y1[G == 1].mean(), y0[G == 0].mean(), y1[G == 0].mean())
fd = np.polyfit(G, y1 - y0, 1)[0]
print(f"interaction {b[3]:.3f} DiD of means {means:.3f} first-difference regression {fd:.3f}")
Key takeaways
- DiD = (change for the treated) − (change for the comparison group); it identifies the ATT under parallel trends and no anticipation.
- The regression interaction, the first-difference regression and the four-means formula are the same estimate.
- Parallel trends is scale-dependent and fails with transitory pre-treatment shocks such as the Ashenfelter dip.
Simulation Lab: The 2×2 Difference-in-Differences
Open full screen →Four means, a parallel counterfactual, and what levels-versus-logs and trend violations do to the estimate.
Mean outcomes before and after a policy for the treated group (blue) and a comparison group (grey). DiD assumes the treated group would have followed the dashed path, parallel to the comparison group in levels. Choose how the untreated world really evolves (parallel in levels or proportional growth) and add a violation to see the bias.