Lesson 29
Difference-in-Differences and Event Studies
Big question
How can policy evaluation use changes over time when randomized assignment is unavailable?
Lesson progress
Complete checkpoints as you learn
Learning objectives
- Explain and apply treated and comparison groups.
- Explain and apply parallel trends.
- Explain and apply two-period DID.
- Explain and apply regression implementation.
- Explain and apply event studies.
- Explain and apply pre-trend diagnostics.
- Explain and apply staggered adoption caution.
- Explain and apply clustered inference.
Simple explanation
This chapter begins from the economic question rather than from software syntax. The goal is to understand what information the data can legitimately provide about difference-in-differences and event studies. Python is used as a transparent laboratory: we state the question, define the target, inspect the data, estimate the model, and then challenge the result. A recurring distinction in econometrics is the difference between describing a pattern, predicting an outcome, and estimating a causal effect. These goals can use similar equations but require different assumptions. A model may forecast well while offering little causal interpretation, and a credible causal design may deliberately use a simple estimator because identification comes from the research design rather than algorithmic complexity. Throughout the chapter, pay attention to units, timing, sample construction, and the information set available to the analyst. Economic data are produced by institutions, markets, households, and firms. They are not abstract arrays. A good empirical workflow therefore combines statistical discipline with substantive economic reasoning.
Key terms
- treated and comparison groups
- A core idea in Chapter 29 that students apply carefully in economic analysis.
- parallel trends
- A core idea in Chapter 29 that students apply carefully in economic analysis.
- two-period DID
- A core idea in Chapter 29 that students apply carefully in economic analysis.
- regression implementation
- A core idea in Chapter 29 that students apply carefully in economic analysis.
- event studies
- A core idea in Chapter 29 that students apply carefully in economic analysis.
- pre-trend diagnostics
- A core idea in Chapter 29 that students apply carefully in economic analysis.
Chapter 29 model
Interpret the expression in words and units before using it in a claim.
Example
Start with a tiny hypothetical sample that can be checked by hand. Write the unit of observation, outcome, explanatory variable or treatment, and the comparison being made. Before estimating anything, ask what would make the comparison informative and what could make it misleading. The difference in group averages is easy to compute, but whether it is a causal effect depends on how exposure was assigned and whether the groups are otherwise comparable. This distinction is the heartbeat of modern applied econometrics.
Prerequisites
- Complete the preceding course chapters or review their summaries as needed.
Full theory and examples
29.2
Economic intuition
This chapter begins from the economic question rather than from software syntax. The goal is to understand what information the data can legitimately provide about difference-in-differences and event studies. Python is used as a transparent laboratory: we state the question, define the target, inspect the data, estimate the model, and then challenge the result.
A recurring distinction in econometrics is the difference between describing a pattern, predicting an outcome, and estimating a causal effect. These goals can use similar equations but require different assumptions. A model may forecast well while offering little causal interpretation, and a credible causal design may deliberately use a simple estimator because identification comes from the research design rather than algorithmic complexity.
Throughout the chapter, pay attention to units, timing, sample construction, and the information set available to the analyst. Economic data are produced by institutions, markets, households, and firms. They are not abstract arrays. A good empirical workflow therefore combines statistical discipline with substantive economic reasoning.
29.3
A small numerical example
Start with a tiny hypothetical sample that can be checked by hand. Write the unit of observation, outcome, explanatory variable or treatment, and the comparison being made. Before estimating anything, ask what would make the comparison informative and what could make it misleading.
The difference in group averages is easy to compute, but whether it is a causal effect depends on how exposure was assigned and whether the groups are otherwise comparable. This distinction is the heartbeat of modern applied econometrics.
| Observation | X / Treatment | Outcome Y | Interpretive note |
|---|---|---|---|
| 1 | 0 | 10 | baseline |
| 2 | 0 | 12 | comparison |
| 3 | 1 | 15 | exposed |
| 4 | 1 | 17 | exposed |
29.4
Formal framework
Write the empirical model in a form that makes the target explicit. A generic conditional-mean representation is
E[Y | X] = m(X), or in a linear approximation, Y_i = β₀ + β₁X_i + u_i.
Here Y is the outcome, X denotes observed information, β parameters summarize the chosen model, and u collects other determinants of the outcome. The crucial question is not only whether β can be estimated, but what β means under the design and assumptions.
29.5
Python demonstration
import pandas as pd, statsmodels.formula.api as smf
rows=[]
for g in [0,1]:
for t in range(8):
for i in range(40): rows.append((g,t,10+.5*t+g+(3 if (g==1 and t>=4) else 0)))
df=pd.DataFrame(rows,columns=['treated','time','y']); df['post']=(df.time>=4).astype(int)
fit=smf.ols('y ~ treated * post + C(time)',data=df).fit()
print('DID',fit.params['treated:post'])
Run the code in a clean notebook and inspect both the numerical output and the data-generating assumptions. The associated notebook in the student package reproduces the example with a fixed random seed.
Figure 29.1. How can policy evaluation use changes over time when randomized assignment is unavailable?
29.6
Interpretation
Interpret results in the units of the variables. Report sign, magnitude, uncertainty, and the population or time period to which the estimate applies. If a variable is logged, categorical, interacted, standardized, or transformed, translate the coefficient accordingly rather than reading it as a raw one-unit effect.
Then separate statistical significance from economic significance. A precise estimate may be too small to matter economically; an economically important estimate may be imprecise. Both dimensions belong in the conclusion.
29.7
Assumptions, diagnostics, and threats to validity
Is the unit of observation appropriate for the question?
Are timing and information sets respected?
Could selection, confounding, measurement error, or reverse causality matter?
Does the uncertainty estimator match the sampling or dependence structure?
Would the result survive reasonable alternative specifications?
Is the task predictive, descriptive, or causal?
Visual evidence

