# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(readr)

# Time series
library(lubridate)
library(tibbletime)

# modeling
library(broom)

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", "MSFT", "NKE", "JPM", "AAPL")

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
##                     AAPL          JPM          MSFT           NKE         TSLA
## 2013-01-31 -1.555896e-01  0.074549669  0.0273281679  0.0463878076  0.102078114
## 2013-02-28 -2.561110e-02  0.038975453  0.0209152190  0.0114291503 -0.074128640
## 2013-03-28  2.850544e-03 -0.030299432  0.0287202422  0.0802400683  0.084208138
## 2013-04-30  2.711615e-04  0.038370224  0.1457769843  0.0749068325  0.354111531
## 2013-05-31  2.217173e-02  0.107826499  0.0599410845 -0.0276354789  0.593716684
## 2013-06-28 -1.258962e-01 -0.033528885 -0.0103686374  0.0322349798  0.093672163
## 2013-07-31  1.321028e-01  0.061462568 -0.0813947587 -0.0120064580  0.223739522
## 2013-08-30  8.044284e-02 -0.097951423  0.0548546234  0.0017291142  0.229971642
## 2013-09-30 -2.172315e-02  0.022697226 -0.0035993926  0.1452419787  0.134706620
## 2013-10-31  9.201506e-02  0.004433629  0.0620378332  0.0420549954 -0.189806595
## 2013-11-29  6.770807e-02  0.104545664  0.0815624709  0.0436478383 -0.228409405
## 2013-12-31  8.861779e-03  0.021781333 -0.0190635066 -0.0032096479  0.167108541
## 2014-01-31 -1.139484e-01 -0.048308186  0.0114284158 -0.0764781573  0.187261714
## 2014-02-28  5.591792e-02  0.026031053  0.0198150934  0.0752105461  0.299722785
## 2014-03-31  1.975618e-02  0.066219733  0.0676173526 -0.0583763371 -0.160783242
## 2014-04-30  9.476115e-02 -0.074830576 -0.0144983919 -0.0123967259 -0.002690122
## 2014-05-30  7.576481e-02 -0.007351308  0.0203073199  0.0560128851 -0.000577422
## 2014-06-30  2.728657e-02  0.036226399  0.0183939603  0.0082872627  0.144457224
## 2014-07-31  2.832624e-02  0.007833555  0.0344127893 -0.0054309227 -0.072372676
## 2014-08-29  7.465170e-02  0.030398506  0.0574849640  0.0212580229  0.188794007
## 2014-09-30 -1.722053e-02  0.013200845  0.0202639852  0.1271460889 -0.105566477
## 2014-10-31  6.948900e-02  0.010691270  0.0126468768  0.0413957407 -0.004046477
## 2014-11-28  1.007311e-01 -0.005305143  0.0244388862  0.0657677131  0.011599801
## 2014-12-31 -7.460642e-02  0.039437908 -0.0288583659 -0.0292642183 -0.094774519
## 2015-01-30  5.961154e-02 -0.134036725 -0.1395468035 -0.0414072845 -0.088365289
## 2015-02-27  9.601591e-02  0.119456915  0.0890367695  0.0543413966 -0.001277808
## 2015-03-31 -3.187422e-02 -0.011488786 -0.0755302700  0.0325175550 -0.074350051
## 2015-04-30  5.769635e-03  0.049912966  0.1792017172 -0.0149627575  0.180226808
## 2015-05-29  4.434131e-02  0.039062509 -0.0308043535  0.0309563662  0.103899574
## 2015-06-30 -3.793790e-02  0.029656532 -0.0595710908  0.0605841811  0.067300935
## 2015-07-31 -3.348139e-02  0.017814066  0.0561508125  0.0645271023 -0.007896616
## 2015-08-31 -6.848851e-02 -0.066827077 -0.0639507777 -0.0305792061 -0.066366250
## 2015-09-30 -2.205791e-02 -0.050062238  0.0168608851  0.0982087930 -0.002653519
## 2015-10-30  8.011232e-02  0.059589022  0.1733947749  0.0634859979 -0.182659777
## 2015-11-30 -5.821134e-03  0.037123310  0.0386863400  0.0094943548  0.106828586
## 2015-12-31 -1.167900e-01 -0.009795582  0.0205777610 -0.0541858823  0.041471519
## 2016-01-29 -7.822393e-02 -0.097447106 -0.0070544578 -0.0078707765 -0.227360626
## 2016-02-29 -1.288349e-03 -0.055281675 -0.0723442836 -0.0067963298  0.003810669
## 2016-03-31  1.197460e-01  0.050564753  0.0820363196  0.0006247027  0.179948109
## 2016-04-29 -1.507312e-01  0.072421467 -0.1020864559 -0.0420291586  0.046721797
## 2016-05-31  6.931447e-02  0.032228035  0.0678426024 -0.0651946423 -0.075597968
## 2016-06-30 -4.359642e-02 -0.049142437 -0.0351387366  0.0025546376 -0.050296440
## 2016-07-29  8.623536e-02  0.036779007  0.1022680737  0.0054200452  0.100785334
## 2016-08-31  2.337613e-02  0.053713008  0.0198809863  0.0378335738 -0.102058091
## 2016-09-30  6.344845e-02 -0.013573309  0.0024333444 -0.0877707388 -0.038366372
## 2016-10-31  4.324742e-03  0.046556644  0.0394879345 -0.0480495747 -0.031364583
## 2016-11-30 -2.183717e-02  0.146281429  0.0123909402 -0.0021945347 -0.043041267
## 2016-12-30  4.684062e-02  0.073564444  0.0307215781  0.0186661315  0.120665178
## 2017-01-31  4.664158e-02 -0.013906877  0.0395980147  0.0399166536  0.164624916
## 2017-02-28  1.255553e-01  0.068386121 -0.0043733118  0.0774509121 -0.007730364
## 2017-03-31  4.754185e-02 -0.031157955  0.0289606838 -0.0222248484  0.107278727
## 2017-04-28 -6.974618e-05 -0.003879573  0.0387185908 -0.0057585537  0.120916212
## 2017-05-31  6.560702e-02 -0.057361574  0.0256724230 -0.0446566444  0.082295892
## 2017-06-30 -5.891592e-02  0.106699010 -0.0131151678  0.1108367409  0.058654468
## 2017-07-31  3.218079e-02  0.009852320  0.0532497371  0.0008469037 -0.111459860
## 2017-08-31  1.016529e-01 -0.009962552  0.0333893149 -0.1082531177  0.095543446
## 2017-09-29 -6.213489e-02  0.049580951 -0.0037519145 -0.0183456975 -0.042474144
## 2017-10-31  9.240380e-02  0.057849386  0.1103419800  0.0587964441 -0.028457409
## 2017-11-30  2.007521e-02  0.038126918  0.0168411100  0.0941688172 -0.070862541
## 2017-12-29 -1.536336e-02  0.022889250  0.0161455604  0.0379618441  0.008061928
## 2018-01-31 -1.069352e-02  0.083669116  0.1049981780  0.0867716061  0.129254571
## 2018-02-28  6.596094e-02 -0.001470657 -0.0084507813 -0.0175977176 -0.032266877
## 2018-03-29 -5.980357e-02 -0.049062932 -0.0270228068 -0.0058207229 -0.253920408
## 2018-04-30 -1.513394e-02 -0.005821929  0.0243532478  0.0289273502  0.099254576
## 2018-05-31  1.267421e-01 -0.016405205  0.0596519636  0.0486581023 -0.031698139
## 2018-06-29 -9.463225e-03 -0.026610394 -0.0023295767  0.1069231359  0.186043257
## 2018-07-31  2.759896e-02  0.103604835  0.0730207858 -0.0353827683 -0.140021491
## 2018-08-31  1.826736e-01 -0.003223966  0.0610882609  0.0689795020  0.011737389
## 2018-09-28 -8.337726e-03 -0.015302446  0.0179980881  0.0301964126 -0.130439038
## 2018-10-31 -3.095146e-02 -0.027461127 -0.0683874373 -0.1213305162  0.242170576
## 2018-11-30 -1.999128e-01  0.019709080  0.0417977833  0.0040296004  0.038271580
## 2018-12-31 -1.240887e-01 -0.130157755 -0.0877903744 -0.0131317871 -0.051761952
## 2019-01-31  5.368727e-02  0.066576942  0.0277686887  0.0992995753 -0.080628789
## 2019-02-28  4.380270e-02  0.008275074  0.0745113702  0.0459483146  0.041032981
## 2019-03-29  9.260248e-02 -0.030451537  0.0514094265 -0.0153199195 -0.133656413
## 2019-04-30  5.490125e-02  0.144249014  0.1019632337  0.0420893416 -0.159123803
## 2019-05-31 -1.326324e-01 -0.090959554 -0.0507468410 -0.1270014485 -0.253945372
## 2019-06-28  1.226769e-01  0.053649663  0.0798438310  0.0845993242  0.188012109
## 2019-07-31  7.361715e-02  0.043933704  0.0170963852  0.0244746131  0.078092373
## 2019-08-30 -1.659835e-02 -0.054383423  0.0149251823 -0.0153644133 -0.068516948
## 2019-09-30  7.042293e-02  0.068847232  0.0084507581  0.1056920642  0.065449565
## 2019-10-31  1.049764e-01  0.067598284  0.0307390208 -0.0476461863  0.268061253
## 2019-11-29  7.469368e-02  0.053308366  0.0577616592  0.0456631267  0.046592176
## 2019-12-31  9.420379e-02  0.056365535  0.0409013361  0.0803304128  0.237359743
## 2020-01-31  5.260223e-02 -0.045422719  0.0764560801 -0.0507165542  0.441578342
## 2020-02-28 -1.218309e-01 -0.130987429 -0.0467647537 -0.0718011265  0.026424253
## 2020-03-31 -7.231392e-02 -0.254395172 -0.0269000318 -0.0771937531 -0.242781458
## 2020-04-30  1.444239e-01  0.072040273  0.1278000957  0.0522720284  0.400209535
## 2020-05-29  8.166680e-02  0.016056248  0.0250743501  0.1253848850  0.065730500
## 2020-06-30  1.374865e-01 -0.033968653  0.1048638181 -0.0053911405  0.257108657
## 2020-07-31  1.528342e-01  0.036757080  0.0073435822 -0.0044971279  0.281420674
## 2020-08-31  1.960349e-01  0.036075786  0.0978088371  0.1387488104  0.554719320
## 2020-09-30 -1.081718e-01 -0.039911711 -0.0697754676  0.1151080585 -0.149762306
## 2020-10-30 -6.188802e-02  0.027455326 -0.0380861001 -0.0444661433 -0.100371771
## 2020-11-30  9.120469e-02  0.184291572  0.0583261568  0.1148920792  0.380308519
## 2020-12-31  1.084717e-01  0.075071083  0.0382645108  0.0510475423  0.217730753
## 2021-01-29 -5.516474e-03  0.019688641  0.0419973023 -0.0573123949  0.117343701
## 2021-02-26 -8.306863e-02  0.134337417  0.0041095520  0.0108995928 -0.161038203
## 2021-03-31  7.312613e-03  0.033803956  0.0144828515 -0.0141220542 -0.011269836
## 2021-04-30  7.345297e-02  0.016198194  0.0672862537 -0.0020339731  0.060292565
## 2021-05-28 -5.181668e-02  0.065610759 -0.0076565538  0.0305596017 -0.126372343
## 2021-06-30  9.450002e-02 -0.054426023  0.0815697881  0.1240976239  0.083547955
## 2021-07-30  6.295843e-02 -0.018724890  0.0504236656  0.0809136527  0.010973846
## 2021-08-31  4.161136e-02  0.052429272  0.0597685016 -0.0150256315  0.068224268
## 2021-09-30 -7.046167e-02  0.023113122 -0.0684060031 -0.1260499364  0.052632612
## 2021-10-29  5.700134e-02  0.043184555  0.1623661354  0.1414101694  0.362230202
## 2021-11-30  9.991934e-02 -0.067316514 -0.0012828541  0.0115891288  0.027237848
## 2021-12-31  7.160300e-02 -0.003026684  0.0171843399 -0.0135063034 -0.079968440
## 2022-01-31 -1.583707e-02 -0.057573476 -0.0783345858 -0.1183306406 -0.120597475
## 2022-02-28 -5.558245e-02 -0.046840509 -0.0379221423 -0.0809942857 -0.073397011
## 2022-03-31  5.588243e-02 -0.039412246  0.0313648959 -0.0124017431  0.213504290
## 2022-04-29 -1.021775e-01 -0.125476025 -0.1052128951 -0.0760991067 -0.213125289
## 2022-05-31 -5.603715e-02  0.102398409 -0.0182426077 -0.0480487606 -0.138340062
## 2022-06-30 -8.493698e-02 -0.160612226 -0.0569095631 -0.1484559805 -0.118657126
## 2022-07-29  1.728046e-01  0.032933900  0.0890143117  0.1173046645  0.280480149
## 2022-08-31 -3.170527e-02 -0.014230406 -0.0689889486 -0.0765610259 -0.075250271
## 2022-09-30 -1.289443e-01 -0.084640318 -0.1157102876 -0.2445217410 -0.038313992
## 2022-10-31  1.039557e-01  0.195049348 -0.0033114907  0.1088674839 -0.153346760
## 2022-11-30 -3.358515e-02  0.093227951  0.0973290178  0.1685054791 -0.155866121
## 2022-12-30 -1.304192e-01 -0.029971260 -0.0619237600  0.0676657853 -0.457813194
## 2023-01-31  1.048295e-01  0.050130385  0.0327737145  0.0845228135  0.340915767
## 2023-02-28  2.291828e-02  0.023932526  0.0089773456 -0.0694248240  0.171904973
## 2023-03-31  1.121214e-01 -0.095372943  0.1448634836  0.0347432649  0.008471140
## 2023-04-28  2.857494e-02  0.066892452  0.0636927152  0.0327266503 -0.233183710
## 2023-05-31  4.502923e-02 -0.018470840  0.0686912449 -0.1855463998  0.216021889
## 2023-06-30  9.014240e-02  0.069243124  0.0363307006  0.0506910085  0.249689452
## 2023-07-31  1.270443e-02  0.089422534 -0.0136595101  0.0001812493  0.021391609
## 2023-08-31 -4.330830e-02 -0.076477651 -0.0224764898 -0.0818939056 -0.035588260
## 2023-09-29 -9.285928e-02 -0.008992658 -0.0373308940 -0.0583951365 -0.030929027
## 2023-10-31 -2.573389e-03 -0.034614450  0.0684206791  0.0721114630 -0.219831983
## 2023-11-30  1.077599e-01  0.115463124  0.1159548882  0.0704384884  0.178463656
## 2023-12-29  1.349119e-02  0.086017829 -0.0076032241 -0.0121757897  0.034390133
## 2024-01-31 -4.314468e-02  0.030883170  0.0557006051 -0.0670408664 -0.282704160
## 2024-02-29 -1.871736e-02  0.064946998  0.0414474954  0.0233633911  0.075015304
## 2024-03-28 -5.264814e-02  0.073747084  0.0169714305 -0.0970692681 -0.138383423
## 2024-04-30 -6.728948e-03 -0.037859866 -0.0775402278 -0.0184713257  0.041724969
## 2024-05-31  1.224143e-01  0.055241399  0.0659666434  0.0297925148 -0.028782134
## 2024-06-28  9.125898e-02 -0.001827738  0.0738549800 -0.2280936492  0.105427913
## 2024-07-31  5.298219e-02  0.056324675 -0.0661283765 -0.0067893818  0.159378271
## 2024-08-30  3.184116e-02  0.054858322 -0.0010956475  0.1070686172 -0.080549177
## 2024-09-30  1.731639e-02 -0.064016755  0.0310605998  0.0636341593  0.200441406
## 2024-10-31 -3.090177e-02  0.057231914 -0.0572677513 -0.1363797092 -0.046070548
## 2024-11-29  5.041448e-02  0.118023364  0.0432438967  0.0210397191  0.323147326
## 2024-12-31  5.368788e-02 -0.040910361 -0.0046392831 -0.0350592955  0.157010663
## 2025-01-31 -5.930769e-02  0.114093001 -0.0153967911  0.0161241073  0.001880189
## 2025-02-28  2.554346e-02 -0.009963511 -0.0425088050  0.0323703069 -0.322794555
## 2025-03-31 -8.501352e-02 -0.075926073 -0.0559455554 -0.2191026418 -0.122658739
## 2025-04-30 -4.432081e-02  0.003364587  0.0515782678 -0.1182557516  0.085028653
## 2025-05-30 -5.507312e-02  0.076243137  0.1542991057  0.0716607065  0.205293048
## 2025-06-30  2.128049e-02  0.093621408  0.0774059356  0.1657368406 -0.086785707
## 2025-07-31  1.162981e-02  0.026405421  0.0700443986  0.0501031776 -0.030005653
## 2025-08-29  1.130062e-01  0.017334807 -0.0499171172  0.0352529047  0.079775694
## 2025-09-30  9.247092e-02  0.045432092  0.0219793711 -0.0987850620  0.286693192
## 2025-10-31  5.997984e-02 -0.008908117 -0.0002703949 -0.0765711460  0.026275247
## 2025-11-28  3.185157e-02  0.006280032 -0.0492394175  0.0006191134 -0.059539610
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             AAPL         JPM        MSFT         NKE        TSLA
## AAPL 0.005901267 0.001301092 0.002171267 0.001831540 0.005255095
## JPM  0.001301092 0.004471347 0.001514195 0.001889714 0.002247494
## MSFT 0.002171267 0.001514195 0.003547420 0.001736476 0.003256758
## NKE  0.001831540 0.001889714 0.001736476 0.005775061 0.001620029
## TSLA 0.005255095 0.002247494 0.003256758 0.001620029 0.028868199
# 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.2, 0.2, 0.1)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.05405827
# 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
##            AAPL        JPM        MSFT        NKE       TSLA
## [1,] 0.01445968 0.01086162 0.008523471 0.00959936 0.01061415
rowSums(component_contribution)
## [1] 0.05405827
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AAPL   JPM  MSFT   NKE  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.267 0.201 0.158 0.178 0.196
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.267
## 2 JPM          0.201
## 3 MSFT         0.158
## 4 NKE          0.178
## 5 TSLA         0.196

