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.0328 
##  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.00659
##  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.0328 
##  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
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
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.0238 
##  2         1 2015-12-31 -0.00269
##  3         1 2016-01-29  0.0246 
##  4         1 2016-02-29 -0.0433 
##  5         1 2016-03-31  0.0700 
##  6         1 2016-04-29  0.0251 
##  7         1 2016-05-31  0.0278 
##  8         1 2016-06-30 -0.0408 
##  9         1 2016-07-29  0.0756 
## 10         1 2016-08-31  0.0206 
## # ... 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.0238   0.00659
##  2         1 2015-12-31 -0.00269 -0.0198 
##  3         1 2016-01-29  0.0246  -0.0786 
##  4         1 2016-02-29 -0.0433  -0.0121 
##  5         1 2016-03-31  0.0700   0.0684 
##  6         1 2016-04-29  0.0251  -0.0194 
##  7         1 2016-05-31  0.0278   0.0362 
##  8         1 2016-06-30 -0.0408  -0.0213 
##  9         1 2016-07-29  0.0756   0.0660 
## 10         1 2016-08-31  0.0206   0.00990
## # ... with 600 more rows
# Sharpe Ratio
RaRb_multi_portfolio %>%
  tq_performance(Ra = Ra, Rb = NULL, performance_fun = SharpeRatio.annualized, scale = 10) 
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         1                          0.821
##  2         2                          1.12 
##  3         3                          0.842
##  4         4                          0.943
##  5         5                          1.10 
##  6         6                          0.931
##  7         7                          0.984
##  8         8                          1.08 
##  9         9                          0.972
## 10        10                          1.05

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 = 10) %>%
  arrange(desc(`AnnualizedSharpeRatio(Rf=0%)`))
## # A tibble: 10 x 2
## # Groups:   portfolio [10]
##    portfolio `AnnualizedSharpeRatio(Rf=0%)`
##        <int>                          <dbl>
##  1         2                          1.12 
##  2         5                          1.10 
##  3         8                          1.08 
##  4        10                          1.05 
##  5         7                          0.984
##  6         9                          0.972
##  7         4                          0.943
##  8         6                          0.931
##  9         3                          0.842
## 10         1                          0.821

Q6 Which weighting scheme would have performed the best?

Hint: Make your argument using the calculated Sharpe

The best weighting scheme would be scheme 2, this is because it has the highest ratio of the three.

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.0515 0.1579 0.1255 0.0802 0.1401 0.1209 0.0936 0.1308
## Alpha              0.0027 0.0091 0.0095 0.0046 0.0081 0.0083 0.0055 0.0076
## AnnualizedAlpha    0.0326 0.1148 0.1203 0.0563 0.1016 0.1038 0.0678 0.0955
## Beta               1.1506 1.2131 1.1314 1.1463 1.1905 1.1392 1.1481 1.1782
## Beta-              0.6901 1.0701 0.8999 0.7766 0.9920 0.8861 0.8176 0.9521
## Beta+              1.5918 1.3261 1.3200 1.4917 1.3538 1.3487 1.4507 1.3679
## Correlation        0.7729 0.7714 0.5955 0.7896 0.7844 0.6727 0.7861 0.7832
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   0.3139 0.8993 0.4774 0.5176 0.8500 0.5588 0.5961 0.8006
## R-squared          0.5974 0.5951 0.3547 0.6234 0.6153 0.4525 0.6180 0.6134
## TrackingError      0.1642 0.1756 0.2629 0.1550 0.1648 0.2164 0.1570 0.1634
## TreynorRatio       0.2034 0.2806 0.2722 0.2292 0.2710 0.2664 0.2405 0.2659
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.1186  0.1116
## Alpha              0.0077  0.0064
## AnnualizedAlpha    0.0967  0.0791
## Beta               1.1444  1.1678
## Beta-              0.8833  0.8887
## Beta+              1.3641  1.4166
## Correlation        0.7106  0.8010
## Correlationp-value 0.0000  0.0000
## InformationRatio   0.6050  0.7314
## R-squared          0.5049  0.6416
## TrackingError      0.1961  0.1525
## TreynorRatio       0.2631  0.2518

The worst weighting scheme would be scheme 4. It has the highest volatility at a level of 0.7151

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.