# 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

symbols <- c("NFLX", "TSLA", "NVDA", "AAPL", "MSFT")

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

2 Convert prices to returns

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 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
##                     AAPL         MSFT          NFLX         NVDA         TSLA
## 2013-01-31 -1.555889e-01  0.027328103  0.5792178352  0.000000000  0.102078114
## 2013-02-28 -2.561100e-02  0.020915096  0.1294686037  0.038221993 -0.074128640
## 2013-03-28  2.850443e-03  0.028719872  0.0063600533  0.013338445  0.084208138
## 2013-04-30  2.708277e-04  0.145777422  0.1323749474  0.070706265  0.354111531
## 2013-05-31  2.217186e-02  0.059941238  0.0460382575  0.054652021  0.593716684
## 2013-06-28 -1.258955e-01 -0.010368881 -0.0693561240 -0.030167103  0.093672163
## 2013-07-31  1.321020e-01 -0.081394517  0.1468489367  0.028091880  0.223739522
## 2013-08-30  8.044276e-02  0.054854108  0.1495238797  0.026270110  0.229971642
## 2013-09-30 -2.172318e-02 -0.003598963  0.0853634080  0.053460238  0.134706620
## 2013-10-31  9.201475e-02  0.062037624  0.0420203453 -0.024066195 -0.189806595
## 2013-11-29  6.770892e-02  0.081562413  0.1260456715  0.032034932 -0.228409405
## 2013-12-31  8.861606e-03 -0.019063171  0.0064580000  0.026567178  0.167108541
## 2014-01-31 -1.139493e-01  0.011428384  0.1059768648 -0.020177251  0.187261714
## 2014-02-28  5.591847e-02  0.019815199  0.0849674837  0.162107468  0.299722785
## 2014-03-31  1.975645e-02  0.067616929 -0.2357727088 -0.025903902 -0.160783242
## 2014-04-30  9.476105e-02 -0.014498365 -0.0890406585  0.030788316 -0.002690122
## 2014-05-30  7.576558e-02  0.020307980  0.2603989261  0.032886654 -0.000577422
## 2014-06-30  2.728599e-02  0.018393364  0.0530628157 -0.024508585  0.144457224
## 2014-07-31  2.832628e-02  0.034413043 -0.0414274490 -0.057729640 -0.072372676
## 2014-08-29  7.465146e-02  0.057484995  0.1221473944  0.110060011  0.188794007
## 2014-09-30 -1.722037e-02  0.020264127 -0.0569909733 -0.052782719 -0.105566477
## 2014-10-31  6.948883e-02  0.012646484 -0.1386422062  0.057399393 -0.004046477
## 2014-11-28  1.007308e-01  0.024438619 -0.1250816593  0.074851994  0.011599801
## 2014-12-31 -7.460607e-02 -0.028857992 -0.0144727517 -0.044863260 -0.094774519
## 2015-01-30  5.961146e-02 -0.139546871  0.2571875640 -0.043319326 -0.088365289
## 2015-02-27  9.601601e-02  0.089036383  0.0722680237  0.142699141 -0.001277808
## 2015-03-31 -3.187433e-02 -0.075529985 -0.1307829000 -0.052582433 -0.074350051
## 2015-04-30  5.769888e-03  0.179201739  0.2893246603  0.058908629  0.180226808
## 2015-05-29  4.434154e-02 -0.030804115  0.1145793884  0.001459992  0.103899574
## 2015-06-30 -3.793836e-02 -0.059571456  0.0513461588 -0.095717166  0.067300935
## 2015-07-31 -3.348105e-02  0.056151282  0.1972314899 -0.007987854 -0.007896616
## 2015-08-31 -6.848914e-02 -0.063950709  0.0062789518  0.123595503 -0.066366250
## 2015-09-30 -2.205755e-02  0.016859871 -0.1079428863  0.092150865 -0.002653519
## 2015-10-30  8.011265e-02  0.173395438  0.0483934975  0.140555297 -0.182659777
## 2015-11-30 -5.821522e-03  0.038686135  0.1292201068  0.115405351  0.106828586
## 2015-12-31 -1.167901e-01  0.020578009 -0.0753374900  0.038347352  0.041471519
## 2016-01-29 -7.822355e-02 -0.007054208 -0.2194782802 -0.118048787 -0.227360626
## 2016-02-29 -1.288474e-03 -0.072344196  0.0169504292  0.071923777  0.003810669
## 2016-03-31  1.197460e-01  0.082036493  0.0902267716  0.127654787  0.179948109
## 2016-04-29 -1.507311e-01 -0.102086827 -0.1270821580 -0.002810490  0.046721797
## 2016-05-31  6.931413e-02  0.067842479  0.1304025124  0.276388275 -0.075597968
## 2016-06-30 -4.359629e-02 -0.035138989 -0.1144251460  0.006188298 -0.050296440
## 2016-07-29  8.623530e-02  0.102268341 -0.0025173508  0.194443436  0.100785334
## 2016-08-31  2.337623e-02  0.019880684  0.0657364217  0.073469255 -0.102058091
## 2016-09-30  6.344851e-02  0.002433835  0.0112245729  0.110693560 -0.038366372
## 2016-10-31  4.324852e-03  0.039487592  0.2367092462  0.037805080 -0.031364583
## 2016-11-30 -2.183759e-02  0.012391053 -0.0650993149  0.260525447 -0.043041267
## 2016-12-30  4.684102e-02  0.030721204  0.0564934510  0.146435921  0.120665178
## 2017-01-31  4.664137e-02  0.039598546  0.1280336724  0.022601696  0.164624916
## 2017-02-28  1.255553e-01 -0.004373438  0.0100411108 -0.071874790 -0.007730364
## 2017-03-31  4.754147e-02  0.028960917  0.0391855218  0.070843795  0.107278727
## 2017-04-28 -6.952873e-05  0.038718451  0.0292677885 -0.043434370  0.120916212
## 2017-05-31  6.560726e-02  0.025672732  0.0689840448  0.326022238  0.082295892
## 2017-06-30 -5.891580e-02 -0.013115343 -0.0874853040  0.001453666  0.058654468
## 2017-07-31  3.218039e-02  0.053249601  0.1954426244  0.117045066 -0.111459860
## 2017-08-31  1.016533e-01  0.033389298 -0.0390093333  0.042639297  0.095543446
## 2017-09-29 -6.213480e-02 -0.003751923  0.0373013834  0.053601106 -0.042474144
## 2017-10-31  9.240336e-02  0.110341786  0.0798772375  0.145700571 -0.028457409
## 2017-11-30  2.007569e-02  0.016841003 -0.0461007257 -0.029245203 -0.070862541
## 2017-12-29 -1.536349e-02  0.016145816  0.0230815823 -0.036583299  0.008061928
## 2018-01-31 -1.069358e-02  0.104998187  0.3422455062  0.239240940  0.129254571
## 2018-02-28  6.596123e-02 -0.008450805  0.0750957953 -0.014959379 -0.032266877
## 2018-03-29 -5.980402e-02 -0.027023077  0.0135328146 -0.043968951 -0.253920408
## 2018-04-30 -1.513377e-02  0.024353350  0.0563153817 -0.029312796  0.099254576
## 2018-05-31  1.267416e-01  0.059652227  0.1180176912  0.115144960 -0.031698139
## 2018-06-29 -9.462362e-03 -0.002329742  0.1073125782 -0.062544504  0.186043257
## 2018-07-31  2.759842e-02  0.073020576 -0.1483894202  0.033048295 -0.140021491
## 2018-08-31  1.826734e-01  0.061088395  0.0857957175  0.137075577  0.011737389
## 2018-09-28 -8.337546e-03  0.017997923  0.0173902455  0.001210296 -0.130439038
## 2018-10-31 -3.095177e-02 -0.068387178 -0.2149050241 -0.287373514  0.242170576
## 2018-11-30 -1.999121e-01  0.041797795 -0.0532519750 -0.253667279  0.038271580
## 2018-12-31 -1.240888e-01 -0.087790324 -0.0667287371 -0.202283381 -0.051761952
## 2019-01-31  5.368679e-02  0.027768697  0.2377564232  0.073974220 -0.080628789
## 2019-02-28  4.380283e-02  0.074511425  0.0533383459  0.071593957  0.041032981
## 2019-03-29  9.260275e-02  0.051409161 -0.0043098364  0.151869962 -0.133656413
## 2019-04-30  5.490093e-02  0.101963118  0.0384589452  0.007987561 -0.159123803
## 2019-05-31 -1.326320e-01 -0.050746478 -0.0764150093 -0.288679658 -0.253945372
## 2019-06-28  1.226768e-01  0.079843639  0.0676869455  0.192591179  0.188012109
## 2019-07-31  7.361705e-02  0.017096998 -0.1286120040  0.026972783  0.078092373
## 2019-08-30 -1.659834e-02  0.014924773 -0.0948922909 -0.006207996 -0.068516948
## 2019-09-30  7.042281e-02  0.008450921 -0.0931610341  0.038414441  0.065449565
## 2019-10-31  1.049764e-01  0.030739282  0.0713417093  0.143947174  0.268061253
## 2019-11-29  7.469361e-02  0.057761607  0.0905829375  0.076031093  0.046592176
## 2019-12-31  9.420386e-02  0.040900975  0.0279227475  0.082163278  0.237359743
## 2020-01-31  5.260197e-02  0.076455888  0.0643897941  0.004790447  0.441578342
## 2020-02-28 -1.218304e-01 -0.046764874  0.0670727184  0.133627251  0.026424253
## 2020-03-31 -7.231393e-02 -0.026899877  0.0173805126 -0.024248602 -0.242781458
## 2020-04-30  1.444239e-01  0.127800209  0.1116390683  0.103279415  0.400209535
## 2020-05-29  8.166673e-02  0.025074073 -0.0002858819  0.194462294  0.065730500
## 2020-06-30  1.374863e-01  0.104864249  0.0807737255  0.068216109  0.257108657
## 2020-07-31  1.528341e-01  0.007343540  0.0717317079  0.111190033  0.281420674
## 2020-08-31  1.960349e-01  0.097808770  0.0799294267  0.231105127  0.554719320
## 2020-09-30 -1.081713e-01 -0.069775353 -0.0573784336  0.011895623 -0.149762306
## 2020-10-30 -6.188832e-02 -0.038086279 -0.0497965496 -0.076501583 -0.100371771
## 2020-11-30  9.120468e-02  0.058326044  0.0309614228  0.066921923  0.380308519
## 2020-12-31  1.084722e-01  0.038264387  0.0970871760 -0.025900207  0.217730753
## 2021-01-29 -5.516909e-03  0.041997611 -0.0155438392 -0.005010659  0.117343701
## 2021-02-26 -8.306866e-02  0.004109673  0.0120609342  0.054293288 -0.161038203
## 2021-03-31  7.312987e-03  0.014482671 -0.0324211662 -0.026723442 -0.011269836
## 2021-04-30  7.345260e-02  0.067286193 -0.0158244342  0.117298188  0.060292565
## 2021-05-28 -5.181668e-02 -0.007656589 -0.0209792775  0.079071182 -0.126372343
## 2021-06-30  9.449997e-02  0.081569533  0.0492815858  0.208331918  0.083547955
## 2021-07-30  6.295838e-02  0.050423873 -0.0203491346 -0.025494077  0.010973846
## 2021-08-31  4.161137e-02  0.059768856  0.0950694939  0.138204279  0.068224268
## 2021-09-30 -7.046175e-02 -0.068406610  0.0698019104 -0.077485066  0.052632612
## 2021-10-29  5.700155e-02  0.162366700  0.1231245610  0.210396526  0.362230202
## 2021-11-30  9.991942e-02 -0.001283200 -0.0727081801  0.245337961  0.027237848
## 2021-12-31  7.160259e-02  0.017184379 -0.0634444996 -0.105149514 -0.079968440
## 2022-01-31 -1.583669e-02 -0.078334718 -0.3438762220 -0.183267094 -0.120597475
## 2022-02-28 -5.558243e-02 -0.037921832 -0.0794420172 -0.004133310 -0.073397011
## 2022-03-31  5.588239e-02  0.031364613 -0.0518377428  0.112575839  0.213504290
## 2022-04-29 -1.021774e-01 -0.105212712 -0.6769151054 -0.386065648 -0.213125289
## 2022-05-31 -5.603745e-02 -0.018242631  0.0365177351  0.006717054 -0.138340062
## 2022-06-30 -8.493680e-02 -0.056909384 -0.1213919016 -0.208219188 -0.118657126
## 2022-07-29  1.728045e-01  0.089014460  0.2516130360  0.180791975  0.280480149
## 2022-08-31 -3.170550e-02 -0.068988993 -0.0059759741 -0.185089280 -0.075250271
## 2022-09-30 -1.289440e-01 -0.115710483  0.0517762558 -0.217576940 -0.038313992
## 2022-10-31  1.039556e-01 -0.003311420  0.2148866190  0.106044032 -0.153346760
## 2022-11-30 -3.358529e-02  0.097328976  0.0457052075  0.226462026 -0.155866121
## 2022-12-30 -1.304189e-01 -0.061923777 -0.0354794336 -0.146693493 -0.457813194
## 2023-01-31  1.048292e-01  0.032773636  0.1823328761  0.290330070  0.340915767
## 2023-02-28  2.291836e-02  0.008977519 -0.0939461699  0.172531588  0.171904973
## 2023-03-31  1.121213e-01  0.144863313  0.0699795345  0.179536587  0.008471140
## 2023-04-28  2.857540e-02  0.063692841 -0.0460542525 -0.001008562 -0.233183710
## 2023-05-31  4.502908e-02  0.068691030  0.1805873321  0.310008230  0.216021889
## 2023-06-30  9.014228e-02  0.036330821  0.1084198950  0.111729626  0.249689452
## 2023-07-31  1.270432e-02 -0.013659516 -0.0034566798  0.099530806  0.021391609
## 2023-08-31 -4.330828e-02 -0.022476613 -0.0121241234  0.054673978 -0.035588260
## 2023-09-29 -9.285929e-02 -0.037330896 -0.1384715458 -0.126218797 -0.030929027
## 2023-10-31 -2.573302e-03  0.068420715  0.0864352180 -0.064546179 -0.219831983
## 2023-11-30  1.077598e-01  0.115954897  0.1408734072  0.137050231  0.178463656
## 2023-12-29  1.349123e-02 -0.007603116  0.0268736655  0.057263034  0.034390133
## 2024-01-31 -4.314471e-02  0.055700578  0.1472315750  0.217059109 -0.282704160
## 2024-02-29 -1.871725e-02  0.041447745  0.0665352594  0.251388330  0.075015304
## 2024-03-28 -5.264823e-02  0.016971388  0.0072878139  0.132940008 -0.138383423
## 2024-04-30 -6.728816e-03 -0.077540552 -0.0979911057 -0.044746758  0.041724969
## 2024-05-31  1.224141e-01  0.065966729  0.1529150450  0.238127601 -0.028782134
## 2024-06-28  9.125916e-02  0.073854902  0.0505386138  0.119508778  0.105427913
## 2024-07-31  5.298203e-02 -0.066128246 -0.0714375604 -0.054220349  0.159378271
## 2024-08-30  3.184121e-02 -0.001095724  0.1099097581  0.019883207 -0.080549177
## 2024-09-30  1.731632e-02  0.031060725  0.0112292200  0.017278086  0.200441406
## 2024-10-31 -3.090171e-02 -0.057267749  0.0638447163  0.089122404 -0.046070548
## 2024-11-29  5.041441e-02  0.043243818  0.1595497355  0.040520679  0.323147326
## 2024-12-31  5.368806e-02 -0.004639215  0.0050728010 -0.028993070  0.157010663
## 2025-01-31 -5.930782e-02 -0.015396712  0.0915374451 -0.111926670  0.001880189
## 2025-02-28  2.554345e-02 -0.042508936  0.0038828370  0.039598644 -0.322794555
## 2025-03-31 -8.501346e-02 -0.055945641 -0.0502225296 -0.141938048 -0.122658739
## 2025-04-30 -4.432092e-02  0.051578471  0.1935925475  0.004970110  0.085028653
## 2025-05-30 -5.507312e-02  0.154299017  0.0645899020  0.215623787  0.205293048
## 2025-06-30  2.128055e-02  0.077405910  0.1036916319  0.156363843 -0.086785707
## 2025-07-31  1.162973e-02  0.070044336 -0.1441174565  0.118521158 -0.030005653
## 2025-08-29  1.130063e-01 -0.049917090  0.0412703555 -0.020963765  0.079775694
## 2025-09-30  9.247092e-02  0.021979507 -0.0077518692  0.068827308  0.286693192
## 2025-10-31  5.997983e-02 -0.000270493 -0.0691108133  0.081830423  0.026275247
## 2025-11-28  3.185163e-02 -0.049239467 -0.0392457361 -0.134540765 -0.059539610
## 2025-12-30 -2.090944e-02 -0.009249723 -0.1372831732  0.057898001  0.054863360
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             AAPL        MSFT        NFLX        NVDA        TSLA
## AAPL 0.005873089 0.002164677 0.002479017 0.004722762 0.005215896
## MSFT 0.002164677 0.003530093 0.003048568 0.004151746 0.003231787
## NFLX 0.002479017 0.003048568 0.016664834 0.006900429 0.006160394
## NVDA 0.004722762 0.004151746 0.006900429 0.014624414 0.006301673
## TSLA 0.005215896 0.003231787 0.006160394 0.006301673 0.028684778
# 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.07286845
# 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       MSFT       NFLX       NVDA       TSLA
## [1,] 0.01362519 0.01093386 0.01841946 0.01963473 0.01025521
rowSums(component_contribution)
## [1] 0.07286845
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AAPL  MSFT  NFLX  NVDA  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.187  0.15 0.253 0.269 0.141
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.187
## 2 MSFT         0.15 
## 3 NFLX         0.253
## 4 NVDA         0.269
## 5 TSLA         0.141

