Difference-in-Differences
Background
Difference-in-Differences estimates a causal treatment effect when full randomisation isn't possible: take the change in the treatment group over time and subtract the change in the control group over the same time. Group-level baselines cancel; what remains is the treatment effect under the parallel-trends assumption.
Problem statement
Implement did_estimate(y_treat_pre, y_treat_post, y_ctrl_pre, y_ctrl_post):
Return a single float.
Input
y_treat_pre— array-like, treatment-group outcomes before the intervention.y_treat_post— array-like, treatment-group outcomes after.y_ctrl_pre— array-like, control-group outcomes before.y_ctrl_post— array-like, control-group outcomes after.
Output
Returns a Python float.
Examples
Example 1 — both groups change identically → zero treatment effect
Input: treatment: 1→2; control: 5→6
Output: 0.0
Example 2 — treatment changes more than control → positive DiD
Input: treatment: 0→3; control: 0→1
Output: 2.0
Example 3 — returns Python float
Input: any valid arrays
Output: isinstance(result, float)
Constraints
- Each input is a non-empty array-like; the function uses the mean over each group/period.
- Returns a Python
float.
Notes
- Parallel-trends assumption. DiD assumes control and treatment would have followed parallel trajectories absent the intervention. Visual inspection of pre-period trends is the standard sanity check.
- Two-way fixed effects. The regression equivalent is
Y ~ treated + post + treated:post; thetreated:postinteraction's coefficient equals DiD. Use that form when you need standard errors and covariates.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: identical changes give zero effect
- •Reference: treatment changes more gives +2
- •Sample: control changes more gives a negative effect