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

Step 1: Import stock prices and calculate returns.

Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.

# Load packages
library(tidyquant)
library(tidyverse)

# Asset Period Returns
stock_returns_monthly <- c("AAPL", "GOOG", "NFLX") %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2015-12-31") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
stock_returns_monthly 
## # A tibble: 216 x 3
## # Groups:   symbol [3]
##    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 206 more rows

Step 2: Import a baseline index fund and calculate returns.

Hint: Use the Technology Select Sector SPDR Fund (XLK) as the baseline fund.

# Baseline Period Returns
baseline_returns_monthly <- "XLK" %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2015-12-31") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 72 x 2
##    date            Rb
##    <date>       <dbl>
##  1 2010-01-29 -0.0993
##  2 2010-02-26  0.0348
##  3 2010-03-31  0.0684
##  4 2010-04-30  0.0126
##  5 2010-05-28 -0.0748
##  6 2010-06-30 -0.0540
##  7 2010-07-30  0.0745
##  8 2010-08-31 -0.0561
##  9 2010-09-30  0.117 
## 10 2010-10-29  0.0578
## # ... with 62 more rows

Step 3A: Aggregating a Portfolio

# Create Vector of Weights
# not all symbols need to be specified. Any symbol not specified by default gets a weight of zero.
wts_map <- tibble(
    symbols = c("AAPL", "NFLX"),
    weights = c(0.5, 0.5)
)
wts_map
## # A tibble: 2 x 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL        0.5
## 2 NFLX        0.5

# Aggregate a Portfolio using Vector of Weights
portfolio_returns_monthly <-
  stock_returns_monthly %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = wts_map, 
                 col_rename  = "Ra")
portfolio_returns_monthly
## # A tibble: 72 x 2
##    date            Ra
##    <date>       <dbl>
##  1 2010-01-29  0.0307
##  2 2010-02-26  0.0629
##  3 2010-03-31  0.130 
##  4 2010-04-30  0.239 
##  5 2010-05-28  0.0682
##  6 2010-06-30 -0.0219
##  7 2010-07-30 -0.0272
##  8 2010-08-31  0.116 
##  9 2010-09-30  0.251 
## 10 2010-10-29  0.0674
## # ... with 62 more rows

Intrepretation

Step 3B: Merging Ra and Rb

# Merging Ra and Rb
RaRb_single_portfolio <- left_join(portfolio_returns_monthly, 
                                   baseline_returns_monthly,
                                   by = "date")
RaRb_single_portfolio
## # A tibble: 72 x 3
##    date            Ra      Rb
##    <date>       <dbl>   <dbl>
##  1 2010-01-29  0.0307 -0.0993
##  2 2010-02-26  0.0629  0.0348
##  3 2010-03-31  0.130   0.0684
##  4 2010-04-30  0.239   0.0126
##  5 2010-05-28  0.0682 -0.0748
##  6 2010-06-30 -0.0219 -0.0540
##  7 2010-07-30 -0.0272  0.0745
##  8 2010-08-31  0.116  -0.0561
##  9 2010-09-30  0.251   0.117 
## 10 2010-10-29  0.0674  0.0578
## # ... with 62 more rows

Step 4: Computing the CAPM Table

RaRb_single_portfolio %>%
    tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  t()
##                       [,1]
## ActivePremium       0.3422
## Alpha               0.0304
## AnnualizedAlpha     0.4330
## Beta                0.7744
## Beta-              -0.2163
## Beta+               0.4860
## Correlation         0.2943
## Correlationp-value  0.0121
## InformationRatio    0.9133
## R-squared           0.0866
## TrackingError       0.3747
## TreynorRatio        0.6080

Interpretation

https://www.investopedia.com/ask/answers/102714/whats-difference-between-alpha-and-beta.asp

https://www.investopedia.com/articles/06/capm.asp