4 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
##                     AAPL         MSFT          NFLX         NVDA         TSLA
## 2013-01-31 -1.555889e-01  0.027328103  0.5792178352  0.000000000  0.102078114
## 2013-02-28 -2.561100e-02  0.020915096  0.1294686037  0.038221993 -0.074128640
## 2013-03-28  2.850443e-03  0.028719872  0.0063600533  0.013338445  0.084208138
## 2013-04-30  2.708277e-04  0.145777422  0.1323749474  0.070706265  0.354111531
## 2013-05-31  2.217186e-02  0.059941238  0.0460382575  0.054652021  0.593716684
## 2013-06-28 -1.258955e-01 -0.010368881 -0.0693561240 -0.030167103  0.093672163
## 2013-07-31  1.321020e-01 -0.081394517  0.1468489367  0.028091880  0.223739522
## 2013-08-30  8.044276e-02  0.054854108  0.1495238797  0.026270110  0.229971642
## 2013-09-30 -2.172318e-02 -0.003598963  0.0853634080  0.053460238  0.134706620
## 2013-10-31  9.201475e-02  0.062037624  0.0420203453 -0.024066195 -0.189806595
## 2013-11-29  6.770892e-02  0.081562413  0.1260456715  0.032034932 -0.228409405
## 2013-12-31  8.861606e-03 -0.019063171  0.0064580000  0.026567178  0.167108541
## 2014-01-31 -1.139493e-01  0.011428384  0.1059768648 -0.020177251  0.187261714
## 2014-02-28  5.591847e-02  0.019815199  0.0849674837  0.162107468  0.299722785
## 2014-03-31  1.975645e-02  0.067616929 -0.2357727088 -0.025903902 -0.160783242
## 2014-04-30  9.476105e-02 -0.014498365 -0.0890406585  0.030788316 -0.002690122
## 2014-05-30  7.576558e-02  0.020307980  0.2603989261  0.032886654 -0.000577422
## 2014-06-30  2.728599e-02  0.018393364  0.0530628157 -0.024508585  0.144457224
## 2014-07-31  2.832628e-02  0.034413043 -0.0414274490 -0.057729640 -0.072372676
## 2014-08-29  7.465146e-02  0.057484995  0.1221473944  0.110060011  0.188794007
## 2014-09-30 -1.722037e-02  0.020264127 -0.0569909733 -0.052782719 -0.105566477
## 2014-10-31  6.948883e-02  0.012646484 -0.1386422062  0.057399393 -0.004046477
## 2014-11-28  1.007308e-01  0.024438619 -0.1250816593  0.074851994  0.011599801
## 2014-12-31 -7.460607e-02 -0.028857992 -0.0144727517 -0.044863260 -0.094774519
## 2015-01-30  5.961146e-02 -0.139546871  0.2571875640 -0.043319326 -0.088365289
## 2015-02-27  9.601601e-02  0.089036383  0.0722680237  0.142699141 -0.001277808
## 2015-03-31 -3.187433e-02 -0.075529985 -0.1307829000 -0.052582433 -0.074350051
## 2015-04-30  5.769888e-03  0.179201739  0.2893246603  0.058908629  0.180226808
## 2015-05-29  4.434154e-02 -0.030804115  0.1145793884  0.001459992  0.103899574
## 2015-06-30 -3.793836e-02 -0.059571456  0.0513461588 -0.095717166  0.067300935
## 2015-07-31 -3.348105e-02  0.056151282  0.1972314899 -0.007987854 -0.007896616
## 2015-08-31 -6.848914e-02 -0.063950709  0.0062789518  0.123595503 -0.066366250
## 2015-09-30 -2.205755e-02  0.016859871 -0.1079428863  0.092150865 -0.002653519
## 2015-10-30  8.011265e-02  0.173395438  0.0483934975  0.140555297 -0.182659777
## 2015-11-30 -5.821522e-03  0.038686135  0.1292201068  0.115405351  0.106828586
## 2015-12-31 -1.167901e-01  0.020578009 -0.0753374900  0.038347352  0.041471519
## 2016-01-29 -7.822355e-02 -0.007054208 -0.2194782802 -0.118048787 -0.227360626
## 2016-02-29 -1.288474e-03 -0.072344196  0.0169504292  0.071923777  0.003810669
## 2016-03-31  1.197460e-01  0.082036493  0.0902267716  0.127654787  0.179948109
## 2016-04-29 -1.507311e-01 -0.102086827 -0.1270821580 -0.002810490  0.046721797
## 2016-05-31  6.931413e-02  0.067842479  0.1304025124  0.276388275 -0.075597968
## 2016-06-30 -4.359629e-02 -0.035138989 -0.1144251460  0.006188298 -0.050296440
## 2016-07-29  8.623530e-02  0.102268341 -0.0025173508  0.194443436  0.100785334
## 2016-08-31  2.337623e-02  0.019880684  0.0657364217  0.073469255 -0.102058091
## 2016-09-30  6.344851e-02  0.002433835  0.0112245729  0.110693560 -0.038366372
## 2016-10-31  4.324852e-03  0.039487592  0.2367092462  0.037805080 -0.031364583
## 2016-11-30 -2.183759e-02  0.012391053 -0.0650993149  0.260525447 -0.043041267
## 2016-12-30  4.684102e-02  0.030721204  0.0564934510  0.146435921  0.120665178
## 2017-01-31  4.664137e-02  0.039598546  0.1280336724  0.022601696  0.164624916
## 2017-02-28  1.255553e-01 -0.004373438  0.0100411108 -0.071874790 -0.007730364
## 2017-03-31  4.754147e-02  0.028960917  0.0391855218  0.070843795  0.107278727
## 2017-04-28 -6.952873e-05  0.038718451  0.0292677885 -0.043434370  0.120916212
## 2017-05-31  6.560726e-02  0.025672732  0.0689840448  0.326022238  0.082295892
## 2017-06-30 -5.891580e-02 -0.013115343 -0.0874853040  0.001453666  0.058654468
## 2017-07-31  3.218039e-02  0.053249601  0.1954426244  0.117045066 -0.111459860
## 2017-08-31  1.016533e-01  0.033389298 -0.0390093333  0.042639297  0.095543446
## 2017-09-29 -6.213480e-02 -0.003751923  0.0373013834  0.053601106 -0.042474144
## 2017-10-31  9.240336e-02  0.110341786  0.0798772375  0.145700571 -0.028457409
## 2017-11-30  2.007569e-02  0.016841003 -0.0461007257 -0.029245203 -0.070862541
## 2017-12-29 -1.536349e-02  0.016145816  0.0230815823 -0.036583299  0.008061928
## 2018-01-31 -1.069358e-02  0.104998187  0.3422455062  0.239240940  0.129254571
## 2018-02-28  6.596123e-02 -0.008450805  0.0750957953 -0.014959379 -0.032266877
## 2018-03-29 -5.980402e-02 -0.027023077  0.0135328146 -0.043968951 -0.253920408
## 2018-04-30 -1.513377e-02  0.024353350  0.0563153817 -0.029312796  0.099254576
## 2018-05-31  1.267416e-01  0.059652227  0.1180176912  0.115144960 -0.031698139
## 2018-06-29 -9.462362e-03 -0.002329742  0.1073125782 -0.062544504  0.186043257
## 2018-07-31  2.759842e-02  0.073020576 -0.1483894202  0.033048295 -0.140021491
## 2018-08-31  1.826734e-01  0.061088395  0.0857957175  0.137075577  0.011737389
## 2018-09-28 -8.337546e-03  0.017997923  0.0173902455  0.001210296 -0.130439038
## 2018-10-31 -3.095177e-02 -0.068387178 -0.2149050241 -0.287373514  0.242170576
## 2018-11-30 -1.999121e-01  0.041797795 -0.0532519750 -0.253667279  0.038271580
## 2018-12-31 -1.240888e-01 -0.087790324 -0.0667287371 -0.202283381 -0.051761952
## 2019-01-31  5.368679e-02  0.027768697  0.2377564232  0.073974220 -0.080628789
## 2019-02-28  4.380283e-02  0.074511425  0.0533383459  0.071593957  0.041032981
## 2019-03-29  9.260275e-02  0.051409161 -0.0043098364  0.151869962 -0.133656413
## 2019-04-30  5.490093e-02  0.101963118  0.0384589452  0.007987561 -0.159123803
## 2019-05-31 -1.326320e-01 -0.050746478 -0.0764150093 -0.288679658 -0.253945372
## 2019-06-28  1.226768e-01  0.079843639  0.0676869455  0.192591179  0.188012109
## 2019-07-31  7.361705e-02  0.017096998 -0.1286120040  0.026972783  0.078092373
## 2019-08-30 -1.659834e-02  0.014924773 -0.0948922909 -0.006207996 -0.068516948
## 2019-09-30  7.042281e-02  0.008450921 -0.0931610341  0.038414441  0.065449565
## 2019-10-31  1.049764e-01  0.030739282  0.0713417093  0.143947174  0.268061253
## 2019-11-29  7.469361e-02  0.057761607  0.0905829375  0.076031093  0.046592176
## 2019-12-31  9.420386e-02  0.040900975  0.0279227475  0.082163278  0.237359743
## 2020-01-31  5.260197e-02  0.076455888  0.0643897941  0.004790447  0.441578342
## 2020-02-28 -1.218304e-01 -0.046764874  0.0670727184  0.133627251  0.026424253
## 2020-03-31 -7.231393e-02 -0.026899877  0.0173805126 -0.024248602 -0.242781458
## 2020-04-30  1.444239e-01  0.127800209  0.1116390683  0.103279415  0.400209535
## 2020-05-29  8.166673e-02  0.025074073 -0.0002858819  0.194462294  0.065730500
## 2020-06-30  1.374863e-01  0.104864249  0.0807737255  0.068216109  0.257108657
## 2020-07-31  1.528341e-01  0.007343540  0.0717317079  0.111190033  0.281420674
## 2020-08-31  1.960349e-01  0.097808770  0.0799294267  0.231105127  0.554719320
## 2020-09-30 -1.081713e-01 -0.069775353 -0.0573784336  0.011895623 -0.149762306
## 2020-10-30 -6.188832e-02 -0.038086279 -0.0497965496 -0.076501583 -0.100371771
## 2020-11-30  9.120468e-02  0.058326044  0.0309614228  0.066921923  0.380308519
## 2020-12-31  1.084722e-01  0.038264387  0.0970871760 -0.025900207  0.217730753
## 2021-01-29 -5.516909e-03  0.041997611 -0.0155438392 -0.005010659  0.117343701
## 2021-02-26 -8.306866e-02  0.004109673  0.0120609342  0.054293288 -0.161038203
## 2021-03-31  7.312987e-03  0.014482671 -0.0324211662 -0.026723442 -0.011269836
## 2021-04-30  7.345260e-02  0.067286193 -0.0158244342  0.117298188  0.060292565
## 2021-05-28 -5.181668e-02 -0.007656589 -0.0209792775  0.079071182 -0.126372343
## 2021-06-30  9.449997e-02  0.081569533  0.0492815858  0.208331918  0.083547955
## 2021-07-30  6.295838e-02  0.050423873 -0.0203491346 -0.025494077  0.010973846
## 2021-08-31  4.161137e-02  0.059768856  0.0950694939  0.138204279  0.068224268
## 2021-09-30 -7.046175e-02 -0.068406610  0.0698019104 -0.077485066  0.052632612
## 2021-10-29  5.700155e-02  0.162366700  0.1231245610  0.210396526  0.362230202
## 2021-11-30  9.991942e-02 -0.001283200 -0.0727081801  0.245337961  0.027237848
## 2021-12-31  7.160259e-02  0.017184379 -0.0634444996 -0.105149514 -0.079968440
## 2022-01-31 -1.583669e-02 -0.078334718 -0.3438762220 -0.183267094 -0.120597475
## 2022-02-28 -5.558243e-02 -0.037921832 -0.0794420172 -0.004133310 -0.073397011
## 2022-03-31  5.588239e-02  0.031364613 -0.0518377428  0.112575839  0.213504290
## 2022-04-29 -1.021774e-01 -0.105212712 -0.6769151054 -0.386065648 -0.213125289
## 2022-05-31 -5.603745e-02 -0.018242631  0.0365177351  0.006717054 -0.138340062
## 2022-06-30 -8.493680e-02 -0.056909384 -0.1213919016 -0.208219188 -0.118657126
## 2022-07-29  1.728045e-01  0.089014460  0.2516130360  0.180791975  0.280480149
## 2022-08-31 -3.170550e-02 -0.068988993 -0.0059759741 -0.185089280 -0.075250271
## 2022-09-30 -1.289440e-01 -0.115710483  0.0517762558 -0.217576940 -0.038313992
## 2022-10-31  1.039556e-01 -0.003311420  0.2148866190  0.106044032 -0.153346760
## 2022-11-30 -3.358529e-02  0.097328976  0.0457052075  0.226462026 -0.155866121
## 2022-12-30 -1.304189e-01 -0.061923777 -0.0354794336 -0.146693493 -0.457813194
## 2023-01-31  1.048292e-01  0.032773636  0.1823328761  0.290330070  0.340915767
## 2023-02-28  2.291836e-02  0.008977519 -0.0939461699  0.172531588  0.171904973
## 2023-03-31  1.121213e-01  0.144863313  0.0699795345  0.179536587  0.008471140
## 2023-04-28  2.857540e-02  0.063692841 -0.0460542525 -0.001008562 -0.233183710
## 2023-05-31  4.502908e-02  0.068691030  0.1805873321  0.310008230  0.216021889
## 2023-06-30  9.014228e-02  0.036330821  0.1084198950  0.111729626  0.249689452
## 2023-07-31  1.270432e-02 -0.013659516 -0.0034566798  0.099530806  0.021391609
## 2023-08-31 -4.330828e-02 -0.022476613 -0.0121241234  0.054673978 -0.035588260
## 2023-09-29 -9.285929e-02 -0.037330896 -0.1384715458 -0.126218797 -0.030929027
## 2023-10-31 -2.573302e-03  0.068420715  0.0864352180 -0.064546179 -0.219831983
## 2023-11-30  1.077598e-01  0.115954897  0.1408734072  0.137050231  0.178463656
## 2023-12-29  1.349123e-02 -0.007603116  0.0268736655  0.057263034  0.034390133
## 2024-01-31 -4.314471e-02  0.055700578  0.1472315750  0.217059109 -0.282704160
## 2024-02-29 -1.871725e-02  0.041447745  0.0665352594  0.251388330  0.075015304
## 2024-03-28 -5.264823e-02  0.016971388  0.0072878139  0.132940008 -0.138383423
## 2024-04-30 -6.728816e-03 -0.077540552 -0.0979911057 -0.044746758  0.041724969
## 2024-05-31  1.224141e-01  0.065966729  0.1529150450  0.238127601 -0.028782134
## 2024-06-28  9.125916e-02  0.073854902  0.0505386138  0.119508778  0.105427913
## 2024-07-31  5.298203e-02 -0.066128246 -0.0714375604 -0.054220349  0.159378271
## 2024-08-30  3.184121e-02 -0.001095724  0.1099097581  0.019883207 -0.080549177
## 2024-09-30  1.731632e-02  0.031060725  0.0112292200  0.017278086  0.200441406
## 2024-10-31 -3.090171e-02 -0.057267749  0.0638447163  0.089122404 -0.046070548
## 2024-11-29  5.041441e-02  0.043243818  0.1595497355  0.040520679  0.323147326
## 2024-12-31  5.368806e-02 -0.004639215  0.0050728010 -0.028993070  0.157010663
## 2025-01-31 -5.930782e-02 -0.015396712  0.0915374451 -0.111926670  0.001880189
## 2025-02-28  2.554345e-02 -0.042508936  0.0038828370  0.039598644 -0.322794555
## 2025-03-31 -8.501346e-02 -0.055945641 -0.0502225296 -0.141938048 -0.122658739
## 2025-04-30 -4.432092e-02  0.051578471  0.1935925475  0.004970110  0.085028653
## 2025-05-30 -5.507312e-02  0.154299017  0.0645899020  0.215623787  0.205293048
## 2025-06-30  2.128055e-02  0.077405910  0.1036916319  0.156363843 -0.086785707
## 2025-07-31  1.162973e-02  0.070044336 -0.1441174565  0.118521158 -0.030005653
## 2025-08-29  1.130063e-01 -0.049917090  0.0412703555 -0.020963765  0.079775694
## 2025-09-30  9.247092e-02  0.021979507 -0.0077518692  0.068827308  0.286693192
## 2025-10-31  5.997983e-02 -0.000270493 -0.0691108133  0.081830423  0.026275247
## 2025-11-28  3.185163e-02 -0.049239467 -0.0392457361 -0.134540765 -0.059539610
## 2025-12-30 -2.090944e-02 -0.009249723 -0.1372831732  0.057898001  0.054863360
cal_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 %>% cal_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1))
## # A tibble: 1 × 5
##    AAPL  MSFT  NFLX  NVDA  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.187  0.15 0.253 0.269 0.141

