What is CAMP? https://www.investopedia.com/terms/c/capm.asp

Q1 Import stock prices and calculate monthly returns.

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

# Load packages
library(tidyquant)
library(tidyverse)

# Asset Period Returns
stock_returns_monthly <- c("AAPL", "GOOG", "NFLX", "MSFT") %>%
    tq_get(get  = "stock.prices",
           from = "2010-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: 432 x 3
## # Groups:   symbol [4]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2010-01-29 -0.103 
##  2 AAPL   2010-02-26  0.0654
##  3 AAPL   2010-03-31  0.148 
##  4 AAPL   2010-04-30  0.111 
##  5 AAPL   2010-05-28 -0.0161
##  6 AAPL   2010-06-30 -0.0208
##  7 AAPL   2010-07-30  0.0227
##  8 AAPL   2010-08-31 -0.0550
##  9 AAPL   2010-09-30  0.167 
## 10 AAPL   2010-10-29  0.0607
## # ... with 422 more rows

Q2 Import a baseline index fund and calculate monthly returns.

Hint: Use the NASDAQ Composite Index as the baseline fund.

# Baseline Period Returns
baseline_returns_monthly <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2018-12-31") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 108 x 2
##    date            Rb
##    <date>       <dbl>
##  1 2010-01-29 -0.0698
##  2 2010-02-26  0.0423
##  3 2010-03-31  0.0714
##  4 2010-04-30  0.0264
##  5 2010-05-28 -0.0829
##  6 2010-06-30 -0.0655
##  7 2010-07-30  0.0690
##  8 2010-08-31 -0.0624
##  9 2010-09-30  0.120 
## 10 2010-10-29  0.0586
## # ... with 98 more rows

Q3 Aggregate a Portfolio

Hint: Create weight tables for three portfolios.

# scaling a single portfolio to many, 3 in this case
stock_returns_monthly_multi <- stock_returns_monthly %>%
    tq_repeat_df(n = 3)
stock_returns_monthly_multi
## # A tibble: 1,296 x 4
## # Groups:   portfolio [3]
##    portfolio symbol date            Ra
##        <int> <chr>  <date>       <dbl>
##  1         1 AAPL   2010-01-29 -0.103 
##  2         1 AAPL   2010-02-26  0.0654
##  3         1 AAPL   2010-03-31  0.148 
##  4         1 AAPL   2010-04-30  0.111 
##  5         1 AAPL   2010-05-28 -0.0161
##  6         1 AAPL   2010-06-30 -0.0208
##  7         1 AAPL   2010-07-30  0.0227
##  8         1 AAPL   2010-08-31 -0.0550
##  9         1 AAPL   2010-09-30  0.167 
## 10         1 AAPL   2010-10-29  0.0607
## # ... with 1,286 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.40, 0.20, 0.20, 0.20,
    0.20, 0.40, 0.20, 0.20,
    0.20, 0.20, 0.40, 0.20
)
stocks <- c("AAPL", "GOOG", "NFLX")
weights_table <-  tibble(stocks) %>%
    tq_repeat_df(n = 4 ) %>%
    bind_cols(tibble(weights)) %>%
    group_by(portfolio)
weights_table
## # A tibble: 12 x 3
## # Groups:   portfolio [4]
##    portfolio stocks weights
##        <int> <chr>    <dbl>
##  1         1 AAPL       0.4
##  2         1 GOOG       0.2
##  3         1 NFLX       0.2
##  4         2 AAPL       0.2
##  5         2 GOOG       0.2
##  6         2 NFLX       0.4
##  7         3 AAPL       0.2
##  8         3 GOOG       0.2
##  9         3 NFLX       0.2
## 10         4 AAPL       0.2
## 11         4 GOOG       0.4
## 12         4 NFLX       0.2




# 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: 324 x 3
## # Groups:   portfolio [3]
##    portfolio date              Ra
##        <int> <date>         <dbl>
##  1         1 2010-01-29 -0.239   
##  2         1 2010-02-26  0.0482  
##  3         1 2010-03-31  0.123   
##  4         1 2010-04-30  0.145   
##  5         1 2010-05-28  0.0245  
##  6         1 2010-06-30 -0.0308  
##  7         1 2010-07-30  0.000600
##  8         1 2010-08-31  0.0474  
##  9         1 2010-09-30  0.222   
## 10         1 2010-10-29  0.0789  
## # ... with 314 more rows

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

The first period return is -23.91% and is calculated using the first portfolio given in the first column. It is computed by using the weights designated in the weight table above for the first portfolio, and multiplying that by each return for each stock.

Q5 Merge Ra and Rb

# Merging Ra and Rb
RaRb_single_portfolio <- left_join(portfolio_returns_monthly_multi , 
                                   baseline_returns_monthly,
                                   by = "date")
RaRb_single_portfolio
## # A tibble: 324 x 4
## # Groups:   portfolio [?]
##    portfolio date              Ra      Rb
##        <int> <date>         <dbl>   <dbl>
##  1         1 2010-01-29 -0.239    -0.0698
##  2         1 2010-02-26  0.0482    0.0423
##  3         1 2010-03-31  0.123     0.0714
##  4         1 2010-04-30  0.145     0.0264
##  5         1 2010-05-28  0.0245   -0.0829
##  6         1 2010-06-30 -0.0308   -0.0655
##  7         1 2010-07-30  0.000600  0.0690
##  8         1 2010-08-31  0.0474   -0.0624
##  9         1 2010-09-30  0.222     0.120 
## 10         1 2010-10-29  0.0789    0.0586
## # ... with 314 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.1740 0.2334 0.1514
## Alpha              0.0124 0.0188 0.0118
## AnnualizedAlpha    0.1594 0.2511 0.1514
## Beta               1.2522 1.2955 1.3506
## Beta-              1.5277 1.6718 1.8565
## Beta+              1.0329 0.8515 0.9200
## Correlation        0.6162 0.4716 0.5543
## Correlationp-value 0.0000 0.0000 0.0000
## InformationRatio   0.7227 0.6438 0.4949
## R-squared          0.3797 0.2224 0.3072
## TrackingError      0.2408 0.3626 0.3058
## TreynorRatio       0.2376 0.2755 0.2035

Q7 Interpret Alpha and Beta.

For Beta, because it is greater than 1, we see our portfolio being more volitile than that of the market. For Alpha, because it is greater than 1, we see it as having done better than the market.

Q8 Display both code and the results of the code on the webpage.

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

Q10 Use the correct slug.