2. Cumulative simple returns. In the previous exercise, you computed the simple returns at a monthly frequency for the S&P500 index. We stored this
as a vector named sp500_simple_returns
, which has been defined for you in this exercise.
You now want to compute cumulative simple returns, starting at period 1 and extending to each period, t, in the same. Recall that you can compute them between periods 0 and t using the following expression:
\[cumulative\_return_{1t} = (1+R_{1})(1+R_{2}) \cdot \cdot \cdot (1+R_{t}) - 1\]In R
, the cumprod()
function can be used to compute the cumulative product for every observation in the sample.
sp500_simple_returns <- c(0.0028467,0.0070683,0.0504281,0.0110606,0.0359878,0.0180858,0.0207628,-0.0149993,0.0494621,
-0.031298,0.0297495,0.0445958,0.0280495,0.0235628,-0.0355829,0.043117,0.0069322,0.0062008,0.0210303,
0.0190583,-0.0150799,0.0376553,-0.0155139,0.0232015,0.0245336,-0.0041885,-0.0310408,0.0548925,-0.0173961,
0.0085208,0.0104914,-0.0210118,0.019742,-0.0625808,-0.0264428,0.0829831,0.000505,-0.0175302,-0.0507353,
-0.0041284,0.0659911,0.0026994,0.0153295,0.0009061,0.0356098,-0.0012192,-0.0012345,-0.0194256,0.0341744,
0.0182008,0.0178843,0.0371983,-0.0003892,0.0090912,0.0115762,0.0048138,0.0193488,0.0005465,0.0193029,
0.0221882,0.0280826,0.0098316,0.0561787,-0.0389474,-0.0268845,0.0027188,0.0216084,0.0048424,0.0360216,
0.0302632,0.0042943,-0.0694034,0.0178594,-0.091777,0.0786844,0.0297289,0.0179243,0.0393134,-0.0657777,
0.0689302,0.0131282,-0.0180917,0.0171812,0.0204317,0.0340471,0.0285898,-0.0016281,-0.0841105,-0.1251193,
0.1268441,0.0452818,0.0183884,0.0551013,0.0700647,-0.039228,-0.0276658,0.1075457,0.0371214,-0.0111366,
0.0260915,0.0424386,0.0524253,0.0054865,0.022214,0.0227481,0.0289903,-0.0475691,0.0691439,-0.0083337,
0.0436129,-0.0525851,-0.0313605,0.0357732,-0.0879567,0.0000532,-0.08392,0.0911163,-0.0424401)
# Add 1 to each simple return.
sp500_simple_returns <- sp500_simple_returns + 1
# Compute the cumulative product for each observation in sp500_simple_returns.
sp500_product <- ___(___)
# Subtract 1 from the cumulative products.
sp500_cumulative_return <- ___ - ___
# Plot the cumulative return.
___
# Add 1 to each simple return.
sp500_simple_returns <- sp500_simple_returns + 1
# Compute the cumulative product for each observation in sp500_simple_returns.
sp500_product <- cumprod(sp500_simple_returns)
# Subtract 1 from the cumulative products.
sp500_cumulative_return <- sp500_product - 1
# Plot the cumulative return.
plot(sp500_cumulative_return)
test_error()
test_object("sp500_product", incorrect_msg="Did you use apply the `cumprod()` function?")
test_object("sp500_cumulative_return", incorrect_msg="Remember to subtract one from the cumulative product.")
test_function("plot", incorrect_msg="Did you use the `plot()` function?")
success_msg("Excellent work.")
cumprod()
function.