You are considering three index funds that follow the three major market indexes: the Dow Jones Industrial Average, the S&P 500, and the NASDAQ Compsite Index.
library(tidyquant)
library(tidyverse)
Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.
# Import data
from = today() - years(30)
Stocks <- tq_get(c("^DJI", "^GSPC"), get = "stock.prices", from = from) %>%
group_by(symbol)
Stocks
## # A tibble: 15,118 x 8
## # Groups: symbol [2]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^DJI 1989-04-17 2332. 2348. 2324. 2338. 13760000 2338.
## 2 ^DJI 1989-04-18 2357. 2386 2351. 2379. 25240000 2379.
## 3 ^DJI 1989-04-19 2377. 2398. 2370. 2387. 21700000 2387.
## 4 ^DJI 1989-04-20 2390. 2399. 2357. 2377. 21410000 2377.
## 5 ^DJI 1989-04-21 2383. 2413. 2373. 2409. 27210000 2409.
## 6 ^DJI 1989-04-24 2397. 2416. 2385. 2403. 18550000 2403.
## 7 ^DJI 1989-04-25 2408. 2422. 2379. 2387. 21850000 2387.
## 8 ^DJI 1989-04-26 2393. 2404. 2374. 2389. 17050000 2389.
## 9 ^DJI 1989-04-27 2393. 2433. 2388. 2419. 25780000 2419.
## 10 ^DJI 1989-04-28 2420. 2430. 2404. 2419. 17410000 2419.
## # ... with 15,108 more rows
Hint: Take Stocks and pipe it to tidyquant::tq_transmute. Assign the result to returns_yearly.
returns_yearly <-
Stocks %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_yearly
## # A tibble: 62 x 3
## # Groups: symbol [2]
## symbol date yearly.returns
## <chr> <date> <dbl>
## 1 ^DJI 1989-12-29 0.178
## 2 ^DJI 1990-12-31 -0.0434
## 3 ^DJI 1991-12-31 0.203
## 4 ^DJI 1992-12-31 0.0417
## 5 ^DJI 1993-12-31 0.137
## 6 ^DJI 1994-12-30 0.0214
## 7 ^DJI 1995-12-29 0.335
## 8 ^DJI 1996-12-31 0.260
## 9 ^DJI 1997-12-31 0.226
## 10 ^DJI 1998-12-31 0.161
## # ... with 52 more rows
Hint: Refer to the ggplot2 cheatsheet. Look for geom_density under One Variable. Use the fill argument to create the plot per each stock.
returns_yearly %>%
ggplot(aes(x = yearly.returns, fill = symbol) ) +
geom_density(alpha = 0.3)
Hint: Take returns_yearly and pipe it to summarise. Calculate the mean yearly returns.
returns_yearly %>%
summarise(mean(yearly.returns))
## # A tibble: 2 x 2
## symbol `mean(yearly.returns)`
## <chr> <dbl>
## 1 ^DJI 0.0917
## 2 ^GSPC 0.0895
Given the data from the code we see the Dow Jones having a higher yearly returns of 0.09171175 when compared to 0.08950051.
Hint: Discuss your answer in terms of standard deviation. Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).
returns_yearly %>%
tq_performance(Ra = yearly.returns, performance_fun = sd)
## # A tibble: 2 x 2
## # Groups: symbol [2]
## symbol sd.1
## <chr> <dbl>
## 1 ^DJI 0.146
## 2 ^GSPC 0.165
S&P is riskier because it has a higher standard deviation of 0.1652162 compared to 0.1456230 for the Dow.
Hint: Discuss your answer in terms of whether the returns are normally distributed. The normality of the distribution may be measured by skewness and kurtosis. To compute the metrics, take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute skewness. Do the same for kurtosis.
returns_yearly %>%
tq_performance(Ra = yearly.returns, performance_fun = skewness)
## # A tibble: 2 x 2
## # Groups: symbol [2]
## symbol skewness.1
## <chr> <dbl>
## 1 ^DJI -0.777
## 2 ^GSPC -0.819
In this case we do see S&P having higher skewness, meaning they are likely to have more higher negative returns meaning it will be more risky.
returns_yearly %>%
tq_performance(Ra = yearly.returns, performance_fun = kurtosis)
## # A tibble: 2 x 2
## # Groups: symbol [2]
## symbol kurtosis.1
## <chr> <dbl>
## 1 ^DJI 0.750
## 2 ^GSPC 0.631
but with the kutosis we see S&P being lower meaning that S&P is less likely to have those high negative returns as often.
Putting both of these together I would say it’s close, as to whether SD is accurate measure, but in this case I would say yes, given the S&P has higher negative returns, regardless of the frequency because it is close.
Hint: Discuss your answer in terms of HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation. To compute the metrics, take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.
returns_yearly %>%
tq_performance(Ra = yearly.returns, performance_fun = table.DownsideRisk)
## # A tibble: 2 x 12
## # Groups: symbol [2]
## symbol `DownsideDeviat~ `DownsideDeviat~ `DownsideDeviat~ GainDeviation
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 ^DJI 0.0712 0.0741 0.0712 0.0888
## 2 ^GSPC 0.0877 0.0908 0.0877 0.0928
## # ... with 7 more variables: `HistoricalES(95%)` <dbl>,
## # `HistoricalVaR(95%)` <dbl>, LossDeviation <dbl>,
## # MaximumDrawdown <dbl>, `ModifiedES(95%)` <dbl>,
## # `ModifiedVaR(95%)` <dbl>, SemiDeviation <dbl>
for ES we see S&P having much more negative returns with -30.93% when compared to -25.30%. for Var we see S&P we see a much higher expected loss within the 95% confidence of -18.2% compared to -11.93% of Dow. for SemiDeviation we see the standard deciation below the mean at 11.05% for Dow and 12.68% for S&P.
Given all these numbers, I feel it’s quite clear that S&P has a much greater downside risk, especially when considering the first two numbers.
Hint: Discuss your answer in terms of Sharpe Ratio. Take returns_yearly and pipe it to tidyquant:: tq_performance. Use the performance_fun argument to compute SharpeRatio. The function returns Sharpe Ratios adjusted to three different types of risks: 1) standard deviation, 2) expected shortfall (5% worst loss), and 3) value at risk (the largest loss at the 95% confidence level).
returns_yearly %>%
tq_performance(Ra = yearly.returns, performance_fun = SharpeRatio)
## # A tibble: 2 x 4
## # Groups: symbol [2]
## symbol `ESSharpe(Rf=0%,p=95~ `StdDevSharpe(Rf=0%,p=~ `VaRSharpe(Rf=0%,p=~
## <chr> <dbl> <dbl> <dbl>
## 1 ^DJI 0.355 0.630 0.534
## 2 ^GSPC 0.297 0.542 0.423
Given the Sharpe Ratio and being a risk adverse advisor you would want to invest in S&P because all 3 of the numbers (Standard Deviation, Expected Shortfall, and value at risk) are lower, meaning you are risking a lot less in all 3 cases.