1. Simple returns. You are given a data frame, stock_prices
, which contains the end-of-month levels of the S&P500
(sp500
) index for the period between 2012 and 2022. You want to compute
the monthly simple returns for both indices, which are defined as follows:
\[simple\_return_{t} = \frac{p_{t}}{p_{t-1}} - 1\]
For a vector X
, we can compute the ratio of by constructing two subsets and applying
the division operator: X_ratio = X[2:length(X)]/X[1:length(X)-1]
, where length()
returns the number of elements in a vector.
date <- c("2012-10-01","2012-11-01","2012-12-01","2013-01-01","2013-02-01","2013-03-01","2013-04-01",
"2013-05-01","2013-06-01","2013-07-01","2013-08-01","2013-09-01","2013-10-01","2013-11-01","2013-12-01",
"2014-01-01","2014-02-01","2014-03-01","2014-04-01","2014-05-01","2014-06-01","2014-07-01","2014-08-01",
"2014-09-01","2014-10-01","2014-11-01","2014-12-01","2015-01-01","2015-02-01","2015-03-01","2015-04-01",
"2015-05-01","2015-06-01","2015-07-01","2015-08-01","2015-09-01","2015-10-01","2015-11-01","2015-12-01",
"2016-01-01","2016-02-01","2016-03-01","2016-04-01","2016-05-01","2016-06-01","2016-07-01","2016-08-01",
"2016-09-01","2016-10-01","2016-11-01","2016-12-01","2017-01-01","2017-02-01","2017-03-01","2017-04-01",
"2017-05-01","2017-06-01","2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01","2017-12-01",
"2018-01-01","2018-02-01","2018-03-01","2018-04-01","2018-05-01","2018-06-01","2018-07-01","2018-08-01",
"2018-09-01","2018-10-01","2018-11-01","2018-12-01","2019-01-01","2019-02-01","2019-03-01","2019-04-01",
"2019-05-01","2019-06-01","2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01",
"2020-01-01","2020-02-01","2020-03-01","2020-04-01","2020-05-01","2020-06-01","2020-07-01","2020-08-01",
"2020-09-01","2020-10-01","2020-11-01","2020-12-01","2021-01-01","2021-02-01","2021-03-01","2021-04-01",
"2021-05-01","2021-06-01","2021-07-01","2021-08-01","2021-09-01","2021-10-01","2021-11-01","2021-12-01",
"2022-01-01","2022-02-01","2022-03-01","2022-04-01","2022-05-01","2022-06-01","2022-07-01","2022-08-01")
date <- as.Date(date)
nikkei22 <- c(8928.29,9446.01,10395.18,11138.66,11559.36,12397.91,13860.86,13774.54,13677.32,13668.32,
13388.86,14455.8,14327.94,15661.87,16291.31,14914.53,14841.07,14827.83,14304.11,14632.38,15162.1,15620.77,
15424.59,16173.52,16413.76,17459.85,17450.77,17674.39,18797.94,19206.99,19520.01,20563.15,20235.73,20585.24,
18890.48,17388.15,19083.1,19747.47,19033.71,17518.3,16026.76,16758.67,16666.05,17234.98,15575.92,16569.27,
16887.4,16449.84,17425.02,18308.48,19114.37,19041.34,19118.99,18909.26,19196.74,19650.57,20033.43,19925.18,
19646.24,20356.28,22011.61,22724.96,22764.94,23098.29,22068.24,21454.3,22467.87,22201.82,22304.51,22553.72,
22865.15,24120.04,21920.46,22351.06,20014.77,20773.49,21385.16,21205.81,22258.73,20601.19,21275.92,21521.53,
20704.37,21755.84,22927.04,23293.91,23656.62,23205.18,21142.96,18917.01,20193.69,21877.89,22288.14,21710,
23139.76,23185.12,22977.13,26433.62,27444.17,27663.39,28966.01,29178.8,28812.63,28860.08,28791.53,27283.59,
28089.54,29452.66,28892.69,27821.76,28791.71,27001.98,26526.82,27821.43,26847.9,27279.8,26393.04,27801.64,28091.53)
sp500 <- c(1412.16,1416.18,1426.19,1498.11,1514.68,1569.19,1597.57,1630.74,1606.28,1685.73,1632.97,1681.55,1756.54,1805.81,
1848.36,1782.59,1859.45,1872.34,1883.95,1923.57,1960.23,1930.67,2003.37,1972.29,2018.05,2067.56,2058.9,1994.99,2104.5,
2067.89,2085.51,2107.39,2063.11,2103.84,1972.18,1920.03,2079.36,2080.41,2043.94,1940.24,1932.23,2059.74,2065.3,2096.96,
2098.86,2173.6,2170.95,2168.27,2126.15,2198.81,2238.83,2278.87,2363.64,2362.72,2384.2,2411.8,2423.41,2470.3,2471.65,
2519.36,2575.26,2647.58,2673.61,2823.81,2713.83,2640.87,2648.05,2705.27,2718.37,2816.29,2901.52,2913.98,2711.74,2760.17,
2506.85,2704.1,2784.49,2834.4,2945.83,2752.06,2941.76,2980.38,2926.46,2976.74,3037.56,3140.98,3230.78,3225.52,2954.22,2584.59,
2912.43,3044.31,3100.29,3271.12,3500.31,3363,3269.96,3621.63,3756.07,3714.24,3811.15,3972.89,4181.17,4204.11,4297.5,4395.26,
4522.68,4307.54,4605.38,4567,4766.18,4515.55,4373.94,4530.41,4131.93,4132.15,3785.38,4130.29,3955)
stock_prices <- data.frame(
date = date,
nikkei22 = nikkei22,
sp500 = sp500
)
# Use stock_prices to define vector sp500.
sp500 <- stock_prices$___
# Compute the ratio of t and t-1 values.
sp500_ratio <- ___[2:length(___)]/sp500[___:length(___)-1]
# Subtract one from the ratio.
sp500_simple_returns <- ___ - ___
# Plot a histogram of simple returns.
___(sp500_simple_returns)
# Use stock_prices to define vector sp500.
sp500 <- stock_prices$sp500
# Compute the ratio of t and t-1 values.
sp500_ratio <- sp500[2:length(sp500)]/sp500[1:length(sp500)-1]
# Subtract one from the ratio.
sp500_simple_returns <- sp500_ratio - 1
# Plot a histogram of simple returns.
hist(sp500_simple_returns)
test_error()
test_object("sp500", incorrect_msg="Use you use the correct column from `stock_prices` to define `sp500`?")
test_object("sp500_simple_returns", incorrect_msg="Did you use subset the vector using the correct indices?")
test_function("hist", incorrect_msg="Did you use the `hist()` function?")
success_msg("Well done! Notice that we could have combined the division and substraction step into a single line of code.")
hist()
function.