# 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("COST", "TSLA", "NFLX", "GOOG")

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

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

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
##                     COST          GOOG          NFLX         TSLA
## 2013-01-31  0.0359115681  0.0660632170  0.5792178072  0.102078114
## 2013-02-28 -0.0076472531  0.0584794011  0.1294683790 -0.074128640
## 2013-03-28  0.0464885488 -0.0087878463  0.0063600708  0.084208138
## 2013-04-30  0.0216284517  0.0375393257  0.1323750202  0.354111531
## 2013-05-31  0.0137932762  0.0550324761  0.0460381788  0.593716684
## 2013-06-28  0.0085374770  0.0104477267 -0.0693560587  0.093672163
## 2013-07-31  0.0601084428  0.0083478658  0.1468488828  0.223739522
## 2013-08-30 -0.0458220385 -0.0471074271  0.1495238153  0.229971642
## 2013-09-30  0.0290715036  0.0336805999  0.0853633452  0.134706620
## 2013-10-31  0.0242754411  0.1626137315  0.0420204523 -0.189806595
## 2013-11-29  0.0635980302  0.0277603361  0.1260456325 -0.228409405
## 2013-12-31 -0.0524563676  0.0560803485  0.0064580908  0.167108541
## 2014-01-31 -0.0575831769  0.0523737221  0.1059769064  0.187261714
## 2014-02-28  0.0414542673  0.0289427457  0.0849673396  0.299722785
## 2014-03-31 -0.0448253593 -0.0863761472 -0.2357726219 -0.160783242
## 2014-04-30  0.0351903031 -0.0559561794 -0.0890406532 -0.002690122
## 2014-05-30  0.0059918074  0.0611852260  0.2603988640 -0.000577422
## 2014-06-30 -0.0074401327  0.0271164958  0.0530627854  0.144457224
## 2014-07-31  0.0234832627 -0.0064174656 -0.0414273713 -0.072372676
## 2014-08-29  0.0296729444  0.0000000000  0.1221472352  0.188794007
## 2014-09-30  0.0344188093  0.0100265853 -0.0569909207 -0.105566477
## 2014-10-31  0.0622562635 -0.0321734129 -0.1386421470 -0.004046477
## 2014-11-28  0.0661384138 -0.0313402973 -0.1250817171  0.011599801
## 2014-12-31 -0.0026066361 -0.0288909045 -0.0144727330 -0.094774519
## 2015-01-30  0.0087100868  0.0153077799  0.2571875124 -0.088365289
## 2015-02-27  0.0623765262  0.0437063825  0.0722680160 -0.001277808
## 2015-03-31  0.0304255797 -0.0188002460 -0.1307827695 -0.074350051
## 2015-04-30 -0.0546593581 -0.0169024997  0.2893246523  0.180226808
## 2015-05-29 -0.0032214231 -0.0097808439  0.1145793430  0.103899574
## 2015-06-30 -0.0542539825 -0.0220411793  0.0513461801  0.067300935
## 2015-07-31  0.0730816914  0.1839181571  0.1972314899 -0.007896616
## 2015-08-31 -0.0340551290 -0.0118342869  0.0062789186 -0.066366250
## 2015-09-30  0.0317637544 -0.0160274954 -0.1079428532 -0.002653519
## 2015-10-30  0.0895905104  0.1555397601  0.0483934447 -0.182659777
## 2015-11-30  0.0232366478  0.0437523489  0.1292201596  0.106828586
## 2015-12-31  0.0004957211  0.0216861540 -0.0753374900  0.041471519
## 2016-01-29 -0.0664309659 -0.0212149794 -0.2194783218 -0.227360626
## 2016-02-29 -0.0045315927 -0.0627391949  0.0169505524  0.003810669
## 2016-03-31  0.0490979494  0.0654275900  0.0902267645  0.179948109
## 2016-04-29 -0.0588894860 -0.0722726575 -0.1270822750  0.046721797
## 2016-05-31  0.0043110957  0.0598051611  0.1304025548 -0.075597968
## 2016-06-30  0.0540987059 -0.0611191370 -0.1144250835 -0.050296440
## 2016-07-29  0.0628101291  0.1050873352 -0.0025174134  0.100785334
## 2016-08-31 -0.0284744524 -0.0022658019  0.0657364022 -0.102058091
## 2016-09-30 -0.0609218486  0.0132615062  0.0112246699 -0.038366372
## 2016-10-31 -0.0308961678  0.0092841269  0.2367091536 -0.031364583
## 2016-11-30  0.0181076140 -0.0343615032 -0.0650992833 -0.043041267
## 2016-12-30  0.0644925915  0.0180152164  0.0564934501  0.120665178
## 2017-01-31  0.0237005699  0.0318398164  0.1280336976  0.164624916
## 2017-02-28  0.0802945114  0.0326201197  0.0100410836 -0.007730364
## 2017-03-31 -0.0550488893  0.0076841895  0.0391854826  0.107278727
## 2017-04-28  0.0569661510  0.0880997263  0.0292677767  0.120916212
## 2017-05-31  0.0587794166  0.0629878112  0.0689841760  0.082295892
## 2017-06-30 -0.1206064481 -0.0599350027 -0.0874853720  0.058654468
## 2017-07-31 -0.0089182333  0.0236741050  0.1954425989 -0.111459860
## 2017-08-31 -0.0080374329  0.0094446956 -0.0390093333  0.095543446
## 2017-09-29  0.0470448974  0.0208390185  0.0373014045 -0.042474144
## 2017-10-31 -0.0197322105  0.0582525689  0.0798771971 -0.028457409
## 2017-11-30  0.1383318001  0.0046809326 -0.0461006656 -0.070862541
## 2017-12-29  0.0091217268  0.0241716306  0.0230816211  0.008061928
## 2018-01-31  0.0459409279  0.1115968360  0.3422453562  0.129254571
## 2018-02-28 -0.0179103531 -0.0573515717  0.0750958659 -0.032266877
## 2018-03-29 -0.0130234025 -0.0683057695  0.0135328404 -0.253920408
## 2018-04-30  0.0452892386 -0.0141136336  0.0563153192  0.099254576
## 2018-05-31  0.0083743648  0.0643892141  0.1180177495 -0.031698139
## 2018-06-29  0.0527603800  0.0278664957  0.1073124981  0.186043257
## 2018-07-31  0.0455079067  0.0871651844 -0.1483892939 -0.140021491
## 2018-08-31  0.0663290930  0.0007637293  0.0857955874  0.011737389
## 2018-09-28  0.0074785902 -0.0205011396  0.0173903689 -0.130439038
## 2018-10-31 -0.0269697142 -0.1028991701 -0.2149050726  0.242170576
## 2018-11-30  0.0138981500  0.0162678569 -0.0532520010  0.038271580
## 2018-12-31 -0.1269317555 -0.0552431137 -0.0667287380 -0.051761952
## 2019-01-31  0.0522183155  0.0750917770  0.2377564149 -0.080628789
## 2019-02-28  0.0216655872  0.0031748582  0.0533383470  0.041032981
## 2019-03-29  0.1016321334  0.0465716277 -0.0043097723 -0.133656413
## 2019-04-30  0.0139030813  0.0128463510  0.0384589024 -0.159123803
## 2019-05-31 -0.0218347898 -0.0740704455 -0.0764149871 -0.253945372
## 2019-06-28  0.0980462181 -0.0208014610  0.0676869856  0.188012109
## 2019-07-31  0.0421259555  0.1183225072 -0.1286120899  0.078092373
## 2019-08-30  0.0693117288 -0.0237704474 -0.0948922673 -0.068516948
## 2019-09-30 -0.0228191606  0.0256755141 -0.0931610198  0.065449565
## 2019-10-31  0.0329299048  0.0331681361  0.0713417349  0.268061253
## 2019-11-29  0.0090469587  0.0349733866  0.0905829220  0.046592176
## 2019-12-31 -0.0198413575  0.0242707998  0.0279227940  0.237359743
## 2020-01-31  0.0387072188  0.0701849397  0.0643897455  0.441578342
## 2020-02-28 -0.0810557568 -0.0684586628  0.0670726963  0.026424253
## 2020-03-31  0.0140919855 -0.1413299968  0.0173805330 -0.242781458
## 2020-04-30  0.0630699038  0.1482720617  0.1116390480  0.400209535
## 2020-05-29  0.0178916769  0.0578073988 -0.0002858456  0.065730500
## 2020-06-30 -0.0171988926 -0.0107722444  0.0807736724  0.257108657
## 2020-07-31  0.0731776305  0.0478934210  0.0717317246  0.281420674
## 2020-08-31  0.0657702230  0.0971010642  0.0799293979  0.554719320
## 2020-09-30  0.0208928021 -0.1061508397 -0.0573783743 -0.149762306
## 2020-10-30  0.0092732145  0.0980590840 -0.0497966282 -0.100371771
## 2020-11-30  0.0912039169  0.0826847930  0.0309615020  0.380308519
## 2020-12-31 -0.0131571276 -0.0050446871  0.0970870744  0.217730753
## 2021-01-29 -0.0668091227  0.0467581545 -0.0155437114  0.117343701
## 2021-02-26 -0.0607612627  0.1039617467  0.0120608627 -0.161038203
## 2021-03-31  0.0628755843  0.0154771302 -0.0324212105 -0.011269836
## 2021-04-30  0.0562815666  0.1527899199 -0.0158244352  0.060292565
## 2021-05-28  0.0164725011  0.0005974000 -0.0209791877 -0.126372343
## 2021-06-30  0.0449724818  0.0385416843  0.0492816132  0.083547955
## 2021-07-30  0.0844260687  0.0760718450 -0.0203491777  0.010973846
## 2021-08-31  0.0582397822  0.0730044848  0.0950695060  0.068224268
## 2021-09-30 -0.0135717006 -0.0875714716  0.0698019211  0.052632612
## 2021-10-29  0.0913578682  0.1066948290  0.1231245456  0.362230202
## 2021-11-30  0.0928768203 -0.0400332327 -0.0727082022  0.027237848
## 2021-12-31  0.0511729417  0.0155160090 -0.0634444870 -0.079968440
## 2022-01-31 -0.1167773301 -0.0640854551 -0.3438762168 -0.120597475
## 2022-02-28  0.0290840861 -0.0059684487 -0.0794420544 -0.073397011
## 2022-03-31  0.1034617976  0.0346686002 -0.0518377235  0.213504290
## 2022-04-29 -0.0781046009 -0.1944949402 -0.6769150653 -0.213125289
## 2022-05-31 -0.1314593431 -0.0081002799  0.0365177336 -0.138340062
## 2022-06-30  0.0276272433 -0.0417810595 -0.1213919403 -0.118657126
## 2022-07-29  0.1234134665  0.0643327822  0.2516130190  0.280480149
## 2022-08-31 -0.0361147826 -0.0663691606 -0.0059760083 -0.075250271
## 2022-09-30 -0.1003082273 -0.1268135963  0.0517762908 -0.038313992
## 2022-10-31  0.0618564802 -0.0156179238  0.2148866613 -0.153346760
## 2022-11-30  0.0725756255  0.0692745192  0.0457051938 -0.155866121
## 2022-12-30 -0.1665905634 -0.1339679494 -0.0354794590 -0.457813194
## 2023-01-31  0.1130547825  0.1182712514  0.1823328028  0.340915767
## 2023-02-28 -0.0524475093 -0.1007318544 -0.0939461074  0.171904973
## 2023-03-31  0.0258717045  0.1412534080  0.0699795803  0.008471140
## 2023-04-28  0.0126990826  0.0397752831 -0.0460542977 -0.233183710
## 2023-05-31  0.0185209142  0.1310217951  0.1805874132  0.216021889
## 2023-06-30  0.0510996471 -0.0196454366  0.1084198198  0.249689452
## 2023-07-31  0.0405679293  0.0956332507 -0.0034566451  0.021391609
## 2023-08-31 -0.0186363046  0.0313565826 -0.0121241583 -0.035588260
## 2023-09-29  0.0281465321 -0.0408674663 -0.1384714676 -0.030929027
## 2023-10-31 -0.0224102040 -0.0509540648  0.0864351944 -0.219831983
## 2023-11-30  0.0722447221  0.0665317218  0.1408733862  0.178463656
## 2023-12-29  0.1300907626  0.0510206888  0.0268736650  0.034390133
## 2024-01-31  0.0513781346  0.0061543537  0.1472315458 -0.282704160
## 2024-02-29  0.0696229645 -0.0143479382  0.0665352476  0.075015304
## 2024-03-28 -0.0152523574  0.0855198712  0.0072878392 -0.138383423
## 2024-04-30 -0.0117661258  0.0781717370 -0.0979910641  0.041724969
## 2024-05-31  0.1136275203  0.0550640917  0.1529149558 -0.028782134
## 2024-06-26  0.0563527807  0.0646657119  0.0546937393  0.097767712
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             COST        GOOG        NFLX        TSLA
## COST 0.003122385 0.001744223 0.001853619 0.002500825
## GOOG 0.001744223 0.004310828 0.003851547 0.003583974
## NFLX 0.001853619 0.003851547 0.017735168 0.006691905
## TSLA 0.002500825 0.003583974 0.006691905 0.029228753
# 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.25, .25)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.07699404
# 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
##             COST       GOOG       NFLX       TSLA
## [1,] 0.007485199 0.01095099 0.02445988 0.03409798
rowSums(component_contribution)
## [1] 0.07699404
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 4
##    COST  GOOG  NFLX  TSLA
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.097 0.142 0.318 0.443
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 COST         0.097
## 2 GOOG         0.142
## 3 NFLX         0.318
## 4 TSLA         0.443

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 
##                     COST          GOOG          NFLX         TSLA
## 2013-01-31  0.0359115681  0.0660632170  0.5792178072  0.102078114
## 2013-02-28 -0.0076472531  0.0584794011  0.1294683790 -0.074128640
## 2013-03-28  0.0464885488 -0.0087878463  0.0063600708  0.084208138
## 2013-04-30  0.0216284517  0.0375393257  0.1323750202  0.354111531
## 2013-05-31  0.0137932762  0.0550324761  0.0460381788  0.593716684
## 2013-06-28  0.0085374770  0.0104477267 -0.0693560587  0.093672163
## 2013-07-31  0.0601084428  0.0083478658  0.1468488828  0.223739522
## 2013-08-30 -0.0458220385 -0.0471074271  0.1495238153  0.229971642
## 2013-09-30  0.0290715036  0.0336805999  0.0853633452  0.134706620
## 2013-10-31  0.0242754411  0.1626137315  0.0420204523 -0.189806595
## 2013-11-29  0.0635980302  0.0277603361  0.1260456325 -0.228409405
## 2013-12-31 -0.0524563676  0.0560803485  0.0064580908  0.167108541
## 2014-01-31 -0.0575831769  0.0523737221  0.1059769064  0.187261714
## 2014-02-28  0.0414542673  0.0289427457  0.0849673396  0.299722785
## 2014-03-31 -0.0448253593 -0.0863761472 -0.2357726219 -0.160783242
## 2014-04-30  0.0351903031 -0.0559561794 -0.0890406532 -0.002690122
## 2014-05-30  0.0059918074  0.0611852260  0.2603988640 -0.000577422
## 2014-06-30 -0.0074401327  0.0271164958  0.0530627854  0.144457224
## 2014-07-31  0.0234832627 -0.0064174656 -0.0414273713 -0.072372676
## 2014-08-29  0.0296729444  0.0000000000  0.1221472352  0.188794007
## 2014-09-30  0.0344188093  0.0100265853 -0.0569909207 -0.105566477
## 2014-10-31  0.0622562635 -0.0321734129 -0.1386421470 -0.004046477
## 2014-11-28  0.0661384138 -0.0313402973 -0.1250817171  0.011599801
## 2014-12-31 -0.0026066361 -0.0288909045 -0.0144727330 -0.094774519
## 2015-01-30  0.0087100868  0.0153077799  0.2571875124 -0.088365289
## 2015-02-27  0.0623765262  0.0437063825  0.0722680160 -0.001277808
## 2015-03-31  0.0304255797 -0.0188002460 -0.1307827695 -0.074350051
## 2015-04-30 -0.0546593581 -0.0169024997  0.2893246523  0.180226808
## 2015-05-29 -0.0032214231 -0.0097808439  0.1145793430  0.103899574
## 2015-06-30 -0.0542539825 -0.0220411793  0.0513461801  0.067300935
## 2015-07-31  0.0730816914  0.1839181571  0.1972314899 -0.007896616
## 2015-08-31 -0.0340551290 -0.0118342869  0.0062789186 -0.066366250
## 2015-09-30  0.0317637544 -0.0160274954 -0.1079428532 -0.002653519
## 2015-10-30  0.0895905104  0.1555397601  0.0483934447 -0.182659777
## 2015-11-30  0.0232366478  0.0437523489  0.1292201596  0.106828586
## 2015-12-31  0.0004957211  0.0216861540 -0.0753374900  0.041471519
## 2016-01-29 -0.0664309659 -0.0212149794 -0.2194783218 -0.227360626
## 2016-02-29 -0.0045315927 -0.0627391949  0.0169505524  0.003810669
## 2016-03-31  0.0490979494  0.0654275900  0.0902267645  0.179948109
## 2016-04-29 -0.0588894860 -0.0722726575 -0.1270822750  0.046721797
## 2016-05-31  0.0043110957  0.0598051611  0.1304025548 -0.075597968
## 2016-06-30  0.0540987059 -0.0611191370 -0.1144250835 -0.050296440
## 2016-07-29  0.0628101291  0.1050873352 -0.0025174134  0.100785334
## 2016-08-31 -0.0284744524 -0.0022658019  0.0657364022 -0.102058091
## 2016-09-30 -0.0609218486  0.0132615062  0.0112246699 -0.038366372
## 2016-10-31 -0.0308961678  0.0092841269  0.2367091536 -0.031364583
## 2016-11-30  0.0181076140 -0.0343615032 -0.0650992833 -0.043041267
## 2016-12-30  0.0644925915  0.0180152164  0.0564934501  0.120665178
## 2017-01-31  0.0237005699  0.0318398164  0.1280336976  0.164624916
## 2017-02-28  0.0802945114  0.0326201197  0.0100410836 -0.007730364
## 2017-03-31 -0.0550488893  0.0076841895  0.0391854826  0.107278727
## 2017-04-28  0.0569661510  0.0880997263  0.0292677767  0.120916212
## 2017-05-31  0.0587794166  0.0629878112  0.0689841760  0.082295892
## 2017-06-30 -0.1206064481 -0.0599350027 -0.0874853720  0.058654468
## 2017-07-31 -0.0089182333  0.0236741050  0.1954425989 -0.111459860
## 2017-08-31 -0.0080374329  0.0094446956 -0.0390093333  0.095543446
## 2017-09-29  0.0470448974  0.0208390185  0.0373014045 -0.042474144
## 2017-10-31 -0.0197322105  0.0582525689  0.0798771971 -0.028457409
## 2017-11-30  0.1383318001  0.0046809326 -0.0461006656 -0.070862541
## 2017-12-29  0.0091217268  0.0241716306  0.0230816211  0.008061928
## 2018-01-31  0.0459409279  0.1115968360  0.3422453562  0.129254571
## 2018-02-28 -0.0179103531 -0.0573515717  0.0750958659 -0.032266877
## 2018-03-29 -0.0130234025 -0.0683057695  0.0135328404 -0.253920408
## 2018-04-30  0.0452892386 -0.0141136336  0.0563153192  0.099254576
## 2018-05-31  0.0083743648  0.0643892141  0.1180177495 -0.031698139
## 2018-06-29  0.0527603800  0.0278664957  0.1073124981  0.186043257
## 2018-07-31  0.0455079067  0.0871651844 -0.1483892939 -0.140021491
## 2018-08-31  0.0663290930  0.0007637293  0.0857955874  0.011737389
## 2018-09-28  0.0074785902 -0.0205011396  0.0173903689 -0.130439038
## 2018-10-31 -0.0269697142 -0.1028991701 -0.2149050726  0.242170576
## 2018-11-30  0.0138981500  0.0162678569 -0.0532520010  0.038271580
## 2018-12-31 -0.1269317555 -0.0552431137 -0.0667287380 -0.051761952
## 2019-01-31  0.0522183155  0.0750917770  0.2377564149 -0.080628789
## 2019-02-28  0.0216655872  0.0031748582  0.0533383470  0.041032981
## 2019-03-29  0.1016321334  0.0465716277 -0.0043097723 -0.133656413
## 2019-04-30  0.0139030813  0.0128463510  0.0384589024 -0.159123803
## 2019-05-31 -0.0218347898 -0.0740704455 -0.0764149871 -0.253945372
## 2019-06-28  0.0980462181 -0.0208014610  0.0676869856  0.188012109
## 2019-07-31  0.0421259555  0.1183225072 -0.1286120899  0.078092373
## 2019-08-30  0.0693117288 -0.0237704474 -0.0948922673 -0.068516948
## 2019-09-30 -0.0228191606  0.0256755141 -0.0931610198  0.065449565
## 2019-10-31  0.0329299048  0.0331681361  0.0713417349  0.268061253
## 2019-11-29  0.0090469587  0.0349733866  0.0905829220  0.046592176
## 2019-12-31 -0.0198413575  0.0242707998  0.0279227940  0.237359743
## 2020-01-31  0.0387072188  0.0701849397  0.0643897455  0.441578342
## 2020-02-28 -0.0810557568 -0.0684586628  0.0670726963  0.026424253
## 2020-03-31  0.0140919855 -0.1413299968  0.0173805330 -0.242781458
## 2020-04-30  0.0630699038  0.1482720617  0.1116390480  0.400209535
## 2020-05-29  0.0178916769  0.0578073988 -0.0002858456  0.065730500
## 2020-06-30 -0.0171988926 -0.0107722444  0.0807736724  0.257108657
## 2020-07-31  0.0731776305  0.0478934210  0.0717317246  0.281420674
## 2020-08-31  0.0657702230  0.0971010642  0.0799293979  0.554719320
## 2020-09-30  0.0208928021 -0.1061508397 -0.0573783743 -0.149762306
## 2020-10-30  0.0092732145  0.0980590840 -0.0497966282 -0.100371771
## 2020-11-30  0.0912039169  0.0826847930  0.0309615020  0.380308519
## 2020-12-31 -0.0131571276 -0.0050446871  0.0970870744  0.217730753
## 2021-01-29 -0.0668091227  0.0467581545 -0.0155437114  0.117343701
## 2021-02-26 -0.0607612627  0.1039617467  0.0120608627 -0.161038203
## 2021-03-31  0.0628755843  0.0154771302 -0.0324212105 -0.011269836
## 2021-04-30  0.0562815666  0.1527899199 -0.0158244352  0.060292565
## 2021-05-28  0.0164725011  0.0005974000 -0.0209791877 -0.126372343
## 2021-06-30  0.0449724818  0.0385416843  0.0492816132  0.083547955
## 2021-07-30  0.0844260687  0.0760718450 -0.0203491777  0.010973846
## 2021-08-31  0.0582397822  0.0730044848  0.0950695060  0.068224268
## 2021-09-30 -0.0135717006 -0.0875714716  0.0698019211  0.052632612
## 2021-10-29  0.0913578682  0.1066948290  0.1231245456  0.362230202
## 2021-11-30  0.0928768203 -0.0400332327 -0.0727082022  0.027237848
## 2021-12-31  0.0511729417  0.0155160090 -0.0634444870 -0.079968440
## 2022-01-31 -0.1167773301 -0.0640854551 -0.3438762168 -0.120597475
## 2022-02-28  0.0290840861 -0.0059684487 -0.0794420544 -0.073397011
## 2022-03-31  0.1034617976  0.0346686002 -0.0518377235  0.213504290
## 2022-04-29 -0.0781046009 -0.1944949402 -0.6769150653 -0.213125289
## 2022-05-31 -0.1314593431 -0.0081002799  0.0365177336 -0.138340062
## 2022-06-30  0.0276272433 -0.0417810595 -0.1213919403 -0.118657126
## 2022-07-29  0.1234134665  0.0643327822  0.2516130190  0.280480149
## 2022-08-31 -0.0361147826 -0.0663691606 -0.0059760083 -0.075250271
## 2022-09-30 -0.1003082273 -0.1268135963  0.0517762908 -0.038313992
## 2022-10-31  0.0618564802 -0.0156179238  0.2148866613 -0.153346760
## 2022-11-30  0.0725756255  0.0692745192  0.0457051938 -0.155866121
## 2022-12-30 -0.1665905634 -0.1339679494 -0.0354794590 -0.457813194
## 2023-01-31  0.1130547825  0.1182712514  0.1823328028  0.340915767
## 2023-02-28 -0.0524475093 -0.1007318544 -0.0939461074  0.171904973
## 2023-03-31  0.0258717045  0.1412534080  0.0699795803  0.008471140
## 2023-04-28  0.0126990826  0.0397752831 -0.0460542977 -0.233183710
## 2023-05-31  0.0185209142  0.1310217951  0.1805874132  0.216021889
## 2023-06-30  0.0510996471 -0.0196454366  0.1084198198  0.249689452
## 2023-07-31  0.0405679293  0.0956332507 -0.0034566451  0.021391609
## 2023-08-31 -0.0186363046  0.0313565826 -0.0121241583 -0.035588260
## 2023-09-29  0.0281465321 -0.0408674663 -0.1384714676 -0.030929027
## 2023-10-31 -0.0224102040 -0.0509540648  0.0864351944 -0.219831983
## 2023-11-30  0.0722447221  0.0665317218  0.1408733862  0.178463656
## 2023-12-29  0.1300907626  0.0510206888  0.0268736650  0.034390133
## 2024-01-31  0.0513781346  0.0061543537  0.1472315458 -0.282704160
## 2024-02-29  0.0696229645 -0.0143479382  0.0665352476  0.075015304
## 2024-03-28 -0.0152523574  0.0855198712  0.0072878392 -0.138383423
## 2024-04-30 -0.0117661258  0.0781717370 -0.0979910641  0.041724969
## 2024-05-31  0.1136275203  0.0550640917  0.1529149558 -0.028782134
## 2024-06-26  0.0563527807  0.0646657119  0.0546937393  0.097767712
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
        
        # 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, .25, .25)) 
## # A tibble: 1 × 4
##    COST  GOOG  NFLX  TSLA
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.097 0.142 0.318 0.443

6 Plot: Column Chart of Component Contribution and Weight

Column Chart of Compound Contribution

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

    #Add weights 
    add_column(weight = c(.25, .25, .25, .25)) %>% 
    
    # Transform to log 
    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 Votality 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?

The largest contributors to my portfolio’s volatility are Netflix Inc. and Tesla Inc, they both contribute to about 44% and 32%. I do believe my portfolio’s volatility is concentrated with Tesla Inc., mainly due to the fact that Tesla Inc.’s volatility accounts for such a high portion of the overall volatility fo the portfolio.