library(tidyverse)
library(tidyquant)

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.

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-10  35.8  36.4  35.5  36.3 246076600     31.5
##  2 AAPL   2010-05-11  36.0  37.1  35.8  36.6 212226700     31.8
##  3 AAPL   2010-05-12  37.0  37.6  37.0  37.4 163594900     32.5
##  4 AAPL   2010-05-13  37.6  37.9  36.6  36.9 149928100     32.0
##  5 AAPL   2010-05-14  36.5  36.6  35.6  36.3 189840700     31.5
##  6 AAPL   2010-05-17  36.4  36.6  35.4  36.3 190708700     31.5
##  7 AAPL   2010-05-18  36.7  36.9  35.8  36.1 195669600     31.3
##  8 AAPL   2010-05-19  35.6  36.1  35.0  35.5 256431700     30.8
##  9 AAPL   2010-05-20  34.6  34.8  33.7  34.0 320728800     29.5
## 10 AAPL   2010-05-21  33.3  34.9  33.0  34.6 305972800     30.0
## # … 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.270 
##  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.254
## 2 AMZN         0.345
## 3 MSFT         0.222
returns_yearly
## # A tibble: 33 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 AAPL   2010-12-31         0.270 
##  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

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,
                   performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 AAPL   0.275
## 2 AMZN   0.368
## 3 MSFT   0.194

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.

yearly_skewness <- returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = skewness)


yearly_kurtosis <- returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = kurtosis)

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.

Downside_risk <- returns_yearly %>%
  tq_performance(Ra = yearly.returns,
                   Rb = NULL,
                   performance_fun = table.DownsideRisk) %>%
  t()

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 = .02) %>%
  t()
##                           [,1]        [,2]        [,3]       
## symbol                    "AAPL"      "AMZN"      "MSFT"     
## ESSharpe(Rf=2%,p=95%)     "1.160834"  "1.304840"  "1.853873" 
## StdDevSharpe(Rf=2%,p=95%) "0.8501845" "0.8821035" "1.0439292"
## VaRSharpe(Rf=2%,p=95%)    "2.533286"  "2.155536"  "3.018652"

As we know any sharpe ratio greater than 1.0 is considered acceptable. Also, we know that the higher the sharpe ratio th better. The higher a sharpe ratio is means that there is a higher degree of expected return and a more relatively low amount of risk in most cases. For this reason, I decided that if I were to choose one of these three stocks I would choose microsoft because they have the highest sharpe ratio. Since their sharpe ratio is the highest that means the stock would give me a more favorable ratio the degree of expected return and the degree of risk. Obviously I would be looking for the highest degree of return and the lowest degree of risk and microsoft would give me the best balance between the two.

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= .01, Rf = .02) %>%
  t()
##                          [,1]        [,2]        [,3]       
## symbol                   "AAPL"      "AMZN"      "MSFT"     
## ESSharpe(Rf=2%,p=1%)     "0.2336311" "0.9015037" "0.5226757"
## StdDevSharpe(Rf=2%,p=1%) "0.8501845" "0.8821035" "1.0439292"
## VaRSharpe(Rf=2%,p=1%)    "2.649956"  "1.221477"  "1.664520"

After making the desired changes to the confidence interval my results and my decision did in fact change. After making the changes and going back to interpret the sharpe ratios once again I was able to come to the conclusion that out of the three stocks I would buy Amazon. I would buy amazon instead of the other two because in this case amazon has the highest sharpe ratio. Seeing that Amazon has the highest sharpe ratio it is clear to me that to make the safest and most profitable decision I must buy amazon. Amazon will give me a better balance between expected degree or return and amount of risk so for that reason, it is the safest and smartest option to buy of the three stocks listed.

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.