Skip to content
DeepForgeby svx
All problems
HardStatisticsst-300

DID Regression Coefficient

Problem

Return the coefficient on the treated-by-post interaction from an OLS regression of outcome on an intercept, treated, post, their product, and a covariate. Fit the 5-parameter model with normal equations and Gaussian elimination with partial pivoting. Return 0.0 for empty or mismatched input or a singular design.

Hint

The interaction picks up the extra change in the treated group.

Test cases

case 1
in: [[2,4,3,5,1,3,6,8],[0,0,0,0,1,1,1,1],[0,0,1,1,0,0,1,1],[1,2,1,2,1,2,1,2]]
expected: 4
case 2
in: [[1,2,4,5,3,3.5,7,8.5],[0,0,0,0,1,1,1,1],[0,0,1,1,0,0,1,1],[0.5,1.5,0.5,1.5,2.5,3.5,2.5,3.5]]
expected: 1.5
case 3
in: [[5,6,5.5,7,4,5,9,10],[0,0,0,0,1,1,1,1],[0,0,1,1,0,0,1,1],[1,1,2,2,3,3,4,4]]
expected: 0
case 4
in: [[2,3,4,5,1,3,6,8],[0,0,0,0,1,1,1,1],[0,0,1,1,0,0,1,1],[1,2,1,2,1.5,2.5,1.5,2.5]]
expected: 2.9999999999999996
Starter codePython
def did_regression_coefficient(outcome, treated, post, covariate):
    # Your code here
    pass

4 test cases · run your code against them in the editor