# 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("HMC", "WMT", "TGT")

prices <- tq_get(x = symbols, 
                 get = "stock.prices", 
                 from = "2012-12-31", 
                 to  = "2023-04-25")

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

Component Contribution Step-by-Step

# 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
##                      HMC           TGT           WMT
## 2013-01-31  0.0200998356  0.0207401596  0.0248959215
## 2013-02-28 -0.0066549588  0.0470673617  0.0117958395
## 2013-03-28  0.0263451800  0.0836038329  0.0620732465
## 2013-04-30  0.0439746672  0.0303601401  0.0378938538
## 2013-05-31 -0.0621735609 -0.0099614681 -0.0317802058
## 2013-06-28 -0.0031788525 -0.0092510365 -0.0046873872
## 2013-07-31 -0.0029571990  0.0341195118  0.0452742835
## 2013-08-30 -0.0328436085 -0.1118619627 -0.0596995892
## 2013-09-30  0.0640437702  0.0105272323  0.0133384818
## 2013-10-31  0.0466152820  0.0125805713  0.0370296261
## 2013-11-29  0.0583255324 -0.0069129537  0.0540190449
## 2013-12-31 -0.0199773013 -0.0103775452 -0.0232522335
## 2014-01-31 -0.0974646212 -0.1106960079 -0.0523038937
## 2014-02-28 -0.0397009050  0.1066820566  0.0002674871
## 2014-03-31 -0.0198911029 -0.0329974794  0.0293265369
## 2014-04-30 -0.0594581327  0.0202854596  0.0420195607
## 2014-05-30  0.0549203754 -0.0769022471 -0.0314089587
## 2014-06-30 -0.0002402223  0.0207483820 -0.0223932027
## 2014-07-31 -0.0031485471  0.0279073667 -0.0200475992
## 2014-08-29 -0.0240833990  0.0169978927  0.0323260251
## 2014-09-30  0.0113455078  0.0425314489  0.0127659485
## 2014-10-31 -0.0650832295 -0.0138152997 -0.0026192839
## 2014-11-28 -0.0576712660  0.1874999017  0.1378163776
## 2014-12-31 -0.0214573194  0.0254832182 -0.0135738497
## 2015-01-30  0.0234358641 -0.0307676680 -0.0105347679
## 2015-02-27  0.0922373323  0.0496020406 -0.0124331196
## 2015-03-31 -0.0068034631  0.0659777396 -0.0142310815
## 2015-04-30  0.0232320477 -0.0402790373 -0.0524139873
## 2015-05-29  0.0203699186  0.0128404328 -0.0433512631
## 2015-06-30 -0.0546518440  0.0287063480 -0.0460133698
## 2015-07-31  0.0470248672  0.0026914765  0.0146947913
## 2015-08-31 -0.0758308511 -0.0448221016 -0.0993584484
## 2015-09-30 -0.0462686081  0.0121506650  0.0016979077
## 2015-10-30  0.1025807637 -0.0189940775 -0.1246694221
## 2015-11-30 -0.0136759128 -0.0547335721  0.0275686541
## 2015-12-31 -0.0232171806  0.0015161203  0.0492988632
## 2016-01-29 -0.1669687716 -0.0026202876  0.0793149339
## 2016-02-29 -0.0496973160  0.0882424144 -0.0003016288
## 2016-03-31  0.0682377781  0.0476663391  0.0392703246
## 2016-04-29 -0.0139966361 -0.0343708838 -0.0239372452
## 2016-05-31  0.0371357823 -0.1372352096  0.0641212993
## 2016-06-30 -0.0919533964  0.0150074308  0.0311567814
## 2016-07-29  0.0682819576  0.0759577498 -0.0006851574
## 2016-08-31  0.1275681074 -0.0627265798 -0.0143680361
## 2016-09-30 -0.0574329163 -0.0217474804  0.0094737433
## 2016-10-31  0.0309813130  0.0007280338 -0.0295509617
## 2016-11-30 -0.0030216391  0.1251762441  0.0058384740
## 2016-12-30 -0.0123577178 -0.0670622258 -0.0116432875
## 2017-01-31  0.0179939192 -0.1135002464 -0.0350397876
## 2017-02-28  0.0411989466 -0.0835534105  0.0608891514
## 2017-03-31 -0.0170062708 -0.0628498668  0.0234091685
## 2017-04-28 -0.0390887948  0.0118879433  0.0421084103
## 2017-05-31 -0.0410365836 -0.0018020680  0.0511566238
## 2017-06-30 -0.0195235215 -0.0532513169 -0.0378582092
## 2017-07-31  0.0227407505  0.0804395238  0.0553879644
## 2017-08-31  0.0028509824 -0.0272904153 -0.0180258692
## 2017-09-29  0.0506523860  0.0789559063  0.0008965137
## 2017-10-31  0.0504640714  0.0005083642  0.1109627769
## 2017-11-30  0.0698717551  0.0247791645  0.1076144230
## 2017-12-29  0.0274914471  0.0855496684  0.0207684451
## 2018-01-31  0.0348887845  0.1421908955  0.0764922338
## 2018-02-28  0.0224163187  0.0107467078 -0.1691628230
## 2018-03-29 -0.0324651442 -0.0826207252 -0.0056774142
## 2018-04-30 -0.0107104615  0.0446461175 -0.0057485917
## 2018-05-31 -0.0786855229  0.0125275480 -0.0629873952
## 2018-06-29 -0.0816445365  0.0433596857  0.0369862619
## 2018-07-31  0.0463956745  0.0581796958  0.0409481429
## 2018-08-31 -0.0341715828  0.0889778314  0.0774627690
## 2018-09-28  0.0216028142  0.0080814374 -0.0205518131
## 2018-10-31 -0.0539563840 -0.0533180658  0.0656292489
## 2018-11-30 -0.0116464313 -0.1560244647 -0.0265764899
## 2018-12-31 -0.0552824644 -0.0710992105 -0.0417362936
## 2019-01-31  0.1282718116  0.0994419249  0.0283644816
## 2019-02-28 -0.0617266706  0.0038816480  0.0324429537
## 2019-03-29 -0.0320138365  0.0997558841 -0.0094924042
## 2019-04-30  0.0261548796 -0.0360265133  0.0530144104
## 2019-05-31 -0.1251152129  0.0473594236 -0.0084088417
## 2019-06-28  0.0569595655  0.0737795035  0.0854575277
## 2019-07-31 -0.0378596504 -0.0024277654 -0.0009961233
## 2019-08-30 -0.0502783483  0.2218672784  0.0394580055
## 2019-09-30  0.1049586393 -0.0012153882  0.0379543403
## 2019-10-31  0.0335564199  0.0000000000 -0.0120370555
## 2019-11-29  0.0417558461  0.1623189835  0.0154855924
## 2019-12-31  0.0137124657  0.0252757336  0.0023740140
## 2020-01-31 -0.1006227757 -0.1464843643 -0.0372903940
## 2020-02-28  0.0023411179 -0.0667811361 -0.0613237571
## 2020-03-31 -0.1242897870 -0.1024521624  0.0581106948
## 2020-04-30  0.0683992494  0.1658370196  0.0674661933
## 2020-05-29  0.0798826281  0.1138939583  0.0248286674
## 2020-06-30 -0.0159368157 -0.0198140966 -0.0351084730
## 2020-07-31 -0.0484967636  0.0484207511  0.0772514922
## 2020-08-31  0.0492790018  0.1882718513  0.0745885886
## 2020-09-30 -0.0699157735  0.0402475082  0.0076053150
## 2020-10-30 -0.0033812247 -0.0335903465 -0.0083257524
## 2020-11-30  0.1600602026  0.1691406393  0.0963907867
## 2020-12-31  0.0256519355 -0.0168515315 -0.0545615515
## 2021-01-29 -0.0647037194  0.0259450822 -0.0257178730
## 2021-02-26  0.0439590768  0.0160102396 -0.0782175350
## 2021-03-31  0.1003837894  0.0767328029  0.0486518570
## 2021-04-30 -0.0126626142  0.0453537305  0.0295951624
## 2021-05-28  0.0471600039  0.0938664454  0.0189582893
## 2021-06-30  0.0290057997  0.0632651915 -0.0071363926
## 2021-07-30 -0.0024892172  0.0768491811  0.0107910843
## 2021-08-31 -0.0590291902 -0.0519785598  0.0418679805
## 2021-09-30  0.0259284405 -0.0765903009 -0.0606838050
## 2021-10-29 -0.0361865349  0.1265019373  0.0695572256
## 2021-11-30 -0.0776508507 -0.0592961736 -0.0606288947
## 2021-12-31  0.0387005538 -0.0521915116  0.0324794702
## 2022-01-31  0.0379355855 -0.0487405501 -0.0343091704
## 2022-02-28  0.0336082625 -0.0940889760 -0.0338249225
## 2022-03-31 -0.0639586273  0.0604567191  0.1008100168
## 2022-04-29 -0.0737813917  0.0745690121  0.0269634002
## 2022-05-31 -0.0527981907 -0.3412238153 -0.1698041909
## 2022-06-30 -0.0305834031 -0.1364655946 -0.0563675996
## 2022-07-29  0.0629844441  0.1456890741  0.0826082211
## 2022-08-31  0.0306303499 -0.0125340202  0.0081250670
## 2022-09-30 -0.1920033081 -0.0774525273 -0.0217358900
## 2022-10-31  0.0554070999  0.1015456551  0.0929242133
## 2022-11-30  0.0710357731  0.0232761434  0.0684915859
## 2022-12-30 -0.0692844154 -0.1141982500 -0.0685300595
## 2023-01-31  0.0822610777  0.1440934537  0.0145629779
## 2023-02-28  0.0452922074 -0.0151216005 -0.0121678593
## 2023-03-31  0.0198253161 -0.0171793346  0.0408371631
## 2023-04-24 -0.0129181128 -0.0102558957  0.0353789147
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              HMC         TGT          WMT
## HMC 0.0035479914 0.001338602 0.0004475078
## TGT 0.0013386019 0.006661768 0.0022292015
## WMT 0.0004475078 0.002229202 0.0027250537
# 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(0.25, 0.25, 0.5)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.04643141
# 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
##              HMC       TGT        WMT
## [1,] 0.007782459 0.0167704 0.02187855
rowSums(component_contribution)
## [1] 0.04643141
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 3
##     HMC   TGT   WMT
##   <dbl> <dbl> <dbl>
## 1 0.168 0.361 0.471
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 HMC          0.168
## 2 TGT          0.361
## 3 WMT          0.471

