Q1 Import stock prices and calculate monthly returns.

Hint: Import Apple, Google, Netflix, Microsoft and Tesla from “2015-01-01” to “2018-12-31”.

library(tidyquant)
library(tidyverse)

# Asset Period Returns
stock_returns_monthly <- c("AAPL", "GOOG", "NFLX", "MSFT", "TSLA") %>%
    tq_get(get  = "stock.prices",
           from = "2015-01-01",
           to   = "2018-12-31") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
stock_returns_monthly 
## # A tibble: 240 x 3
## # Groups:   symbol [5]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 AAPL   2015-01-30  0.0716 
##  2 AAPL   2015-02-27  0.101  
##  3 AAPL   2015-03-31 -0.0314 
##  4 AAPL   2015-04-30  0.00579
##  5 AAPL   2015-05-29  0.0453 
##  6 AAPL   2015-06-30 -0.0372 
##  7 AAPL   2015-07-31 -0.0329 
##  8 AAPL   2015-08-31 -0.0662 
##  9 AAPL   2015-09-30 -0.0218 
## 10 AAPL   2015-10-30  0.0834 
## # ... with 230 more rows

Q2 Import S&P500 as a baseline index fund and calculate monthly returns.

# Baseline Period Returns
baseline_returns_monthly <- "^GSPC" %>%
    tq_get(get  = "stock.prices",
           from = "2015-01-01",
           to   = "2018-12-31") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 48 x 2
##    date             Rb
##    <date>        <dbl>
##  1 2015-01-30 -0.0307 
##  2 2015-02-27  0.0549 
##  3 2015-03-31 -0.0174 
##  4 2015-04-30  0.00852
##  5 2015-05-29  0.0105 
##  6 2015-06-30 -0.0210 
##  7 2015-07-31  0.0197 
##  8 2015-08-31 -0.0626 
##  9 2015-09-30 -0.0264 
## 10 2015-10-30  0.0830 
## # ... with 38 more rows

Q3 Aggregate three Portfolios with the following weighting schemes.

stock_returns_monthly_multi <- stock_returns_monthly %>%
    tq_repeat_df(n = 3)
stock_returns_monthly_multi
## # A tibble: 720 x 4
## # Groups:   portfolio [3]
##    portfolio symbol date             Ra
##        <int> <chr>  <date>        <dbl>
##  1         1 AAPL   2015-01-30  0.0716 
##  2         1 AAPL   2015-02-27  0.101  
##  3         1 AAPL   2015-03-31 -0.0314 
##  4         1 AAPL   2015-04-30  0.00579
##  5         1 AAPL   2015-05-29  0.0453 
##  6         1 AAPL   2015-06-30 -0.0372 
##  7         1 AAPL   2015-07-31 -0.0329 
##  8         1 AAPL   2015-08-31 -0.0662 
##  9         1 AAPL   2015-09-30 -0.0218 
## 10         1 AAPL   2015-10-30  0.0834 
## # ... with 710 more rows
# Create Vector of Weights
# not all symbols need to be specified. Any symbol not specified by default gets a weight of zero.
weights <- c(
    0.1, 0.1, 0.1, 0.1, 0.6,
    0.1, 0.1, 0.1, 0.6, 0.1,
    0.1, 0.1, 0.6, 0.1, 0.1

)
stocks <- c("AAPL", "GOOG", "NFLX", "MSFT", "TSLA")
weights_table <-  tibble(stocks) %>%
    tq_repeat_df(n = 3) %>%
    bind_cols(tibble(weights)) %>%
    group_by(portfolio)
weights_table
## # A tibble: 15 x 3
## # Groups:   portfolio [3]
##    portfolio stocks weights
##        <int> <chr>    <dbl>
##  1         1 AAPL       0.1
##  2         1 GOOG       0.1
##  3         1 NFLX       0.1
##  4         1 MSFT       0.1
##  5         1 TSLA       0.6
##  6         2 AAPL       0.1
##  7         2 GOOG       0.1
##  8         2 NFLX       0.1
##  9         2 MSFT       0.6
## 10         2 TSLA       0.1
## 11         3 AAPL       0.1
## 12         3 GOOG       0.1
## 13         3 NFLX       0.6
## 14         3 MSFT       0.1
## 15         3 TSLA       0.1


# Aggregate a Portfolio using Vector of Weights
portfolio_returns_monthly_multi  <-
  stock_returns_monthly_multi %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = weights_table, 
                 col_rename  = "Ra")
