Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.
## # 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 ^IXIC 1990-04-23 423. 425. 418. 420. 125990000 420.
## 2 ^IXIC 1990-04-24 422. 422. 419. 419. 126790000 419.
## 3 ^IXIC 1990-04-25 420 421. 419. 421. 121710000 421.
## 4 ^IXIC 1990-04-26 422. 422 419. 421. 115930000 421.
## 5 ^IXIC 1990-04-27 421. 421. 418. 418 116010000 418
## 6 ^IXIC 1990-04-30 418. 420. 417 420. 105790000 420.
## 7 ^IXIC 1990-05-01 422 422. 421. 422. 124130000 422.
## 8 ^IXIC 1990-05-02 423. 424. 422. 424. 143260000 424.
## 9 ^IXIC 1990-05-03 425 427. 424. 425. 160850000 425.
## 10 ^IXIC 1990-05-04 427. 429. 426. 429. 136810000 429.
## # … with 22,667 more rows
Hint: Take the adjusted variable from Stocks, and calculate yearly returns using ***tq_transmute().
## # A tibble: 93 x 3
## # Groups: symbol [3]
## symbol date yearly.returns
## <chr> <date> <dbl>
## 1 ^IXIC 1990-12-31 -0.110
## 2 ^IXIC 1991-12-31 0.569
## 3 ^IXIC 1992-12-31 0.155
## 4 ^IXIC 1993-12-31 0.147
## 5 ^IXIC 1994-12-30 -0.0320
## 6 ^IXIC 1995-12-29 0.399
## 7 ^IXIC 1996-12-31 0.227
## 8 ^IXIC 1997-12-31 0.216
## 9 ^IXIC 1998-12-31 0.396
## 10 ^IXIC 1999-12-31 0.856
## # … with 83 more rows
Hint: Take returns_yearly and pipe it to summarise. Calculate the mean yearly returns.
## # A tibble: 3 x 2
## symbol returns_avg
## <chr> <dbl>
## 1 ^IXIC 0.137
## 2 MSFT 0.275
## 3 WMT 0.160
Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol sd.1
## <chr> <dbl>
## 1 ^IXIC 0.278
## 2 MSFT 0.402
## 3 WMT 0.327
The Microsoft would be the riskiest in terms of standard deviation.
Hint: when the return distribution is not normal, the standard deviation is not an appropriate measure of risk. One can use skewness and kurtosis to detect non-normal returns. Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute skewness. Do the same for kurtosis.
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol skewness.1
## <chr> <dbl>
## 1 ^IXIC 0.179
## 2 MSFT 0.275
## 3 WMT 1.42
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol kurtosis.1
## <chr> <dbl>
## 1 ^IXIC 0.302
## 2 MSFT 0.431
## 3 WMT 1.52
All of three have positive skewness and kurtosis, that suggests positive returns are more likely.
Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.
## [,1] [,2] [,3]
## symbol "^IXIC" "MSFT" "WMT"
## DownsideDeviation(0%) "0.1245" "0.1448" "0.0829"
## DownsideDeviation(MAR=0.833333333333333%) "0.1279" "0.1475" "0.0866"
## DownsideDeviation(Rf=0%) "0.1245" "0.1448" "0.0829"
## GainDeviation "0.2050" "0.3226" "0.3151"
## HistoricalES(95%) "-0.3991" "-0.5362" "-0.2471"
## HistoricalVaR(95%) "-0.3541" "-0.3317" "-0.2197"
## LossDeviation "0.1598" "0.2388" "0.0885"
## MaximumDrawdown "0.6718" "0.6285" "0.3206"
## ModifiedES(95%) "-0.4069" "-0.4899" "-0.5204"
## ModifiedVaR(95%) "-0.2976" "-0.3414" "-0.2176"
## SemiDeviation "0.1892" "0.2659" "0.1707"
Microsoft’s downside risk is greater. Because has more volatile monthly returns below the mean than Walmart and NASDAQ, the largest loss one could expect with 95% confidence is larger for NASDAQ and Walmart, and the average of the 5% most negative monthly returns is larger for NASDAQ and Walmart.
Hint: Make your argument based on the three Sharpe Ratios.
## # A tibble: 3 x 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=2%,p=1%)` `StdDevSharpe(Rf=2%,p=1%… `VaRSharpe(Rf=2%,p=1%…
## <chr> <dbl> <dbl> <dbl>
## 1 ^IXIC 0.205 0.419 0.243
## 2 MSFT 0.364 0.633 0.428
## 3 WMT 0.140 0.428 1.13
I still coose Microsoft because it has the highest Sharpe ratio.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.