# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.

1 Import stock prices

Choose your stocks from 2012-12-31 to present.

symbols <- c ("AAPL", "NVDA", "CAT", "PANW", "MU")

prices <- tq_get(x = symbols,
                 get = "stockprices",
                 from = "2012-12-31",
                 to = "2017-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 Calculate Component Contribution to Portfolio Volatility

# Transform data into wide form
asset_returns_wide_tbl <- asset_returns_tbl %>%

    pivot_wider(names_from = asset, values_from = returns) %>%

    column_to_rownames(var = "date")

asset_returns_wide_tbl
##                     AAPL          CAT           MU         NVDA         PANW
## 2013-01-31 -1.555890e-01  0.093471933  0.175992517  0.000000000  0.033801967
## 2013-02-28 -2.561100e-02 -0.063136786  0.102976776  0.038221756  0.099145277
## 2013-03-28  2.850230e-03 -0.060239055  0.174735038  0.013338527 -0.076993676
## 2013-04-30  2.713249e-04 -0.020398777 -0.057748011  0.070706514 -0.045174729
## 2013-05-31  2.217178e-02  0.013257782  0.215043012  0.054652291 -0.108858131
## 2013-06-28 -1.258956e-01 -0.039341839  0.204477182 -0.030167173 -0.140504164
## 2013-07-31  1.321021e-01  0.012025615 -0.079112874  0.028091739  0.149123100
## 2013-08-30  8.044306e-02 -0.004472805  0.024619094  0.026270020 -0.018977508
## 2013-09-30 -2.172369e-02  0.010365541  0.252623729  0.053460279 -0.046896869
## 2013-10-31  9.201458e-02  0.006499095  0.011948802 -0.024065809 -0.083248723
## 2013-11-29  6.770849e-02  0.014765750  0.176838889  0.032034376  0.169550514
## 2013-12-31  8.862382e-03  0.070835530  0.030340904  0.026567252  0.140240545
## 2014-01-31 -1.139492e-01  0.040081304  0.057618143 -0.020177653  0.033872544
## 2014-02-28  5.591817e-02  0.032064305  0.048707476  0.162107696  0.179654694
## 2014-03-31  1.975668e-02  0.024449125 -0.022153688 -0.025903662 -0.036497725
## 2014-04-30  9.476094e-02  0.064783261  0.098915627  0.030788614 -0.075993526
## 2014-05-30  7.576548e-02 -0.030537573  0.090355734  0.032886123  0.163988351
## 2014-06-30  2.728609e-02  0.061090690  0.141934420 -0.024508289  0.112742150
## 2014-07-31  2.832628e-02 -0.069370433 -0.075626978 -0.057729535 -0.036310197
## 2014-08-29  7.465163e-02  0.079347810  0.064947905  0.110060014  0.049814281
## 2014-09-30 -1.722045e-02 -0.096566812  0.049665858 -0.052782803  0.143453790
## 2014-10-31  6.948906e-02  0.031337365 -0.034747051  0.057399149  0.074617510
## 2014-11-28  1.007305e-01 -0.008019244  0.082897808  0.074852689  0.151579455
## 2014-12-31 -7.460606e-02 -0.094485117 -0.026495466 -0.044863810 -0.003502063
## 2015-01-30  5.961167e-02 -0.126849531 -0.179070547 -0.043319296  0.030690097
## 2015-02-27  9.601585e-02  0.035983546  0.046721806  0.142699409  0.118002746
## 2015-03-31 -3.187439e-02 -0.035233429 -0.122644740 -0.052582892  0.026779267
## 2015-04-30  5.769750e-03  0.090380104  0.036196409  0.058908534  0.011164221
## 2015-05-29  4.434109e-02 -0.018118944 -0.007135072  0.001460313  0.137475291
## 2015-06-30 -3.793804e-02 -0.005877828 -0.393719205 -0.095717051  0.030276306
## 2015-07-31 -3.348085e-02 -0.066586658 -0.017670842 -0.007987904  0.061762045
## 2015-08-31 -6.848883e-02 -0.028247182 -0.120420457  0.123595366 -0.123625244
## 2015-09-30 -2.205762e-02 -0.156595576 -0.091175012  0.092151012  0.046287498
## 2015-10-30  8.011220e-02  0.121525802  0.100274038  0.140555063 -0.066090204
## 2015-11-30 -5.821165e-03 -0.004669457 -0.038785784  0.115405558  0.151520831
## 2015-12-31 -1.167904e-01 -0.066733719 -0.117783251  0.038347116 -0.061646019
## 2016-01-29 -7.822347e-02 -0.075478756 -0.249802183 -0.118048728 -0.164049592
## 2016-02-29 -1.288649e-03  0.084088241 -0.036938478  0.071923946 -0.031945141
## 2016-03-31  1.197464e-01  0.122727235 -0.015165968  0.127654854  0.119324351
## 2016-04-29 -1.507311e-01  0.025002423  0.026391316 -0.002810585 -0.078190190
## 2016-05-31  6.931421e-02 -0.069387835  0.168270156  0.276388073 -0.145351868
## 2016-06-30 -4.359655e-02  0.044505466  0.078589953  0.006188202 -0.061813428
## 2016-07-29  8.623515e-02  0.097302086 -0.001454482  0.194443859  0.065104040
## 2016-08-31  2.337663e-02 -0.009835692  0.182442951  0.073469155  0.017269193
## 2016-09-30  6.344835e-02  0.079939435  0.075320066  0.110693545  0.179351041
## 2016-10-31  4.324705e-03 -0.052892461 -0.035493044  0.037805144 -0.035129476
## 2016-11-30 -2.183721e-02  0.135386472  0.129370811  0.260525304 -0.135250850
## 2016-12-30  4.684051e-02 -0.029954202  0.115447267  0.146435835 -0.071883528
## 2017-01-31  4.664186e-02  0.039263383  0.095227550  0.022601720  0.165521149
## 2017-02-28  1.255552e-01  0.010399406 -0.028182939 -0.071874908  0.028987614
## 2017-03-31  4.754124e-02 -0.041184054  0.209397809  0.070843779 -0.298670465
## 2017-04-28 -6.929851e-05  0.105775090 -0.043492946 -0.043433854 -0.038631680
## 2017-05-31  6.560736e-02  0.030528576  0.106191525  0.326022111  0.089751836
## 2017-06-30 -5.891578e-02  0.019071482 -0.030020541  0.001453841  0.120748730
## 2017-07-31  3.218049e-02  0.065833456 -0.060038538  0.117044528 -0.015287009
## 2017-08-31  1.016529e-01  0.030593366  0.128316744  0.042639409  0.006881714
## 2017-09-29 -6.213501e-02  0.059637903  0.207189783  0.053601174  0.082491922
## 2017-10-31  9.240396e-02  0.091138883  0.119222700  0.145700563  0.021284693
## 2017-11-30  2.007501e-02  0.038640053 -0.044297771 -0.029244688 -0.009899308
## 2017-12-29 -1.536350e-02  0.110110061 -0.030418012 -0.036583793 -0.005572971
calculate_componet_contribution <- function(.data, w) {
  
  # Covariance of asset returns
  covariance_matrix <- cov(.data)

  # Standard deviation of portfolio
  # Summarizes how much each asset's returns vary with those of other assets      within the portfolio into a single number
  sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

  # Component contribution
  # Similar to the formula for sd_portfolio
  # Mathematical trick to summarize the same, sd_portfolio, by asset instead of   a single number
  component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]

  # Component contribution in percentage
  component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

  return(component_percentages)
}

asset_returns_wide_tbl %>% calculate_componet_contribution(w = c(.2, .2, .2, .2,.2))
## # A tibble: 1 × 5
##    AAPL   CAT    MU  NVDA  PANW
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.154 0.139 0.295 0.226 0.186

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>% 
  
  calculate_componet_contribution(w = c(.2, .2, .2, .2,.2)) %>%
  
  # Transform to long form
  pivot_longer(cols = everything(), names_to ="Asset", values_to = "Contribution") %>%
  
  # Add weights
  add_column(weight = c(.2, .2, .2, .2,.2)) %>%
  
  # Transform to long
  pivot_longer(cols = c(Contribution, weight), names_to = "type", values_to = "value")

plot_data %>% 
  
  ggplot(aes(x = Asset, y = value, fill = type)) +
  geom_col(position = "dodge") +

  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  scale_fill_tq() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_tq() +
  
  labs(title = "Percent Contribution to Portfolio Volatility and Weight",
       y = "Percent",
       x = NULL)

Which of the assets in your portfolio the largest contributor to the portfolio volatility? Do you think your portfolio risk is concentrated in any one asset?