# 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.555893e-01  0.093472536  0.175992310  0.000000000  0.033801967
## 2013-02-28 -2.561100e-02 -0.063137329  0.102976672  0.038221651  0.099145277
## 2013-03-28  2.850586e-03 -0.060238871  0.174735271  0.013338929 -0.076993676
## 2013-04-30  2.709697e-04 -0.020398648 -0.057748011  0.070705925 -0.045174729
## 2013-05-31  2.217172e-02  0.013257778  0.215043012  0.054652389 -0.108858131
## 2013-06-28 -1.258957e-01 -0.039342213  0.204477114 -0.030167358 -0.140504164
## 2013-07-31  1.321022e-01  0.012025743 -0.079112732  0.028091569  0.149123100
## 2013-08-30  8.044300e-02 -0.004472996  0.024618876  0.026270291 -0.018977508
## 2013-09-30 -2.172350e-02  0.010365668  0.252623761  0.053460534 -0.046896869
## 2013-10-31  9.201516e-02  0.006499534  0.011948914 -0.024065807 -0.083248723
## 2013-11-29  6.770856e-02  0.014765373  0.176839074  0.032034374  0.169550514
## 2013-12-31  8.861935e-03  0.070835699  0.030340629  0.026567250  0.140240545
## 2014-01-31 -1.139494e-01  0.040081073  0.057618148 -0.020177409  0.033872544
## 2014-02-28  5.591793e-02  0.032064842  0.048707642  0.162107372  0.179654694
## 2014-03-31  1.975657e-02  0.024448588 -0.022153520 -0.025903803 -0.036497725
## 2014-04-30  9.476116e-02  0.064782967  0.098915379  0.030788550 -0.075993526
## 2014-05-30  7.576510e-02 -0.030537178  0.090355597  0.032886527  0.163988351
## 2014-06-30  2.728610e-02  0.061090018  0.141934082 -0.024508555  0.112742150
## 2014-07-31  2.832656e-02 -0.069369862 -0.075626375 -0.057729683 -0.036310197
## 2014-08-29  7.465181e-02  0.079347810  0.064947477  0.110060101  0.049814281
## 2014-09-30 -1.722079e-02 -0.096567123  0.049666386 -0.052782607  0.143453790
## 2014-10-31  6.948892e-02  0.031337877 -0.034747634  0.057399017  0.074617510
## 2014-11-28  1.007311e-01 -0.008019344  0.082898381  0.074852396  0.151579455
## 2014-12-31 -7.460651e-02 -0.094485553 -0.026495460 -0.044863577 -0.003502063
## 2015-01-30  5.961183e-02 -0.126849197 -0.179070570 -0.043318976  0.030690097
## 2015-02-27  9.601592e-02  0.035983912  0.046721797  0.142698651  0.118002746
## 2015-03-31 -3.187439e-02 -0.035234048 -0.122645004 -0.052581854  0.026779267
## 2015-04-30  5.769819e-03  0.090380011  0.036196620  0.058908060  0.011164221
## 2015-05-29  4.434154e-02 -0.018118715 -0.007135211  0.001460425  0.137475291
## 2015-06-30 -3.793871e-02 -0.005877592 -0.393719205 -0.095717039  0.030276306
## 2015-07-31 -3.348078e-02 -0.066586777 -0.017671265 -0.007988151  0.061762045
## 2015-08-31 -6.848891e-02 -0.028246857 -0.120420153  0.123595271 -0.123625244
## 2015-09-30 -2.205755e-02 -0.156595597 -0.091174958  0.092151131  0.046287498
## 2015-10-30  8.011214e-02  0.121525632  0.100274104  0.140555336 -0.066090204
## 2015-11-30 -5.820951e-03 -0.004669457 -0.038785784  0.115404997  0.151520831
## 2015-12-31 -1.167904e-01 -0.066734144 -0.117783043  0.038347652 -0.061646019
## 2016-01-29 -7.822355e-02 -0.075478233 -0.249802124 -0.118048960 -0.164049592
## 2016-02-29 -1.288561e-03  0.084087864 -0.036938652  0.071924263 -0.031945141
## 2016-03-31  1.197462e-01  0.122727251 -0.015166434  0.127654209  0.119324351
## 2016-04-29 -1.507312e-01  0.025002860  0.026392054 -0.002810242 -0.078190190
## 2016-05-31  6.931422e-02 -0.069388277  0.168269792  0.276388350 -0.145351868
## 2016-06-30 -4.359638e-02  0.044505409  0.078590095  0.006187786 -0.061813428
## 2016-07-29  8.623523e-02  0.097302046 -0.001454411  0.194443895  0.065104040
## 2016-08-31  2.337671e-02 -0.009835578  0.182442500  0.073469246  0.017269193
## 2016-09-30  6.344819e-02  0.079939766  0.075320413  0.110693411  0.179351041
## 2016-10-31  4.324852e-03 -0.052892675 -0.035493382  0.037805494 -0.035129476
## 2016-11-30 -2.183759e-02  0.135386768  0.129370539  0.260525062 -0.135250850
## 2016-12-30  4.684095e-02 -0.029954702  0.115447947  0.146435576 -0.071883528
## 2017-01-31  4.664144e-02  0.039263586  0.095227372  0.022602084  0.165521149
## 2017-02-28  1.255557e-01  0.010399600 -0.028182939 -0.071874716  0.028987614
## 2017-03-31  4.754117e-02 -0.041184248  0.209397606  0.070843944 -0.298670465
## 2017-04-28 -6.952873e-05  0.105775090 -0.043492743 -0.043434396 -0.038631680
## 2017-05-31  6.560725e-02  0.030528664  0.106191589  0.326022163  0.089751836
## 2017-06-30 -5.891602e-02  0.019071480 -0.030020605  0.001453841  0.120748730
## 2017-07-31  3.218039e-02  0.065833289 -0.060038468  0.117044662 -0.015287009
## 2017-08-31  1.016533e-01  0.030593604  0.128316614  0.042639638  0.006881714
## 2017-09-29 -6.213512e-02  0.059637820  0.207189943  0.053601270  0.082491922
## 2017-10-31  9.240388e-02  0.091139011  0.119222512  0.145700237  0.021284693
## 2017-11-30  2.007521e-02  0.038639980 -0.044297867 -0.029245074 -0.009899308
## 2017-12-29 -1.536302e-02  0.110110163 -0.030418208 -0.036583207 -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? My portfolio has a beta of 1.28, which means it is more sensitive to market movements than the overall market. For every 1% change in the market, my portfolio is expected to move approximately 1.28% in the same direction. This means my portfolio has higher systematic risk than the market. If the market increases by 1%, my portfolio would be expected to increase by 1.28%, but if the market decreases by 1%, my portfolio could decrease by 1.28%.