Portfolio

The portfolio uses the same five assets and weights as Code Along 12. The tickers are in alphabetical order so they line up with the weights.

Asset Description Weight
AGG U.S. aggregate bonds 25%
EEM Emerging market stocks 25%
EFA International developed market stocks 20%
IJS U.S. small-cap value stocks 20%
SPY S&P 500 10%
tickers <- c("AGG", "EEM", "EFA", "IJS", "SPY")
w       <- c(0.25, 0.25, 0.20, 0.20, 0.10)

sum(w)  # must equal 1
## [1] 1

1 Import stock prices

The time frame starts on 2012-12-31 (so we can compute a January 2013 return) and runs to the present.

prices <- tq_get(tickers,
                 get  = "stock.prices",
                 from = "2012-12-31",
                 to   = Sys.Date())

prices
## # A tibble: 17,485 × 8
##    symbol date        open  high   low close  volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
##  1 AGG    2012-12-31  111.  111.  111.  111.  905400     76.1
##  2 AGG    2013-01-02  111.  111   111.  111. 1137300     76.0
##  3 AGG    2013-01-03  111.  111.  111.  111. 1762900     75.8
##  4 AGG    2013-01-04  111.  111.  111.  111. 1286800     75.9
##  5 AGG    2013-01-07  111.  111.  111.  111. 1854400     75.8
##  6 AGG    2013-01-08  111.  111.  111.  111. 1784500     75.9
##  7 AGG    2013-01-09  111.  111.  111.  111. 1346900     75.8
##  8 AGG    2013-01-10  111.  111.  111.  111. 1128700     75.8
##  9 AGG    2013-01-11  111.  111.  111.  111. 1120000     75.9
## 10 AGG    2013-01-14  111.  111.  111.  111.  870200     76.0
## # ℹ 17,475 more rows

2 Convert prices to returns

Monthly log returns for each asset. We drop December 2012 (no earlier price to compare with) and the current month, which is not finished yet.

asset_returns_tbl <- prices %>%
  group_by(symbol) %>%
  tq_transmute(select     = adjusted,
               mutate_fun = periodReturn,
               period     = "monthly",
               type       = "log") %>%
  ungroup() %>%
  rename(asset = symbol, returns = monthly.returns) %>%
  filter(date >= "2013-01-01",
         date <  floor_date(Sys.Date(), "month"))

asset_returns_tbl
## # A tibble: 830 × 3
##    asset date         returns
##    <chr> <date>         <dbl>
##  1 AGG   2013-01-31 -0.00623 
##  2 AGG   2013-02-28  0.00589 
##  3 AGG   2013-03-28  0.000985
##  4 AGG   2013-04-30  0.00964 
##  5 AGG   2013-05-31 -0.0202  
##  6 AGG   2013-06-28 -0.0158  
##  7 AGG   2013-07-31  0.00269 
##  8 AGG   2013-08-30 -0.00830 
##  9 AGG   2013-09-30  0.0111  
## 10 AGG   2013-10-31  0.00829 
## # ℹ 820 more rows

3 Component contribution

Transform to wide form (one column per asset, dates as row names), then use the custom function from Code Along 12.

asset_returns_wide_tbl <- asset_returns_tbl %>%
  pivot_wider(names_from = asset, values_from = returns) %>%
  column_to_rownames(var = "date")

head(asset_returns_wide_tbl)
##                      AGG          EEM         EFA          IJS         SPY
## 2013-01-31 -0.0062317015 -0.002935407  0.03660622  0.052133102  0.04992318
## 2013-02-28  0.0058911844 -0.023105094 -0.01296939  0.016175435  0.01267796
## 2013-03-28  0.0009850148 -0.010235145  0.01296939  0.040257903  0.03726793
## 2013-04-30  0.0096398929  0.012084458  0.04896797  0.001222620  0.01903046
## 2013-05-31 -0.0202143664 -0.049483102 -0.03065577  0.041976338  0.02333501
## 2013-06-28 -0.0157781145 -0.054728558 -0.02714450 -0.001402978 -0.01343421
calculate_component_contribution <- function(.data, w) {

  # Covariance matrix of the asset returns
  covariance_matrix <- cov(.data)

  # Portfolio standard deviation (matrix multiplication)
  sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

  # Component contribution (simple multiplication by the weights)
  component_contribution <- (w %*% covariance_matrix * w) / sd_portfolio[1, 1]

  # Component contribution as a percentage of portfolio risk
  component_percentages <- component_contribution / sd_portfolio[1, 1]

  return(component_percentages)
}
asset_returns_wide_tbl %>%
  calculate_component_contribution(w = w)
##             AGG      EEM       EFA       IJS       SPY
## [1,] 0.04715772 0.319454 0.2274496 0.2989382 0.1070005

4 Plot: column chart of component contribution and weight

plot_data <- asset_returns_wide_tbl %>%
  calculate_component_contribution(w = w) %>%
  as_tibble() %>%
  pivot_longer(cols = everything(),
               names_to = "asset",
               values_to = "contribution") %>%
  add_column(weight = w)

plot_data
## # A tibble: 5 × 3
##   asset contribution weight
##   <chr>        <dbl>  <dbl>
## 1 AGG         0.0472   0.25
## 2 EEM         0.319    0.25
## 3 EFA         0.227    0.2 
## 4 IJS         0.299    0.2 
## 5 SPY         0.107    0.1
plot_data %>%
  pivot_longer(cols = c(contribution, weight),
               names_to = "type",
               values_to = "value") %>%
  ggplot(aes(x = asset, y = value, fill = type)) +
  geom_col(position = "dodge") +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  theme_tq() +
  scale_fill_tq() +
  labs(title = "Percent Contribution to Volatility and Weight",
       x = NULL,
       y = "Percent") +
  theme(plot.title = element_text(hjust = 0.5))

5 Questions

Which of the assets in your portfolio is the largest contributor to the portfolio volatility?

The largest contributor is EEM. It accounts for 32% of the portfolio’s standard deviation while making up 25% of the portfolio by weight. The smallest contributor is AGG, which contributes only 5% of the risk with a weight of 25%.

Do you think your portfolio risk is concentrated in any one asset?

EEM contributes 32% of the risk, compared with 25% of the weight, so it contributes more risk than its size would suggest. The chart shows how risk is spread across the five assets: the assets that are stock-like (such as EEM, EFA, IJS, and SPY) tend to move together, which adds up to most of the portfolio’s volatility, while the bond fund AGG acts as a damper because its returns are much less volatile. [After knitting, edit this paragraph to match your plot: say whether one bar is much taller than the rest, and whether the risk looks spread out or concentrated.]