# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(plotly)

Goal

Calculate and visualize your portfolio’s beta.

Choose your stocks and the baseline market.

from 2012-12-31 to present

1 Import Stock Prices

symbols <- c("FNV", "WPM", "NEM", "CCJ", "BWXT", "NVDA", "TSM", "ASML", "CAT", "ITW", "HON", "PLD", "EQIX", "PSA", "TPL", "XOM", "CVX")

prices <- tq_get(x = symbols, 
                 get  = "stock.prices",
                 from = "2012-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"))

asset_returns_tbl
## # A tibble: 2,839 × 3
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 ASML  2013-01-31  0.154 
##  2 ASML  2013-02-28 -0.0563
##  3 ASML  2013-03-28 -0.0430
##  4 ASML  2013-04-30  0.0992
##  5 ASML  2013-05-31  0.0887
##  6 ASML  2013-06-28 -0.0269
##  7 ASML  2013-07-31  0.128 
##  8 ASML  2013-08-30 -0.0327
##  9 ASML  2013-09-30  0.126 
## 10 ASML  2013-10-31 -0.0420
## # ℹ 2,829 more rows

3 Assign a Weight to Each Asset (Change the Weighting Scheme)

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
##  [1] "ASML" "BWXT" "CAT"  "CCJ"  "CVX"  "EQIX" "FNV"  "HON"  "ITW"  "NEM" 
## [11] "NVDA" "PLD"  "PSA"  "TPL"  "TSM"  "WPM"  "XOM"
weights <- c(0.10, 0.10, 0.10, 0.05, 0.05, 0.04, 0.03, 0.03, 0.04, 0.03, 0.03, 0.07, 0.07, 0.06, 0.07, 0.07, 0.06)
weights
##  [1] 0.10 0.10 0.10 0.05 0.05 0.04 0.03 0.03 0.04 0.03 0.03 0.07 0.07 0.06 0.07
## [16] 0.07 0.06
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 17 × 2
##    symbols weights
##    <chr>     <dbl>
##  1 ASML       0.1 
##  2 BWXT       0.1 
##  3 CAT        0.1 
##  4 CCJ        0.05
##  5 CVX        0.05
##  6 EQIX       0.04
##  7 FNV        0.03
##  8 HON        0.03
##  9 ITW        0.04
## 10 NEM        0.03
## 11 NVDA       0.03
## 12 PLD        0.07
## 13 PSA        0.07
## 14 TPL        0.06
## 15 TSM        0.07
## 16 WPM        0.07
## 17 XOM        0.06

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: 167 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0457 
##  2 2013-02-28 -0.0170 
##  3 2013-03-28  0.0106 
##  4 2013-04-30 -0.00468
##  5 2013-05-31  0.0276 
##  6 2013-06-28 -0.0378 
##  7 2013-07-31  0.0389 
##  8 2013-08-30 -0.00842
##  9 2013-09-30  0.0294 
## 10 2013-10-31  0.00176
## # ℹ 157 more rows

5 Calculate CAPM Beta

5.1 Get Market Returns

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

5.2 Join Returns

portfolio_market_returns_tbl <- left_join(market_returns_tbl, 
                                  portfolio_returns_tbl, 
                                  by = "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.964

6 Plot: Scatterplot With Regression Line

portfolio_market_returns_tbl %>%
    
    ggplot(aes(x = market_returns, 
               y = portfolio_returns)) +
    geom_point(color = "gold3") +
    geom_smooth(method = "lm", se = FALSE, 
                size = 1.5, color = tidyquant::palette_light()[5]) +
    
    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?

With a CAPM Beta of 0.964, my portfolio is less sensitive than the market, but only slightly. Looking at the regression line (beta coefficient) in the plot above, we can see that the data points of returns generally follow the upward trend of the line, even if some of the points stray from it. We can also see that the regression line has a slope close to 1, only slightly under 45 degrees, consistent with our beta being near (if slightly under) 1. With how the plot confirms the beta coefficient, I conclude that my portfolio responds almost proportionally to the market with slightly smaller movements on average.