# 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.

symbol <- c("JNJ", "TSLA", "XOM", "COST", "PLUG")

prices <- tq_get(x = symbol,
                 get = "stock.prices",
                 from = "2012-12-31",
                 to = "2024-11-20")

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"))
##     asset      date   returns 
##   "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 = symbol, values_from = monthly.returns) %>%

    column_to_rownames(var = "date")

asset_returns_wide_tbl
##                    COST          JNJ         PLUG         TSLA           XOM
## 2013-01-31  0.035911945  0.053060375 -0.105360542  0.102078114  0.0387543810
## 2013-02-28 -0.007647638  0.037163374 -0.973449109 -0.074128640  0.0016888002
## 2013-03-28  0.046488544  0.068791668  0.257829093  0.084208138  0.0062342603
## 2013-04-30  0.021628332  0.044382786 -0.318453748  0.354111531 -0.0125070739
## 2013-05-31  0.013793111 -0.004907716  0.864997447  0.593716684  0.0233882767
## 2013-06-28  0.008537951  0.019760926  0.000000000  0.093672163 -0.0013273565
## 2013-07-31  0.060108422  0.085242882  0.146603481  0.223739522  0.0369405138
## 2013-08-30 -0.045822553 -0.071351674  0.186102231  0.229971642 -0.0659982908
## 2013-09-30  0.029072171  0.003235024  0.333773247  0.134706620 -0.0129331028
## 2013-10-31  0.024275301  0.066058516 -0.243622124 -0.189806595  0.0407664885
## 2013-11-29  0.063598031  0.028851325  0.243622124 -0.228409405  0.0489499222
## 2013-12-31 -0.052456420 -0.032969376  0.739359980  0.167108541  0.0793508382
## 2014-01-31 -0.057583514 -0.034658801  0.667001925  0.187261714 -0.0935721860
## 2014-02-28  0.041454711  0.047598785  0.435902263  0.299722785  0.0506882216
## 2014-03-31 -0.044825579  0.064219652  0.418935683 -0.160783242  0.0145401954
## 2014-04-30  0.035190543  0.030675921 -0.423227531 -0.002690122  0.0472877339
## 2014-05-30  0.005991936  0.008622210 -0.066691417 -0.000577422 -0.0117624448
## 2014-06-30 -0.007440224  0.030670267  0.073122250  0.144457224  0.0014909652
## 2014-07-31  0.023482928 -0.044265229  0.146797756 -0.072372676 -0.0174333560
## 2014-08-29  0.029673008  0.042462723  0.029092933  0.188794007  0.0121839607
## 2014-09-30  0.034419187  0.027198663 -0.195308705 -0.105566477 -0.0559283600
## 2014-10-31  0.062256493  0.011102114  0.025807859 -0.004046477  0.0278899130
## 2014-11-28  0.066138010  0.010843296 -0.209437511  0.011599801 -0.0587311330
## 2014-12-31 -0.002606783 -0.034586860 -0.241638116 -0.094774519  0.0208763385
## 2015-01-30  0.008709936 -0.043287512 -0.116533788 -0.088365289 -0.0559440150
## 2015-02-27  0.062376904  0.030366817  0.142851071 -0.001277808  0.0202290224
## 2015-03-31  0.030425468 -0.018808108 -0.173271730 -0.074350051 -0.0408029952
## 2015-04-30 -0.054659548 -0.014014468 -0.019493777  0.180226808  0.0275004442
## 2015-05-29 -0.003220835  0.016696934  0.068467825  0.103899574 -0.0169135700
## 2015-06-30 -0.054254101 -0.027127029 -0.104543847  0.067300935 -0.0237542039
## 2015-07-31  0.073081374  0.027826034  0.055569799 -0.007896616 -0.0491444208
## 2015-08-31 -0.034055494 -0.056565176 -0.415164450 -0.066366250 -0.0421067432
## 2015-09-30  0.031764306 -0.006726420  0.067822597 -0.002653519 -0.0118993599
## 2015-10-30  0.089590378  0.079061674  0.275310793 -0.182659777  0.1069197068
## 2015-11-30  0.023236655  0.009421067 -0.091169401  0.106828586 -0.0044590586
## 2015-12-31  0.000495634  0.014513149 -0.041769484  0.041471519 -0.0464969892
## 2016-01-29 -0.066431125  0.016605781 -0.120749464 -0.227360626 -0.0012834676
## 2016-02-29 -0.004531572  0.014566706  0.106429424  0.003810669  0.0381512799
## 2016-03-31  0.049097941  0.028023049 -0.014528087  0.179948109  0.0420240108
## 2016-04-29 -0.058889334  0.035231539  0.004866185  0.046721797  0.0559477838
## 2016-05-31  0.004310947  0.012542223 -0.070380791 -0.075597968  0.0153586432
## 2016-06-30  0.054099094  0.073625891 -0.031748668 -0.050296440  0.0516637645
## 2016-07-29  0.062809806  0.031885289 -0.038360897  0.100785334 -0.0524503179
## 2016-08-31 -0.028474213 -0.041526631 -0.143960698 -0.102058091 -0.0120674199
## 2016-09-30 -0.060921480 -0.010190893  0.098238493 -0.038366372  0.0016055062
## 2016-10-31 -0.030896982 -0.018281574 -0.111225676 -0.031364583 -0.0464326934
## 2016-11-30  0.018108153 -0.034388299 -0.110456973 -0.043041267  0.0554778179
## 2016-12-30  0.064492836  0.034527321 -0.132489147  0.120665178  0.0333437737
## 2017-01-31  0.023700127 -0.017158823 -0.124052742  0.164624916 -0.0731881368
## 2017-02-28  0.080294594  0.082738354  0.018692227 -0.007730364 -0.0220116886
## 2017-03-31 -0.055049043  0.018966316  0.245122415  0.107278727  0.0084490079
## 2017-04-28  0.056966470 -0.008709072  0.484392374  0.120916212 -0.0043994828
## 2017-05-31  0.058779609  0.044592972 -0.185899375  0.082295892 -0.0047857724
## 2017-06-30 -0.120607271  0.031014799  0.092373294  0.058654468  0.0028535435
## 2017-07-31 -0.008917960  0.003245474  0.102415020 -0.111459860 -0.0085839596
## 2017-08-31 -0.008037436  0.003700190 -0.054558931  0.095543446 -0.0378142406
## 2017-09-29  0.047044880 -0.017989625  0.198544303 -0.042474144  0.0714090331
## 2017-10-31 -0.019731613  0.069807834  0.087968780 -0.028457409  0.0165733576
## 2017-11-30  0.138331510  0.005531422 -0.184429054 -0.070862541  0.0084926205
## 2017-12-29  0.009121520  0.002795224 -0.004228332  0.008061928  0.0041932493
## 2018-01-31  0.045941233 -0.011010908 -0.201141599  0.129254571  0.0428282851
## 2018-02-28 -0.017910822 -0.055635678 -0.036943480 -0.032266877 -0.1318700926
## 2018-03-29 -0.013022924 -0.013409802  0.016000326 -0.253920408 -0.0150319318
## 2018-04-30  0.045288973 -0.013037874 -0.032260831  0.099254576  0.0412241103
## 2018-05-31  0.008374727 -0.048454043  0.021622443 -0.031698139  0.0539941265
## 2018-06-29  0.052759860  0.014276412  0.077159069  0.186043257  0.0181745262
## 2018-07-31  0.045508118  0.088137051 -0.009950321 -0.140021491 -0.0148566356
## 2018-08-31  0.066328873  0.022884467 -0.015113623  0.011737389 -0.0063144542
## 2018-09-28  0.007478649  0.025508850 -0.025708394 -0.130439038  0.0587370041
## 2018-10-31 -0.026969518  0.013086218 -0.037139512  0.242170576 -0.0648680278
## 2018-11-30  0.013898104  0.054528986 -0.055569864  0.038271580  0.0078246164
## 2018-12-31 -0.126931673 -0.129552148 -0.344504401 -0.051761952 -0.1534590429
## 2019-01-31  0.052218144  0.030750664  0.099699356 -0.080628789  0.0719896506
## 2019-02-28  0.021665759  0.033009703  0.267404855  0.041032981  0.0865808465
## 2019-03-29  0.101632012  0.022791324  0.293253179 -0.133656413  0.0221494164
## 2019-04-30  0.013903040  0.010036162  0.036813937 -0.159123803 -0.0064561867
## 2019-05-31 -0.021834826 -0.067016839  0.027724522 -0.253945372 -0.1146884144
## 2019-06-28  0.098046114  0.060144721 -0.129077020  0.188012109  0.0795537178
## 2019-07-31  0.042125958 -0.067260935 -0.017937683  0.078092373 -0.0300704007
## 2019-08-30  0.069311756 -0.006921029 -0.018265330 -0.068516948 -0.0700193054
## 2019-09-30 -0.022819555  0.007914974  0.192256687  0.065449565  0.0306299919
## 2019-10-31  0.032930626  0.020351102  0.007575786  0.268061253 -0.0440077037
## 2019-11-29  0.009046621  0.047350747  0.386416902  0.046592176  0.0202411299
## 2019-12-31 -0.019841196  0.059164431 -0.210404523  0.237359743  0.0239300372
## 2020-01-31  0.038707452  0.020357529  0.202682423  0.441578342 -0.1162795950
## 2020-02-28 -0.081056209 -0.095301314  0.114619906  0.026424253 -0.1743976440
## 2020-03-31  0.014092527 -0.025226455 -0.203747667 -0.242781458 -0.3036196012
## 2020-04-30  0.063069435  0.134712865  0.166184489  0.400209535  0.2020105090
## 2020-05-29  0.017891754 -0.001726668  0.007151451  0.065730500 -0.0025503816
## 2020-06-30 -0.017198698 -0.056134685  0.667890271  0.257108657 -0.0166319552
## 2020-07-31  0.073177499  0.035829044 -0.062834736  0.281420674 -0.0608480629
## 2020-08-31  0.065770185  0.057786468  0.520891483  0.554719320 -0.0326587201
## 2020-09-30  0.020892692 -0.029973701  0.032591010 -0.149762306 -0.1513585487
## 2020-10-30  0.009273319 -0.082356971  0.043056644 -0.100371771 -0.0510942073
## 2020-11-30  0.091203888  0.060665570  0.633927798  0.380308519  0.1799161766
## 2020-12-31 -0.013156903  0.084138870  0.250724824  0.217730753  0.0779220303
## 2021-01-29 -0.066809312  0.035884212  0.622119524  0.117343701  0.0841770123
## 2021-02-26 -0.060761212 -0.022829160 -0.266742949 -0.161038203  0.2095516686
## 2021-03-31  0.062875809  0.036496449 -0.300021935 -0.011269836  0.0264988259
## 2021-04-30  0.056281313 -0.009906075 -0.228809681  0.060292565  0.0249415482
## 2021-05-28  0.016472559  0.045504138  0.074007768 -0.126372343  0.0340119026
## 2021-06-30  0.044972220 -0.027008371  0.107670484  0.083547955  0.0776017392
## 2021-07-30  0.084426259  0.044287858 -0.225779305  0.010973846 -0.0914021796
## 2021-08-31  0.058239810  0.011311017 -0.045752307  0.068224268 -0.0394118509
## 2021-09-30 -0.013571575 -0.069537533 -0.020155665  0.052632612  0.0759144218
## 2021-10-29  0.091357728  0.008508624  0.404420425  0.362230202  0.0917181731
## 2021-11-30  0.092877053 -0.037077899  0.040456056  0.027237848 -0.0611759092
## 2021-12-31  0.051173018  0.092665727 -0.344737141 -0.079968440  0.0223093348
## 2022-01-31 -0.116777672  0.007106151 -0.255269353 -0.120597475  0.2162230546
## 2022-02-28  0.029084111 -0.039444391  0.145293224 -0.073397011  0.0428972706
## 2022-03-31  0.103461692  0.074112259  0.123347232  0.213504290  0.0518094492
## 2022-04-29 -0.078104723  0.018060855 -0.308281941 -0.213125289  0.0316995694
## 2022-05-31 -0.131459191  0.001238598 -0.128785346 -0.138340062  0.1289512411
## 2022-06-30  0.027627456 -0.011315499 -0.109095228 -0.118657126 -0.1141958096
## 2022-07-29  0.123413124 -0.016987643  0.252989440  0.280480149  0.1238366852
## 2022-08-31 -0.036114416 -0.071828849  0.273048842 -0.075250271 -0.0042515463
## 2022-09-30 -0.100308418  0.012442495 -0.288633569 -0.038313992 -0.0906031875
## 2022-10-31  0.061856501  0.062926559 -0.273660614 -0.153346760  0.2383519633
## 2022-11-30  0.072575688  0.029334560 -0.001252317 -0.155866121  0.0127896120
## 2022-12-30 -0.166590582 -0.007613294 -0.254811417 -0.457813194 -0.0093845534
## 2023-01-31  0.113054825 -0.077846715  0.319114973  0.340915767  0.0504724961
## 2023-02-28 -0.052447579 -0.057021030 -0.135043397  0.171904973 -0.0463491311
## 2023-03-31  0.025871761  0.011289243 -0.238048946  0.008471140 -0.0022771981
## 2023-04-28  0.012698949  0.054610335 -0.260744469 -0.233183710  0.0761769980
## 2023-05-31  0.018521010 -0.046706649 -0.081890120  0.216021889 -0.1381857498
## 2023-06-30  0.051099779  0.065279818  0.222181620  0.249689452  0.0484265242
## 2023-07-31  0.040567717  0.012070541  0.233293937  0.021391609 -0.0000932604
## 2023-08-31 -0.018636354 -0.028309147 -0.438788597 -0.035588260  0.0443351667
## 2023-09-29  0.028146710 -0.037366981 -0.107200943 -0.030929027  0.0558785461
## 2023-10-31 -0.022410226 -0.048745265 -0.254892260 -0.219831983 -0.1050959627
## 2023-11-30  0.072244637  0.049698505 -0.377011292  0.178463656 -0.0207188490
## 2023-12-29  0.130090809  0.013359230  0.107832714  0.034390133 -0.0272311522
## 2024-01-31  0.051378159  0.013686624 -0.011173343 -0.282704160  0.0279123754
## 2024-02-29  0.069622951  0.023050881 -0.231606190  0.075015304  0.0257465663
## 2024-03-28 -0.015252350 -0.019965061 -0.025826375 -0.138383423  0.1062785612
## 2024-04-30 -0.011766129 -0.089894744 -0.398223988  0.041724969  0.0173130899
## 2024-05-31  0.113627500  0.022333968  0.365724781 -0.028782134 -0.0004867061
## 2024-06-28  0.048326090 -0.003483192 -0.357104046  0.105427913 -0.0184186892
## 2024-07-31 -0.032061480  0.076943326  0.058349927  0.159378271  0.0296970934
## 2024-08-30  0.082151670  0.057060246 -0.272946388 -0.080549177  0.0025225098
## 2024-09-30 -0.006588376 -0.023177441  0.184093035  0.200441406 -0.0061235293
## 2024-10-31 -0.014017432 -0.013668186 -0.142420316 -0.046070548 -0.0037607114
## 2024-11-19  0.063387472 -0.043860515 -0.020619329  0.325578013  0.0239011149
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.2, 0.2, 0.2, 0.2, 0.2))
## # A tibble: 1 × 5
##    COST   JNJ  PLUG  TSLA   XOM
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1  0.05 0.033 0.543 0.312 0.062

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(0.2, 0.2, 0.2, 0.2, 0.2)) %>%
    
    # Transform to long form 
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weights
    add_column(weight = c(0.2, 0.2, 0.2, 0.2, 0.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? PLUG is easily the largest contributor to portfolio volatility with TSLA following closely behind. PLUG contributes roughly 47% of the portfolio while TSLA holds around 26% making for a total of 73% of the overall portfolio. The portfolio is concentrated in two assets which make it a little safer than if PLUG was the main contributor, it would be smart to diversify to assetts more similar to TSLA.