You are considering three index funds that follow the three major market indexes: the Dow Jones Industrial Average, the S&P 500, and the NASDAQ Compsite Index.

Q1. Load tidyquant and tidyverse packages.

library(tidyquant)
library(tidyverse)

Q2. Import the Dow Jones Industrial Average, the S&P 500, and the NASDAQ Compsite Index for the last 30 years. Save the imported data under Stocks.

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

from = today() - years(30)
Stocks <- tq_get(c("^DJI","^GSPC","^IXIC"), get = "stock.prices", from = from) %>%
group_by(symbol)
Stocks
## # 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 ^DJI   1989-04-17 2332. 2348. 2324. 2338. 13760000    2338.
##  2 ^DJI   1989-04-18 2357. 2386  2351. 2379. 25240000    2379.
##  3 ^DJI   1989-04-19 2377. 2398. 2370. 2387. 21700000    2387.
##  4 ^DJI   1989-04-20 2390. 2399. 2357. 2377. 21410000    2377.
##  5 ^DJI   1989-04-21 2383. 2413. 2373. 2409. 27210000    2409.
##  6 ^DJI   1989-04-24 2397. 2416. 2385. 2403. 18550000    2403.
##  7 ^DJI   1989-04-25 2408. 2422. 2379. 2387. 21850000    2387.
##  8 ^DJI   1989-04-26 2393. 2404. 2374. 2389. 17050000    2389.
##  9 ^DJI   1989-04-27 2393. 2433. 2388. 2419. 25780000    2419.
## 10 ^DJI   1989-04-28 2420. 2430. 2404. 2419. 17410000    2419.
## # ... with 22,667 more rows

Q3. Calculate yearly returns, and save the result under returns_yearly.

Hint: Take Stocks and pipe it to tidyquant::tq_transmute. Assign the result to returns_yearly.

returns_yearly <-
 Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
    
returns_yearly
## # A tibble: 93 x 3
## # Groups:   symbol [3]
##    symbol date       yearly.returns
##    <chr>  <date>              <dbl>
##  1 ^DJI   1989-12-29         0.178 
##  2 ^DJI   1990-12-31        -0.0434
##  3 ^DJI   1991-12-31         0.203 
##  4 ^DJI   1992-12-31         0.0417
##  5 ^DJI   1993-12-31         0.137 
##  6 ^DJI   1994-12-30         0.0214
##  7 ^DJI   1995-12-29         0.335 
##  8 ^DJI   1996-12-31         0.260 
##  9 ^DJI   1997-12-31         0.226 
## 10 ^DJI   1998-12-31         0.161 
## # ... with 83 more rows

Q4. Create a density plot for returns of all considered index funds.

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

returns_yearly %>%
  ggplot(aes(x= yearly.returns, fill= symbol)) +
  geom_density(alpha = 0.3)

Q5. Which of the considered index funds has the highest expected yearly return?

Hint: Take returns_yearly and pipe it to summarise. Calculate the mean yearly returns.

returns_yearly %>%
  summarise(mean = mean(yearly.returns))
## # A tibble: 3 x 2
##   symbol   mean
##   <chr>   <dbl>
## 1 ^DJI   0.0917
## 2 ^GSPC  0.0895
## 3 ^IXIC  0.134

-Out of all the stocks given the one with the highest yearly return is NASDAQ. With this being said NASDAQ has a 13.4% return yearly versus the others which have a 8.95 and 9.17% of yearly return. With this being said NASDAQ may not be the one to invest in yet because the risk factors havent been calculated.

Q6. Which of the considered index funds is the riskiest?

Hint: Discuss your answer in terms of 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, performance_fun = sd)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol  sd.1
##   <chr>  <dbl>
## 1 ^DJI   0.146
## 2 ^GSPC  0.165
## 3 ^IXIC  0.276

-NASDAQ also has the highest standard deviation which in standards devation terms it makes it look like it is the riskiest to invest. With NASDAQ being riskier it causes a higher fluctuation between the stock over the last 30 years. Also, with NASDAQ being higher in fluctuation it can cause standard deviation to not be a accurate measure of risk. Also, NASDAQ being higher in standard deviation with higher fluctuation it can become more volatile. The standard deviation is 0.275 for NASDAQ versus S & P 500 which is 0.165 and Dow Jones is 0.145.

Q7. Would you consider the standard deviation as an appropriate measure of risk for the considered index funds?

Hint: Discuss your answer in terms of whether the returns are normally distributed. The normality of the distribution may be measured by skewness and kurtosis. To compute the metrics, 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, performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 ^DJI       -0.777
## 2 ^GSPC      -0.819
## 3 ^IXIC       0.179

