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

Asset Period Returns

# Load packages  
library(tidyquant)
library(tidyverse)

from <- today() - years(5)
stock_returns_monthly <- c("AMZN", "NFLX", "TSLA") %>%
    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 AMZN   2016-04-29  0       
##  2 AMZN   2016-05-31  0.0958  
##  3 AMZN   2016-06-30 -0.00992 
##  4 AMZN   2016-07-29  0.0604  
##  5 AMZN   2016-08-31  0.0136  
##  6 AMZN   2016-09-30  0.0886  
##  7 AMZN   2016-10-31 -0.0567  
##  8 AMZN   2016-11-30 -0.0497  
##  9 AMZN   2016-12-30 -0.000933
## 10 AMZN   2017-01-31  0.0982  
## # … with 173 more rows

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

Baseline Period Returns

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 2016-04-29  0      
##  2 2016-05-31  0.0362 
##  3 2016-06-30 -0.0213 
##  4 2016-07-29  0.0660 
##  5 2016-08-31  0.00990
##  6 2016-09-30  0.0189 
##  7 2016-10-31 -0.0231 
##  8 2016-11-30  0.0259 
##  9 2016-12-30  0.0112 
## 10 2017-01-31  0.0430 
## # … with 51 more rows

Q3 Aggregate for 10 portfolios with the following weighting schemes.

Aggregate

#
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 AMZN   2016-04-29  0       
##  2         1 AMZN   2016-05-31  0.0958  
##  3         1 AMZN   2016-06-30 -0.00992 
##  4         1 AMZN   2016-07-29  0.0604  
##  5         1 AMZN   2016-08-31  0.0136  
##  6         1 AMZN   2016-09-30  0.0886  
##  7         1 AMZN   2016-10-31 -0.0567  
##  8         1 AMZN   2016-11-30 -0.0497  
##  9         1 AMZN   2016-12-30 -0.000933
## 10         1 AMZN   2017-01-31  0.0982  
## # … 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("TSLA", "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 TSLA       0.8
##  2         1 AMZN       0.1
##  3         1 NFLX       0.1
##  4         2 TSLA       0.1
##  5         2 AMZN       0.8
##  6         2 NFLX       0.1
##  7         3 TSLA       0.1
##  8         3 AMZN       0.1
##  9         3 NFLX       0.8
## 10         4 TSLA       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 2016-04-29  0      
##  2         1 2016-05-31 -0.0347 
##  3         1 2016-06-30 -0.0516 
##  4         1 2016-07-29  0.0886 
##  5         1 2016-08-31 -0.0675 
##  6         1 2016-09-30 -0.0161 
##  7         1 2016-10-31  0.00120
##  8         1 2016-11-30 -0.0463 
##  9         1 2016-12-30  0.101  
## 10         1 2017-01-31  0.163  
## # … with 600 more rows

Q4 Calcualte the Sharpe Ratio per portfolio.

Calculate performance

# Merging Ra and Rb
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 2016-04-29  0        0      
##  2         1 2016-05-31 -0.0347   0.0362 
##  3         1 2016-06-30 -0.0516  -0.0213 
##  4         1 2016-07-29  0.0886   0.0660 
##  5         1 2016-08-31 -0.0675   0.00990
##  6         1 2016-09-30 -0.0161   0.0189 
##  7         1 2016-10-31  0.00120 -0.0231 
##  8         1 2016-11-30 -0.0463   0.0259 
##  9         1 2016-12-30  0.101    0.0112 
## 10         1 2017-01-31  0.163    0.0430 
## # … with 600 more rows
# Beta and Alpha
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.4102 0.1952 0.2072 0.3612 0.2362 0.2424 0.3342 0.2551
## Alpha              0.0155 0.0086 0.0140 0.0130 0.0097 0.0129 0.0123 0.0104
## AnnualizedAlpha    0.2024 0.1076 0.1816 0.1681 0.1233 0.1659 0.1585 0.1321
## Beta               1.9166 1.3061 1.1156 1.7490 1.3888 1.2725 1.6538 1.4256
## Beta-              0.3888 0.8608 0.8610 0.6543 0.8389 0.8397 0.7291 0.8265
## Beta+              2.7186 1.5075 1.4671 2.4390 1.7220 1.6960 2.2776 1.8217
## Correlation        0.6200 0.7921 0.5728 0.6948 0.7939 0.6722 0.7232 0.7819
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   0.9436 1.1067 0.7719 1.0994 1.2438 1.0123 1.1661 1.2534
## R-squared          0.3844 0.6274 0.3281 0.4827 0.6303 0.4519 0.5231 0.6114
## TrackingError      0.4348 0.1764 0.2684 0.3285 0.1899 0.2394 0.2866 0.2035
## TreynorRatio       0.3374 0.3306 0.3977 0.3418 0.3404 0.3763 0.3451 0.3448
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.2587  0.3039
## Alpha              0.0124  0.0111
## AnnualizedAlpha    0.1588  0.1417
## Beta               1.3505  1.5857
## Beta-              0.8277  0.7785
## Beta+              1.8050  2.1088
## Correlation        0.7106  0.7604
## Correlationp-value 0.0000  0.0000
## InformationRatio   1.1161  1.2281
## R-squared          0.5049  0.5782
## TrackingError      0.2318  0.2474
## TreynorRatio       0.3667  0.3408
# 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                           1.25
##  2         2                           1.56
##  3         3                           1.36
##  4         4                           1.42
##  5         5                           1.61
##  6         6                           1.51
##  7         7                           1.49
##  8         8                           1.61
##  9         9                           1.55
## 10        10                           1.55

