Goal

Calculate and visualize your portfolio’s beta — a measure of its sensitivity to the overall market.


1. Import Stock Prices

symbols <- c("MSFT", "GOOGL", "AAPL", "NVDA", "META")

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"))

head(asset_returns_tbl)
## # A tibble: 6 Ă— 3
##   asset date         returns
##   <chr> <date>         <dbl>
## 1 AAPL  2013-01-31 -0.156   
## 2 AAPL  2013-02-28 -0.0256  
## 3 AAPL  2013-03-28  0.00285 
## 4 AAPL  2013-04-30  0.000271
## 5 AAPL  2013-05-31  0.0222  
## 6 AAPL  2013-06-28 -0.126

3. Assign a Weight to Each Asset

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
weights <- c(0.2, 0.2, 0.2, 0.2, 0.2)

w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 Ă— 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL        0.2
## 2 GOOGL       0.2
## 3 META        0.2
## 4 MSFT        0.2
## 5 NVDA        0.2

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")

head(portfolio_returns_tbl)
## # A tibble: 6 Ă— 2
##   date        returns
##   <date>        <dbl>
## 1 2013-01-31  0.0179 
## 2 2013-02-28 -0.00726
## 3 2013-03-28 -0.00542
## 4 2013-04-30  0.0673 
## 5 2013-05-31  0.0121 
## 6 2013-06-28 -0.0269

5. Calculate CAPM Beta

5.1 Get Market Returns

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

5.2 Join Portfolio and Market Returns

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

head(portfolio_market_returns_tbl)
## # A tibble: 6 Ă— 3
##   date       market_returns portfolio_returns
##   <date>              <dbl>             <dbl>
## 1 2013-01-31         0.0499           0.0179 
## 2 2013-02-28         0.0127          -0.00726
## 3 2013-03-28         0.0373          -0.00542
## 4 2013-04-30         0.0190           0.0673 
## 5 2013-05-31         0.0233           0.0121 
## 6 2013-06-28        -0.0134          -0.0269

5.3 Compute CAPM Beta

beta_tbl <- portfolio_market_returns_tbl %>%
  tq_performance(Ra              = portfolio_returns,
                 Rb              = market_returns,
                 performance_fun = CAPM.beta)

beta_tbl
## # A tibble: 1 Ă— 1
##   CAPM.beta.1
##         <dbl>
## 1       0.939

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, 
              linewidth = 1.5, color = tidyquant::palette_light()[3]) +
  labs(y = "Portfolio Returns",
       x = "Market Returns",
       title = "Portfolio vs. Market Returns (CAPM Beta Visualization)") +
  coord_cartesian(xlim = c(0, 0.1), ylim = c(0, 0.1))
## `geom_smooth()` using formula = 'y ~ x'


Interpretation

Compare the scatter plot and regression line to the computed beta: - If the slope of the regression line visually aligns with the beta value, this confirms the result.
- In this case, if your calculated beta ≈ 0.94, your portfolio moves slightly less than one-for-one with market changes—indicating moderate sensitivity.