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

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2012-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 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          GOOG         TSLA
## 2013-01-31 -1.555887e-01  0.0660632080  0.102078114
## 2013-02-28 -2.561145e-02  0.0584793511 -0.074128640
## 2013-03-28  2.850702e-03 -0.0087878418  0.084208138
## 2013-04-30  2.710086e-04  0.0375393327  0.354111531
## 2013-05-31  2.217154e-02  0.0550324391  0.593716684
## 2013-06-28 -1.258956e-01  0.0104477434  0.093672163
## 2013-07-31  1.321023e-01  0.0083479056  0.223739522
## 2013-08-30  8.044304e-02 -0.0471074867  0.229971642
## 2013-09-30 -2.172372e-02  0.0336806370  0.134706620
## 2013-10-31  9.201534e-02  0.1626137505 -0.189806595
## 2013-11-29  6.770788e-02  0.0277602928 -0.228409405
## 2013-12-31  8.862634e-03  0.0560803365  0.167108541
## 2014-01-31 -1.139489e-01  0.0523737609  0.187261714
## 2014-02-28  5.591751e-02  0.0289427614  0.299722785
## 2014-03-31  1.975673e-02 -0.0863761364 -0.160783242
## 2014-04-30  9.476110e-02 -0.0559562015 -0.002690122
## 2014-05-30  7.576512e-02  0.0611851953 -0.000577422
## 2014-06-30  2.728615e-02  0.0271165418  0.144457224
## 2014-07-31  2.832627e-02 -0.0064175087 -0.072372676
## 2014-08-29  7.465200e-02  0.0000000000  0.188794007
## 2014-09-30 -1.722056e-02  0.0100265748 -0.105566477
## 2014-10-31  6.948877e-02 -0.0321734004 -0.004046477
## 2014-11-28  1.007308e-01 -0.0313402562  0.011599801
## 2014-12-31 -7.460627e-02 -0.0288909396 -0.094774519
## 2015-01-30  5.961153e-02  0.0153077595 -0.088365289
## 2015-02-27  9.601603e-02  0.0437064112 -0.001277808
## 2015-03-31 -3.187407e-02 -0.0188002433 -0.074350051
## 2015-04-30  5.769401e-03 -0.0169025049  0.180226808
## 2015-05-29  4.434131e-02 -0.0097808555  0.103899574
## 2015-06-30 -3.793804e-02 -0.0220411324  0.067300935
## 2015-07-31 -3.348109e-02  0.1839181120 -0.007896616
## 2015-08-31 -6.848869e-02 -0.0118342682 -0.066366250
## 2015-09-30 -2.205802e-02 -0.0160275013 -0.002653519
## 2015-10-30  8.011270e-02  0.1555397655 -0.182659777
## 2015-11-30 -5.821273e-03  0.0437523876  0.106828586
## 2015-12-31 -1.167903e-01  0.0216860981  0.041471519
## 2016-01-29 -7.822355e-02 -0.0212149455 -0.227360626
## 2016-02-29 -1.288098e-03 -0.0627391814  0.003810669
## 2016-03-31  1.197458e-01  0.0654275972  0.179948109
## 2016-04-29 -1.507311e-01 -0.0722726737  0.046721797
## 2016-05-31  6.931423e-02  0.0598051273 -0.075597968
## 2016-06-30 -4.359645e-02 -0.0611191519 -0.050296440
## 2016-07-29  8.623536e-02  0.1050873811  0.100785334
## 2016-08-31  2.337658e-02 -0.0022658095 -0.102058091
## 2016-09-30  6.344828e-02  0.0132614510 -0.038366372
## 2016-10-31  4.324864e-03  0.0092841259 -0.031364583
## 2016-11-30 -2.183779e-02 -0.0343614231 -0.043041267
## 2016-12-30  4.684088e-02  0.0180151860  0.120665178
## 2017-01-31  4.664166e-02  0.0318398148  0.164624916
## 2017-02-28  1.255552e-01  0.0326201551 -0.007730364
## 2017-03-31  4.754174e-02  0.0076841334  0.107278727
## 2017-04-28 -6.977553e-05  0.0880997072  0.120916212
## 2017-05-31  6.560735e-02  0.0629878514  0.082295892
## 2017-06-30 -5.891576e-02 -0.0599349813  0.058654468
## 2017-07-31  3.218009e-02  0.0236740764 -0.111459860
## 2017-08-31  1.016531e-01  0.0094447320  0.095543446
## 2017-09-29 -6.213462e-02  0.0208389627 -0.042474144
## 2017-10-31  9.240386e-02  0.0582525619 -0.028457409
## 2017-11-30  2.007500e-02  0.0046809278 -0.070862541
## 2017-12-29 -1.536336e-02  0.0241716816  0.008061928
## 2018-01-31 -1.069327e-02  0.1115967977  0.129254571
## 2018-02-28  6.596085e-02 -0.0573515363 -0.032266877
## 2018-03-29 -5.980401e-02 -0.0683057911 -0.253920408
## 2018-04-30 -1.513365e-02 -0.0141136013  0.099254576
## 2018-05-31  1.267421e-01  0.0643892104 -0.031698139
## 2018-06-29 -9.463004e-03  0.0278664412  0.186043257
## 2018-07-31  2.759894e-02  0.0871651977 -0.140021491
## 2018-08-31  1.826734e-01  0.0007637383  0.011737389
## 2018-09-28 -8.337558e-03 -0.0205011212 -0.130439038
## 2018-10-31 -3.095163e-02 -0.1028991940  0.242170576
## 2018-11-30 -1.999127e-01  0.0162678497  0.038271580
## 2018-12-31 -1.240883e-01 -0.0552430908 -0.051761952
## 2019-01-31  5.368659e-02  0.0750917633 -0.080628789
## 2019-02-28  4.380292e-02  0.0031748658  0.041032981
## 2019-03-29  9.260274e-02  0.0465716000 -0.133656413
## 2019-04-30  5.490095e-02  0.0128463508 -0.159123803
## 2019-05-31 -1.326323e-01 -0.0740704388 -0.253945372
## 2019-06-28  1.226770e-01 -0.0208014268  0.188012109
## 2019-07-31  7.361703e-02  0.1183225212  0.078092373
## 2019-08-30 -1.659831e-02 -0.0237704598 -0.068516948
## 2019-09-30  7.042257e-02  0.0256754911  0.065449565
## 2019-10-31  1.049765e-01  0.0331681683  0.268061253
## 2019-11-29  7.469361e-02  0.0349733745  0.046592176
## 2019-12-31  9.420416e-02  0.0242708200  0.237359743
## 2020-01-31  5.260184e-02  0.0701849229  0.441578342
## 2020-02-28 -1.218304e-01 -0.0684586699  0.026424253
## 2020-03-31 -7.231408e-02 -0.1413300045 -0.242781458
## 2020-04-30  1.444237e-01  0.1482720126  0.400209535
## 2020-05-29  8.166695e-02  0.0578074152  0.065730500
## 2020-06-30  1.374866e-01 -0.0107722396  0.257108657
## 2020-07-31  1.528340e-01  0.0478934632  0.281420674
## 2020-08-31  1.960349e-01  0.0971010192  0.554719320
## 2020-09-30 -1.081716e-01 -0.1061508444 -0.149762306
## 2020-10-30 -6.188808e-02  0.0980590984 -0.100371771
## 2020-11-30  9.120451e-02  0.0826848054  0.380308519
## 2020-12-31  1.084722e-01 -0.0050446871  0.217730753
## 2021-01-29 -5.516731e-03  0.0467581807  0.117343701
## 2021-02-26 -8.306882e-02  0.1039617312 -0.161038203
## 2021-03-31  7.312803e-03  0.0154771551 -0.011269836
## 2021-04-30  7.345307e-02  0.1527899272  0.060292565
## 2021-05-28 -5.181680e-02  0.0005973539 -0.126372343
## 2021-06-30  9.450024e-02  0.0385416917  0.083547955
## 2021-07-30  6.295807e-02  0.0760718632  0.010973846
## 2021-08-31  4.161138e-02  0.0730044972  0.068224268
## 2021-09-30 -7.046179e-02 -0.0875715218  0.052632612
## 2021-10-29  5.700133e-02  0.1066948571  0.362230202
## 2021-11-30  9.991941e-02 -0.0400331863  0.027237848
## 2021-12-31  7.160300e-02  0.0155159098 -0.079968440
## 2022-01-31 -1.583704e-02 -0.0640854569 -0.120597475
## 2022-02-28 -5.558223e-02 -0.0059684318 -0.073397011
## 2022-03-31  5.588250e-02  0.0346686594  0.213504290
## 2022-04-29 -1.021776e-01 -0.1944949765 -0.213125289
## 2022-05-31 -5.603748e-02 -0.0081002677 -0.138340062
## 2022-06-30 -8.493662e-02 -0.0417810430 -0.118657126
## 2022-07-29  1.728043e-01  0.0643327981  0.280480149
## 2022-08-31 -3.170519e-02 -0.0663691661 -0.075250271
## 2022-09-30 -1.289443e-01 -0.1268136091 -0.038313992
## 2022-10-31  1.039557e-01 -0.0156179247 -0.153346760
## 2022-11-30 -3.358518e-02  0.0692744730 -0.155866121
## 2022-12-30 -1.304192e-01 -0.1339679475 -0.457813194
## 2023-01-31  1.048293e-01  0.1182712791  0.340915767
## 2023-02-28  2.291843e-02 -0.1007318735  0.171904973
## 2023-03-31  1.121214e-01  0.1412534049  0.008471140
## 2023-04-28  2.857495e-02  0.0397753044 -0.233183710
## 2023-05-31  4.502911e-02  0.1310217889  0.216021889
## 2023-06-30  9.014247e-02 -0.0196454013  0.249689452
## 2023-07-31  1.270439e-02  0.0956332676  0.021391609
## 2023-08-31 -4.330826e-02  0.0313565983 -0.035588260
## 2023-09-29 -9.285934e-02 -0.0408674979 -0.030929027
## 2023-10-31 -2.573260e-03 -0.0509540728 -0.219831983
## 2023-11-30  1.077598e-01  0.0665317068  0.178463656
## 2023-12-01  6.768349e-03 -0.0044902847 -0.005220199
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             AAPL        GOOG        TSLA
## AAPL 0.006361280 0.002267303 0.005640621
## GOOG 0.002267303 0.004398953 0.003821751
## TSLA 0.005640621 0.003821751 0.029513390
# 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.30, 0.30, 0.40)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.09148578
# 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      GOOG       TSLA
## [1,] 0.01588714 0.0115709 0.06402773
rowSums(component_contribution)
## [1] 0.09148578
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 3
##    AAPL  GOOG  TSLA
##   <dbl> <dbl> <dbl>
## 1 0.174 0.126   0.7
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.174
## 2 GOOG         0.126
## 3 TSLA         0.7
calculate_component_contribution <- function(asset_returns_wide_tbl, w) {

    # Covariance of asset returns
    covariance_matrix <- cov(asset_returns_wide_tbl)
    
    # Standard deviation of portfolio
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

    # Component contribution
    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)
    
}

6 Plot: Colum Chart of Component Contribution and Weight

# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.30,0.30,0.40)) %>%
    gather(key = "asset", value = "contribution") %>%
    add_column(weights = c(0.30,0.30,0.40)) %>%
    pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%

    ggplot(aes(asset, value, fill = type)) +
    geom_col(position = "dodge") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    theme_tq() +
    scale_fill_tq() +

    labs(title = "Percent Contribution to Volatility",
         y = "percent",
         x = "asset") 

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?

Tesla is the largest contributor to my portfolio volatility. My portfolio risk is concentrated highly on TSLA, as it has 40% of the weight but contributes to 70% of the portfolio.