Q1 Get monthly returns of Facebook, Amazon, and Netflix for the last 5 years.

library(tidyquant)
library(tidyverse)

from <- today() - years(5)
stock_returns_monthly <- c("FB", "AMZN", "NFLX") %>%
    tq_get(get  = "stock.prices",
           from = from) %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
stock_returns_monthly
## # A tibble: 183 x 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 FB     2015-11-30 -0.0287 
##  2 FB     2015-12-31  0.00403
##  3 FB     2016-01-29  0.0721 
##  4 FB     2016-02-29 -0.0471 
##  5 FB     2016-03-31  0.0672 
##  6 FB     2016-04-29  0.0305 
##  7 FB     2016-05-31  0.0105 
##  8 FB     2016-06-30 -0.0381 
##  9 FB     2016-07-29  0.0845 
## 10 FB     2016-08-31  0.0176 
## # ... with 173 more rows

Q2 Get monthly returns of NASDAQ for the same period as the baseline.

baseline_returns_monthly <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = from) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 61 x 2
##    date              Rb
##    <date>         <dbl>
##  1 2015-11-30  0.000735
##  2 2015-12-31 -0.0198  
##  3 2016-01-29 -0.0786  
##  4 2016-02-29 -0.0121  
##  5 2016-03-31  0.0684  
##  6 2016-04-29 -0.0194  
##  7 2016-05-31  0.0362  
##  8 2016-06-30 -0.0213  
##  9 2016-07-29  0.0660  
## 10 2016-08-31  0.00990 
## # ... with 51 more rows

Q3 Aggregate for 10 portfolios with the following weighting schemes.

#
stock_returns_monthly_multi <- stock_returns_monthly %>%
    tq_repeat_df(n = 10)
stock_returns_monthly_multi
## # A tibble: 1,830 x 4
## # Groups:   portfolio [10]
##    portfolio symbol date             Ra
##        <int> <chr>  <date>        <dbl>
##  1         1 FB     2015-11-30 -0.0287 
##  2         1 FB     2015-12-31  0.00403
##  3         1 FB     2016-01-29  0.0721 
##  4         1 FB     2016-02-29 -0.0471 
##  5         1 FB     2016-03-31  0.0672 
##  6         1 FB     2016-04-29  0.0305 
##  7         1 FB     2016-05-31  0.0105 
##  8         1 FB     2016-06-30 -0.0381 
##  9         1 FB     2016-07-29  0.0845 
## 10         1 FB     2016-08-31  0.0176 
## # ... with 1,820 more rows
# Assign weights to individual stocks
weights <- c(
    0.80, 0.10, 0.10,
    0.10, 0.80, 0.10,
    0.10, 0.10, 0.80,
    0.60, 0.20, 0.20,
    0.20, 0.60, 0.20,
    0.20, 0.20, 0.60,
    0.50, 0.25, 0.25,
    0.25, 0.50, 0.25,
    0.25, 0.25, 0.50,
    0.40, 0.40, 0.20
)
stocks <- c("FB", "AMZN", "NFLX")
weights_table <-  tibble(stocks) %>%
    tq_repeat_df(n = 10) %>%
    bind_cols(tibble(weights)) %>%
    group_by(portfolio)
weights_table
## # A tibble: 30 x 3
## # Groups:   portfolio [10]
##    portfolio stocks weights
##        <int> <chr>    <dbl>
##  1         1 FB         0.8
##  2         1 AMZN       0.1
##  3         1 NFLX       0.1
##  4         2 FB         0.1
##  5         2 AMZN       0.8
##  6         2 NFLX       0.1
##  7         3 FB         0.1
##  8         3 AMZN       0.1
##  9         3 NFLX       0.8
## 10         4 FB         0.6
## # ... with 20 more rows
# Aggregate a Portfolio using Vector of Weights
portfolio_returns_monthly  <-
  stock_returns_monthly_multi %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = weights_table, 
                 col_rename  = "Ra")
portfolio_returns_monthly 
## # A tibble: 610 x 3
## # Groups:   portfolio [10]
##    portfolio date             Ra
##        <int> <date>        <dbl>
##  1         1 2015-11-30 -0.0239 
##  2         1 2015-12-31 -0.00250
##  3         1 2016-01-29  0.0255 
##  4         1 2016-02-29 -0.0434 
##  5         1 2016-03-31  0.0699 
##  6         1 2016-04-29  0.0254 
##  7         1 2016-05-31  0.0274 
##  8         1 2016-06-30 -0.0407 
##  9         1 2016-07-29  0.0758 
## 10         1 2016-08-31  0.0205 
## # ... with 600 more rows