returns_yearly %>%
  tq_performance(Ra = yearly.returns, performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups:   symbol [3]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 ^DJI        0.750
## 2 ^GSPC       0.631
## 3 ^IXIC       0.435

-In terms of skewness there is more negative returns occuring for Dow Jones and S & P 500. On the other hand for NASDAQ they have a positive skewness which means that more positive returns are going to occur more often. For S & P 500 and Dow Jones since they have a negative larger negative returns are going to occur more often causing the a longer tail to go to the left due to the negative returns. But for NASDAQ since it has a positive, and larger positive returns are going to occur the graph will be skewed to the right. Also with the positive returns occuring there will be a longer tail to the right. Therefore standard deviation isn’t a good measure based on skewness because standard deviation needs to be symmetrical. None, of the stocks are symmetrical they either have a tail going to the left or a tail going to the right. Typically, standard deviation has a bell curve that is generally in the center with no extra tails, therefore no stock could be measured using standard deviation. -In terms of kurtosis there is going to be fatter tails for all the stocks since they all have returns that are larger or smaller than 0. Also, with this being said a larger negative tail is going to occur for S & P 500 and Dow Jones because they have a negative interval. NASDAQ has a positive number above 0 which leads to a fatter tail as well. Since all arent’t at 0 they are more likely to have positive and negative returns to happen. Therefore with a fatter tail means that it is going to be measure more on a vertical axis. -Overall, standard deviation is not a good measure because they have negative and positive returns that are above or below zero causing fatter and a longer skewness to happen on a graph. With all these stocks they arent evenly distrubuted, causing standard deviation to not be a good measure. ## Q8. Which of the considered index funds poses the greatest downside risks? Hint: Discuss your answer in terms of HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation. To compute the metrics, 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, performance_fun = table.DownsideRisk) %>%
  t()
##                                           [,1]      [,2]      [,3]     
## symbol                                    "^DJI"    "^GSPC"   "^IXIC"  
## DownsideDeviation(0%)                     "0.0712"  "0.0877"  "0.1266" 
## DownsideDeviation(MAR=0.833333333333333%) "0.0741"  "0.0908"  "0.1300" 
## DownsideDeviation(Rf=0%)                  "0.0712"  "0.0877"  "0.1266" 
## GainDeviation                             "0.0888"  "0.0928"  "0.2028" 
## HistoricalES(95%)                         "-0.2530" "-0.3093" "-0.3991"
## HistoricalVaR(95%)                        "-0.1193" "-0.1820" "-0.3541"
## LossDeviation                             "0.1092"  "0.1261"  "0.1606" 
## MaximumDrawdown                           "0.3384"  "0.4012"  "0.6718" 
## ModifiedES(95%)                           "-0.2584" "-0.3013" "-0.4083"
## ModifiedVaR(95%)                          "-0.1718" "-0.2116" "-0.2957"
## SemiDeviation                             "0.1105"  "0.1268"  "0.1883"

-For the semi-deviation Dow Jones has a 0.0712 which means that it is 0.0712 below the standard deviation, S & P 500 has a deviation of 0.1268 below standard deviation, and for NASDAQ it has a 0.1883 below the standard deviation. Therefore based on semi-deviation NASDAQ has the highest rate that falls below standard deviation. -For VaR from the Dow Jones stock the largest loss that could occur is -11.93 on a scale of 95% accuracy. For S & P 500 the largest loss that could occur is -18.20 on a scale of 95% accuracy. Finally, for NASDAQ the largest loss that could occur is -35.41 on a scale of 95% accuracy. With this being said there still is a 5% chance that the loss could be greater than that but in terms of risk the riskiest investment would be NASDAQ. -In terms of ES this measures the remaining 5% and the highest return that could occur is a loss happened not on the 95% scale. For the Dow Jones there is a -25.30 chance on a 5% scale that this is going to be the most average return to occur. For S & P 500 there is a -30.93 chance on a 5% scale that this is going to be the most average return to occur. Finally, for NASDAQ there is a -39.91 chance on a 5% scale that this is going to be the average negative return to occur.
- By looking at and analyzing all the stocks given NASDAQ has the highest downside risk in all the categories. Semi-deviation is higher causing the standard deviation to be higher for NASDAQ. Also, when VAr was measured there is a higher risk of investment in NASDAQ as well because the highest negative return that could occur is 17.20 more negative than S & P 500. Finally, in terms of looking at it from es NASDAQ has the highest return most likely to occur on a 5% scale. Overall, NASDAQ would be the one with the greatest downside risk with all the high numbers that have occured.

Q9. Which of the considered index funds would you choose if you were a risk-averse investor?

Hint: Discuss your answer in terms of Sharpe Ratio. Take returns_yearly and pipe it to tidyquant:: tq_performance. Use the performance_fun argument to compute SharpeRatio. The function returns Sharpe Ratios adjusted to three different types of risks: 1) standard deviation, 2) expected shortfall (5% worst loss), and 3) value at risk (the largest loss at the 95% confidence level).

returns_yearly %>%
  tq_performance(Ra = yearly.returns, performance_fun = SharpeRatio, p = 0.95)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0%,p=95~ `StdDevSharpe(Rf=0%,p=~ `VaRSharpe(Rf=0%,p=~
##   <chr>                  <dbl>                   <dbl>                <dbl>
## 1 ^DJI                   0.355                   0.630                0.534
## 2 ^GSPC                  0.297                   0.542                0.423
## 3 ^IXIC                  0.328                   0.486                0.454

-The best stock to invest in if you were a risk-averse investor would be Dow Jones because there is less risk involved. Being a risk-averse you would want the least risky stock even though it wont have the highest return. The higher the sharpe ratio the better off you are. When it comes to looking at sharpe ratio Dow Jones had the highest which means when you put your initial investment in based on risk you are going to end up getting a hire reward over the risk involved. Overall, the reward is higher than the risk that is involved due to the fluctuations of volutility that is involved. By looking at the Dow Jones stock the standard deviation is 0.6297, expected shortfall by looking at a 5% loss is 35.49%, and the largest loss that could happen is 53.38 at a 95% confidence. With these numbers being calculated the more risk that is involved means a higher reward, being a risk-averse having a higher sharpe ratio means its better, which allows for a higher reward over the investment that is being involved.

Q10.a. Display both code and the results of the code on the webpage.

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

Q10.c. Use the correct slug.