portfolio_returns_monthly_multi 
## # A tibble: 144 x 3
## # Groups:   portfolio [3]
##    portfolio date            Ra
##        <int> <date>       <dbl>
##  1         1 2015-01-30 -0.0210
##  2         1 2015-02-27  0.0329
##  3         1 2015-03-31 -0.0683
##  4         1 2015-04-30  0.168 
##  5         1 2015-05-29  0.0801
##  6         1 2015-06-30  0.0375
##  7         1 2015-07-31  0.0463
##  8         1 2015-08-31 -0.0468
##  9         1 2015-09-30 -0.0227
## 10         1 2015-10-30 -0.0472
## # ... with 134 more rows

Q4 What is the portfolio’s return on the first period? Explain how it’s computed.

The return on the first period is -.02. We get this by ((Apple stock first period= .071)) x ((apple weight= .1)) + ((Google stock first period =0.018)) X ((Google weight =*.1)) + ((Netflix stock first period =0.266)) X ((Netflix weight= .1)) + (( Microsoft first period stock -0.136) x (Microsoft weight.1)) + ((Tesla Stock first period = -0.0716) x (Tesla weight= .6)) = -.02 <- for first portolio.

Q5 Merge Ra and Rb

RaRb_single_portfolio <- left_join(portfolio_returns_monthly_multi, baseline_returns_monthly,by = "date")
RaRb_single_portfolio
## # A tibble: 144 x 4
## # Groups:   portfolio [?]
##    portfolio date            Ra       Rb
##        <int> <date>       <dbl>    <dbl>
##  1         1 2015-01-30 -0.0210 -0.0307 
##  2         1 2015-02-27  0.0329  0.0549 
##  3         1 2015-03-31 -0.0683 -0.0174 
##  4         1 2015-04-30  0.168   0.00852
##  5         1 2015-05-29  0.0801  0.0105 
##  6         1 2015-06-30  0.0375 -0.0210 
##  7         1 2015-07-31  0.0463  0.0197 
##  8         1 2015-08-31 -0.0468 -0.0626 
##  9         1 2015-09-30 -0.0227 -0.0264 
## 10         1 2015-10-30 -0.0472  0.0830 
## # ... with 134 more rows

Q6 Compute the CAPM Table

RaRb_single_portfolio %>%
    tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  t()
##                      [,1]   [,2]   [,3]
## portfolio          1.0000 2.0000 3.0000
## ActivePremium      0.1431 0.2005 0.3499
## Alpha              0.0132 0.0149 0.0275
## AnnualizedAlpha    0.1709 0.1937 0.3856
## Beta               0.8465 1.2389 1.2484
## Beta-              0.8137 1.0441 1.6631
## Beta+              0.0575 1.2787 0.9734
## Correlation        0.4093 0.6901 0.4150
## Correlationp-value 0.0039 0.0000 0.0034
## InformationRatio   0.6457 1.2965 1.0878
## R-squared          0.1675 0.4763 0.1722
## TrackingError      0.2216 0.1546 0.3217
## TreynorRatio       0.2261 0.2008 0.3190

Q7 Interpret Beta.

For Porfolio 1 we see that it’s beta is only .8465 which means that it is less riskier than our market (S&P 500)

For portfolio 2 we see that it’s beta is 1.2389 which means that it is more riskier than our market (S&P 500)

For portfolio 3 we see that it’s beta is 1.2484 which means that it is more riskier than our market (S&P 500)

Overall, portfolio 3 has the highest level of volatility, and is considered to be the riskiest portfolio.

Q8 Interpret Alpha.

What we see in Alpha is that portfolio 1,2, & 3 outperformed its benchmark (S&P 500) by 1.3%, 1.4%, 2.75% for that month. Portfolio 3 outperformed its benchmark the most out of the 3 portfolios. This could be because of how volatile that portfolio is.

Q9 Interpret AnnualizedAlpha.

For portfolio 1 it’s AnnualizedAlpha outperformed its benchmark by 17.09% for that year

For portfolio 2 it’s AnnualizedAlpha outperformed its benchmark by 19.37% for that year

For portfolio 3 it’s AnnualizedAlpha outperformed its benchmark by 38.56% for that year

We see that portfolio 3 outperformed its benchmark far better than the other portfolios, more than doubling both portfolio 1 and 2.

Q10.a Display both code and the results of the code on the webpage.

Q10.b Display the title and your name correctly at the top of the webpage.

Q10.c Use the correct slug.