All problems
Granger Causality F from Series
Problem
Test Granger causality by fitting two OLS regressions on t = 1..n-1: the restricted model y[t] = a + b * y[t-1] and the unrestricted model y[t] = a + b * y[t-1] + c * x[t-1]. Return the F-statistic (SSE_r - SSE_u) / (SSE_u / (m - 3)) with m = n - 1 observations and three unrestricted parameters.
Return 0.0 for fewer than five points, a singular fit, or a perfect unrestricted fit with SSE_u = 0.
Hint
A large F means the lagged x variable explains residual variation that lagged y cannot.
Test cases
case 1
in: [[1,2,1,3,2,4,3,5],[5,1,4,2,6,3,7,4]]
expected: 27.867455621301787
case 2
in: [[1,2,3,4,5,6],[0,1,2,3,4,5]]
expected: 0
case 3
in: [[2,3,3,4,4,5,6,7],[1,0,2,1,3,2,4,3]]
expected: 8.34001196172248
case 4
in: [[1,2,3],[1,2,3]]
expected: 0
Starter codePython
def granger_causality_f(y, x):
# Your code here
pass4 test cases · run your code against them in the editor