Q4 Calcualte the Sharpe Ratio per portfolio.

RaRb_multi_portfolio <- left_join(portfolio_returns_monthly , 
                                   baseline_returns_monthly,
                                   by = "date")
RaRb_multi_portfolio
## # A tibble: 610 x 4
## # Groups:   portfolio [10]
##    portfolio date             Ra        Rb
##        <int> <date>        <dbl>     <dbl>
##  1         1 2015-11-30 -0.0239   0.000735
##  2         1 2015-12-31 -0.00250 -0.0198  
##  3         1 2016-01-29  0.0255  -0.0786  
##  4         1 2016-02-29 -0.0434  -0.0121  
##  5         1 2016-03-31  0.0699   0.0684  
##  6         1 2016-04-29  0.0254  -0.0194  
##  7         1 2016-05-31  0.0274   0.0362  
##  8         1 2016-06-30 -0.0407  -0.0213  
##  9         1 2016-07-29  0.0758   0.0660  
## 10         1 2016-08-31  0.0205   0.00990 
## # ... with 600 more rows
# Sharpe Ratio
RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12) 
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         1                          0.909
##  2         2                          1.25 
##  3         3                          0.933
##  4         4                          1.04 
##  5         5                          1.23 
##  6         6                          1.03 
##  7         7                          1.09 
##  8         8                          1.20 
##  9         9                          1.08 
## 10        10                          1.16

Q5 Sort the portfolios in descending order of Sharpe Ratio.

Hint: Use dplyr::arrange().

RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 12) %>%
arrange(desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         2                          1.25 
##  2         5                          1.23 
##  3         8                          1.20 
##  4        10                          1.16 
##  5         7                          1.09 
##  6         9                          1.08 
##  7         4                          1.04 
##  8         6                          1.03 
##  9         3                          0.933
## 10         1                          0.909

Q6 Which weighting scheme would have performed the best?

Hint: Make your argument using the calculated Sharpe

The weighting scheme that performed the best was the second weighting scheme. It had 10% weight on both FB and NFLX while having 80% weight on AMZN. We know this because the second scheme had the highest sharp ratio at 1.25.

Q7 Which weighting scheme is most volatile?

Hint: Calculate Beta from the Capital Asset Pricing Model. Make your argument based on the calculated Beta.

RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  t()
##                      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]
## portfolio          1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000
## ActivePremium      0.0511 0.1557 0.1225 0.0790 0.1380 0.1184 0.0921 0.1288
## Alpha              0.0027 0.0090 0.0093 0.0045 0.0080 0.0081 0.0054 0.0075
## AnnualizedAlpha    0.0328 0.1138 0.1170 0.0557 0.1004 0.1012 0.0668 0.0942
## Beta               1.1484 1.2108 1.1353 1.1452 1.1891 1.1414 1.1474 1.1772
## Beta-              0.6889 1.0701 0.8992 0.7752 0.9917 0.8852 0.8162 0.9516
## Beta+              1.5794 1.3206 1.3370 1.4857 1.3507 1.3588 1.4473 1.3661
## Correlation        0.7716 0.7701 0.5990 0.7893 0.7840 0.6759 0.7865 0.7834
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   0.3105 0.8846 0.4682 0.5099 0.8366 0.5503 0.5874 0.7886
## R-squared          0.5954 0.5931 0.3588 0.6231 0.6147 0.4569 0.6186 0.6137
## TrackingError      0.1646 0.1760 0.2617 0.1550 0.1649 0.2151 0.1568 0.1633
## TreynorRatio       0.2023 0.2783 0.2676 0.2273 0.2685 0.2625 0.2382 0.2633
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.1163  0.1099
## Alpha              0.0076  0.0063
## AnnualizedAlpha    0.0945  0.0782
## Beta               1.1458  1.1664
## Beta-              0.8824  0.8876
## Beta+              1.3707  1.4122
## Correlation        0.7134  0.8008
## Correlationp-value 0.0000  0.0000
## InformationRatio   0.5965  0.7205
## R-squared          0.5089  0.6413
## TrackingError      0.1949  0.1525
## TreynorRatio       0.2597  0.2496

The weighting scheme that is the most volatile is scheme 2. This is know surprise. Lots of times performance comes with risk and volatility. Scheme 2 has a beta of 1.2108. Then next most volatile weighting scheme is scheme 5 with a beta of 1.1891.

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.