Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-122

Rolling Sharpe

Problem

Compute the rolling Sharpe ratio with window size k: window_mean / window_std, using the population standard deviation.

Return an empty list if k is not positive or exceeds the series; windows with near zero standard deviation (below 1e-12) produce 0.0.

Hint

The ratio is not annualized; it is the raw reward per unit of risk in the window.

Test cases

case 1
in: [[0.02,0.01,0.03,-0.01],2]
expected: [3,2,0.4999999999999999]
case 2
in: [[0.1,0.2,0.3],3]
expected: [2.4494897427831783]
case 3
in: [[0,0,0],2]
expected: [0,0]
case 4
in: [[0.05,0.05,0.05],3]
expected: [0]
Starter codePython
def rolling_sharpe(returns, k):
    # Your code here
    pass

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