Component Contribution with a Custom Function

# 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
##                      HMC           TGT           WMT
## 2013-01-31  0.0200998356  0.0207401596  0.0248959215
## 2013-02-28 -0.0066549588  0.0470673617  0.0117958395
## 2013-03-28  0.0263451800  0.0836038329  0.0620732465
## 2013-04-30  0.0439746672  0.0303601401  0.0378938538
## 2013-05-31 -0.0621735609 -0.0099614681 -0.0317802058
## 2013-06-28 -0.0031788525 -0.0092510365 -0.0046873872
## 2013-07-31 -0.0029571990  0.0341195118  0.0452742835
## 2013-08-30 -0.0328436085 -0.1118619627 -0.0596995892
## 2013-09-30  0.0640437702  0.0105272323  0.0133384818
## 2013-10-31  0.0466152820  0.0125805713  0.0370296261
## 2013-11-29  0.0583255324 -0.0069129537  0.0540190449
## 2013-12-31 -0.0199773013 -0.0103775452 -0.0232522335
## 2014-01-31 -0.0974646212 -0.1106960079 -0.0523038937
## 2014-02-28 -0.0397009050  0.1066820566  0.0002674871
## 2014-03-31 -0.0198911029 -0.0329974794  0.0293265369
## 2014-04-30 -0.0594581327  0.0202854596  0.0420195607
## 2014-05-30  0.0549203754 -0.0769022471 -0.0314089587
## 2014-06-30 -0.0002402223  0.0207483820 -0.0223932027
## 2014-07-31 -0.0031485471  0.0279073667 -0.0200475992
## 2014-08-29 -0.0240833990  0.0169978927  0.0323260251
## 2014-09-30  0.0113455078  0.0425314489  0.0127659485
## 2014-10-31 -0.0650832295 -0.0138152997 -0.0026192839
## 2014-11-28 -0.0576712660  0.1874999017  0.1378163776
## 2014-12-31 -0.0214573194  0.0254832182 -0.0135738497
## 2015-01-30  0.0234358641 -0.0307676680 -0.0105347679
## 2015-02-27  0.0922373323  0.0496020406 -0.0124331196
## 2015-03-31 -0.0068034631  0.0659777396 -0.0142310815
## 2015-04-30  0.0232320477 -0.0402790373 -0.0524139873
## 2015-05-29  0.0203699186  0.0128404328 -0.0433512631
## 2015-06-30 -0.0546518440  0.0287063480 -0.0460133698
## 2015-07-31  0.0470248672  0.0026914765  0.0146947913
## 2015-08-31 -0.0758308511 -0.0448221016 -0.0993584484
## 2015-09-30 -0.0462686081  0.0121506650  0.0016979077
## 2015-10-30  0.1025807637 -0.0189940775 -0.1246694221
## 2015-11-30 -0.0136759128 -0.0547335721  0.0275686541
## 2015-12-31 -0.0232171806  0.0015161203  0.0492988632
## 2016-01-29 -0.1669687716 -0.0026202876  0.0793149339
## 2016-02-29 -0.0496973160  0.0882424144 -0.0003016288
## 2016-03-31  0.0682377781  0.0476663391  0.0392703246
## 2016-04-29 -0.0139966361 -0.0343708838 -0.0239372452
## 2016-05-31  0.0371357823 -0.1372352096  0.0641212993
## 2016-06-30 -0.0919533964  0.0150074308  0.0311567814
## 2016-07-29  0.0682819576  0.0759577498 -0.0006851574
## 2016-08-31  0.1275681074 -0.0627265798 -0.0143680361
## 2016-09-30 -0.0574329163 -0.0217474804  0.0094737433
## 2016-10-31  0.0309813130  0.0007280338 -0.0295509617
## 2016-11-30 -0.0030216391  0.1251762441  0.0058384740
## 2016-12-30 -0.0123577178 -0.0670622258 -0.0116432875
## 2017-01-31  0.0179939192 -0.1135002464 -0.0350397876
## 2017-02-28  0.0411989466 -0.0835534105  0.0608891514
## 2017-03-31 -0.0170062708 -0.0628498668  0.0234091685
## 2017-04-28 -0.0390887948  0.0118879433  0.0421084103
## 2017-05-31 -0.0410365836 -0.0018020680  0.0511566238
## 2017-06-30 -0.0195235215 -0.0532513169 -0.0378582092
## 2017-07-31  0.0227407505  0.0804395238  0.0553879644
## 2017-08-31  0.0028509824 -0.0272904153 -0.0180258692
## 2017-09-29  0.0506523860  0.0789559063  0.0008965137
## 2017-10-31  0.0504640714  0.0005083642  0.1109627769
## 2017-11-30  0.0698717551  0.0247791645  0.1076144230
## 2017-12-29  0.0274914471  0.0855496684  0.0207684451
## 2018-01-31  0.0348887845  0.1421908955  0.0764922338
## 2018-02-28  0.0224163187  0.0107467078 -0.1691628230
## 2018-03-29 -0.0324651442 -0.0826207252 -0.0056774142
## 2018-04-30 -0.0107104615  0.0446461175 -0.0057485917
## 2018-05-31 -0.0786855229  0.0125275480 -0.0629873952
## 2018-06-29 -0.0816445365  0.0433596857  0.0369862619
## 2018-07-31  0.0463956745  0.0581796958  0.0409481429
## 2018-08-31 -0.0341715828  0.0889778314  0.0774627690
## 2018-09-28  0.0216028142  0.0080814374 -0.0205518131
## 2018-10-31 -0.0539563840 -0.0533180658  0.0656292489
## 2018-11-30 -0.0116464313 -0.1560244647 -0.0265764899
## 2018-12-31 -0.0552824644 -0.0710992105 -0.0417362936
## 2019-01-31  0.1282718116  0.0994419249  0.0283644816
## 2019-02-28 -0.0617266706  0.0038816480  0.0324429537
## 2019-03-29 -0.0320138365  0.0997558841 -0.0094924042
## 2019-04-30  0.0261548796 -0.0360265133  0.0530144104
## 2019-05-31 -0.1251152129  0.0473594236 -0.0084088417
## 2019-06-28  0.0569595655  0.0737795035  0.0854575277
## 2019-07-31 -0.0378596504 -0.0024277654 -0.0009961233
## 2019-08-30 -0.0502783483  0.2218672784  0.0394580055
## 2019-09-30  0.1049586393 -0.0012153882  0.0379543403
## 2019-10-31  0.0335564199  0.0000000000 -0.0120370555
## 2019-11-29  0.0417558461  0.1623189835  0.0154855924
## 2019-12-31  0.0137124657  0.0252757336  0.0023740140
## 2020-01-31 -0.1006227757 -0.1464843643 -0.0372903940
## 2020-02-28  0.0023411179 -0.0667811361 -0.0613237571
## 2020-03-31 -0.1242897870 -0.1024521624  0.0581106948
## 2020-04-30  0.0683992494  0.1658370196  0.0674661933
## 2020-05-29  0.0798826281  0.1138939583  0.0248286674
## 2020-06-30 -0.0159368157 -0.0198140966 -0.0351084730
## 2020-07-31 -0.0484967636  0.0484207511  0.0772514922
## 2020-08-31  0.0492790018  0.1882718513  0.0745885886
## 2020-09-30 -0.0699157735  0.0402475082  0.0076053150
## 2020-10-30 -0.0033812247 -0.0335903465 -0.0083257524
## 2020-11-30  0.1600602026  0.1691406393  0.0963907867
## 2020-12-31  0.0256519355 -0.0168515315 -0.0545615515
## 2021-01-29 -0.0647037194  0.0259450822 -0.0257178730
## 2021-02-26  0.0439590768  0.0160102396 -0.0782175350
## 2021-03-31  0.1003837894  0.0767328029  0.0486518570
## 2021-04-30 -0.0126626142  0.0453537305  0.0295951624
## 2021-05-28  0.0471600039  0.0938664454  0.0189582893
## 2021-06-30  0.0290057997  0.0632651915 -0.0071363926
## 2021-07-30 -0.0024892172  0.0768491811  0.0107910843
## 2021-08-31 -0.0590291902 -0.0519785598  0.0418679805
## 2021-09-30  0.0259284405 -0.0765903009 -0.0606838050
## 2021-10-29 -0.0361865349  0.1265019373  0.0695572256
## 2021-11-30 -0.0776508507 -0.0592961736 -0.0606288947
## 2021-12-31  0.0387005538 -0.0521915116  0.0324794702
## 2022-01-31  0.0379355855 -0.0487405501 -0.0343091704
## 2022-02-28  0.0336082625 -0.0940889760 -0.0338249225
## 2022-03-31 -0.0639586273  0.0604567191  0.1008100168
## 2022-04-29 -0.0737813917  0.0745690121  0.0269634002
## 2022-05-31 -0.0527981907 -0.3412238153 -0.1698041909
## 2022-06-30 -0.0305834031 -0.1364655946 -0.0563675996
## 2022-07-29  0.0629844441  0.1456890741  0.0826082211
## 2022-08-31  0.0306303499 -0.0125340202  0.0081250670
## 2022-09-30 -0.1920033081 -0.0774525273 -0.0217358900
## 2022-10-31  0.0554070999  0.1015456551  0.0929242133
## 2022-11-30  0.0710357731  0.0232761434  0.0684915859
## 2022-12-30 -0.0692844154 -0.1141982500 -0.0685300595
## 2023-01-31  0.0822610777  0.1440934537  0.0145629779
## 2023-02-28  0.0452922074 -0.0151216005 -0.0121678593
## 2023-03-31  0.0198253161 -0.0171793346  0.0408371631
## 2023-04-24 -0.0129181128 -0.0102558957  0.0353789147
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(.25, .25, .5))
## # A tibble: 1 × 3
##     HMC   TGT   WMT
##   <dbl> <dbl> <dbl>
## 1 0.168 0.361 0.471

6 Plot: Colum Chart of Component Contribution and Weight

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

Walmart is the largest contributor to the portfolio volatility. Higher stock price volatility often means higher risk for future investment, but usually means a higher return as well. Honda is the lowest contributor to the portfolio volatility and would have smaller gap between stock prices.Looking at the chart, Walmart seems to be the riskier asset in my portfolio compared to the other assets.