# Load packages

# Core
library(tidyverse)
library(tidyquant)

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("NXPI", "IFX.DE", "AMD", "ON")

prices <- tq_get(
  x = symbols,
  get = "stock.prices",
  from = "2020-01-01",
  to = "2025-01-01"
)

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
  group_by(symbol) %>%
    
  tq_transmute(
    select       = adjusted,
    mutate_fun   = periodReturn,
    period       = "monthly",
    type         = "arithmetic",
    col_rename   = "returns") %>%
    
  slice(-1) %>%
    
  ungroup() %>%
    
  rename(asset = symbol)

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull(asset)
symbols
## [1] "AMD"    "IFX.DE" "NXPI"   "ON"
# weights
weights <- c(0.25, 0.25, 0.25, 0.25)
weights
## [1] 0.25 0.25 0.25 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMD        0.25
## 2 IFX.DE     0.25
## 3 NXPI       0.25
## 4 ON         0.25

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: 63 × 2
##    date       returns
##    <date>       <dbl>
##  1 2020-02-28 -0.0895
##  2 2020-03-31 -0.222 
##  3 2020-04-30  0.226 
##  4 2020-05-29  0.0330
##  5 2020-06-30  0.119 
##  6 2020-07-31  0.138 
##  7 2020-08-31  0.0941
##  8 2020-09-30 -0.0114
##  9 2020-10-30  0.0372
## 10 2020-11-30  0.196 
## # ℹ 53 more rows

5 Calculate CAPM Beta

5.1 Get market returns

market_returns_tbl <- tq_get(x = "SPY",
       get  = "stock.prices",
       from = "2020-01-01",
       to   = "2025-01-01") %>%
    
    # convert prices to returns
    tq_transmute(select       = adjusted,
                 mutate_fun   = periodReturn,
                 period       = "monthly",
                 type         = "arithmetic",
                 col_rename   = "market_returns") %>%
    
    slice(-1)

5.2 Join returns

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

5.3 CAPM Beta

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

6 Plot: Scatter with regression line

portfolio_market_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?

My portfolio has a beta of 1.59, meaning it is more sensitive to market movements. A 1% change in the market is associated with about a 1.59% change in my portfolio in the same direction, on average. The plot supports this because the regression line slopes upward with a slope greater than 1.