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

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("SOXX", "TKR", "VUG", "AMZN", "GOOG")
prices <- tq_get(x = symbols, 
                 from = "2012-12-31",
                 to = "2017-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
##                     AMZN         GOOG          SOXX          TKR           VUG
## 2013-01-31  0.0566799395  0.066063119  0.0736956541  0.114082142  0.0430334357
## 2013-02-28 -0.0046435024  0.058479459  0.0357686626  0.017230575  0.0083088693
## 2013-03-28  0.0083654162 -0.008787734  0.0230208366  0.040763093  0.0368230497
## 2013-04-30 -0.0487507497  0.037539261  0.0174122233 -0.073509893  0.0159983454
## 2013-05-31  0.0588686246  0.055032502  0.0553678447  0.080705788  0.0172450660
## 2013-06-28  0.0310507506  0.010447926 -0.0004303340 -0.008492491 -0.0216963217
## 2013-07-31  0.0813355350  0.008347749  0.0225705991  0.037318876  0.0542897375
## 2013-08-30 -0.0695574090 -0.047107580 -0.0401998941 -0.037342681 -0.0187174308
## 2013-09-30  0.1067688897  0.033680583  0.0702342071  0.074566456  0.0470973134
## 2013-10-31  0.1521839116  0.162613735  0.0327655006 -0.134288360  0.0416678251
## 2013-11-29  0.0781496860  0.027760470  0.0086744432 -0.015675427  0.0235279362
## 2013-12-31  0.0130490386  0.056080235  0.0488094212  0.061987141  0.0336528591
## 2014-01-31 -0.1059765119  0.052373778 -0.0109244335  0.022622306 -0.0309967602
## 2014-02-28  0.0094619003  0.028942704  0.0649985891  0.073370082  0.0545748068
## 2014-03-31 -0.0737086161 -0.086376026  0.0402397688 -0.026524180 -0.0160270194
## 2014-04-30 -0.1007565303 -0.055956255 -0.0187465048  0.070601620  0.0004273962
## 2014-05-30  0.0273091844  0.061185266  0.0432891808  0.021942954  0.0350914141
## 2014-06-30  0.0383836202  0.027116542  0.0591089517  0.054836959  0.0236358427
## 2014-07-31 -0.0369768154 -0.006417554 -0.0444936283 -0.091839809 -0.0156937494
## 2014-08-29  0.0799468404  0.000000000  0.0621604860  0.027541401  0.0449871859
## 2014-09-30 -0.0502010184  0.010026700 -0.0097161254 -0.066173554 -0.0184983840
## 2014-10-31 -0.0540982347 -0.032173597  0.0034485548  0.014055263  0.0295577402
## 2014-11-28  0.1031187277 -0.031340134  0.0696886848  0.001243055  0.0301274880
## 2014-12-31 -0.0872368614 -0.028890926  0.0021624351 -0.002573332 -0.0095566244
## 2015-01-30  0.1330922557  0.015307741 -0.0498834823 -0.115881672 -0.0153485725
## 2015-02-27  0.0697992426  0.043706308  0.0917339607  0.117114780  0.0605783069
## 2015-03-31 -0.0214295755 -0.018800246 -0.0270949003 -0.008036555 -0.0400888150
## 2015-04-30  0.1253212736 -0.016902286 -0.0129530862 -0.070027035  0.0277167383
## 2015-05-29  0.0175090293 -0.009781027  0.0852589925  0.001594533  0.0146757424
## 2015-06-30  0.0112589814 -0.022041146 -0.0904462244 -0.066894094 -0.0166314395
## 2015-07-31  0.2111621090  0.183918166 -0.0506703705 -0.091271349  0.0321717002
## 2015-08-31 -0.0443525782 -0.011834286 -0.0530147657 -0.042101032 -0.0650267279
## 2015-09-30 -0.0019516837 -0.016027554 -0.0133623161 -0.144070351 -0.0290306413
## 2015-10-30  0.2010808743  0.155539785  0.0945730136  0.139334986  0.0859390131
## 2015-11-30  0.0602956777  0.043752243  0.0228548043  0.028397461  0.0014627934
## 2015-12-31  0.0165440008  0.021686218 -0.0173875082 -0.120150835 -0.0245402791
## 2016-01-29 -0.1410054620 -0.021214957 -0.0788287094 -0.074027508 -0.0619418705
## 2016-02-29 -0.0605352209 -0.062739331  0.0171945729  0.124959459 -0.0040074349
## 2016-03-31  0.0717834363  0.065427847  0.0842683891  0.115732397  0.0695527429
## 2016-04-29  0.1053453760 -0.072272746 -0.0473828593  0.061941369 -0.0079222271
## 2016-05-31  0.0915002899  0.059805082  0.0832943117 -0.061353521  0.0245980907
## 2016-06-30 -0.0099694639 -0.061119209 -0.0109391689 -0.081096328 -0.0066357606
## 2016-07-29  0.0586021229  0.105087543  0.1049331982  0.087093232  0.0467448248
## 2016-08-31  0.0135476418 -0.002265908  0.0460390331  0.020216217 -0.0031211179
## 2016-09-30  0.0848953908  0.013261584  0.0410582398  0.036810056  0.0061454951
## 2016-10-31 -0.0583893058  0.009284187 -0.0145315501 -0.061318411 -0.0262563919
## 2016-11-30 -0.0509721927 -0.034361624  0.0695437183  0.173579808  0.0120860512
## 2016-12-30 -0.0009330556  0.018015256  0.0299243284  0.016508311  0.0116044725
## 2017-01-31  0.0936394059  0.031839802  0.0410304414  0.111888420  0.0346462405
## 2017-02-28  0.0258446800  0.032620079  0.0277666465  0.001338200  0.0429098134
## 2017-03-31  0.0479423007  0.007684128  0.0430886241  0.022372300  0.0129725273
## 2017-04-28  0.0424566944  0.088099785 -0.0057145088  0.065298938  0.0225128792
## 2017-05-31  0.0725778018  0.062987824  0.0836815541 -0.039001731  0.0276619220
## 2017-06-30 -0.0271286156 -0.059934898 -0.0516122898  0.002164811 -0.0042531136
## 2017-07-31  0.0202278808  0.023674019  0.0472267496 -0.016349121  0.0251071762
## 2017-08-31 -0.0072953953  0.009444726  0.0286374222 -0.008133783  0.0116766114
## 2017-09-29 -0.0198260355  0.020839091  0.0505265472  0.079270816  0.0102948966
## 2017-10-31  0.1395154056  0.058252416  0.0854756648 -0.029260621  0.0285162228
## 2017-11-30  0.0626577318  0.004680975 -0.0009271588  0.062725425  0.0239437904
## 2017-12-29 -0.0062057845  0.024171632 -0.0142352441 -0.015144082  0.0087060393
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AMZN         GOOG         SOXX          TKR          VUG
## AMZN 0.0054660218 0.0023733627 0.0012543585 0.0003269421 0.0014118587
## GOOG 0.0023733627 0.0028612925 0.0009434078 0.0001886207 0.0008644433
## SOXX 0.0012543585 0.0009434078 0.0022221911 0.0018435164 0.0010305121
## TKR  0.0003269421 0.0001886207 0.0018435164 0.0052061441 0.0011040162
## VUG  0.0014118587 0.0008644433 0.0010305121 0.0011040162 0.0009277446
# 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.04173187
# 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        SOXX         TKR         VUG
## [1,] 0.01448109 0.009713882 0.007024053 0.007903909 0.002608931
rowSums(component_contribution)
## [1] 0.04173187
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AMZN  GOOG  SOXX   TKR   VUG
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.347 0.233 0.168 0.189 0.063
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.347
## 2 GOOG         0.233
## 3 SOXX         0.168
## 4 TKR          0.189
## 5 VUG          0.063
# 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          SOXX          TKR           VUG
## 2013-01-31  0.0566799395  0.066063119  0.0736956541  0.114082142  0.0430334357
## 2013-02-28 -0.0046435024  0.058479459  0.0357686626  0.017230575  0.0083088693
## 2013-03-28  0.0083654162 -0.008787734  0.0230208366  0.040763093  0.0368230497
## 2013-04-30 -0.0487507497  0.037539261  0.0174122233 -0.073509893  0.0159983454
## 2013-05-31  0.0588686246  0.055032502  0.0553678447  0.080705788  0.0172450660
## 2013-06-28  0.0310507506  0.010447926 -0.0004303340 -0.008492491 -0.0216963217
## 2013-07-31  0.0813355350  0.008347749  0.0225705991  0.037318876  0.0542897375
## 2013-08-30 -0.0695574090 -0.047107580 -0.0401998941 -0.037342681 -0.0187174308
## 2013-09-30  0.1067688897  0.033680583  0.0702342071  0.074566456  0.0470973134
## 2013-10-31  0.1521839116  0.162613735  0.0327655006 -0.134288360  0.0416678251
## 2013-11-29  0.0781496860  0.027760470  0.0086744432 -0.015675427  0.0235279362
## 2013-12-31  0.0130490386  0.056080235  0.0488094212  0.061987141  0.0336528591
## 2014-01-31 -0.1059765119  0.052373778 -0.0109244335  0.022622306 -0.0309967602
## 2014-02-28  0.0094619003  0.028942704  0.0649985891  0.073370082  0.0545748068
## 2014-03-31 -0.0737086161 -0.086376026  0.0402397688 -0.026524180 -0.0160270194
## 2014-04-30 -0.1007565303 -0.055956255 -0.0187465048  0.070601620  0.0004273962
## 2014-05-30  0.0273091844  0.061185266  0.0432891808  0.021942954  0.0350914141
## 2014-06-30  0.0383836202  0.027116542  0.0591089517  0.054836959  0.0236358427
## 2014-07-31 -0.0369768154 -0.006417554 -0.0444936283 -0.091839809 -0.0156937494
## 2014-08-29  0.0799468404  0.000000000  0.0621604860  0.027541401  0.0449871859
## 2014-09-30 -0.0502010184  0.010026700 -0.0097161254 -0.066173554 -0.0184983840
## 2014-10-31 -0.0540982347 -0.032173597  0.0034485548  0.014055263  0.0295577402
## 2014-11-28  0.1031187277 -0.031340134  0.0696886848  0.001243055  0.0301274880
## 2014-12-31 -0.0872368614 -0.028890926  0.0021624351 -0.002573332 -0.0095566244
## 2015-01-30  0.1330922557  0.015307741 -0.0498834823 -0.115881672 -0.0153485725
## 2015-02-27  0.0697992426  0.043706308  0.0917339607  0.117114780  0.0605783069
## 2015-03-31 -0.0214295755 -0.018800246 -0.0270949003 -0.008036555 -0.0400888150
## 2015-04-30  0.1253212736 -0.016902286 -0.0129530862 -0.070027035  0.0277167383
## 2015-05-29  0.0175090293 -0.009781027  0.0852589925  0.001594533  0.0146757424
## 2015-06-30  0.0112589814 -0.022041146 -0.0904462244 -0.066894094 -0.0166314395
## 2015-07-31  0.2111621090  0.183918166 -0.0506703705 -0.091271349  0.0321717002
## 2015-08-31 -0.0443525782 -0.011834286 -0.0530147657 -0.042101032 -0.0650267279
## 2015-09-30 -0.0019516837 -0.016027554 -0.0133623161 -0.144070351 -0.0290306413
## 2015-10-30  0.2010808743  0.155539785  0.0945730136  0.139334986  0.0859390131
## 2015-11-30  0.0602956777  0.043752243  0.0228548043  0.028397461  0.0014627934
## 2015-12-31  0.0165440008  0.021686218 -0.0173875082 -0.120150835 -0.0245402791
## 2016-01-29 -0.1410054620 -0.021214957 -0.0788287094 -0.074027508 -0.0619418705
## 2016-02-29 -0.0605352209 -0.062739331  0.0171945729  0.124959459 -0.0040074349
## 2016-03-31  0.0717834363  0.065427847  0.0842683891  0.115732397  0.0695527429
## 2016-04-29  0.1053453760 -0.072272746 -0.0473828593  0.061941369 -0.0079222271
## 2016-05-31  0.0915002899  0.059805082  0.0832943117 -0.061353521  0.0245980907
## 2016-06-30 -0.0099694639 -0.061119209 -0.0109391689 -0.081096328 -0.0066357606
## 2016-07-29  0.0586021229  0.105087543  0.1049331982  0.087093232  0.0467448248
## 2016-08-31  0.0135476418 -0.002265908  0.0460390331  0.020216217 -0.0031211179
## 2016-09-30  0.0848953908  0.013261584  0.0410582398  0.036810056  0.0061454951
## 2016-10-31 -0.0583893058  0.009284187 -0.0145315501 -0.061318411 -0.0262563919
## 2016-11-30 -0.0509721927 -0.034361624  0.0695437183  0.173579808  0.0120860512
## 2016-12-30 -0.0009330556  0.018015256  0.0299243284  0.016508311  0.0116044725
## 2017-01-31  0.0936394059  0.031839802  0.0410304414  0.111888420  0.0346462405
## 2017-02-28  0.0258446800  0.032620079  0.0277666465  0.001338200  0.0429098134
## 2017-03-31  0.0479423007  0.007684128  0.0430886241  0.022372300  0.0129725273
## 2017-04-28  0.0424566944  0.088099785 -0.0057145088  0.065298938  0.0225128792
## 2017-05-31  0.0725778018  0.062987824  0.0836815541 -0.039001731  0.0276619220
## 2017-06-30 -0.0271286156 -0.059934898 -0.0516122898  0.002164811 -0.0042531136
## 2017-07-31  0.0202278808  0.023674019  0.0472267496 -0.016349121  0.0251071762
## 2017-08-31 -0.0072953953  0.009444726  0.0286374222 -0.008133783  0.0116766114
## 2017-09-29 -0.0198260355  0.020839091  0.0505265472  0.079270816  0.0102948966
## 2017-10-31  0.1395154056  0.058252416  0.0854756648 -0.029260621  0.0285162228
## 2017-11-30  0.0626577318  0.004680975 -0.0009271588  0.062725425  0.0239437904
## 2017-12-29 -0.0062057845  0.024171632 -0.0142352441 -0.015144082  0.0087060393
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
##    AMZN  GOOG  SOXX   TKR   VUG
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.347 0.233 0.168 0.189 0.063
plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform to long from 
    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: Column Chart of Component Contribution and Weight

plot_data_weights <- asset_returns_wide_tbl %>%
    calculate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform to long from
    pivot_longer(cols = everything(), names_to = "asset", values_to = "Contribution") %>%
    
    # Add weights
    add_column(weight = c(.25, .25, .2, .2, .1)) %>%
    
    # Transform to long
    pivot_longer(cols = c(Contribution, weight), names_to = "type", values_to = "value")

plot_data_weights %>%
    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_tq() +
    theme(plot.title = element_text(hjust = 0.5)) +
    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?

AMZN is the largest contributor to portfolio volatility, with VUG having the smallest impact. AMZN has a disproportionately large impact compared to weight, and is the only one with a larger contribution than weight!