Suppose that you consider investing in two stocks: Microsoft and General Electric. As a prudent investor, you analyze the historical performance of the stocks during the period of 1990-01-01 to 2017-12-31.

Q1 Load tidyquant and tidyverse packages.


library(tidyquant)
library(tidyverse)

Q2 Import stock prices of S&P 500 and NASDAQ Compsite Index for the last 20 years.

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

# Import data
from = today() - years(20)
Stocks <- tq_get(c("^GSPC", "^IXIC"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 10,064 x 8
## # Groups:   symbol [2]
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  1999-04-09 1344. 1351. 1335. 1348.  716100000    1348.
##  2 ^GSPC  1999-04-12 1348. 1359. 1333. 1359.  810800000    1359.
##  3 ^GSPC  1999-04-13 1359. 1362. 1344. 1350.  810900000    1350.
##  4 ^GSPC  1999-04-14 1350. 1357. 1326. 1328.  952000000    1328.
##  5 ^GSPC  1999-04-15 1328. 1332. 1308. 1323. 1089800000    1323.
##  6 ^GSPC  1999-04-16 1323. 1325. 1311. 1319  1002300000    1319 
##  7 ^GSPC  1999-04-19 1319  1340. 1284. 1289. 1214400000    1289.
##  8 ^GSPC  1999-04-20 1289. 1306. 1284. 1306.  985400000    1306.
##  9 ^GSPC  1999-04-21 1306. 1336. 1302. 1336.  920000000    1336.
## 10 ^GSPC  1999-04-22 1336. 1359. 1336. 1359.  927900000    1359.
## # ... with 10,054 more rows

Q3 Calculate monthly returns, and save the result under returns_monthly.

Hint: Take the adjusted variable from Stocks, and calculate monthly returns using tq_transmute(), instead of tq_mutate(), which is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.


returns_monthly <-
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly") 

returns_monthly
## # A tibble: 482 x 3
## # Groups:   symbol [2]
##    symbol date       monthly.returns
##    <chr>  <date>               <dbl>
##  1 ^GSPC  1999-04-30        -0.00977
##  2 ^GSPC  1999-05-28        -0.0250 
##  3 ^GSPC  1999-06-30         0.0544 
##  4 ^GSPC  1999-07-30        -0.0320 
##  5 ^GSPC  1999-08-31        -0.00625
##  6 ^GSPC  1999-09-30        -0.0286 
##  7 ^GSPC  1999-10-29         0.0625 
##  8 ^GSPC  1999-11-30         0.0191 
##  9 ^GSPC  1999-12-31         0.0578 
## 10 ^GSPC  2000-01-31        -0.0509 
## # ... with 472 more rows

Note that there are a variety of functions available with the mutate_fun argument: See above for the result of tq_transmute_fun_options(). Note that tq_mutate allows to calculate returns using quantmod::periodReturn. Google the quantmod package manual for more information on the periodReturn function.

Q4 Create a density plot for returns of both stocks.

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_monthly %>%
    ggplot(aes(x = monthly.returns, fill = symbol) ) +
    geom_density(alpha = 0.3) 

Q5 Which of the two stocks have higher expected monthly return?

Hint: Take returns_monthly and pipe it to summarise. Calculate the mean monthly returns.

NASDAQ has more mass on the positive side on returns meaning it would have higher expected monthly returns. On top of that is has a lot more mass the further you go into the positives in return.

Q6 Which of the two stocks is riskier?

Hint: Discuss your answer in terms of standard deviation. Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute sd (standard deviation).

returns_monthly %>%
  tq_performance(Ra = monthly.returns, performance_fun = SharpeRatio)
## # A tibble: 2 x 4
## # Groups:   symbol [2]
##   symbol `ESSharpe(Rf=0%,p=95~ `StdDevSharpe(Rf=0%,p=~ `VaRSharpe(Rf=0%,p=~
##   <chr>                  <dbl>                   <dbl>                <dbl>
## 1 ^GSPC                 0.0402                  0.0972               0.0579
## 2 ^IXIC                 0.0433                  0.105                0.0653

In terms of standard deviation the stocks are very close but NASDAQ has a higher risk in terms of standard deviation (Being higher by .003).

Q7 Are the returns normally distributed?

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_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute skewness. Do the same for kurtosis.

For interpretation, see a hypothetical example below.

MSFT had a postiive skewness and positive kurtosis, making a large positive return more likely than a large negative return, whereas S&P 500 had a negative skewness and a positive kurtosis, which means a large negative return is more likely than a large postive return.

returns_monthly %>%
  tq_performance(Ra = monthly.returns, performance_fun = skewness)
## # A tibble: 2 x 2
## # Groups:   symbol [2]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 ^GSPC      -0.571
## 2 ^IXIC      -0.364
returns_monthly %>%
  tq_performance(Ra = monthly.returns, performance_fun = kurtosis)
## # A tibble: 2 x 2
## # Groups:   symbol [2]
##   symbol kurtosis.1
##   <chr>       <dbl>
## 1 ^GSPC        1.10
## 2 ^IXIC        1.57

Both Stock Indexes have negative skewness and positive kurtosis, meaning that both have a higher chance at high negative loses rather than high positive loses.

Q8 What additional risk measures that focus on describing the potential losses do investors use, when the return distribution is not normal? List three.

Beta, VaR, and SharpeRatio

Q9 Calculate the downside risk measures by revising the code below. Which of the two stocks has greater downside risks? Discuss HistoricalES(95%), HistoricalVaR(95%), and SemiDeviation.

Hint: Take returns_monthly and pipe it to tidyquant::tq_performance. Use the performance_fun argument to compute table.DownsideRisk.

For interpretation, see a hypothetical example below.

returns_monthly %>%
  tq_performance(Ra = monthly.returns, performance_fun = table.DownsideRisk)
## # A tibble: 2 x 12
## # Groups:   symbol [2]
##   symbol `DownsideDeviat~ `DownsideDeviat~ `DownsideDeviat~ GainDeviation
##   <chr>             <dbl>            <dbl>            <dbl>         <dbl>
## 1 ^GSPC            0.0297           0.0338           0.0297        0.0237
## 2 ^IXIC            0.0446           0.0487           0.0446        0.0398
## # ... with 7 more variables: `HistoricalES(95%)` <dbl>,
## #   `HistoricalVaR(95%)` <dbl>, LossDeviation <dbl>,
## #   MaximumDrawdown <dbl>, `ModifiedES(95%)` <dbl>,
## #   `ModifiedVaR(95%)` <dbl>, SemiDeviation <dbl>

HistoricalES(95%) (-0.0949 and -0.1456) means that each percentage (-9.49% and -14.56%) is the average of the 5% most negative returns. HistoricalVaR(95%) (-0.0748 and -0.1047) means that the percentage respectively (-7.48% and -10.47%) will be the maximum loss expected with 95% confidence. Semi Deviation(0.0316 and 0.0479) Semi-Deviation of (0.0316 and 0.0479) means that the standard deviation of returns below the mean return is 3.16% and 4.79% respectively.

Q10 Which of the two stocks would you choose?

Hint: Discuss your answer in terms of risk and reward.

Sharpe Ratio measures return per unit of risk.

Given i am a risk adverse investor, I would personally invest in the S&P 500 given the risk is a lot lower and still has good positive returns. Even if the return are not as high as NASDAQ, the risk that comes with NASDAQ is unappealing to me. Risk wise definetely go with S&P500, where with reward we see a higher one with NASDAQ, but the ratio between the two makes the risk reward for S&P500 much better.