All problems
Rolling Correlation
Problem
Compute the rolling Pearson correlation between x and y with window size k for each aligned window.
Return an empty list if the lengths differ or k is out of range; windows with zero variance in either series produce 0.0.
Hint
The result lies in [-1, 1]; a flat window cannot correlate with anything.
Test cases
case 1
in: [[1,2,3,4,5],[2,4,6,8,10],3]
expected: [0.9999999999999998,0.9999999999999998,0.9999999999999998]
case 2
in: [[1,2,3,4],[4,3,2,1],2]
expected: [-0.9999999999999998,-0.9999999999999998,-0.9999999999999998]
case 3
in: [[1,1,1,1],[1,2,3,4],2]
expected: [0,0,0]
case 4
in: [[1,2,3],[1,2,3],3]
expected: [0.9999999999999998]
Starter codePython
def rolling_correlation(x, y, k):
# Your code here
pass4 test cases · run your code against them in the editor