Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-277

2SLS Second Stage

Problem

Perform the second stage of two-stage least squares: fit x_hat from the first-stage OLS of x on z, then regress y on x_hat with an intercept.

Return [intercept, slope]; if the first stage is degenerate return [mean(y), 0.0], and empty input returns [0.0, 0.0].

Hint

The second stage regresses the outcome on the instrument-fitted regressor.

Test cases

case 1
in: [[1,2,3,4],[2,4,6,8],[1,3,5,7]]
expected: [-1,1]
case 2
in: [[1,2,3],[3,5,4],[1,2,4]]
expected: [-9.666666666666666,3]
case 3
in: [[2,2,2],[1,2,3],[5,6,7]]
expected: [6,0]
case 4
in: [[],[],[]]
expected: [0,0]
Starter codePython
def tsls_second_stage(z, x, y):
    # Returns [intercept, slope]
    # Your code here
    pass

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