What is CAPM? https://www.investopedia.com/terms/c/capm.asp
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
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
Hint: Assign a weight of 1/4 to each stock.
# 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", "MSFT", "GOOG"),
weights = c(0.25, 0.25, 0.25, 0.25)
)
wts_map
## # A tibble: 4 x 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.25
## 2 NFLX 0.25
## 3 MSFT 0.25
## 4 GOOG 0.25
# 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: 108 x 2
## date Ra
## <date> <dbl>
## 1 2010-01-29 -0.0456
## 2 2010-02-26 0.0380
## 3 2010-03-31 0.0934
## 4 2010-04-30 0.131
## 5 2010-05-28 -0.000946
## 6 2010-06-30 -0.0465
## 7 2010-07-30 0.0145
## 8 2010-08-31 0.0498
## 9 2010-09-30 0.207
## 10 2010-10-29 0.0843
## # ... with 98 more rows
The first period return is -4.56%. It’s computed by adding all the returns from the different stocks and dividing by 4 to have equal weight.
# Merging Ra and Rb
RaRb_single_portfolio <- left_join(portfolio_returns_monthly,
baseline_returns_monthly,
by = "date")
RaRb_single_portfolio
## # A tibble: 108 x 3
## date Ra Rb
## <date> <dbl> <dbl>
## 1 2010-01-29 -0.0456 -0.0698
## 2 2010-02-26 0.0380 0.0423
## 3 2010-03-31 0.0934 0.0714
## 4 2010-04-30 0.131 0.0264
## 5 2010-05-28 -0.000946 -0.0829
## 6 2010-06-30 -0.0465 -0.0655
## 7 2010-07-30 0.0145 0.0690
## 8 2010-08-31 0.0498 -0.0624
## 9 2010-09-30 0.207 0.120
## 10 2010-10-29 0.0843 0.0586
## # ... with 98 more rows
RaRb_single_portfolio %>%
tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
t()
## [,1]
## ActivePremium 0.1961
## Alpha 0.0146
## AnnualizedAlpha 0.1897
## Beta 1.1815
## Beta- 1.4071
## Beta+ 0.8970
## Correlation 0.5794
## Correlationp-value 0.0000
## InformationRatio 0.7893
## R-squared 0.3357
## TrackingError 0.2485
## TreynorRatio 0.2705
Given this Alpha we see this portfolio when compared to the Nasdaq, outperforming the market by about 1.46%. When looking at Beta we see this portfolio being 18.15% more volitile than that of the market.