Guided practice
- 1Modify the notebook so that one assumption is deliberately violated. Re-estimate the model, compare the result, and explain why the estimate changed. This turns diagnostics from a checklist into an experiment.
Exercises
- 1Concept: In one paragraph, distinguish association, prediction, and causation for the chapter topic.
- 2Python: Reproduce the demonstration with a different documented random seed and verify that the qualitative conclusion is stable.
- 3Applied econometrics: Replace the simulated outcome with a legally shareable economic variable and document its units, source, and sample.
- 4Interpretation: Write a 150-word results paragraph that reports magnitude and uncertainty without exaggerating the evidence.
- 5Research challenge: Propose one alternative design or robustness check that would address the most important threat to validity.
Source and downloads
Chapter 29 of Fundamentals of Python for Financial Econometrics by Mohammad Safavi, Ph.D.. The lesson is an original Ceteris Lab web adaptation of the supplied publication package.
Live Python
Difference-in-Differences and Event Studies: live Python
Difference-in-Differences and Event Studies: live Python
Stdout
Run Python to see results here.
Status / stderr
Ready to run Python in your browser.
Line-by-line guide
- Line 1Display a result so students can inspect the output.
Python walkthrough
- 1Connect the conceptual chapter to an explicit, reproducible workflow.
Live notebook
Run this lesson as a notebook
Open an editable notebook cell-by-cell, run Python in the browser, and download the `.ipynb` file for later.
Related dataset
Ceteris Lab teaching sample
Estimated time
25 to 40 min
Packages
pandas, numpy, statsmodels, patsy
Expected output
A regression or inference table with coefficients, uncertainty, and short interpretation notes.
Learning goals
- Load and inspect Ceteris Lab teaching sample.
- Run the Python cells connected to Difference-in-Differences and Event Studies.
- Interpret the output using treated and comparison groups and parallel trends.
Common errors
- File not found: check that wage_sample.csv is installed or use the course data folder.
- Package import error: use the browser notebook first, then download for local Jupyter if your local packages differ.
- Column name error: compare your variable names with the dataset variables listed for this notebook.
Dataset path helper
import pandas as pd
df = pd.read_csv("/data/wage_sample.csv")
df.head()Interactive activity
Chapter 29 interactive
Assumption stress test
What should determine the strength of an econometric claim?
Immediate feedback
Choose a decision, then test how the claim changes as evidence becomes stronger or weaker.
Try it yourself
Write one plain-English sentence explaining the main idea from this lesson.
Common mistakes
Check these before you move on.
A regression coefficient describes a pattern unless the assumptions or research design support a causal interpretation.
Quick quiz
Which statement best answers the Chapter 29 opening question: How can policy evaluation use changes over time when randomized assignment is unavailable?
Quick quiz
Which practice should be avoided when applying Difference-in-Differences and Event Studies?
Quick quiz
What is the most defensible way to interpret the Python demonstration?
Quick quiz
Why does Chapter 29 matter in an applied econometrics workflow?
Key takeaway
Difference-in-Differences and Event Studies is most useful when the economic question, statistical target, assumptions, code, and interpretation point in the same direction. Python makes the workflow reproducible; econometric reasoning determines what the output can mean.