# Chapter 29: Difference-in-Differences and Event Studies
# Fundamentals of Python for Financial Econometrics - Difference-in-Differences and Event Studies
# Dataset: Ceteris Lab teaching sample

# Chapter 29: Difference-in-Differences and Event Studies
# **Economic question:** How can policy evaluation use changes over time when randomized assignment is unavailable?
#
# Difference-in-differences identifies a treatment effect from differential changes under a parallel-trends assumption.

# %% Cell 2
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'])

# Interpretation checklist
# - State the unit of observation and units of every variable.
# - Separate association, prediction, and causation.
# - Report magnitude and uncertainty.
# - Identify the most important threat to validity.
