Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.
## Warning in system("timedatectl", intern = TRUE): running command 'timedatectl'
## had status 1
## # 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 MSFT 1990-04-23 0.818 0.826 0.795 0.819 64876800 0.526
## 2 MSFT 1990-04-24 0.830 0.830 0.785 0.806 47913600 0.517
## 3 MSFT 1990-04-25 0.809 0.816 0.792 0.806 53868800 0.517
## 4 MSFT 1990-04-26 0.812 0.816 0.795 0.797 46294400 0.511
## 5 MSFT 1990-04-27 0.793 0.795 0.773 0.781 57123200 0.501
## 6 MSFT 1990-04-30 0.781 0.806 0.774 0.806 51449600 0.517
## 7 MSFT 1990-05-01 0.816 0.816 0.788 0.793 52473600 0.509
## 8 MSFT 1990-05-02 0.795 0.812 0.795 0.809 33264000 0.519
## 9 MSFT 1990-05-03 0.812 0.830 0.809 0.819 45891200 0.526
## 10 MSFT 1990-05-04 0.826 0.858 0.819 0.858 47811200 0.550
## # … 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 MSFT 1990-12-31 0.275
## 2 MSFT 1991-12-31 1.22
## 3 MSFT 1992-12-31 0.151
## 4 MSFT 1993-12-31 -0.0556
## 5 MSFT 1994-12-30 0.516
## 6 MSFT 1995-12-29 0.436
## 7 MSFT 1996-12-31 0.883
## 8 MSFT 1997-12-31 0.564
## 9 MSFT 1998-12-31 1.15
## 10 MSFT 1999-12-31 0.684
## # … 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
Microsoft has the highest expected yearly return at 0.275.
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 MSFT 0.402
## 2 ^IXIC 0.278
## 3 WMT 0.327
Microsoft is the riskiest stock in terms at 0.4022 and the least risky is the Nasdaq at 0.2780
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 MSFT 0.275
## 2 ^IXIC 0.179
## 3 WMT 1.42
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol kurtosis.1
## <chr> <dbl>
## 1 MSFT 0.431
## 2 ^IXIC 0.302
## 3 WMT 1.52
The standard deviation does not take into account risk. Both Microsoft and Nasdaq are more balanced in terms of skewness (MSFT 0.2749, ^IXIC 0.1787). However Walmart is more likely to have extreme positive returns. The Nasdaq has the lowest kurtosis number meaning it is close to normal distribution but Walmart has a 1.5222 kurtosis meaning that it is likely to have extreme positive and negative returns.
Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.
## [,1] [,2] [,3]
## symbol "MSFT" "^IXIC" "WMT"
## DownsideDeviation(0%) "0.1448" "0.1245" "0.0829"
## DownsideDeviation(MAR=0.833333333333333%) "0.1475" "0.1279" "0.0866"
## DownsideDeviation(Rf=0%) "0.1448" "0.1245" "0.0829"
## GainDeviation "0.3226" "0.2050" "0.3151"
## HistoricalES(95%) "-0.5362" "-0.3991" "-0.2471"
## HistoricalVaR(95%) "-0.3317" "-0.3541" "-0.2197"
## LossDeviation "0.2388" "0.1598" "0.0885"
## MaximumDrawdown "0.6285" "0.6718" "0.3206"
## ModifiedES(95%) "-0.4899" "-0.4069" "-0.5204"
## ModifiedVaR(95%) "-0.3414" "-0.2976" "-0.2176"
## SemiDeviation "0.2659" "0.1892" "0.1707"
In terms of the Var the Nasdaq has the greatest downside of risk at -0.3541 this means that the largest loss you can expect is 35.41% with 95% confidence. The HistoricalES of Microsoft has a value of -0.5362 meaning that -53.62% is the average return for 5% of 95%. The SemiDeviation of Microsoft is 0.2659 meaning that the mean return is 0.2659 below the standard deviation.
Hint: Make your argument based on the three Sharpe Ratios.
## # A tibble: 3 x 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=2%,p=95%… `StdDevSharpe(Rf=2%,p=95… `VaRSharpe(Rf=2%,p=95…
## <chr> <dbl> <dbl> <dbl>
## 1 MSFT 0.520 0.633 0.746
## 2 ^IXIC 0.286 0.419 0.392
## 3 WMT 0.269 0.428 0.644
I would chose Microsoft because they have the highest Sharp ratio of 0.5197 meaning they have the greatest average return.
Hint: Make your argument based on the three Sharpe Ratios.
## # A tibble: 3 x 4
## # Groups: symbol [3]
## symbol `ESSharpe(Rf=2%,p=99%… `StdDevSharpe(Rf=2%,p=99… `VaRSharpe(Rf=2%,p=99…
## <chr> <dbl> <dbl> <dbl>
## 1 MSFT 0.364 0.633 0.428
## 2 ^IXIC 0.205 0.419 0.243
## 3 WMT 0.140 0.428 1.13
I would still chose microsoft because Microsoft still has the highest Sharpe ratio of 0.3644 with Nasdaq at 0.2046 and Walmart at 0.1401.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.