Q5 Sort the portfolios in descending order of Sharpe Ratio.

Hint: Use dplyr::arrange().

Calculate performance

# Merging Ra and Rb
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 2016-04-29  0        0      
##  2         1 2016-05-31 -0.0347   0.0362 
##  3         1 2016-06-30 -0.0516  -0.0213 
##  4         1 2016-07-29  0.0886   0.0660 
##  5         1 2016-08-31 -0.0675   0.00990
##  6         1 2016-09-30 -0.0161   0.0189 
##  7         1 2016-10-31  0.00120 -0.0231 
##  8         1 2016-11-30 -0.0463   0.0259 
##  9         1 2016-12-30  0.101    0.0112 
## 10         1 2017-01-31  0.163    0.0430 
## # … with 600 more rows
# Beta and Alpha
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.4102 0.1952 0.2072 0.3612 0.2362 0.2424 0.3342 0.2551
## Alpha              0.0155 0.0086 0.0140 0.0130 0.0097 0.0129 0.0123 0.0104
## AnnualizedAlpha    0.2024 0.1076 0.1816 0.1681 0.1233 0.1659 0.1585 0.1321
## Beta               1.9166 1.3061 1.1156 1.7490 1.3888 1.2725 1.6538 1.4256
## Beta-              0.3888 0.8608 0.8610 0.6543 0.8389 0.8397 0.7291 0.8265
## Beta+              2.7186 1.5075 1.4671 2.4390 1.7220 1.6960 2.2776 1.8217
## Correlation        0.6200 0.7921 0.5728 0.6948 0.7939 0.6722 0.7232 0.7819
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   0.9436 1.1067 0.7719 1.0994 1.2438 1.0123 1.1661 1.2534
## R-squared          0.3844 0.6274 0.3281 0.4827 0.6303 0.4519 0.5231 0.6114
## TrackingError      0.4348 0.1764 0.2684 0.3285 0.1899 0.2394 0.2866 0.2035
## TreynorRatio       0.3374 0.3306 0.3977 0.3418 0.3404 0.3763 0.3451 0.3448
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.2587  0.3039
## Alpha              0.0124  0.0111
## AnnualizedAlpha    0.1588  0.1417
## Beta               1.3505  1.5857
## Beta-              0.8277  0.7785
## Beta+              1.8050  2.1088
## Correlation        0.7106  0.7604
## Correlationp-value 0.0000  0.0000
## InformationRatio   1.1161  1.2281
## R-squared          0.5049  0.5782
## TrackingError      0.2318  0.2474
## TreynorRatio       0.3667  0.3408
# Sharpe Ratio
RaRb_multi_portfolio %>%
  arrange(desc(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        10                           1.55
##  2         9                           1.55
##  3         8                           1.61
##  4         7                           1.49
##  5         6                           1.51
##  6         5                           1.61
##  7         4                           1.42
##  8         3                           1.36
##  9         2                           1.56
## 10         1                           1.25

I used the code arrange(desc(portfolio)) to make the portfolio descend under Sharpe Ratio.

Q6 Which weighting scheme would have performed the best?

Hint: Make your argument using the calculated Sharpe

10 –> Tesla(0.40), Amazon(0.40), Netflix (0.20) performed the best because:

this weighting scheme was the most evenly distributed weight scheme out of all of the ones in the listed weight table. The more evenly distributed weight the better the three companies will perform. as a whole

Q7 Which weighting scheme is most volatile?

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

# Beta and Alpha
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.4102 0.1952 0.2072 0.3612 0.2362 0.2424 0.3342 0.2551
## Alpha              0.0155 0.0086 0.0140 0.0130 0.0097 0.0129 0.0123 0.0104
## AnnualizedAlpha    0.2024 0.1076 0.1816 0.1681 0.1233 0.1659 0.1585 0.1321
## Beta               1.9166 1.3061 1.1156 1.7490 1.3888 1.2725 1.6538 1.4256
## Beta-              0.3888 0.8608 0.8610 0.6543 0.8389 0.8397 0.7291 0.8265
## Beta+              2.7186 1.5075 1.4671 2.4390 1.7220 1.6960 2.2776 1.8217
## Correlation        0.6200 0.7921 0.5728 0.6948 0.7939 0.6722 0.7232 0.7819
## Correlationp-value 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## InformationRatio   0.9436 1.1067 0.7719 1.0994 1.2438 1.0123 1.1661 1.2534
## R-squared          0.3844 0.6274 0.3281 0.4827 0.6303 0.4519 0.5231 0.6114
## TrackingError      0.4348 0.1764 0.2684 0.3285 0.1899 0.2394 0.2866 0.2035
## TreynorRatio       0.3374 0.3306 0.3977 0.3418 0.3404 0.3763 0.3451 0.3448
##                      [,9]   [,10]
## portfolio          9.0000 10.0000
## ActivePremium      0.2587  0.3039
## Alpha              0.0124  0.0111
## AnnualizedAlpha    0.1588  0.1417
## Beta               1.3505  1.5857
## Beta-              0.8277  0.7785
## Beta+              1.8050  2.1088
## Correlation        0.7106  0.7604
## Correlationp-value 0.0000  0.0000
## InformationRatio   1.1161  1.2281
## R-squared          0.5049  0.5782
## TrackingError      0.2318  0.2474
## TreynorRatio       0.3667  0.3408

Tesla(0.80), Amazon(0.10), Netflix (0.10)

This weight scheme is the most volatile because it has the most amount of change in it’s Beta from Beta- and Beta +

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.