library(tidyquant)
library(tidyverse)

Q1 Import stock prices of Apple, Microsoft and Amazon for the last 10 years.

Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.

# Import stock prices
from = today() - years(10)
Stocks <- 
  tq_get(c("AAPL", "AMZN", "MSFT"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 7,551 x 8
## # Groups:   symbol [3]
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2010-05-06  36.3  36.9  28.5  35.2 321465200     30.5
##  2 AAPL   2010-05-07  34.8  35.2  32.2  33.7 419004600     29.2
##  3 AAPL   2010-05-10  35.8  36.4  35.5  36.3 246076600     31.5
##  4 AAPL   2010-05-11  36.0  37.1  35.8  36.6 212226700     31.8
##  5 AAPL   2010-05-12  37.0  37.6  37.0  37.4 163594900     32.5
##  6 AAPL   2010-05-13  37.6  37.9  36.6  36.9 149928100     32.0
##  7 AAPL   2010-05-14  36.5  36.6  35.6  36.3 189840700     31.5
##  8 AAPL   2010-05-17  36.4  36.6  35.4  36.3 190708700     31.5
##  9 AAPL   2010-05-18  36.7  36.9  35.8  36.1 195669600     31.3
## 10 AAPL   2010-05-19  35.6  36.1  35.0  35.5 256431700     30.8
## # … with 7,541 more rows

Q2 Calculate yearly returns.

Hint: Take the adjusted variable from Stocks, and calculate yearly returns using ***tq_transmute().

returns_yearly <-
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_yearly
## # A tibble: 33 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 AAPL   2010-12-31         0.310 
##  2 AAPL   2011-12-30         0.256 
##  3 AAPL   2012-12-31         0.326 
##  4 AAPL   2013-12-31         0.0807
##  5 AAPL   2014-12-31         0.406 
##  6 AAPL   2015-12-31        -0.0301
##  7 AAPL   2016-12-30         0.125 
##  8 AAPL   2017-12-29         0.485 
##  9 AAPL   2018-12-31        -0.0539
## 10 AAPL   2019-12-31         0.890 
## # … with 23 more rows

Q3 Which of the three stocks has the highest expected yearly return?

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 AAPL         0.255
## 2 AMZN         0.345
## 3 MSFT         0.221

Amazon has the highest expected yearly return at 0.345.

Q4 Calculate standard deviation of the yearly returns. Which of the three stocks is the riskiest in terms of standard deviation?

Hint: 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, #Calculating downside risk
                 performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 AAPL   0.277
## 2 AMZN   0.369
## 3 MSFT   0.195

Out of the three stocks Amazon is the riskiest in terms of standard deviation becuase it is higher than the other two stocks. Amazon’s SD is 0.369.

Q5 Is the standard deviation appropriate measure of risk for the three stocks? Calculate skewness and kurtosis, and discuss them in your answer.

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.

returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                 Rb = NULL, #Calculating downside risk
                 performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 AAPL        0.971
## 2 AMZN        0.731
## 3 MSFT        0.345
returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                 Rb = NULL, #Calculating downside risk
                 performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 AAPL        0.462
## 2 AMZN        0.649
## 3 MSFT       -0.804

Standard deviation is not an appropriate way to measure stocks.

Q6 Which of the three stocks poses greater downside risk? Calculate HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation, and discuss them in your answer.

Hint: Take returns_yearly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk. Fully interpret downside risk measures in at least 100 words.

returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                 Rb = NULL, #Calculating downside risk
                 performance_fun = table.DownsideRisk) %>%
  t()
##                                           [,1]      [,2]      [,3]     
## symbol                                    "AAPL"    "AMZN"    "MSFT"   
## DownsideDeviation(0%)                     "0.0186"  "0.0679"  "0.0151" 
## DownsideDeviation(MAR=0.833333333333333%) "0.0221"  "0.0708"  "0.0185" 
## DownsideDeviation(Rf=0%)                  "0.0186"  "0.0679"  "0.0151" 
## GainDeviation                             "0.2625"  "0.3150"  "0.1663" 
## HistoricalES(95%)                         "-0.0539" "-0.2218" "-0.0452"
## HistoricalVaR(95%)                        "-0.0420" "-0.1301" "-0.0333"
## LossDeviation                             "0.0168"  "0.1297"  "0.0168" 
## MaximumDrawdown                           "0.0539"  "0.2218"  "0.0656" 
## ModifiedES(95%)                           "-0.1977" "-0.2510" "-0.1106"
## ModifiedVaR(95%)                          "-0.0991" "-0.1527" "-0.0690"
## SemiDeviation                             "0.1601"  "0.2232"  "0.1228"

Amazon demonstrates the greatest risk. Nasdaq’s highest VaR is recorded at -0.13. Amazon’s historical ES is -0.22, which is significanlty higher than the other two stocks. This represents a much higher chance of risk.

Q7 Which of the three stocks would you choose? Calculate the Sharpe ratios with an annualized risk-free rate of 3% and a default confidence interval of 0.95.

Hint: Make your argument based on the three Sharpe Ratios. Fully interpret Sharpe Ratios in at least 100 words.

returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                 Rb = NULL,
                 performance_fun = SharpeRatio,
                 Rf = 0.03)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=3%,p=95%… `StdDevSharpe(Rf=3%,p=95… `VaRSharpe(Rf=3%,p=95…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 AAPL                     1.14                     0.813                   2.27
## 2 AMZN                     1.25                     0.853                   2.06
## 3 MSFT                     1.72                     0.979                   2.76

Based off of the chart showing the numbers above. I would chose to invest my money in Microsoft.

Q7.a Repeat Q7 but at a confidence interval of 0.99. Does it change your answer in Q7?

Hint: Make your argument based on the three Sharpe Ratios. Fully interpret Sharpe Ratios in at least 100 words.

returns_yearly %>% 
  tq_performance(Ra = yearly.returns,
                 Rb = NULL,
                 performance_fun = SharpeRatio,
                 p = 0.99, Rf = 0.03)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=3%,p=99%… `StdDevSharpe(Rf=3%,p=99… `VaRSharpe(Rf=3%,p=99…
##   <chr>                   <dbl>                     <dbl>                  <dbl>
## 1 AAPL                    0.225                     0.813                   2.14
## 2 AMZN                    0.831                     0.853                   1.18
## 3 MSFT                    0.458                     0.979                   1.58

Yes, this changes my answer because now I would rather invest my money in Amazon.

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

Hint: Use message, 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.