# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices and calculate returns
returns_yearly <- c("^DJI", "^GSPC", "^IXIC") %>%
    tq_get(get  = "stock.prices",
           from = "1990-01-01",
           to   = "2020-11-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted,
                 mutate_fun = yearlyReturn)
returns_yearly
## # A tibble: 91 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 ^DJI   1992-12-31         0.0406
##  2 ^DJI   1993-12-31         0.137 
##  3 ^DJI   1994-12-30         0.0214
##  4 ^DJI   1995-12-29         0.335 
##  5 ^DJI   1996-12-31         0.260 
##  6 ^DJI   1997-12-31         0.226 
##  7 ^DJI   1998-12-31         0.161 
##  8 ^DJI   1999-12-31         0.252 
##  9 ^DJI   2000-12-29        -0.0617
## 10 ^DJI   2001-12-31        -0.0710
## # ... with 81 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_yearly, aes(x = yearly.returns, fill = symbol)) +
  geom_density(alpha = 0.3)

Q2 Which stock has higher expected yearly return?

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

returns_yearly %>%
    tq_performance(
        Ra = yearly.returns, 
        Rb = NULL, #baseline returns, not needed 
        performance_fun = mean
    )
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol mean.1
##   <chr>   <dbl>
## 1 ^DJI   0.0871
## 2 ^GSPC  0.0880
## 3 ^IXIC  0.143

The Nasdaq has the higher expected yearly return which has a mean of 0.143.

Q3 Which stock is riskier?

Hint: Discuss your answer in terms of the 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, #baseline returns, not needed 
        performance_fun = sd
    )
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 ^DJI   0.151
## 2 ^GSPC  0.169
## 3 ^IXIC  0.279

The Nasdaq is also the riskiest stock because it has a standard deviation of 0.279.

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_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute the skewness. Do the same for the kurtosis.

returns_yearly %>%
    tq_performance(
        Ra = yearly.returns, 
        Rb = NULL, #baseline returns, not needed 
        performance_fun = skewness
    )
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 ^DJI      -0.669 
## 2 ^GSPC     -0.700 
## 3 ^IXIC      0.0914
returns_yearly %>%
    tq_performance(
        Ra = yearly.returns, 
        Rb = NULL, #baseline returns, not needed 
        performance_fun = kurtosis
    )
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 ^DJI        0.410
## 2 ^GSPC       0.357
## 3 ^IXIC       0.289

You do in fact need more than just the standard deviation to measure risk. Different outliers can screw the data a lot. Also if you look at the kurtosis of the data you can see where the data is all clustered on the graph. If these factors are not taken into account, standard deviation is not as effective for measuring risk.

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

Hint: 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, #baseline returns, not needed 
        performance_fun = table.DownsideRisk
    )
## # A tibble: 3 x 12
## # Groups:   symbol [3]
##   symbol `DownsideDeviatio~ `DownsideDeviation(~ `DownsideDeviati~ GainDeviation
##   <chr>               <dbl>                <dbl>             <dbl>         <dbl>
## 1 ^DJI               0.0743               0.0775            0.0743        0.0935
## 2 ^GSPC              0.0882               0.0913            0.0882        0.102 
## 3 ^IXIC              0.127                0.130             0.127         0.200 
## # ... with 7 more variables: HistoricalES(95%) <dbl>, HistoricalVaR(95%) <dbl>,
## #   LossDeviation <dbl>, MaximumDrawdown <dbl>, ModifiedES(95%) <dbl>,
## #   ModifiedVaR(95%) <dbl>, SemiDeviation <dbl>

By looking at the three categories on the table, HistoricalES(95%), HistoricanVAR(95%) and SemiDeviation, you can see that the Nasdaq has the greatest downside risk.

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_yearly %>%
    tq_performance(
        Ra = yearly.returns, 
        Rb = NULL, #baseline returns, not needed 
        performance_fun = SharpeRatio,
        p = .95
    )
## # 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 ^DJI                    0.331                     0.576                  0.476
## 2 ^GSPC                   0.292                     0.520                  0.407
## 3 ^IXIC                   0.342                     0.512                  0.477

I would choose the Nasdaq because it has the highest values in the 2 out of 3 of the various categories.

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_yearly %>%
    tq_performance(
        Ra = yearly.returns, 
        Rb = NULL, #baseline returns, not needed 
        performance_fun = SharpeRatio,
        p = .99
    )
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0%,p=99%~ `StdDevSharpe(Rf=0%,p=99~ `VaRSharpe(Rf=0%,p=99~
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 ^DJI                    0.193                     0.576                  0.271
## 2 ^GSPC                   0.169                     0.520                  0.239
## 3 ^IXIC                   0.237                     0.512                  0.289

I would still choose the Nasdaq because it still has the highest values in 2 of the 3 categories.

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.