# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Calculate and visualize your portfolio’s beta.

Choose your stocks and the baseline market.

from 2017-12-31 to present

1 Import stock prices

# Choose stocks
symbols <- c("MSFT", "JPM", "GM", "TMUS", "IRVRF")

prices <- tq_get(x    = symbols, 
                get  = "stock.prices",
                from = "2017-12-31", 
                to   = "2022-12-31")

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>%
    
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly",
                 type       = "log") %>%
    
    slice(-1) %>%
    
    ungroup() %>%
    
    set_names(c("asset", "date", "returns"))

3 Assign a weight to each asset (change the weigting scheme)

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GM"    "IRVRF" "JPM"   "MSFT"  "TMUS"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 GM         0.25
## 2 IRVRF      0.25
## 3 JPM        0.2 
## 4 MSFT       0.2 
## 5 TMUS       0.1

4 Build a portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset, 
                 returns_col  = returns, 
                 weights      = w_tbl, 
                 rebalance_on = "months", 
                 col_rename   = "returns")

portfolio_returns_tbl
## # A tibble: 59 × 2
##    date        returns
##    <date>        <dbl>
##  1 2018-02-28  0.0296 
##  2 2018-03-29 -0.0283 
##  3 2018-04-30  0.0459 
##  4 2018-05-31  0.00510
##  5 2018-06-29 -0.0272 
##  6 2018-07-31 -0.00462
##  7 2018-08-31  0.0213 
##  8 2018-09-28 -0.0278 
##  9 2018-10-31  0.0614 
## 10 2018-11-30  0.0806 
## # … with 49 more rows

5 Calculate CAPM Beta

5.1 Get market returns

market_returns_tbl <- tq_get(x    = "NDX", 
                get  = "stock.prices",
                from = "2017-12-31", 
                to   = "2022-12-31") %>%
    
    # Convert prices to monthly returns
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly",
                 type       = "log",
                 col_rename = "returns") %>%
    
    slice(-1)

market_returns_tbl
## # A tibble: 59 × 2
##    date        returns
##    <date>        <dbl>
##  1 2018-02-28 -0.0138 
##  2 2018-03-29 -0.0407 
##  3 2018-04-30  0.00371
##  4 2018-05-31  0.0534 
##  5 2018-06-29  0.0104 
##  6 2018-07-31  0.0268 
##  7 2018-08-31  0.0568 
##  8 2018-09-28 -0.00352
##  9 2018-10-31 -0.0906 
## 10 2018-11-30 -0.00260
## # … with 49 more rows

5.2 Join returns

portfolio_market_returns_tbl <- left_join(market_returns_tbl, 
                                  portfolio_returns_tbl, 
                                  "date") %>%
    
    set_names("date", "market_returns", "portfolio_returns")

5.3 CAPM Beta

portfolio_market_returns_tbl  %>%
    
    tq_performance(Ra = portfolio_returns, 
                   Rb = market_returns, 
                   performance_fun = CAPM.beta)
## # A tibble: 1 × 1
##   CAPM.beta.1
##         <dbl>
## 1       0.766

6 Plot: Scatter with regression line

portfolio_market_returns_tbl %>%
    
    ggplot(aes(x = market_returns, 
               y = portfolio_returns)) +
    geom_point(color = "cornflowerblue") +
    geom_smooth(method = "lm", se = FALSE, 
                size = 1.5, color = tidyquant::palette_light()[3]) +
    
    labs(y = "Portfolio Returns",
         x = "Market Returns")

How sensitive is your portfolio to the market? Discuss in terms of the beta coefficient. Does the plot confirm the beta coefficient you calculated?

The proposed portfolio has a 0.76 beta coefficient with the NDX NASDAQ index. NDX was chosen as the baseline market, due to the portfolio having 0.45 weight in assets that are in the information technology and communications services sectors. Theoretically, this is an indication that the portfolio is less volatile than the baseline market. The portfolio could be assumed to have similar movements to NDX, but has slightly less systemic risk due to the diversification of the remaining assets. Speaking to the regression plot, there is an evident lack of conformity to the beta coefficient. Returns are highly scattered and do not closely align with the marginal slope of the beta coefficient regression model. The weak relationship between the two addresses a non-significant influence, which would reflect in the portfolio not having equivalent returns in comparison to the market. However, since the beta is being calculated using historical data points during several years of poor market conditions (especially for information technology), it is less meaningful for an investor to assume future movements to be in line with the current broader market.