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.
library(tidyquant)
library(tidyverse)
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("^GSPC","^IXIC", "^DJI"), 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 ^GSPC 1989-04-17 301. 302. 301. 302. 128540000 302.
## 2 ^GSPC 1989-04-18 302. 306. 302. 306. 208650000 306.
## 3 ^GSPC 1989-04-19 306. 308. 305. 307. 191510000 307.
## 4 ^GSPC 1989-04-20 307. 308. 305. 306. 175970000 306.
## 5 ^GSPC 1989-04-21 306. 310. 306. 310. 187310000 310.
## 6 ^GSPC 1989-04-24 310. 310. 308. 309. 142100000 309.
## 7 ^GSPC 1989-04-25 309. 310. 307. 307. 165430000 307.
## 8 ^GSPC 1989-04-26 307. 307. 306. 307. 146090000 307.
## 9 ^GSPC 1989-04-27 307. 310. 307. 310. 191170000 310.
## 10 ^GSPC 1989-04-28 310. 310. 308. 310. 158390000 310.
## # ... with 22,667 more rows
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 ^GSPC 1989-12-29 0.171
## 2 ^GSPC 1990-12-31 -0.0656
## 3 ^GSPC 1991-12-31 0.263
## 4 ^GSPC 1992-12-31 0.0446
## 5 ^GSPC 1993-12-31 0.0706
## 6 ^GSPC 1994-12-30 -0.0154
## 7 ^GSPC 1995-12-29 0.341
## 8 ^GSPC 1996-12-31 0.203
## 9 ^GSPC 1997-12-31 0.310
## 10 ^GSPC 1998-12-31 0.267
## # ... with 83 more rows
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)
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 ^DJI 0.0917
## 2 ^GSPC 0.0895
## 3 ^IXIC 0.134
The NASDAQ has a higher expected yearly return, due to it having a higher return average than the other stocks.
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,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = sd)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol sd.1
## <chr> <dbl>
## 1 ^GSPC 0.165
## 2 ^IXIC 0.276
## 3 ^DJI 0.146
The NASDAQ would be the riskier stock becasue it has the highest standard deviation of the three.
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,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = skewness)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol skewness.1
## <chr> <dbl>
## 1 ^GSPC -0.819
## 2 ^IXIC 0.179
## 3 ^DJI -0.777
returns_yearly %>%
tq_performance(Ra = yearly.returns,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = kurtosis)
## # A tibble: 3 x 2
## # Groups: symbol [3]
## symbol kurtosis.1
## <chr> <dbl>
## 1 ^GSPC 0.631
## 2 ^IXIC 0.435
## 3 ^DJI 0.750
I do believe that the standard devitation is an appropriate form of measuring risk for stocks that are noramlly distributed. However, these stocks are not noramlly distributed. NASDAQ has both a postiive skewness and positive kurtosis. This means a large positive return is more likely than a large negative return, whereas both the DOW JONES and S&P 500 have a negative skewness and a positive kurtosis, meaning a large negative return is more likely than a large postive return.
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,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = table.DownsideRisk) %>%
t()
## [,1] [,2] [,3]
## symbol "^GSPC" "^IXIC" "^DJI"
## DownsideDeviation(0%) "0.0877" "0.1266" "0.0712"
## DownsideDeviation(MAR=0.833333333333333%) "0.0908" "0.1300" "0.0741"
## DownsideDeviation(Rf=0%) "0.0877" "0.1266" "0.0712"
## GainDeviation "0.0928" "0.2028" "0.0888"
## HistoricalES(95%) "-0.3093" "-0.3991" "-0.2530"
## HistoricalVaR(95%) "-0.1820" "-0.3541" "-0.1193"
## LossDeviation "0.1261" "0.1606" "0.1092"
## MaximumDrawdown "0.4012" "0.6718" "0.3384"
## ModifiedES(95%) "-0.3013" "-0.4083" "-0.2584"
## ModifiedVaR(95%) "-0.2116" "-0.2957" "-0.1718"
## SemiDeviation "0.1268" "0.1883" "0.1105"
NASDAQ appears to present greater downside risk than S&P 500 and DOW JONES. For example, yearly returns are more volatile below the mean for NASDAQ (semideviation of 0.1883) than S&P 500 (semideviation of 0.1268) and DOW JONES (semideviation of 0.1105); the largest loss one could expect with 95% confidence is larger for NASDAQ (VaR of -0.3541 at 5%) than S&P 500 (VaR of -0.1820 at 5%) and the DOW JONES (VaR of -0.1193); and the average of the 5% most negative yearly returns is larger for NAQDAQ (ES of -0.3991 at 5%) than S&P 500 (ES of -0.3093 at 5%) and DOW JONES (ES of -0.2530.
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,
Rb = NULL, # Calculataing downside risk measures doesn't require Rb
performance_fun = SharpeRatio)%>%
t()
## [,1] [,2] [,3]
## symbol "^GSPC" "^IXIC" "^DJI"
## ESSharpe(Rf=0%,p=95%) "0.2970939" "0.3284609" "0.3549842"
## StdDevSharpe(Rf=0%,p=95%) "0.5417175" "0.4863393" "0.6297887"
## VaRSharpe(Rf=0%,p=95%) "0.4230432" "0.4535240" "0.5338788"
I would choose the DOW JONES because it has a higher return per unit of risk.