LIVE·WAITING…
connecting to the live quote stream…
--:--:--UTC
#05
CAGR from an equity curve
easyFinanceacceptance —~10 min
Implement cagr(equity, periods_per_year) — the compound annual growth rate of an equity curve sampled once per period: (last / first) ** (periods_per_year / (n - 1)) - 1, where n is the number of samples.
A backtest that doubled looks different over two years than over ten. CAGR is how the portfolio page annualises a curve of any length. Return the rate rounded to 4 decimal places.
example 1
input
equity=[100.0, 101.0, 103.0, 102.5, 104.0, 106.0], periods_per_year=252
output
17.8545
example 2
input
equity=[100.0, 104.0, 103.0, 108.0, 110.0, 112.0, 115.0], periods_per_year=12
output
0.3225
constraints
- 2 ≤ n ≤ 10_000
- every value > 0
- periods_per_year ∈ {12, 52, 252}
teacher · 2 hints · the first is free
↗ read the theory
the chapter this problem is cut from
●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 cagr()
equity=[100.0, 101.0, 103.0, 102.5, 104.0, 106.0], periods_per_year=252
expected
17.8545
the judge calls cagr() 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