All problems
IV First Stage Coefficients
Problem
Fit the first stage of instrumental variables by OLS of the endogenous regressor x on the instrument z with an intercept.
Return [intercept, slope]; if z is constant return [mean(x), 0.0], and empty input returns [0.0, 0.0].
Hint
The instrument must move the endogenous regressor for 2SLS to work.
Test cases
case 1
in: [[1,2,3,4],[2,4,6,8]]
expected: [0,2]
case 2
in: [[1,2,3],[3,5,4]]
expected: [3,0.5]
case 3
in: [[2,2,2],[1,2,3]]
expected: [2,0]
case 4
in: [[],[]]
expected: [0,0]
Starter codePython
def iv_first_stage(z, x):
# Returns [intercept, slope]
# Your code here
pass4 test cases · run your code against them in the editor