# 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("MSFT", "NVDA", "JPM")

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

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
##                     JPM         MSFT         NVDA
## 2013-01-31  0.074549371  0.027328297  0.000000000
## 2013-02-28  0.038975113  0.020914941  0.038221807
## 2013-03-28 -0.030298647  0.028720379  0.013338782
## 2013-04-30  0.038370157  0.145776799  0.070706177
## 2013-05-31  0.107826357  0.059941585  0.054651713
## 2013-06-28 -0.033528862 -0.010368831 -0.030167097
## 2013-07-31  0.061462630 -0.081394816  0.028091759
## 2013-08-30 -0.097951632  0.054854365  0.026270183
## 2013-09-30  0.022697097 -0.003599362  0.053460599
## 2013-10-31  0.004434237  0.062037800 -0.024066063
## 2013-11-29  0.104545241  0.081562438  0.032034410
## 2013-12-31  0.021781462 -0.019063222  0.026567290
## 2014-01-31 -0.048308597  0.011428906 -0.020177130
## 2014-02-28  0.026031073  0.019814935  0.162107132
## 2014-03-31  0.066220207  0.067616771 -0.025903709
## 2014-04-30 -0.074830935 -0.014498435  0.030788617
## 2014-05-30 -0.007350960  0.020307559  0.032886676
## 2014-06-30  0.036226495  0.018393893 -0.024508542
## 2014-07-31  0.007833188  0.034412925 -0.057729776
## 2014-08-29  0.030398845  0.057484821  0.110059941
## 2014-09-30  0.013200658  0.020264564 -0.052782596
## 2014-10-31  0.010691059  0.012646070  0.057399216
## 2014-11-28 -0.005305158  0.024438929  0.074851983
## 2014-12-31  0.039438222 -0.028858273 -0.044863799
## 2015-01-30 -0.134037159 -0.139546547 -0.043318356
## 2015-02-27  0.119457300  0.089036164  0.142698606
## 2015-03-31 -0.011488802 -0.075529590 -0.052582548
## 2015-04-30  0.049913149  0.179201067  0.058908812
## 2015-05-29  0.039062678 -0.030803738  0.001459937
## 2015-06-30  0.029656122 -0.059571763 -0.095716669
## 2015-07-31  0.017814142  0.056151425 -0.007988069
## 2015-08-31 -0.066827146 -0.063950951  0.123595013
## 2015-09-30 -0.050062410  0.016860631  0.092151047
## 2015-10-30  0.059589441  0.173395295  0.140555334
## 2015-11-30  0.037123221  0.038686061  0.115405301
## 2015-12-31 -0.009795700  0.020577823  0.038347505
## 2016-01-29 -0.097446955 -0.007054635 -0.118048630
## 2016-02-29 -0.055281699 -0.072343802  0.071923561
## 2016-03-31  0.050564563  0.082036442  0.127654923
## 2016-04-29  0.072421363 -0.102086883 -0.002810574
## 2016-05-31  0.032228259  0.067842387  0.276388206
## 2016-06-30 -0.049142583 -0.035138659  0.006187857
## 2016-07-29  0.036778855  0.102268281  0.194444059
## 2016-08-31  0.053713291  0.019880876  0.073468995
## 2016-09-30 -0.013573062  0.002433568  0.110693560
## 2016-10-31  0.046556064  0.039487575  0.037805049
## 2016-11-30  0.146282034  0.012391129  0.260525344
## 2016-12-30  0.073564572  0.030721335  0.146435959
## 2017-01-31 -0.013907492  0.039598491  0.022601896
## 2017-02-28  0.068386204 -0.004373571 -0.071874805
## 2017-03-31 -0.031157990  0.028960707  0.070843775
## 2017-04-28 -0.003879569  0.038718363 -0.043434142
## 2017-05-31 -0.057361189  0.025673025  0.326022101
## 2017-06-30  0.106698430 -0.013115667  0.001453608
## 2017-07-31  0.009852587  0.053250193  0.117044954
## 2017-08-31 -0.009962338  0.033388856  0.042639542
## 2017-09-29  0.049581231 -0.003751831  0.053600901
## 2017-10-31  0.057848799  0.110341763  0.145700636
## 2017-11-30  0.038126933  0.016841455 -0.029244912
## 2017-12-29  0.022889426  0.016145805 -0.036583671
## 2018-01-31  0.083669221  0.104997518  0.239240760
## 2018-02-28 -0.001470681 -0.008450432 -0.014959241
## 2018-03-29 -0.049063139 -0.027022755 -0.043969066
## 2018-04-30 -0.005821571  0.024352974 -0.029312749
## 2018-05-31 -0.016405414  0.059652087  0.115145209
## 2018-06-29 -0.026610132 -0.002329567 -0.062544712
## 2018-07-31  0.103604772  0.073020713  0.033048545
## 2018-08-31 -0.003224123  0.061088316  0.137075561
## 2018-09-28 -0.015302451  0.017997938  0.001210333
## 2018-10-31 -0.027461014 -0.068387374 -0.287373466
## 2018-11-30  0.019708974  0.041798127 -0.253667486
## 2018-12-31 -0.130157877 -0.087790547 -0.202283131
## 2019-01-31  0.066577395  0.027768727  0.073974068
## 2019-02-28  0.008274611  0.074511548  0.071593962
## 2019-03-29 -0.030451285  0.051409238  0.151869897
## 2019-04-30  0.144248756  0.101963126  0.007987591
## 2019-05-31 -0.090959541 -0.050746872 -0.288679712
## 2019-06-28  0.053650069  0.079843827  0.192591102
## 2019-07-31  0.043933176  0.017096946  0.026972977
## 2019-08-30 -0.054383187  0.014924903 -0.006208027
## 2019-09-30  0.068847083  0.008451012  0.038414287
## 2019-10-31  0.067598394  0.030738801  0.143947033
## 2019-11-29  0.053308492  0.057761278  0.076031623
## 2019-12-31  0.056365128  0.040901392  0.082162775
## 2020-01-31 -0.045422318  0.076455999  0.004790923
## 2020-02-28 -0.130987623 -0.046764791  0.133626727
## 2020-03-31 -0.254395011 -0.026899910 -0.024248033
## 2020-04-30  0.072039884  0.127800332  0.103279103
## 2020-05-29  0.016056899  0.025074069  0.194462007
## 2020-06-30 -0.033969243  0.104863800  0.068216657
## 2020-07-31  0.036757393  0.007343942  0.111189630
## 2020-08-31  0.036075516  0.097808640  0.231105512
## 2020-09-30 -0.039911491 -0.069775299  0.011895278
## 2020-10-30  0.027455543 -0.038085916 -0.076501367
## 2020-11-30  0.184291360  0.058325708  0.066921886
## 2020-12-31  0.075071037  0.038264447 -0.025900109
## 2021-01-29  0.019688661  0.041997603 -0.005010607
## 2021-02-26  0.134337352  0.004109443  0.054293069
## 2021-03-31  0.033804300  0.014482799 -0.026723480
## 2021-04-30  0.016197861  0.067286232  0.117298210
## 2021-05-28  0.065610782 -0.007656455  0.079071026
## 2021-06-30 -0.054425899  0.081569398  0.208332078
## 2021-07-30 -0.018724906  0.050423879 -0.025494172
## 2021-08-31  0.052429340  0.059768779  0.138204347
## 2021-09-30  0.023113281 -0.068406170 -0.077484837
## 2021-10-29  0.043184345  0.162366265  0.210396168
## 2021-11-30 -0.067316649 -0.001282984  0.245338401
## 2021-12-31 -0.003026564  0.017184177 -0.105149815
## 2022-01-31 -0.057573526 -0.078334406 -0.183267150
## 2022-02-28 -0.046840396 -0.037922089 -0.004133324
## 2022-03-31 -0.039412579  0.031364824  0.112575904
## 2022-04-29 -0.125475909 -0.105212739 -0.386065593
## 2022-05-31  0.102398667 -0.018242778  0.006716933
## 2022-06-30 -0.160612397 -0.056909491 -0.208219302
## 2022-07-29  0.032933872  0.089014403  0.180792182
## 2022-08-31 -0.014230351 -0.068988817 -0.185089286
## 2022-09-30 -0.084640075 -0.115710511 -0.217576763
## 2022-10-31  0.195049034 -0.003311575  0.106044068
## 2022-11-30  0.093228115  0.097329002  0.226462001
## 2022-12-30 -0.029971251 -0.061923757 -0.146693755
## 2023-01-31  0.050130267  0.032773717  0.290330267
## 2023-02-28  0.023932540  0.008977432  0.172531504
## 2023-03-31 -0.095372940  0.144863440  0.179536377
## 2023-04-28  0.066892425  0.063692759 -0.001008479
## 2023-05-31 -0.018471055  0.068691244  0.310008358
## 2023-06-30  0.069243373  0.036330637  0.111729642
## 2023-07-31  0.089422440 -0.013659528  0.099530706
## 2023-08-31 -0.076477604 -0.022476376  0.054674047
## 2023-09-29 -0.008992648 -0.037330914 -0.126218735
## 2023-10-31 -0.034614472  0.068420489 -0.064546216
## 2023-11-30  0.115463231  0.115955040  0.137050301
## 2023-12-08  0.015512087 -0.012428109  0.015701912
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              JPM        MSFT        NVDA
## JPM  0.004701854 0.001633922 0.002797903
## MSFT 0.001633922 0.003580588 0.004116407
## NVDA 0.002797903 0.004116407 0.015021891
# Standard deviation of portfolio
# Summarizes how much each asset's returns vary with those of other assets within the portfolio into a single number
w <- c(.4, .3, .3)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.06504699
# 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
##             JPM       MSFT       NVDA
## [1,] 0.01974135 0.01366397 0.03164167
rowSums(component_contribution)
## [1] 0.06504699
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 3
##     JPM  MSFT  NVDA
##   <dbl> <dbl> <dbl>
## 1 0.303  0.21 0.486
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 JPM          0.303
## 2 MSFT         0.21 
## 3 NVDA         0.486
# 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
##                     JPM         MSFT         NVDA
## 2013-01-31  0.074549371  0.027328297  0.000000000
## 2013-02-28  0.038975113  0.020914941  0.038221807
## 2013-03-28 -0.030298647  0.028720379  0.013338782
## 2013-04-30  0.038370157  0.145776799  0.070706177
## 2013-05-31  0.107826357  0.059941585  0.054651713
## 2013-06-28 -0.033528862 -0.010368831 -0.030167097
## 2013-07-31  0.061462630 -0.081394816  0.028091759
## 2013-08-30 -0.097951632  0.054854365  0.026270183
## 2013-09-30  0.022697097 -0.003599362  0.053460599
## 2013-10-31  0.004434237  0.062037800 -0.024066063
## 2013-11-29  0.104545241  0.081562438  0.032034410
## 2013-12-31  0.021781462 -0.019063222  0.026567290
## 2014-01-31 -0.048308597  0.011428906 -0.020177130
## 2014-02-28  0.026031073  0.019814935  0.162107132
## 2014-03-31  0.066220207  0.067616771 -0.025903709
## 2014-04-30 -0.074830935 -0.014498435  0.030788617
## 2014-05-30 -0.007350960  0.020307559  0.032886676
## 2014-06-30  0.036226495  0.018393893 -0.024508542
## 2014-07-31  0.007833188  0.034412925 -0.057729776
## 2014-08-29  0.030398845  0.057484821  0.110059941
## 2014-09-30  0.013200658  0.020264564 -0.052782596
## 2014-10-31  0.010691059  0.012646070  0.057399216
## 2014-11-28 -0.005305158  0.024438929  0.074851983
## 2014-12-31  0.039438222 -0.028858273 -0.044863799
## 2015-01-30 -0.134037159 -0.139546547 -0.043318356
## 2015-02-27  0.119457300  0.089036164  0.142698606
## 2015-03-31 -0.011488802 -0.075529590 -0.052582548
## 2015-04-30  0.049913149  0.179201067  0.058908812
## 2015-05-29  0.039062678 -0.030803738  0.001459937
## 2015-06-30  0.029656122 -0.059571763 -0.095716669
## 2015-07-31  0.017814142  0.056151425 -0.007988069
## 2015-08-31 -0.066827146 -0.063950951  0.123595013
## 2015-09-30 -0.050062410  0.016860631  0.092151047
## 2015-10-30  0.059589441  0.173395295  0.140555334
## 2015-11-30  0.037123221  0.038686061  0.115405301
## 2015-12-31 -0.009795700  0.020577823  0.038347505
## 2016-01-29 -0.097446955 -0.007054635 -0.118048630
## 2016-02-29 -0.055281699 -0.072343802  0.071923561
## 2016-03-31  0.050564563  0.082036442  0.127654923
## 2016-04-29  0.072421363 -0.102086883 -0.002810574
## 2016-05-31  0.032228259  0.067842387  0.276388206
## 2016-06-30 -0.049142583 -0.035138659  0.006187857
## 2016-07-29  0.036778855  0.102268281  0.194444059
## 2016-08-31  0.053713291  0.019880876  0.073468995
## 2016-09-30 -0.013573062  0.002433568  0.110693560
## 2016-10-31  0.046556064  0.039487575  0.037805049
## 2016-11-30  0.146282034  0.012391129  0.260525344
## 2016-12-30  0.073564572  0.030721335  0.146435959
## 2017-01-31 -0.013907492  0.039598491  0.022601896
## 2017-02-28  0.068386204 -0.004373571 -0.071874805
## 2017-03-31 -0.031157990  0.028960707  0.070843775
## 2017-04-28 -0.003879569  0.038718363 -0.043434142
## 2017-05-31 -0.057361189  0.025673025  0.326022101
## 2017-06-30  0.106698430 -0.013115667  0.001453608
## 2017-07-31  0.009852587  0.053250193  0.117044954
## 2017-08-31 -0.009962338  0.033388856  0.042639542
## 2017-09-29  0.049581231 -0.003751831  0.053600901
## 2017-10-31  0.057848799  0.110341763  0.145700636
## 2017-11-30  0.038126933  0.016841455 -0.029244912
## 2017-12-29  0.022889426  0.016145805 -0.036583671
## 2018-01-31  0.083669221  0.104997518  0.239240760
## 2018-02-28 -0.001470681 -0.008450432 -0.014959241
## 2018-03-29 -0.049063139 -0.027022755 -0.043969066
## 2018-04-30 -0.005821571  0.024352974 -0.029312749
## 2018-05-31 -0.016405414  0.059652087  0.115145209
## 2018-06-29 -0.026610132 -0.002329567 -0.062544712
## 2018-07-31  0.103604772  0.073020713  0.033048545
## 2018-08-31 -0.003224123  0.061088316  0.137075561
## 2018-09-28 -0.015302451  0.017997938  0.001210333
## 2018-10-31 -0.027461014 -0.068387374 -0.287373466
## 2018-11-30  0.019708974  0.041798127 -0.253667486
## 2018-12-31 -0.130157877 -0.087790547 -0.202283131
## 2019-01-31  0.066577395  0.027768727  0.073974068
## 2019-02-28  0.008274611  0.074511548  0.071593962
## 2019-03-29 -0.030451285  0.051409238  0.151869897
## 2019-04-30  0.144248756  0.101963126  0.007987591
## 2019-05-31 -0.090959541 -0.050746872 -0.288679712
## 2019-06-28  0.053650069  0.079843827  0.192591102
## 2019-07-31  0.043933176  0.017096946  0.026972977
## 2019-08-30 -0.054383187  0.014924903 -0.006208027
## 2019-09-30  0.068847083  0.008451012  0.038414287
## 2019-10-31  0.067598394  0.030738801  0.143947033
## 2019-11-29  0.053308492  0.057761278  0.076031623
## 2019-12-31  0.056365128  0.040901392  0.082162775
## 2020-01-31 -0.045422318  0.076455999  0.004790923
## 2020-02-28 -0.130987623 -0.046764791  0.133626727
## 2020-03-31 -0.254395011 -0.026899910 -0.024248033
## 2020-04-30  0.072039884  0.127800332  0.103279103
## 2020-05-29  0.016056899  0.025074069  0.194462007
## 2020-06-30 -0.033969243  0.104863800  0.068216657
## 2020-07-31  0.036757393  0.007343942  0.111189630
## 2020-08-31  0.036075516  0.097808640  0.231105512
## 2020-09-30 -0.039911491 -0.069775299  0.011895278
## 2020-10-30  0.027455543 -0.038085916 -0.076501367
## 2020-11-30  0.184291360  0.058325708  0.066921886
## 2020-12-31  0.075071037  0.038264447 -0.025900109
## 2021-01-29  0.019688661  0.041997603 -0.005010607
## 2021-02-26  0.134337352  0.004109443  0.054293069
## 2021-03-31  0.033804300  0.014482799 -0.026723480
## 2021-04-30  0.016197861  0.067286232  0.117298210
## 2021-05-28  0.065610782 -0.007656455  0.079071026
## 2021-06-30 -0.054425899  0.081569398  0.208332078
## 2021-07-30 -0.018724906  0.050423879 -0.025494172
## 2021-08-31  0.052429340  0.059768779  0.138204347
## 2021-09-30  0.023113281 -0.068406170 -0.077484837
## 2021-10-29  0.043184345  0.162366265  0.210396168
## 2021-11-30 -0.067316649 -0.001282984  0.245338401
## 2021-12-31 -0.003026564  0.017184177 -0.105149815
## 2022-01-31 -0.057573526 -0.078334406 -0.183267150
## 2022-02-28 -0.046840396 -0.037922089 -0.004133324
## 2022-03-31 -0.039412579  0.031364824  0.112575904
## 2022-04-29 -0.125475909 -0.105212739 -0.386065593
## 2022-05-31  0.102398667 -0.018242778  0.006716933
## 2022-06-30 -0.160612397 -0.056909491 -0.208219302
## 2022-07-29  0.032933872  0.089014403  0.180792182
## 2022-08-31 -0.014230351 -0.068988817 -0.185089286
## 2022-09-30 -0.084640075 -0.115710511 -0.217576763
## 2022-10-31  0.195049034 -0.003311575  0.106044068
## 2022-11-30  0.093228115  0.097329002  0.226462001
## 2022-12-30 -0.029971251 -0.061923757 -0.146693755
## 2023-01-31  0.050130267  0.032773717  0.290330267
## 2023-02-28  0.023932540  0.008977432  0.172531504
## 2023-03-31 -0.095372940  0.144863440  0.179536377
## 2023-04-28  0.066892425  0.063692759 -0.001008479
## 2023-05-31 -0.018471055  0.068691244  0.310008358
## 2023-06-30  0.069243373  0.036330637  0.111729642
## 2023-07-31  0.089422440 -0.013659528  0.099530706
## 2023-08-31 -0.076477604 -0.022476376  0.054674047
## 2023-09-29 -0.008992648 -0.037330914 -0.126218735
## 2023-10-31 -0.034614472  0.068420489 -0.064546216
## 2023-11-30  0.115463231  0.115955040  0.137050301
## 2023-12-08  0.015512087 -0.012428109  0.015701912
calculate_component_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_component_contribution(w = c(.4, .3, .3))
## # A tibble: 1 × 3
##     JPM  MSFT  NVDA
##   <dbl> <dbl> <dbl>
## 1 0.303  0.21 0.486

## 6 Plot: Column Chart of Component Contribution and Weight

```r
plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.4, .3, .3 )) %>%
    
    # Transform to long from
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add Weights
    add_column(weight = c(.4, .3, .3)) %>%
    
    # 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?

The largest contributor to my portfolio volatility is NVDA. I also think that the risk is concentrated in just this one asset.