# 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("TSLA", "GM", "F", "VWAGY", "HMC")

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

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
##                       F           GM           HMC         TSLA        VWAGY
## 2013-01-31  0.007336756 -0.026002601  0.0200995093  0.102078031  0.064215123
## 2013-02-28 -0.026605638 -0.034036848 -0.0066549284 -0.074128613 -0.115423770
## 2013-03-28  0.041931622  0.024377977  0.0263453501  0.084208141 -0.088817884
## 2013-04-30  0.041703448  0.103057744  0.0439744910  0.354111527  0.026375833
## 2013-05-31  0.141581447  0.094307173 -0.0621736172  0.593716693  0.096247008
## 2013-06-28 -0.013483445 -0.017262121 -0.0031780333  0.093672182 -0.091618654
## 2013-07-31  0.093098847  0.074043304 -0.0029573295  0.223739545  0.160361704
## 2013-08-30 -0.041735918 -0.051190634 -0.0328437017  0.229971572 -0.029720616
## 2013-09-30  0.041143421  0.053975023  0.0640436001  0.134706682  0.020720399
## 2013-10-31  0.019821107  0.026880105  0.0466152820 -0.189806650  0.077410242
## 2013-11-29 -0.001755214  0.047048621  0.0583256474 -0.228409431  0.060000025
## 2013-12-31 -0.101594309  0.053782270 -0.0199772990  0.167108548  0.050043363
## 2014-01-31 -0.022950382 -0.124657837 -0.0974647384  0.187261770 -0.121744161
## 2014-02-28  0.028337892  0.003320495 -0.0397010395  0.299722757  0.050921438
## 2014-03-31  0.013553323 -0.041582403 -0.0198911056 -0.160783192 -0.003538364
## 2014-04-30  0.042433462  0.001741920 -0.0594581411 -0.002690159  0.051431045
## 2014-05-30  0.017797109  0.002895926  0.0549205210 -0.000577395 -0.017739712
## 2014-06-30  0.047514889  0.056847845 -0.0002400844  0.144457218 -0.012839034
## 2014-07-31 -0.005702922 -0.070765302 -0.0031487540 -0.072372711 -0.113428953
## 2014-08-29  0.022655540  0.028565235 -0.0240834007  0.188794049 -0.028930399
## 2014-09-30 -0.163093621 -0.077044917  0.0113453685 -0.105566506 -0.085568941
## 2014-10-31 -0.039619017 -0.017051300 -0.0650831689 -0.004046457  0.031192751
## 2014-11-28  0.110104385  0.062645752 -0.0576711957  0.011599756  0.060052779
## 2014-12-31 -0.014729871  0.052200652 -0.0214572402 -0.094774520 -0.049077712
## 2015-01-30 -0.042160126 -0.067847631  0.0234357851 -0.088365243  0.033568588
## 2015-02-27  0.105088804  0.134335865  0.0922371951 -0.001277805  0.119851939
## 2015-03-31 -0.012315524  0.013256068 -0.0068033921 -0.074350086  0.020700499
## 2015-04-30 -0.011906557 -0.067279899  0.0232324763  0.180226844 -0.008425621
## 2015-05-29 -0.040690553  0.025624412  0.0203699129  0.103899535 -0.048170305
## 2015-06-30 -0.010602742 -0.065923977 -0.0546521218  0.067300966 -0.044761681
## 2015-07-31 -0.001898280 -0.056152946  0.0470248672 -0.007896618 -0.137766531
## 2015-08-31 -0.066924149 -0.067950701 -0.0758309266 -0.066366266 -0.080207861
## 2015-09-30 -0.021866865  0.031737992 -0.0462685326 -0.002653542 -0.435171713
## 2015-10-30  0.097565965  0.150909441  0.1025805496 -0.182659742  0.142824196
## 2015-11-30 -0.032947141  0.036285470 -0.0136758072  0.106828579  0.068465190
## 2015-12-31 -0.016890095 -0.051926810 -0.0232171461  0.041471546  0.039679266
## 2016-01-29 -0.132400509 -0.137529729 -0.1669686100 -0.227360646 -0.159059088
## 2016-02-29  0.046634303 -0.006770703 -0.0496974954  0.003810669  0.037151216
## 2016-03-31  0.076161100  0.077871888  0.0682378701  0.179948112  0.055333314
## 2016-04-29  0.015403854  0.011703297 -0.0139967232  0.046721799  0.096626884
## 2016-05-31 -0.005175740 -0.016487161  0.0371357015 -0.075597977 -0.033447859
## 2016-06-30 -0.070635678 -0.087467219 -0.0919533206 -0.050296472 -0.132097955
## 2016-07-29  0.018039856  0.108394905  0.0682822215  0.100785361  0.089411514
## 2016-08-31 -0.004750777  0.011976111  0.1275677086 -0.102058076 -0.019457830
## 2016-09-30 -0.042973841  0.007190813 -0.0574326494 -0.038366402 -0.003453010
## 2016-10-31 -0.015339604 -0.005365343  0.0309812729 -0.031364578  0.028473391
## 2016-11-30  0.018566119  0.088671380 -0.0030217949 -0.043041257 -0.084675967
## 2016-12-30  0.014113889  0.019727231 -0.0123574831  0.120665160  0.048389217
## 2017-01-31  0.034771189  0.049555106  0.0179939952  0.164624944  0.112170194
## 2017-02-28  0.013660239  0.006262731  0.0411988660 -0.007730394 -0.055747943
## 2017-03-31 -0.073678290 -0.030823172 -0.0170066098  0.107278734 -0.011932610
## 2017-04-28 -0.001325447 -0.020572346 -0.0390884516  0.120916240  0.069859507
## 2017-05-31 -0.030989702 -0.020709546 -0.0410365803  0.082295867 -0.023278798
## 2017-06-30  0.006275316  0.040144677 -0.0195235199  0.058654469 -0.009433293
## 2017-07-31  0.015438118  0.029617271  0.0227407486 -0.111459838  0.010387650
## 2017-08-31 -0.017079154  0.015444210  0.0028507379  0.095543417 -0.016349177
## 2017-09-29  0.081784628  0.110065867  0.0506526263 -0.042474117  0.088558904
## 2017-10-31  0.037094664  0.062400588  0.0504640676 -0.028457431  0.103465516
## 2017-11-30  0.020170179  0.002555852  0.0698716815 -0.070862536  0.102101072
## 2017-12-29 -0.002399350 -0.040906314  0.0274914471  0.008061927 -0.026479480
## 2018-01-31 -0.105436218  0.034055999  0.0348891714  0.129254601  0.094444139
## 2018-02-28 -0.033367140 -0.074888180  0.0224158688 -0.032266877 -0.111278685
## 2018-03-29  0.043344906 -0.069457110 -0.0324649835 -0.253920406  0.007017644
## 2018-04-30  0.027664547  0.010947247 -0.0107106250  0.099254538  0.014627840
## 2018-05-31  0.027206617  0.150332569 -0.0786853503 -0.031698143 -0.087929227
## 2018-06-29 -0.042446693 -0.071745352 -0.0816446819  0.186043287 -0.120069953
## 2018-07-31 -0.083701496 -0.038550787  0.0463957870 -0.140021525  0.046189492
## 2018-08-31 -0.057392759 -0.050308109 -0.0341714275  0.011737398 -0.076961092
## 2018-09-28 -0.024560851 -0.057473261  0.0216027346 -0.130439016  0.077539609
## 2018-10-31  0.049722318  0.083167879 -0.0539564546  0.242170573 -0.051019463
## 2018-11-30 -0.014768206  0.036494367 -0.0116465101  0.038271593  0.009389684
## 2018-12-31 -0.207067247 -0.115757994 -0.0552823801 -0.051761993 -0.061227715
## 2019-01-31  0.157317704  0.154022656  0.1282718385 -0.080628787  0.107473689
## 2019-02-28 -0.003414863  0.011719771 -0.0617269002  0.041033018  0.013722370
## 2019-03-29  0.001139539 -0.052301861 -0.0320136367 -0.133656450 -0.080989684
## 2019-04-30  0.190040996  0.048661792  0.0261548378 -0.159123800  0.095925726
## 2019-05-31 -0.093207012 -0.155520757 -0.1251153425 -0.253945344 -0.124659696
## 2019-06-28  0.071929545  0.155338371  0.0569596555  0.188012089  0.080335055
## 2019-07-31 -0.056064774  0.045906401 -0.0378594740  0.078092368 -0.007631271
## 2019-08-30 -0.038507303 -0.083996251 -0.0502785247 -0.068516906 -0.043354260
## 2019-09-30 -0.001091253  0.020446991  0.1049588063  0.065449564  0.052738536
## 2019-10-31 -0.047969144 -0.008574409  0.0335561721  0.268061223  0.100444784
## 2019-11-29  0.053270513 -0.031713933  0.0417560817  0.046592166  0.003425802
## 2019-12-31  0.026145053  0.027200399  0.0137123108  0.237359751  0.014884832
## 2020-01-31 -0.036128659 -0.091791709 -0.1006226912  0.441578346 -0.053779144
## 2020-02-28 -0.236842446 -0.090529736  0.0023410334  0.026424249 -0.083833726
## 2020-03-31 -0.365332951 -0.371610449 -0.1242897870 -0.242781463 -0.249361721
## 2020-04-30  0.052431288  0.070147055  0.0683991603  0.400209547  0.131429985
## 2020-05-29  0.114941221  0.149332320  0.0798827172  0.065730498  0.054701127
## 2020-06-30  0.062785698 -0.022666049 -0.0159367321  0.257108653  0.024723422
## 2020-07-31  0.083579125 -0.016338254 -0.0484967594  0.281420680 -0.034914640
## 2020-08-31  0.031275694  0.174321192  0.0492789140  0.554719315  0.137923181
## 2020-09-30 -0.023739964 -0.001350835 -0.0699157735 -0.149762308 -0.010653364
## 2020-10-30  0.148989324  0.154391973 -0.0033813147 -0.100371771 -0.098050597
## 2020-11-30  0.160965292  0.238718188  0.1600602926  0.380308523  0.175662767
## 2020-12-31 -0.032459570 -0.051485344  0.0256519355  0.217730754  0.116345233
## 2021-01-29  0.180613654  0.196470070 -0.0647037991  0.117343701  0.013339931
## 2021-02-26  0.105360568  0.012744095  0.0439591565 -0.161038203  0.102810990
## 2021-03-31  0.045937129  0.112813519  0.1003838583 -0.011269836  0.438312900
## 2021-04-30 -0.059706697 -0.004185397 -0.0126626832  0.060292565 -0.135185659
## 2021-05-28  0.230396151  0.035874420  0.0471600039 -0.126372344  0.131459735
## 2021-06-30  0.022457669 -0.002363368  0.0290057997  0.083547956 -0.095227261
## 2021-07-30 -0.063193673 -0.040174328 -0.0024891523  0.010973846  0.027147070
## 2021-08-31 -0.068225167 -0.148216013 -0.0590292551  0.068224264  0.007205146
## 2021-09-30  0.083166769  0.072780940  0.0259284405  0.052632613 -0.071603016
## 2021-10-29  0.187487138  0.032110279 -0.0361864654  0.362230204  0.046768311
## 2021-11-30  0.121503744  0.061283570 -0.0776509202  0.027237847 -0.157786067
## 2021-12-31  0.079120386  0.013047489  0.0387006261 -0.079968439  0.047335796
## 2022-01-31 -0.017760569 -0.106062068  0.0379355133 -0.120597477 -0.015878922
## 2022-02-28 -0.144997294 -0.121012131  0.0336081952 -0.073397011 -0.108303046
## 2022-03-31 -0.037718427 -0.065909281 -0.0639585600  0.213504290 -0.044398898
## 2022-04-29 -0.170852734 -0.143048254 -0.0737813917 -0.213125290 -0.133820858
## 2022-05-31 -0.034486199  0.020107937 -0.0527981907 -0.138340063  0.081543941
## 2022-06-30 -0.206290712 -0.197115141 -0.0305834031 -0.118657123 -0.209661233
## 2022-07-29  0.277522715  0.132507571  0.0629844441  0.280480148  0.081139901
## 2022-08-31  0.046680685  0.054662548  0.0306303499 -0.075250273 -0.065348569
## 2022-09-30 -0.308009789 -0.174552744 -0.1920033081 -0.038313991 -0.130525931
## 2022-10-31  0.177099596  0.201406997  0.0554070999 -0.153346761  0.050708255
## 2022-11-30  0.049274122  0.032830890  0.0710357731 -0.155866119  0.118497899
## 2022-12-30 -0.178300799 -0.184845146 -0.0692844154 -0.457813197 -0.094483434
## 2023-01-31  0.149842171  0.156017455  0.0822610777  0.340915768  0.109761116
## 2023-02-28 -0.051572812 -0.014860512  0.0452922074  0.171904975  0.017036244
## 2023-03-31  0.042973779 -0.052313989  0.0198253161  0.008471140 -0.037870274
## 2023-04-20 -0.059682605 -0.089492861 -0.0202104500 -0.241249704 -0.028471614
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(0.21, 0.25, 0.2, 0.2, 0.14))
## # A tibble: 1 × 5
##       F    GM   HMC  TSLA VWAGY
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.211 0.254 0.092 0.319 0.125

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(0.21, 0.25, 0.2, 0.2, 0.14)) %>%

    #Transform to long form
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    #Add weight
    add_column(weight = c(0.21, 0.25, 0.2, 0.2, 0.14)) %>%
    
    #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 asset in my portfolio thats the largest contributor to the portfolio volatility is TSLA. My portfolio risk is not concentrated in one asset. The highest contribtution is TSLA with just over 30%, and the second highest contributor is GM with a little over 25%. The first weights I had assigned to my portfolio, had my portfolio risk concentrated in TSLA with just over 50% conttribution. But I went back and changed the weights to try and balance things a little better, and not have my portfolio risk concentrated in any one asset.