5 Visualizing Component Contribution

plot_data <- asset_returns_wide_tbl %>% 
    cal_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>% 
    # Transform to long form
    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 Rolling Component Contribution

plot_data <- asset_returns_wide_tbl %>% 
    cal_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>% 
    # Transform to long form
    pivot_longer(cols = everything(), names_to = "asset", values_to = "Contribution") %>% 
    add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
    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)) + 
    theme(plot.title = element_text(hjust = 0.5)) +
    theme_tq()

    labs (title = "Percent Contribution to Portfolio Volatility and weight", y = "Percent", x = NULL)
## <ggplot2::labels> List of 3
##  $ y    : chr "Percent"
##  $ x    : NULL
##  $ title: chr "Percent Contribution to Portfolio Volatility and weight"

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? With AAPL and MSFT being the most weighted in the portfolio they still do not contribute heavily to the standard deviation of the portfolio. This is because both AAPL and MSFT are large and stable stocks that generally carry low risk relative to the market. The clear outliers on our chart are NVDA and NFLX, which both carry >25% of the standard deviation contributions. TSLA is the lowest contributor due to the low weight in the portfolio. Given the same weight as other assets, TSLA would provide more volatility than many of the other assets in the portfolio. The portfolio risk is largely centered around NFLX and NVDA with AAPL and MSFT providing steady lower risk returns and TLSA owning to small of piece of the portfolio to greatly influence overall portfolio returns.