# 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
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)
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, #returns
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
IXIC has a higher expected yeaerly return at a mean of 0.143, 14.3% return whcih is better than DJI at 0.0871, 8.71% return and GSPC at 0.0880, 8.8% return
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, #returns
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
IXIC is the riskier stock at a standard deviation of 0.279 cmopared to DJI at 0.151 and GSPC at 0.169
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, #returns
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, #returns
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
Standard deviation alone is not good enough as a measure of risk. You would need more additional downside risk measurements because the skewness shows that DJI and GSPC are more likely to have large negative returns while IXIC more likely to have large positive returns. However the kurtosis shows that they all will have extreme positive and negative returns more often. The standard deviation is also not normaly distrubted so we cannot use it. This tells us that standard deviation alone is not good enough as a measure of risk.
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, #returns
performance_fun = table.DownsideRisk
) %>%
t( )
## [,1] [,2] [,3]
## symbol "^DJI" "^GSPC" "^IXIC"
## DownsideDeviation(0%) "0.0743" "0.0882" "0.1270"
## DownsideDeviation(MAR=0.833333333333333%) "0.0775" "0.0913" "0.1304"
## DownsideDeviation(Rf=0%) "0.0743" "0.0882" "0.1270"
## GainDeviation "0.0935" "0.1023" "0.2005"
## HistoricalES(95%) "-0.2530" "-0.3093" "-0.3991"
## HistoricalVaR(95%) "-0.1291" "-0.1820" "-0.3541"
## LossDeviation "0.1078" "0.1254" "0.1605"
## MaximumDrawdown "0.3384" "0.4012" "0.6718"
## ModifiedES(95%) "-0.2632" "-0.3017" "-0.4179"
## ModifiedVaR(95%) "-0.1831" "-0.2161" "-0.2995"
## SemiDeviation "0.1131" "0.1275" "0.1931"
IXIC has the greatest downside risk. This is because its absolute values for HistoricalES, HistoricalVaR and SemiDeviation are all greater than DJI and GSPC absolute values. In all risk measures IXIC poses a greater risk but it also the stock that give the highest expected return.
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, #returns
performance_fun = SharpeRatio
)
## # 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 DJI because it has a higher value for Sharpe Ratios of VaR and semideviation. It lacks barely behind IXIC in Sharpe Ratio ES of 0.342 for IXIC and 0.331 for DJI. Comparing all of this, I would go with DJI as the stock of investment for me.
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, #returns
performance_fun = SharpeRatio,
p = 0.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
My answer is differnet from question 6 being that I would choose IXIC because it has a higher value for Sharpe Ratios of VaR and ES. It lacks barely behind IXIC in Sharpe Ratio semideviation of 0.576 for DJI and 0.512 for IXIC. Comparing all of this, I would go with IXIC as the stock of investment for me.
Hint: Use message, warning, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.