# 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", "AMZN", "GOOG")
prices <- tq_get(x = symbols, 
               get = "stock.prices",
              from = "2012-12-31", 
                to = "2022-11-30")

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "GOOG" "NFLX"
# weights
weights <- c(0.4, 0.3, 0.3)
weights
## [1] 0.4 0.3 0.3
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 3 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN        0.4
## 2 GOOG        0.3
## 3 NFLX        0.3
# 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
##                     AMZN          GOOG          NFLX
## 2013-01-31  0.0566799640  0.0660631831  0.5792178375
## 2013-02-28 -0.0046435329  0.0584793486  0.1294683583
## 2013-03-28  0.0083654117 -0.0087878479  0.0063600861
## 2013-04-30 -0.0487507638  0.0375393197  0.1323750291
## 2013-05-31  0.0588686422  0.0550324476  0.0460381605
## 2013-06-28  0.0310507858  0.0104477556 -0.0693560534
## 2013-07-31  0.0813355112  0.0083478971  0.1468488958
## 2013-08-30 -0.0695574024 -0.0471074838  0.1495238068
## 2013-09-30  0.1067688764  0.0336806418  0.0853633332
## 2013-10-31  0.1521839130  0.1626137477  0.0420204569
## 2013-11-29  0.0781496951  0.0277603002  0.1260456343
## 2013-12-31  0.0130490358  0.0560803333  0.0064581012
## 2014-01-31 -0.1059765070  0.0523737790  0.1059769044
## 2014-02-28  0.0094619111  0.0289427312  0.0849673277
## 2014-03-31 -0.0737086127 -0.0863761259 -0.2357726146
## 2014-04-30 -0.1007565625 -0.0559562158 -0.0890406639
## 2014-05-30  0.0273092148  0.0611851999  0.2603988793
## 2014-06-30  0.0383835737  0.0271165731  0.0530627817
## 2014-07-31 -0.0369767889 -0.0064175303 -0.0414273689
## 2014-08-29  0.0799468534  0.0000000000  0.1221472235
## 2014-09-30 -0.0502010221  0.0100265967 -0.0569909090
## 2014-10-31 -0.0540982353 -0.0321734316 -0.1386421452
## 2014-11-28  0.1031187000 -0.0313402589 -0.1250817146
## 2014-12-31 -0.0872368443 -0.0288909325 -0.0144727373
## 2015-01-30  0.1330922758  0.0153077539  0.2571875149
## 2015-02-27  0.0697991955  0.0437064101  0.0722680075
## 2015-03-31 -0.0214295288 -0.0188002191 -0.1307827615
## 2015-04-30  0.1253212631 -0.0169024972  0.2893246451
## 2015-05-29  0.0175090073 -0.0097808812  0.1145793405
## 2015-06-30  0.0112589801 -0.0220411356  0.0513461850
## 2015-07-31  0.2111621241  0.1839181120  0.1972314914
## 2015-08-31 -0.0443525737 -0.0118342674  0.0062789167
## 2015-09-30 -0.0019516780 -0.0160274734 -0.1079428564
## 2015-10-30  0.2010808557  0.1555397600  0.0483934436
## 2015-11-30  0.0602956898  0.0437523757  0.1292201633
## 2015-12-31  0.0165439780  0.0216860935 -0.0753374935
## 2016-01-29 -0.1410054619 -0.0212149413 -0.2194783232
## 2016-02-29 -0.0605352242 -0.0627391852  0.0169505597
## 2016-03-31  0.0717834457  0.0654275933  0.0902267574
## 2016-04-29  0.1053453885 -0.0722726709 -0.1270822691
## 2016-05-31  0.0915002937  0.0598051264  0.1304025553
## 2016-06-30 -0.0099694796 -0.0611191305 -0.1144250904
## 2016-07-29  0.0586021200  0.1050873704 -0.0025174095
## 2016-08-31  0.0135476463 -0.0022658099  0.0657364027
## 2016-09-30  0.0848953859  0.0132614453  0.0112246689
## 2016-10-31 -0.0583892995  0.0092841246  0.2367091561
## 2016-11-30 -0.0509721788 -0.0343614296 -0.0650992854
## 2016-12-30 -0.0009330597  0.0180152075  0.0564934497
## 2017-01-31  0.0936394046  0.0318397938  0.1280337001
## 2017-02-28  0.0258446771  0.0326201762  0.0100410824
## 2017-03-31  0.0479423059  0.0076841317  0.0391854848
## 2017-04-28  0.0424566809  0.0880996911  0.0292677740
## 2017-05-31  0.0725778079  0.0629878581  0.0689841737
## 2017-06-30 -0.0271286060 -0.0599349707 -0.0874853678
## 2017-07-31  0.0202278723  0.0236740767  0.1954425985
## 2017-08-31 -0.0072953921  0.0094447148 -0.0390093335
## 2017-09-29 -0.0198260414  0.0208389789  0.0373014023
## 2017-10-31  0.1395154081  0.0582525579  0.0798771993
## 2017-11-30  0.0626577388  0.0046809143 -0.0461006664
## 2017-12-29 -0.0062057977  0.0241716956  0.0230816217
## 2018-01-31  0.2156265512  0.1115967979  0.3422453554
## 2018-02-28  0.0415536373 -0.0573515384  0.0750958655
## 2018-03-29 -0.0440034786 -0.0683058032  0.0135328397
## 2018-04-30  0.0788802990 -0.0141135901  0.0563153181
## 2018-05-31  0.0397392491  0.0643892025  0.1180177507
## 2018-06-29  0.0421636779  0.0278664423  0.1073124992
## 2018-07-31  0.0446635758  0.0871652022 -0.1483892953
## 2018-08-31  0.1243079035  0.0007637357  0.0857955889
## 2018-09-28 -0.0048359762 -0.0205011202  0.0173903683
## 2018-10-31 -0.2258870091 -0.1028991926 -0.2149050722
## 2018-11-30  0.0560700362  0.0162678420 -0.0532520013
## 2018-12-31 -0.1180514840 -0.0552430740 -0.0667287372
## 2019-01-31  0.1348080379  0.0750917624  0.2377564137
## 2019-02-28 -0.0469930675  0.0031748503  0.0533383468
## 2019-03-29  0.0824420113  0.0465716130 -0.0043097707
## 2019-04-30  0.0786806236  0.0128463483  0.0384589024
## 2019-05-31 -0.0818753437 -0.0740704511 -0.0764149876
## 2019-06-28  0.0646557723 -0.0208014127  0.0676869840
## 2019-07-31 -0.0142806642  0.1183225218 -0.1286120898
## 2019-08-30 -0.0496880811 -0.0237704621 -0.0948922666
## 2019-09-30 -0.0229951136  0.0256754913 -0.0931610203
## 2019-10-31  0.0232034025  0.0331681677  0.0713417365
## 2019-11-29  0.0134958246  0.0349733697  0.0905829219
## 2019-12-31  0.0257863460  0.0242708227  0.0279227920
## 2020-01-31  0.0834803057  0.0701849208  0.0643897455
## 2020-02-28 -0.0642332025 -0.0684586737  0.0670726979
## 2020-03-31  0.0344213016 -0.1413300016  0.0173805324
## 2020-04-30  0.2381504772  0.1482720197  0.1116390477
## 2020-05-29 -0.0128673704  0.0578074081 -0.0002858453
## 2020-06-30  0.1218341292 -0.0107722406  0.0807736733
## 2020-07-31  0.1372488981  0.0478934714  0.0717317239
## 2020-08-31  0.0866005697  0.0971010186  0.0799293985
## 2020-09-30 -0.0916533239 -0.1061508537 -0.0573783747
## 2020-10-30 -0.0364089200  0.0980591037 -0.0497966291
## 2020-11-30  0.0425228235  0.0826848099  0.0309615021
## 2020-12-31  0.0276719595 -0.0050446895  0.0970870739
## 2021-01-29 -0.0156985927  0.0467581828 -0.0155437098
## 2021-02-26 -0.0359675611  0.1039617233  0.0120608629
## 2021-03-31  0.0003717143  0.0154771554 -0.0324212116
## 2021-04-30  0.1139202371  0.1527899325 -0.0158244343
## 2021-05-28 -0.0730764730  0.0005973520 -0.0209791874
## 2021-06-30  0.0651836186  0.0385416919  0.0492816124
## 2021-07-30 -0.0332696344  0.0760718620 -0.0203491783
## 2021-08-31  0.0421339383  0.0730045001  0.0950695058
## 2021-09-30 -0.0550034402 -0.0875715239  0.0698019221
## 2021-10-29  0.0262547660  0.1066948595  0.1231245460
## 2021-11-30  0.0391473427 -0.0400331883 -0.0727082035
## 2021-12-31 -0.0505062023  0.0155159060 -0.0634444870
## 2022-01-31 -0.1085098100 -0.0640854511 -0.3438762152
## 2022-02-28  0.0263230081 -0.0059684383 -0.0794420553
## 2022-03-31  0.0596239187  0.0346686656 -0.0518377244
## 2022-04-29 -0.2711856809 -0.1944949821 -0.6769150623
## 2022-05-31 -0.0333130912 -0.0081002690  0.0365177293
## 2022-06-30 -0.1238178220 -0.0417810349 -0.1213919387
## 2022-07-29  0.2394860603  0.0643327907  0.2516130202
## 2022-08-31 -0.0625299199 -0.0663691584 -0.0059760068
## 2022-09-30 -0.1149865786 -0.1268136085  0.0517762870
## 2022-10-31 -0.0981105379 -0.0156179261  0.2148866636
## 2022-11-29 -0.1029338971  0.0082062320 -0.0381304976
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl) 