4 Component Contribution with a Custom Function

sset_returns_wide_tbl <- asset_returns_tbl %>%

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

    column_to_rownames(var = "date")

asset_returns_wide_tbl
##                     AAPL          JPM          MSFT           NKE         TSLA
## 2013-01-31 -1.555896e-01  0.074549669  0.0273281679  0.0463878076  0.102078114
## 2013-02-28 -2.561110e-02  0.038975453  0.0209152190  0.0114291503 -0.074128640
## 2013-03-28  2.850544e-03 -0.030299432  0.0287202422  0.0802400683  0.084208138
## 2013-04-30  2.711615e-04  0.038370224  0.1457769843  0.0749068325  0.354111531
## 2013-05-31  2.217173e-02  0.107826499  0.0599410845 -0.0276354789  0.593716684
## 2013-06-28 -1.258962e-01 -0.033528885 -0.0103686374  0.0322349798  0.093672163
## 2013-07-31  1.321028e-01  0.061462568 -0.0813947587 -0.0120064580  0.223739522
## 2013-08-30  8.044284e-02 -0.097951423  0.0548546234  0.0017291142  0.229971642
## 2013-09-30 -2.172315e-02  0.022697226 -0.0035993926  0.1452419787  0.134706620
## 2013-10-31  9.201506e-02  0.004433629  0.0620378332  0.0420549954 -0.189806595
## 2013-11-29  6.770807e-02  0.104545664  0.0815624709  0.0436478383 -0.228409405
## 2013-12-31  8.861779e-03  0.021781333 -0.0190635066 -0.0032096479  0.167108541
## 2014-01-31 -1.139484e-01 -0.048308186  0.0114284158 -0.0764781573  0.187261714
## 2014-02-28  5.591792e-02  0.026031053  0.0198150934  0.0752105461  0.299722785
## 2014-03-31  1.975618e-02  0.066219733  0.0676173526 -0.0583763371 -0.160783242
## 2014-04-30  9.476115e-02 -0.074830576 -0.0144983919 -0.0123967259 -0.002690122
## 2014-05-30  7.576481e-02 -0.007351308  0.0203073199  0.0560128851 -0.000577422
## 2014-06-30  2.728657e-02  0.036226399  0.0183939603  0.0082872627  0.144457224
## 2014-07-31  2.832624e-02  0.007833555  0.0344127893 -0.0054309227 -0.072372676
## 2014-08-29  7.465170e-02  0.030398506  0.0574849640  0.0212580229  0.188794007
## 2014-09-30 -1.722053e-02  0.013200845  0.0202639852  0.1271460889 -0.105566477
## 2014-10-31  6.948900e-02  0.010691270  0.0126468768  0.0413957407 -0.004046477
## 2014-11-28  1.007311e-01 -0.005305143  0.0244388862  0.0657677131  0.011599801
## 2014-12-31 -7.460642e-02  0.039437908 -0.0288583659 -0.0292642183 -0.094774519
## 2015-01-30  5.961154e-02 -0.134036725 -0.1395468035 -0.0414072845 -0.088365289
## 2015-02-27  9.601591e-02  0.119456915  0.0890367695  0.0543413966 -0.001277808
## 2015-03-31 -3.187422e-02 -0.011488786 -0.0755302700  0.0325175550 -0.074350051
## 2015-04-30  5.769635e-03  0.049912966  0.1792017172 -0.0149627575  0.180226808
## 2015-05-29  4.434131e-02  0.039062509 -0.0308043535  0.0309563662  0.103899574
## 2015-06-30 -3.793790e-02  0.029656532 -0.0595710908  0.0605841811  0.067300935
## 2015-07-31 -3.348139e-02  0.017814066  0.0561508125  0.0645271023 -0.007896616
## 2015-08-31 -6.848851e-02 -0.066827077 -0.0639507777 -0.0305792061 -0.066366250
## 2015-09-30 -2.205791e-02 -0.050062238  0.0168608851  0.0982087930 -0.002653519
## 2015-10-30  8.011232e-02  0.059589022  0.1733947749  0.0634859979 -0.182659777
## 2015-11-30 -5.821134e-03  0.037123310  0.0386863400  0.0094943548  0.106828586
## 2015-12-31 -1.167900e-01 -0.009795582  0.0205777610 -0.0541858823  0.041471519
## 2016-01-29 -7.822393e-02 -0.097447106 -0.0070544578 -0.0078707765 -0.227360626
## 2016-02-29 -1.288349e-03 -0.055281675 -0.0723442836 -0.0067963298  0.003810669
## 2016-03-31  1.197460e-01  0.050564753  0.0820363196  0.0006247027  0.179948109
## 2016-04-29 -1.507312e-01  0.072421467 -0.1020864559 -0.0420291586  0.046721797
## 2016-05-31  6.931447e-02  0.032228035  0.0678426024 -0.0651946423 -0.075597968
## 2016-06-30 -4.359642e-02 -0.049142437 -0.0351387366  0.0025546376 -0.050296440
## 2016-07-29  8.623536e-02  0.036779007  0.1022680737  0.0054200452  0.100785334
## 2016-08-31  2.337613e-02  0.053713008  0.0198809863  0.0378335738 -0.102058091
## 2016-09-30  6.344845e-02 -0.013573309  0.0024333444 -0.0877707388 -0.038366372
## 2016-10-31  4.324742e-03  0.046556644  0.0394879345 -0.0480495747 -0.031364583
## 2016-11-30 -2.183717e-02  0.146281429  0.0123909402 -0.0021945347 -0.043041267
## 2016-12-30  4.684062e-02  0.073564444  0.0307215781  0.0186661315  0.120665178
## 2017-01-31  4.664158e-02 -0.013906877  0.0395980147  0.0399166536  0.164624916
## 2017-02-28  1.255553e-01  0.068386121 -0.0043733118  0.0774509121 -0.007730364
## 2017-03-31  4.754185e-02 -0.031157955  0.0289606838 -0.0222248484  0.107278727
## 2017-04-28 -6.974618e-05 -0.003879573  0.0387185908 -0.0057585537  0.120916212
## 2017-05-31  6.560702e-02 -0.057361574  0.0256724230 -0.0446566444  0.082295892
## 2017-06-30 -5.891592e-02  0.106699010 -0.0131151678  0.1108367409  0.058654468
## 2017-07-31  3.218079e-02  0.009852320  0.0532497371  0.0008469037 -0.111459860
## 2017-08-31  1.016529e-01 -0.009962552  0.0333893149 -0.1082531177  0.095543446
## 2017-09-29 -6.213489e-02  0.049580951 -0.0037519145 -0.0183456975 -0.042474144
## 2017-10-31  9.240380e-02  0.057849386  0.1103419800  0.0587964441 -0.028457409
## 2017-11-30  2.007521e-02  0.038126918  0.0168411100  0.0941688172 -0.070862541
## 2017-12-29 -1.536336e-02  0.022889250  0.0161455604  0.0379618441  0.008061928
## 2018-01-31 -1.069352e-02  0.083669116  0.1049981780  0.0867716061  0.129254571
## 2018-02-28  6.596094e-02 -0.001470657 -0.0084507813 -0.0175977176 -0.032266877
## 2018-03-29 -5.980357e-02 -0.049062932 -0.0270228068 -0.0058207229 -0.253920408
## 2018-04-30 -1.513394e-02 -0.005821929  0.0243532478  0.0289273502  0.099254576
## 2018-05-31  1.267421e-01 -0.016405205  0.0596519636  0.0486581023 -0.031698139
## 2018-06-29 -9.463225e-03 -0.026610394 -0.0023295767  0.1069231359  0.186043257
## 2018-07-31  2.759896e-02  0.103604835  0.0730207858 -0.0353827683 -0.140021491
## 2018-08-31  1.826736e-01 -0.003223966  0.0610882609  0.0689795020  0.011737389
## 2018-09-28 -8.337726e-03 -0.015302446  0.0179980881  0.0301964126 -0.130439038
## 2018-10-31 -3.095146e-02 -0.027461127 -0.0683874373 -0.1213305162  0.242170576
## 2018-11-30 -1.999128e-01  0.019709080  0.0417977833  0.0040296004  0.038271580
## 2018-12-31 -1.240887e-01 -0.130157755 -0.0877903744 -0.0131317871 -0.051761952
## 2019-01-31  5.368727e-02  0.066576942  0.0277686887  0.0992995753 -0.080628789
## 2019-02-28  4.380270e-02  0.008275074  0.0745113702  0.0459483146  0.041032981
## 2019-03-29  9.260248e-02 -0.030451537  0.0514094265 -0.0153199195 -0.133656413
## 2019-04-30  5.490125e-02  0.144249014  0.1019632337  0.0420893416 -0.159123803
## 2019-05-31 -1.326324e-01 -0.090959554 -0.0507468410 -0.1270014485 -0.253945372
## 2019-06-28  1.226769e-01  0.053649663  0.0798438310  0.0845993242  0.188012109
## 2019-07-31  7.361715e-02  0.043933704  0.0170963852  0.0244746131  0.078092373
## 2019-08-30 -1.659835e-02 -0.054383423  0.0149251823 -0.0153644133 -0.068516948
## 2019-09-30  7.042293e-02  0.068847232  0.0084507581  0.1056920642  0.065449565
## 2019-10-31  1.049764e-01  0.067598284  0.0307390208 -0.0476461863  0.268061253
## 2019-11-29  7.469368e-02  0.053308366  0.0577616592  0.0456631267  0.046592176
## 2019-12-31  9.420379e-02  0.056365535  0.0409013361  0.0803304128  0.237359743
## 2020-01-31  5.260223e-02 -0.045422719  0.0764560801 -0.0507165542  0.441578342
## 2020-02-28 -1.218309e-01 -0.130987429 -0.0467647537 -0.0718011265  0.026424253
## 2020-03-31 -7.231392e-02 -0.254395172 -0.0269000318 -0.0771937531 -0.242781458
## 2020-04-30  1.444239e-01  0.072040273  0.1278000957  0.0522720284  0.400209535
## 2020-05-29  8.166680e-02  0.016056248  0.0250743501  0.1253848850  0.065730500
## 2020-06-30  1.374865e-01 -0.033968653  0.1048638181 -0.0053911405  0.257108657
## 2020-07-31  1.528342e-01  0.036757080  0.0073435822 -0.0044971279  0.281420674
## 2020-08-31  1.960349e-01  0.036075786  0.0978088371  0.1387488104  0.554719320
## 2020-09-30 -1.081718e-01 -0.039911711 -0.0697754676  0.1151080585 -0.149762306
## 2020-10-30 -6.188802e-02  0.027455326 -0.0380861001 -0.0444661433 -0.100371771
## 2020-11-30  9.120469e-02  0.184291572  0.0583261568  0.1148920792  0.380308519
## 2020-12-31  1.084717e-01  0.075071083  0.0382645108  0.0510475423  0.217730753
## 2021-01-29 -5.516474e-03  0.019688641  0.0419973023 -0.0573123949  0.117343701
## 2021-02-26 -8.306863e-02  0.134337417  0.0041095520  0.0108995928 -0.161038203
## 2021-03-31  7.312613e-03  0.033803956  0.0144828515 -0.0141220542 -0.011269836
## 2021-04-30  7.345297e-02  0.016198194  0.0672862537 -0.0020339731  0.060292565
## 2021-05-28 -5.181668e-02  0.065610759 -0.0076565538  0.0305596017 -0.126372343
## 2021-06-30  9.450002e-02 -0.054426023  0.0815697881  0.1240976239  0.083547955
## 2021-07-30  6.295843e-02 -0.018724890  0.0504236656  0.0809136527  0.010973846
## 2021-08-31  4.161136e-02  0.052429272  0.0597685016 -0.0150256315  0.068224268
## 2021-09-30 -7.046167e-02  0.023113122 -0.0684060031 -0.1260499364  0.052632612
## 2021-10-29  5.700134e-02  0.043184555  0.1623661354  0.1414101694  0.362230202
## 2021-11-30  9.991934e-02 -0.067316514 -0.0012828541  0.0115891288  0.027237848
## 2021-12-31  7.160300e-02 -0.003026684  0.0171843399 -0.0135063034 -0.079968440
## 2022-01-31 -1.583707e-02 -0.057573476 -0.0783345858 -0.1183306406 -0.120597475
## 2022-02-28 -5.558245e-02 -0.046840509 -0.0379221423 -0.0809942857 -0.073397011
## 2022-03-31  5.588243e-02 -0.039412246  0.0313648959 -0.0124017431  0.213504290
## 2022-04-29 -1.021775e-01 -0.125476025 -0.1052128951 -0.0760991067 -0.213125289
## 2022-05-31 -5.603715e-02  0.102398409 -0.0182426077 -0.0480487606 -0.138340062
## 2022-06-30 -8.493698e-02 -0.160612226 -0.0569095631 -0.1484559805 -0.118657126
## 2022-07-29  1.728046e-01  0.032933900  0.0890143117  0.1173046645  0.280480149
## 2022-08-31 -3.170527e-02 -0.014230406 -0.0689889486 -0.0765610259 -0.075250271
## 2022-09-30 -1.289443e-01 -0.084640318 -0.1157102876 -0.2445217410 -0.038313992
## 2022-10-31  1.039557e-01  0.195049348 -0.0033114907  0.1088674839 -0.153346760
## 2022-11-30 -3.358515e-02  0.093227951  0.0973290178  0.1685054791 -0.155866121
## 2022-12-30 -1.304192e-01 -0.029971260 -0.0619237600  0.0676657853 -0.457813194
## 2023-01-31  1.048295e-01  0.050130385  0.0327737145  0.0845228135  0.340915767
## 2023-02-28  2.291828e-02  0.023932526  0.0089773456 -0.0694248240  0.171904973
## 2023-03-31  1.121214e-01 -0.095372943  0.1448634836  0.0347432649  0.008471140
## 2023-04-28  2.857494e-02  0.066892452  0.0636927152  0.0327266503 -0.233183710
## 2023-05-31  4.502923e-02 -0.018470840  0.0686912449 -0.1855463998  0.216021889
## 2023-06-30  9.014240e-02  0.069243124  0.0363307006  0.0506910085  0.249689452
## 2023-07-31  1.270443e-02  0.089422534 -0.0136595101  0.0001812493  0.021391609
## 2023-08-31 -4.330830e-02 -0.076477651 -0.0224764898 -0.0818939056 -0.035588260
## 2023-09-29 -9.285928e-02 -0.008992658 -0.0373308940 -0.0583951365 -0.030929027
## 2023-10-31 -2.573389e-03 -0.034614450  0.0684206791  0.0721114630 -0.219831983
## 2023-11-30  1.077599e-01  0.115463124  0.1159548882  0.0704384884  0.178463656
## 2023-12-29  1.349119e-02  0.086017829 -0.0076032241 -0.0121757897  0.034390133
## 2024-01-31 -4.314468e-02  0.030883170  0.0557006051 -0.0670408664 -0.282704160
## 2024-02-29 -1.871736e-02  0.064946998  0.0414474954  0.0233633911  0.075015304
## 2024-03-28 -5.264814e-02  0.073747084  0.0169714305 -0.0970692681 -0.138383423
## 2024-04-30 -6.728948e-03 -0.037859866 -0.0775402278 -0.0184713257  0.041724969
## 2024-05-31  1.224143e-01  0.055241399  0.0659666434  0.0297925148 -0.028782134
## 2024-06-28  9.125898e-02 -0.001827738  0.0738549800 -0.2280936492  0.105427913
## 2024-07-31  5.298219e-02  0.056324675 -0.0661283765 -0.0067893818  0.159378271
## 2024-08-30  3.184116e-02  0.054858322 -0.0010956475  0.1070686172 -0.080549177
## 2024-09-30  1.731639e-02 -0.064016755  0.0310605998  0.0636341593  0.200441406
## 2024-10-31 -3.090177e-02  0.057231914 -0.0572677513 -0.1363797092 -0.046070548
## 2024-11-29  5.041448e-02  0.118023364  0.0432438967  0.0210397191  0.323147326
## 2024-12-31  5.368788e-02 -0.040910361 -0.0046392831 -0.0350592955  0.157010663
## 2025-01-31 -5.930769e-02  0.114093001 -0.0153967911  0.0161241073  0.001880189
## 2025-02-28  2.554346e-02 -0.009963511 -0.0425088050  0.0323703069 -0.322794555
## 2025-03-31 -8.501352e-02 -0.075926073 -0.0559455554 -0.2191026418 -0.122658739
## 2025-04-30 -4.432081e-02  0.003364587  0.0515782678 -0.1182557516  0.085028653
## 2025-05-30 -5.507312e-02  0.076243137  0.1542991057  0.0716607065  0.205293048
## 2025-06-30  2.128049e-02  0.093621408  0.0774059356  0.1657368406 -0.086785707
## 2025-07-31  1.162981e-02  0.026405421  0.0700443986  0.0501031776 -0.030005653
## 2025-08-29  1.130062e-01  0.017334807 -0.0499171172  0.0352529047  0.079775694
## 2025-09-30  9.247092e-02  0.045432092  0.0219793711 -0.0987850620  0.286693192
## 2025-10-31  5.997984e-02 -0.008908117 -0.0002703949 -0.0765711460  0.026275247
## 2025-11-28  3.185157e-02  0.006280032 -0.0492394175  0.0006191134 -0.059539610
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, .2, .2, .1))
## # A tibble: 1 × 5
##    AAPL   JPM  MSFT   NKE  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.267 0.201 0.158 0.178 0.196

5 Visualizing Component Contribution

Column chart of componenet contribution

plot_data <- asset_returns_wide_tbl %>% 
    
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution")

plot_data %>%
    
    ggplot(aes(x = Asset, y = Contribution)) +
    geom_col(fill = "cornflowerblue") +
    
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    
    labs(title = "Percent Contribution to Portfolio Volatility")

6 Plot: Colum Chart of Component Contribution and Weight

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

AAPL is the largest contributor to the portfolio volatility at around 27%. The second most is JPM at around 20%. These are also the largest weighted assets in the portfolio. The risk is not necessarily concentrated in any one asset. Although AAPL has the highest contribution to the volatility, it is not significant enough compared to the other assets to say the risk is concentrated in AAPL.