Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-121

Rolling Beta

Problem

Compute the rolling OLS beta of y on x with window size k: sum((x - mean_x) * (y - mean_y)) / sum((x - mean_x)^2) for each aligned window.

Return an empty list if the lengths differ or k is out of range; windows where x has zero variance produce 0.0.

Hint

Beta is the slope of the regression of y on x within each window.

Test cases

case 1
in: [[2,4,6,8],[1,2,3,4],2]
expected: [2,2,2]
case 2
in: [[1,1,1],[1,2,3],2]
expected: [0,0]
case 3
in: [[1,2,4],[1,1,1],3]
expected: [0]
case 4
in: [[3,5,6],[1,2,3],2]
expected: [2,1]
Starter codePython
def rolling_beta(y, x, k):
    # Your code here
    pass

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