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.
from = today() - years(30)
Stocks <- tq_get(c("^DJI", "^GSPC", "^IXIC"), get = "stock.prices", from = from) %>%
group_by(symbol)
Stocks
## # A tibble: 22,677 x 8
## # Groups: symbol [3]
## 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 22,667 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: 93 x 3
## # Groups: symbol [3]
## 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 83 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(returns_avg = mean(yearly.returns))
## # A tibble: 3 x 2
## symbol returns_avg
## <chr> <dbl>
## 1 ^DJI 0.0917
## 2 ^GSPC 0.0895
## 3 ^IXIC 0.134
NASDAQ has the highest expected yearly return with a mean of 13.4%. Dow Jones has a mean of 9.17% and S&P 500 has a mean of 8.95%.
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,
Rb = NULL,
performance_fun = sd)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol sd.1
## <chr> <dbl>
## 1 ^DJI 0.146
## 2 ^GSPC 0.165
## 3 ^IXIC 0.276
NASDAQ is also the riskiest index fund with a standard deviation of 0.275. Dow Jones has a standard deviation of 0.145 and S&P 500 of 0.165. It has the largest standard deviation so it has the highest chance of having a larger variation of returns.
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,
Rb = NULL,
performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol skewness.1
## <chr> <dbl>
## 1 ^DJI -0.777
## 2 ^GSPC -0.819
## 3 ^IXIC 0.179
returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL,
performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol kurtosis.1
## <chr> <dbl>
## 1 ^DJI 0.750
## 2 ^GSPC 0.631
## 3 ^IXIC 0.435
Dow Jones and S&P 500 have a negative skewness which means large negative returns will occur more often. NASDAQ has a positive skewness which means large positive returns are likely to occur more often. Dow Jones, S&P 500, and NASDAQ all have kurtosis that is above zero so larger returns of both positive abd negative are likely to occur more often (they will have fatter tails). Because of this information, I have determined that standard deviation is not an appropriate measure of risk for these indexes because they are not normally distributed so there is a greater chance of variation.
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,
Rb = NULL,
performance_fun = table.DownsideRisk) %>%
t()
## [,1] [,2] [,3]
## symbol "^DJI" "^GSPC" "^IXIC"
## DownsideDeviation(0%) "0.0712" "0.0877" "0.1266"
## DownsideDeviation(MAR=0.833333333333333%) "0.0741" "0.0908" "0.1300"
## DownsideDeviation(Rf=0%) "0.0712" "0.0877" "0.1266"
## GainDeviation "0.0888" "0.0928" "0.2028"
## HistoricalES(95%) "-0.2530" "-0.3093" "-0.3991"
## HistoricalVaR(95%) "-0.1193" "-0.1820" "-0.3541"
## LossDeviation "0.1092" "0.1261" "0.1606"
## MaximumDrawdown "0.3384" "0.4012" "0.6718"
## ModifiedES(95%) "-0.2584" "-0.3013" "-0.4083"
## ModifiedVaR(95%) "-0.1718" "-0.2116" "-0.2957"
## SemiDeviation "0.1105" "0.1268" "0.1883"
The Semi-Deviation for Dow Jones is 0.1105, S&P 500 is 0.1268, and NASDAQ is 0.1883 which means the standard deviation of returns below the mean is 0.1105, 0.1268 and 0.1883 respectively.
VaR of -0.1193 for Dow Jones means that -11.93% is the largest loss one could expect with 95% confidence. -0.1820 for S&P 500 means that -18.2% is the largest loss one could expect with 95% confidence. -0.3541 for NASDAQ means that -35.41% is the largest loss one could expect with 95% confidence. It is possible to loose more than 11.93%, 18.2%, and 35.41% but there is only a 5% chance of loosing more.
ES of -0.2530 for Dow Jones, -0.3093 for S&P 500, and -0.3991 for NASDAQ means that -25.3%, -30.93%, and -39.91% is the average of the 5% most negative returns.
NASDAQ appears to present greater downside risk than S&P 500 & Dow Jones. For example, monthly returns are more volatile below the mean for NASDAQ (semideviation of 0.1883) than S&P 500 (semideviation of 0.1268) and Dow Jones (semideviation of 0.1105); the largest loss one could expect with 95% confidence is larger for NASDAQ (VaR of -0.3541 at 95%) than S&P 500 (VaR of -0.1820 at 95%) and Dow Jones (VaR of -0.1193 at 95%); and the average of the 5% most negative monthly returns is larger for NASDAQ (ES of -0.3991 at 5%) than S&P 500 (ES of -0.3093 at 5%) and Dow Jones (ES of -0.2530).
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: 3 x 4
## # Groups: symbol [3]
## 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
## 3 ^IXIC 0.328 0.486 0.454
When considering the three different funds, as a risk-averse investor I would choose the Dow Jones Industrial Average. The reasoning behing that is it has the lowest downside risks and the higest Sharpe Ration. The higher Sharpe Ratio shows how much excess return we may recieve due to the risk involved. Because it has the lowest downside risk and the highest Sharpe Ratio it has less voltality and a higher chance of a positive return.