# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices and calculate returns
returns_monthly <- c("AMZN", "AAPL", "MSFT") %>%
    tq_get(get  = "stock.prices",
           from = "1990-01-01",
           to   = "2020-11-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted,
                 mutate_fun = monthlyReturn)
returns_monthly
## # A tibble: 1,022 x 3
## # Groups:   symbol [3]
##    symbol date       monthly.returns
##    <chr>  <date>               <dbl>
##  1 AMZN   1997-05-30         -0.234 
##  2 AMZN   1997-06-30          0.0278
##  3 AMZN   1997-07-31          0.554 
##  4 AMZN   1997-08-29         -0.0239
##  5 AMZN   1997-09-30          0.855 
##  6 AMZN   1997-10-31          0.172 
##  7 AMZN   1997-11-28         -0.189 
##  8 AMZN   1997-12-31          0.217 
##  9 AMZN   1998-01-30         -0.0207
## 10 AMZN   1998-02-27          0.305 
## # … with 1,012 more rows

Q1 Create a density plot for the returns of the given stocks.

Hint: Refer to the ggplot2 cheatsheet. Look for geom_density under One Variable. Use the fill argument to create the plot per each stock.

ggplot(returns_monthly, aes(x = monthly.returns, fill = symbol)) +
  geom_density(alpha = 0.3)

Q2 Which stock has higher expected monthly return?

Hint: Discuss your answer in terms of the mean. Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute the mean.

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = mean)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol mean.1
##   <chr>   <dbl>
## 1 AMZN   0.0394
## 2 AAPL   0.0244
## 3 MSFT   0.0208

Amazon has a higher expected monthly return because it has the highest mean of 0.0394 between the three stocks. This includes Apple and Microsoft with means of 0.0244 and 0.0208.

Q3 Which stock is riskier?

Hint: Discuss your answer in terms of the standard deviation. Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol   sd.1
##   <chr>   <dbl>
## 1 AMZN   0.174 
## 2 AAPL   0.125 
## 3 MSFT   0.0885

The higher the standard deviation the riskier the stock and although Amazon has a higher expected monthly return it is a riskier stock with the standard deviation of 0.174. Apple and Microsoft trail behind with standard deviations of 0.125 and 0.0885.

Q4 Is the standard deviation enough as a risk measure? Or do you need additional downside risk measurements? Why? Or why not?

Hint: Discuss your answer in terms of the skewness and the kurtosis. Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute the skewness. Do the same for the kurtosis.

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 AMZN        1.97 
## 2 AAPL       -0.248
## 3 MSFT        0.420
returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 AMZN        10.7 
## 2 AAPL         1.60
## 3 MSFT         2.19

Standard deviation is enough for a risk measure because when you look at the skewness and kurtosis for the three stocks Amazon is a huge gap higher than Apple and Microsoft in both risk measures and in the standard deviation of all three stocks.

Q5 Calculate the downside risk measures. Which stock has the greatest downside risk? Discuss HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation.

Hint: Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = table.DownsideRisk) %>% t()
##                            [,1]      [,2]      [,3]     
## symbol                     "AMZN"    "AAPL"    "MSFT"   
## DownsideDeviation(0%)      "0.0827"  "0.0777"  "0.0491" 
## DownsideDeviation(MAR=10%) "0.0866"  "0.0817"  "0.0531" 
## DownsideDeviation(Rf=0%)   "0.0827"  "0.0777"  "0.0491" 
## GainDeviation              "0.1583"  "0.0797"  "0.0662" 
## HistoricalES(95%)          "-0.2813" "-0.2557" "-0.1577"
## HistoricalVaR(95%)         "-0.1998" "-0.1639" "-0.1217"
## LossDeviation              "0.0902"  "0.0831"  "0.0513" 
## MaximumDrawdown            "0.9307"  "0.7967"  "0.6670" 
## ModifiedES(95%)            "-0.2510" "-0.2823" "-0.1437"
## ModifiedVaR(95%)           "-0.0989" "-0.1854" "-0.1098"
## SemiDeviation              "0.1026"  "0.0900"  "0.0596"
returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = table.DownsideRisk, p = 0.95) %>% t()
##                            [,1]      [,2]      [,3]     
## symbol                     "AMZN"    "AAPL"    "MSFT"   
## DownsideDeviation(0%)      "0.0827"  "0.0777"  "0.0491" 
## DownsideDeviation(MAR=10%) "0.0866"  "0.0817"  "0.0531" 
## DownsideDeviation(Rf=0%)   "0.0827"  "0.0777"  "0.0491" 
## GainDeviation              "0.1583"  "0.0797"  "0.0662" 
## HistoricalES(95%)          "-0.2813" "-0.2557" "-0.1577"
## HistoricalVaR(95%)         "-0.1998" "-0.1639" "-0.1217"
## LossDeviation              "0.0902"  "0.0831"  "0.0513" 
## MaximumDrawdown            "0.9307"  "0.7967"  "0.6670" 
## ModifiedES(95%)            "-0.2510" "-0.2823" "-0.1437"
## ModifiedVaR(95%)           "-0.0989" "-0.1854" "-0.1098"
## SemiDeviation              "0.1026"  "0.0900"  "0.0596"

Amazon is still leading with the downside risk measure:

HistoricalES(95%) AMZN –> “-0.2813” HistoricalVaR(95%) AMZN –> “-0.1998”

                AAPL       MSFT

HistoricalES(95%) “-0.2557” “-0.1577”

HistoricalVaR(95%) “-0.1639” “-0.1217”

Q6 Which stock would you choose? Calculate and interpret the Sharpe Ratio.

Hint: Assume that the risk free rate is zero and 95% confidence level. Note that the Sharpe Ratios are calculated using different risk measures: ES, VaR and semideviation. Make your argument based on all three Sharpe Ratios.

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = SharpeRatio)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0%,p=95%… `StdDevSharpe(Rf=0%,p=95… `VaRSharpe(Rf=0%,p=95…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 AMZN                   0.157                      0.226                  0.398
## 2 AAPL                   0.0865                     0.196                  0.132
## 3 MSFT                   0.145                      0.235                  0.190

I would choose the Microsoft stock because it poses the least amount of risk between the three stocks overall, but if it was based solely off of the Sharpe Ratio I would choose Apple because it has the lowest Sharpe Ratios between the three stocks.

Q7 Redo Q6 at the 99% confidence level instead of the 95% confidence level. Which stock would you choose now? Is your answer different from Q6? Why? Or why not?

Hint: Google tq_performance(). Discuss in terms of ES, VaR and semideviation and their differences between 95% and 99%.

returns_monthly %>%
    tq_performance(
        Ra = monthly.returns, 
        Rb = NULL, 
        performance_fun = SharpeRatio, 
        Rf = 0.01 / 12)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0.1%,p=9… `StdDevSharpe(Rf=0.1%,p=… `VaRSharpe(Rf=0.1%,p=…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 AMZN                   0.154                      0.221                  0.390
## 2 AAPL                   0.0835                     0.189                  0.127
## 3 MSFT                   0.139                      0.226                  0.182

I would still keep my answer the same because the stocks changed a little bit and went down Sharpe Ratio wise but Apple is still the least riskiest stock when you’re looking at the three stocks based on Sharpe Ratio. Overall though, Microsoft is the least riskiest based on the different measures in this quiz that we analyzed.

Q8 Hide the messages and warnings, but display the code and its results on the webpage.

Hint: Use message, warning, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.