Cumulative Returns: Gains Multiply, Not Add

Time SeriesEasy+10 XP+10

Cumulative Returns: Gains Multiply, Not Add

You have a series of period-by-period returns and want to know how you're doing overall. The tempting move is to add them up. That's wrong, and the error compounds along with the returns.

Returns multiply. Each period scales your position by (1 + r), so after several periods:

cumulativet=[∏i≤t(1+ri)]−1cumulative_t = \left[\prod_{i \le t} (1 + r_i)\right] - 1cumulativet​=[i≤t∏​(1+ri​)]−1

Task: write cumulative_returns(returns) returning the cumulative return after each period, each rounded to 4 decimal places.

  • returns[t] is that period's return as a fraction: 0.1 is a 10% gain, -0.05 a 5% loss.
  • Keep a running growth factor starting at 1.0, multiply by (1 + r) each period, and report growth - 1.
  • The output is the same length as the input, each entry being the total return from the start up to and including that period.
  • Carry the unrounded growth factor forward and round only what you emit.

The - 1 at the end converts a factor back into a return: a growth factor of 1.21 is a 21% gain. Mixing up the two is the most common bug here — report the factor and all your numbers are off by exactly 1.

Two 10% gains make the point: additively that's 20%, but multiplicatively 1.1 × 1.1 = 1.21, a 21% gain. The extra 1% is the second period's gain on the first period's gain, and over many periods that gap becomes the whole story.

Examples

returns = [0.1, 0.1]
returns [0.1, 0.21]
returns = [0.5, -0.5]
returns [0.5, -0.25]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run