covariance_matrix
##             AMZN        GOOG        NFLX
## AMZN 0.007464105 0.003459674 0.006611838
## GOOG 0.003459674 0.004055594 0.003952824
## NFLX 0.006611838 0.003952824 0.019141280
# 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.4, 0.3, 0.3)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.08006651
# 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
##            AMZN       GOOG      NFLX
## [1,] 0.03001053 0.01418719 0.0358688
rowSums(component_contribution)
## [1] 0.08006651
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 3
##    AMZN  GOOG  NFLX
##   <dbl> <dbl> <dbl>
## 1 0.375 0.177 0.448
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.375
## 2 GOOG         0.177
## 3 NFLX         0.448
# 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
##                     AMZN          GOOG          NFLX
## 2013-01-31  0.0566799640  0.0660631831  0.5792178375
## 2013-02-28 -0.0046435329  0.0584793486  0.1294683583
## 2013-03-28  0.0083654117 -0.0087878479  0.0063600861
## 2013-04-30 -0.0487507638  0.0375393197  0.1323750291
## 2013-05-31  0.0588686422  0.0550324476  0.0460381605
## 2013-06-28  0.0310507858  0.0104477556 -0.0693560534
## 2013-07-31  0.0813355112  0.0083478971  0.1468488958
## 2013-08-30 -0.0695574024 -0.0471074838  0.1495238068
## 2013-09-30  0.1067688764  0.0336806418  0.0853633332
## 2013-10-31  0.1521839130  0.1626137477  0.0420204569
## 2013-11-29  0.0781496951  0.0277603002  0.1260456343
## 2013-12-31  0.0130490358  0.0560803333  0.0064581012
## 2014-01-31 -0.1059765070  0.0523737790  0.1059769044
## 2014-02-28  0.0094619111  0.0289427312  0.0849673277
## 2014-03-31 -0.0737086127 -0.0863761259 -0.2357726146
## 2014-04-30 -0.1007565625 -0.0559562158 -0.0890406639
## 2014-05-30  0.0273092148  0.0611851999  0.2603988793
## 2014-06-30  0.0383835737  0.0271165731  0.0530627817
## 2014-07-31 -0.0369767889 -0.0064175303 -0.0414273689
## 2014-08-29  0.0799468534  0.0000000000  0.1221472235
## 2014-09-30 -0.0502010221  0.0100265967 -0.0569909090
## 2014-10-31 -0.0540982353 -0.0321734316 -0.1386421452
## 2014-11-28  0.1031187000 -0.0313402589 -0.1250817146
## 2014-12-31 -0.0872368443 -0.0288909325 -0.0144727373
## 2015-01-30  0.1330922758  0.0153077539  0.2571875149
## 2015-02-27  0.0697991955  0.0437064101  0.0722680075
## 2015-03-31 -0.0214295288 -0.0188002191 -0.1307827615
## 2015-04-30  0.1253212631 -0.0169024972  0.2893246451
## 2015-05-29  0.0175090073 -0.0097808812  0.1145793405
## 2015-06-30  0.0112589801 -0.0220411356  0.0513461850
## 2015-07-31  0.2111621241  0.1839181120  0.1972314914
## 2015-08-31 -0.0443525737 -0.0118342674  0.0062789167
## 2015-09-30 -0.0019516780 -0.0160274734 -0.1079428564
## 2015-10-30  0.2010808557  0.1555397600  0.0483934436
## 2015-11-30  0.0602956898  0.0437523757  0.1292201633
## 2015-12-31  0.0165439780  0.0216860935 -0.0753374935
## 2016-01-29 -0.1410054619 -0.0212149413 -0.2194783232
## 2016-02-29 -0.0605352242 -0.0627391852  0.0169505597
## 2016-03-31  0.0717834457  0.0654275933  0.0902267574
## 2016-04-29  0.1053453885 -0.0722726709 -0.1270822691
## 2016-05-31  0.0915002937  0.0598051264  0.1304025553
## 2016-06-30 -0.0099694796 -0.0611191305 -0.1144250904
## 2016-07-29  0.0586021200  0.1050873704 -0.0025174095
## 2016-08-31  0.0135476463 -0.0022658099  0.0657364027
## 2016-09-30  0.0848953859  0.0132614453  0.0112246689
## 2016-10-31 -0.0583892995  0.0092841246  0.2367091561
## 2016-11-30 -0.0509721788 -0.0343614296 -0.0650992854
## 2016-12-30 -0.0009330597  0.0180152075  0.0564934497
## 2017-01-31  0.0936394046  0.0318397938  0.1280337001
## 2017-02-28  0.0258446771  0.0326201762  0.0100410824
## 2017-03-31  0.0479423059  0.0076841317  0.0391854848
## 2017-04-28  0.0424566809  0.0880996911  0.0292677740
## 2017-05-31  0.0725778079  0.0629878581  0.0689841737
## 2017-06-30 -0.0271286060 -0.0599349707 -0.0874853678
## 2017-07-31  0.0202278723  0.0236740767  0.1954425985
## 2017-08-31 -0.0072953921  0.0094447148 -0.0390093335
## 2017-09-29 -0.0198260414  0.0208389789  0.0373014023
## 2017-10-31  0.1395154081  0.0582525579  0.0798771993
## 2017-11-30  0.0626577388  0.0046809143 -0.0461006664
## 2017-12-29 -0.0062057977  0.0241716956  0.0230816217
## 2018-01-31  0.2156265512  0.1115967979  0.3422453554
## 2018-02-28  0.0415536373 -0.0573515384  0.0750958655
## 2018-03-29 -0.0440034786 -0.0683058032  0.0135328397
## 2018-04-30  0.0788802990 -0.0141135901  0.0563153181
## 2018-05-31  0.0397392491  0.0643892025  0.1180177507
## 2018-06-29  0.0421636779  0.0278664423  0.1073124992
## 2018-07-31  0.0446635758  0.0871652022 -0.1483892953
## 2018-08-31  0.1243079035  0.0007637357  0.0857955889
## 2018-09-28 -0.0048359762 -0.0205011202  0.0173903683
## 2018-10-31 -0.2258870091 -0.1028991926 -0.2149050722
## 2018-11-30  0.0560700362  0.0162678420 -0.0532520013
## 2018-12-31 -0.1180514840 -0.0552430740 -0.0667287372
## 2019-01-31  0.1348080379  0.0750917624  0.2377564137
## 2019-02-28 -0.0469930675  0.0031748503  0.0533383468
## 2019-03-29  0.0824420113  0.0465716130 -0.0043097707
## 2019-04-30  0.0786806236  0.0128463483  0.0384589024
## 2019-05-31 -0.0818753437 -0.0740704511 -0.0764149876
## 2019-06-28  0.0646557723 -0.0208014127  0.0676869840
## 2019-07-31 -0.0142806642  0.1183225218 -0.1286120898
## 2019-08-30 -0.0496880811 -0.0237704621 -0.0948922666
## 2019-09-30 -0.0229951136  0.0256754913 -0.0931610203
## 2019-10-31  0.0232034025  0.0331681677  0.0713417365
## 2019-11-29  0.0134958246  0.0349733697  0.0905829219
## 2019-12-31  0.0257863460  0.0242708227  0.0279227920
## 2020-01-31  0.0834803057  0.0701849208  0.0643897455
## 2020-02-28 -0.0642332025 -0.0684586737  0.0670726979
## 2020-03-31  0.0344213016 -0.1413300016  0.0173805324
## 2020-04-30  0.2381504772  0.1482720197  0.1116390477
## 2020-05-29 -0.0128673704  0.0578074081 -0.0002858453
## 2020-06-30  0.1218341292 -0.0107722406  0.0807736733
## 2020-07-31  0.1372488981  0.0478934714  0.0717317239
## 2020-08-31  0.0866005697  0.0971010186  0.0799293985
## 2020-09-30 -0.0916533239 -0.1061508537 -0.0573783747
## 2020-10-30 -0.0364089200  0.0980591037 -0.0497966291
## 2020-11-30  0.0425228235  0.0826848099  0.0309615021
## 2020-12-31  0.0276719595 -0.0050446895  0.0970870739
## 2021-01-29 -0.0156985927  0.0467581828 -0.0155437098
## 2021-02-26 -0.0359675611  0.1039617233  0.0120608629
## 2021-03-31  0.0003717143  0.0154771554 -0.0324212116
## 2021-04-30  0.1139202371  0.1527899325 -0.0158244343
## 2021-05-28 -0.0730764730  0.0005973520 -0.0209791874
## 2021-06-30  0.0651836186  0.0385416919  0.0492816124
## 2021-07-30 -0.0332696344  0.0760718620 -0.0203491783
## 2021-08-31  0.0421339383  0.0730045001  0.0950695058
## 2021-09-30 -0.0550034402 -0.0875715239  0.0698019221
## 2021-10-29  0.0262547660  0.1066948595  0.1231245460
## 2021-11-30  0.0391473427 -0.0400331883 -0.0727082035
## 2021-12-31 -0.0505062023  0.0155159060 -0.0634444870
## 2022-01-31 -0.1085098100 -0.0640854511 -0.3438762152
## 2022-02-28  0.0263230081 -0.0059684383 -0.0794420553
## 2022-03-31  0.0596239187  0.0346686656 -0.0518377244
## 2022-04-29 -0.2711856809 -0.1944949821 -0.6769150623
## 2022-05-31 -0.0333130912 -0.0081002690  0.0365177293
## 2022-06-30 -0.1238178220 -0.0417810349 -0.1213919387
## 2022-07-29  0.2394860603  0.0643327907  0.2516130202
## 2022-08-31 -0.0625299199 -0.0663691584 -0.0059760068
## 2022-09-30 -0.1149865786 -0.1268136085  0.0517762870
## 2022-10-31 -0.0981105379 -0.0156179261  0.2148866636
## 2022-11-29 -0.1029338971  0.0082062320 -0.0381304976
cal_component_contribution <- function(.data, w) {
    
        # Covariance of asset returns
    covariance_matrix <- cov(asset_returns_wide_tbl) 
    
    covariance_matrix
    
    # 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.4, 0.3, 0.3)
    
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
    sd_portfolio
    
    
    # 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
    
    rowSums(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 %>% cal_component_contribution(w = c(0.4, 0.3, 0.3))
## # A tibble: 1 × 3
##    AMZN  GOOG  NFLX
##   <dbl> <dbl> <dbl>
## 1 0.375 0.177 0.448

6 Plot: Column Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>% 
    
    cal_component_contribution(w = c(0.4, 0.3, 0.3)) %>%
    
    #Transform to long form
    pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    #Add weights
    add_column(weight = c(.4, 0.3, 0.3)) %>%
    
    #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?

Netflix is the highest contributor in terms of volatility. My risk is fairly spread out as my highest weighted stock has the middle amount of volatility and my other two stocks, which are weighted equally, have both the most and least volatility. Hopefully the difference between those two would offset the average a little bit.