LIVE
connecting to the live quote stream…
--:--:--UTC
easyQuant○
#11

Sharpe ratio, annualised

easyQuantacceptance —~15 min

Implement sharpe(returns, rf, periods) — the annualised Sharpe ratio of a list of per-period simple returns: the mean excess return over its sample standard deviation, scaled by sqrt(periods). rf is the ANNUAL risk-free rate; convert it to a per-period rate by dividing by periods before subtracting.

It is the number every backtest is judged on, and the one most often quoted wrong. Use the sample standard deviation (statistics.stdev, n − 1 in the denominator). Return the ratio rounded to 4 decimal places.

example 1
input
returns=[0.01, -0.005, 0.012, 0.003, -0.002, 0.008], rf=0.0, periods=252
output
10.0698
example 2
input
returns=[0.01, -0.005, 0.012, 0.003, -0.002, 0.008], rf=0.05, periods=252
output
9.6087
note

Same returns, 5% annual risk-free: the ratio drops because the excess return does.

constraints
  • 2 ≤ len(returns) ≤ 10_000
  • 0 ≤ rf ≤ 0.1
  • periods ∈ {12, 52, 252}
  • returns are not all equal
teacher · 3 hints · the first is free
↗ read the theory
the chapter this problem is cut from
▶ open in the workbench
run the same math on a real tape
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to sharpe()
returns=[0.01, -0.005, 0.012, 0.003, -0.002, 0.008], rf=0.0, periods=252
expected
10.0698

the judge calls sharpe() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice