# 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("AMZN", "AAPL", "NFLX", "BA", "DELL")

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

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

Refresh your memory on covariance with this video. Click this link Refresh your memory on matrix multiplication. Click this link

# 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          AMZN           BA          DELL          NFLX
## 2013-01-31 -1.555892e-01  0.0566799395 -0.019969949            NA  0.5792178072
## 2013-02-28 -2.561138e-02 -0.0046435024  0.046601939            NA  0.1294683790
## 2013-03-28  2.850562e-03  0.0083654162  0.110095674            NA  0.0063600708
## 2013-04-30  2.709386e-04 -0.0487507497  0.062753170            NA  0.1323750202
## 2013-05-31  2.217203e-02  0.0588686246  0.085096687            NA  0.0460381788
## 2013-06-28 -1.258958e-01  0.0310507506  0.033955624            NA -0.0693560587
## 2013-07-31  1.321021e-01  0.0813355350  0.025635077            NA  0.1468488828
## 2013-08-30  8.044261e-02 -0.0695574090 -0.006749785            NA  0.1495238153
## 2013-09-30 -2.172315e-02  0.1067688897  0.122816942            NA  0.0853633452
## 2013-10-31  9.201505e-02  0.1521839116  0.104934858            NA  0.0420204523
## 2013-11-29  6.770866e-02  0.0781496860  0.031968188            NA  0.1260456325
## 2013-12-31  8.861869e-03  0.0130490386  0.016547578            NA  0.0064580908
## 2014-01-31 -1.139490e-01 -0.1059765119 -0.085859577            NA  0.1059769064
## 2014-02-28  5.591782e-02  0.0094619003  0.034424612            NA  0.0849673396
## 2014-03-31  1.975639e-02 -0.0737086161 -0.026966000            NA -0.2357726219
## 2014-04-30  9.476103e-02 -0.1007565303  0.027741407            NA -0.0890406532
## 2014-05-30  7.576563e-02  0.0273091844  0.052752842            NA  0.2603988640
## 2014-06-30  2.728624e-02  0.0383836202 -0.061128359            NA  0.0530627854
## 2014-07-31  2.832591e-02 -0.0369768154 -0.054512688            NA -0.0414273713
## 2014-08-29  7.465193e-02  0.0799468404  0.057165138            NA  0.1221472352
## 2014-09-30 -1.722056e-02 -0.0502010184  0.004563589            NA -0.0569909207
## 2014-10-31  6.948926e-02 -0.0540982347 -0.019581225            NA -0.1386421470
## 2014-11-28  1.007306e-01  0.1031187277  0.078747200            NA -0.1250817171
## 2014-12-31 -7.460643e-02 -0.0872368614 -0.033142251            NA -0.0144727330
## 2015-01-30  5.961205e-02  0.1330922557  0.111901739            NA  0.2571875124
## 2015-02-27  9.601567e-02  0.0697992426  0.043169824            NA  0.0722680160
## 2015-03-31 -3.187407e-02 -0.0214295755 -0.005117847            NA -0.1307827695
## 2015-04-30  5.769333e-03  0.1253212736 -0.045948900            NA  0.2893246523
## 2015-05-29  4.434144e-02  0.0175090293 -0.013481606            NA  0.1145793430
## 2015-06-30 -3.793810e-02  0.0112589814 -0.012892364            NA  0.0513461801
## 2015-07-31 -3.348109e-02  0.2111621090  0.038535711            NA  0.1972314899
## 2015-08-31 -6.848876e-02 -0.0443525782 -0.091881861            NA  0.0062789186
## 2015-09-30 -2.205802e-02 -0.0019516837  0.002064089            NA -0.1079428532
## 2015-10-30  8.011256e-02  0.2010808743  0.122869652            NA  0.0483934447
## 2015-11-30 -5.821062e-03  0.0602956777 -0.011689082            NA  0.1292201596
## 2015-12-31 -1.167903e-01  0.0165440008 -0.005930306            NA -0.0753374900
## 2016-01-29 -7.822364e-02 -0.1410054620 -0.185327485            NA -0.2194783218
## 2016-02-29 -1.288443e-03 -0.0605352209 -0.007154290            NA  0.0169505524
## 2016-03-31  1.197462e-01  0.0717834363  0.071505722            NA  0.0902267645
## 2016-04-29 -1.507309e-01  0.1053453760  0.060077592            NA -0.1270822750
## 2016-05-31  6.931413e-02  0.0915002899 -0.058196872            NA  0.1304025548
## 2016-06-30 -4.359653e-02 -0.0099694639  0.029062308            NA -0.1144250835
## 2016-07-29  8.623552e-02  0.0586021229  0.028765445            NA -0.0025174134
## 2016-08-31  2.337642e-02  0.0135476418 -0.023751875            NA  0.0657364022
## 2016-09-30  6.344807e-02  0.0848953908  0.017535843  0.0674995991  0.0112246699
## 2016-10-31  4.325083e-03 -0.0583893058  0.078020196  0.0266297988  0.2367091536
## 2016-11-30 -2.183771e-02 -0.0509721927  0.063160775  0.0871470938 -0.0650992833
## 2016-12-30  4.684095e-02 -0.0009330556  0.033441199  0.0259850791  0.0564934501
## 2017-01-31  4.664172e-02  0.0936394059  0.048520839  0.1361883707  0.1280336976
## 2017-02-28  1.255551e-01  0.0258446800  0.106475437  0.0079065249  0.0100410836
## 2017-03-31  4.754151e-02  0.0479423007 -0.018875371  0.0092495794  0.0391854826
## 2017-04-28 -6.988918e-05  0.0424566944  0.044078039  0.0462010058  0.0292677767
## 2017-05-31  6.560736e-02  0.0725778018  0.022661775  0.0334096925  0.0689841760
## 2017-06-30 -5.891555e-02 -0.0271286156  0.052531574 -0.1270672503 -0.0874853720
## 2017-07-31  3.218031e-02  0.0202278808  0.203833152  0.0504173387  0.1954425989
## 2017-08-31  1.016532e-01 -0.0072953953 -0.005668811  0.1534614697 -0.0390093333
## 2017-09-29 -6.213502e-02 -0.0198260355  0.058939491  0.0299746314  0.0373014045
## 2017-10-31  9.240378e-02  0.1395154056  0.014721324  0.0695367425  0.0798771971
## 2017-11-30  2.007509e-02  0.0626577318  0.075774587 -0.0562846239 -0.0461006656
## 2017-12-29 -1.536317e-02 -0.0062057845  0.063374875  0.0381188297  0.0230816211
## 2018-01-31 -1.069346e-02  0.2156265497  0.183671320 -0.1254093498  0.3422453562
## 2018-02-28  6.596113e-02  0.0415536279  0.026806721  0.0354857673  0.0750958659
## 2018-03-29 -5.980381e-02 -0.0440034760 -0.099576390 -0.0146442027  0.0135328404
## 2018-04-30 -1.513384e-02  0.0788803060  0.017175192 -0.0198654870  0.0563153192
## 2018-05-31  1.267419e-01  0.0397392430  0.059238873  0.1167762226  0.1180177495
## 2018-06-29 -9.462919e-03  0.0421636787 -0.048433870  0.0474549318  0.1073124981
## 2018-07-31  2.759911e-02  0.0446635734  0.060121339  0.0897270267 -0.1483892939
## 2018-08-31  1.826732e-01  0.1243079079 -0.033725945  0.0386925890  0.0857955874
## 2018-09-28 -8.337699e-03 -0.0048359814  0.081506865  0.0098299283  0.0173903689
## 2018-10-31 -3.095141e-02 -0.2258869989 -0.046901721 -0.0718137349 -0.2149050726
## 2018-11-30 -1.999127e-01  0.0560700324 -0.018483266  0.1543877724 -0.0532520010
## 2018-12-31 -1.240886e-01 -0.1180514843 -0.072529663 -0.1782432889 -0.0667287380
## 2019-01-31  5.368679e-02  0.1348080312  0.178749177 -0.0057457752  0.2377564149
## 2019-02-28  4.380329e-02 -0.0469930640  0.136842584  0.1387142937  0.0533383470
## 2019-03-29  9.260229e-02  0.0824420184 -0.142782563  0.0501372080 -0.0043097723
## 2019-04-30  5.490119e-02  0.0786806224 -0.009827463  0.1385240707  0.0384589024
## 2019-05-31 -1.326325e-01 -0.0818753491 -0.094675438 -0.1239772153 -0.0764149871
## 2019-06-28  1.226770e-01  0.0646557767  0.063511708 -0.1589199394  0.0676869856
## 2019-07-31  7.361680e-02 -0.0142806686 -0.064771221  0.1280539375 -0.1286120899
## 2019-08-30 -1.659809e-02 -0.0496880810  0.071211574 -0.1137861536 -0.0948922673
## 2019-09-30  7.042271e-02 -0.0229951159  0.044006196  0.0063837142 -0.0931610198
## 2019-10-31  1.049765e-01  0.0232034080 -0.112726399  0.0196664296  0.0713417349
## 2019-11-29  7.469361e-02  0.0134958216  0.080262991 -0.0868565930  0.0905829220
## 2019-12-31  9.420415e-02  0.0257863501 -0.116964019  0.0580860151  0.0279227940
## 2020-01-31  5.260173e-02  0.0834803026 -0.023260958 -0.0523281799  0.0643897455
## 2020-02-28 -1.218305e-01 -0.0642332026 -0.139796995 -0.1868016335  0.0670726963
## 2020-03-31 -7.231409e-02  0.0344213022 -0.612285506 -0.0227480644  0.0173805330
## 2020-04-30  1.444238e-01  0.2381504762 -0.055983703  0.0763989596  0.1116390480
## 2020-05-29  8.166685e-02 -0.0128673719  0.033676984  0.1508323295 -0.0002858456
## 2020-06-30  1.374866e-01  0.1218341331  0.228545433  0.1014446864  0.0807736724
## 2020-07-31  1.528340e-01  0.1372488933 -0.148529138  0.0852655164  0.0717317246
## 2020-08-31  1.960348e-01  0.0866005735  0.083852427  0.0993591076  0.0799293979
## 2020-09-30 -1.081712e-01 -0.0916533253 -0.038927502  0.0240720902 -0.0573783743
## 2020-10-30 -6.188842e-02 -0.0364089187 -0.135001990 -0.1162699140 -0.0497966282
## 2020-11-30  9.120465e-02  0.0425228214  0.377964845  0.1358726270  0.0309615020
## 2020-12-31  1.084720e-01  0.0276719582  0.015773526  0.0598830014  0.0970870744
## 2021-01-29 -5.516614e-03 -0.0156985929 -0.097419264 -0.0054725239 -0.0155437114
## 2021-02-26 -8.306888e-02 -0.0359675607  0.087796344  0.1063613149  0.0120608627
## 2021-03-31  7.312867e-03  0.0003717151  0.183531490  0.0837269294 -0.0324212105
## 2021-04-30  7.345284e-02  0.1139202399 -0.083519893  0.1092892726 -0.0158244352
## 2021-05-28 -5.181669e-02 -0.0730764715  0.052824308  0.0031476350 -0.0209791877
## 2021-06-30  9.450048e-02  0.0651836137 -0.030665425  0.0103879902  0.0492816132
## 2021-07-30  6.295806e-02 -0.0332696301 -0.056147275 -0.0310789579 -0.0203491777
## 2021-08-31  4.161127e-02  0.0421339363 -0.031304389  0.0086562816  0.0950695060
## 2021-09-30 -7.046168e-02 -0.0550034409  0.002002560  0.0653333383  0.0698019211
## 2021-10-29  5.700133e-02  0.0262547651 -0.060491089  0.0556140061  0.1231245456
## 2021-11-30  9.991940e-02  0.0391473463 -0.045354506  0.0128753228 -0.0727082022
## 2021-12-31  7.160282e-02 -0.0505062029  0.017386520 -0.0053268165 -0.0634444870
## 2022-01-31 -1.583695e-02 -0.1085098142 -0.005379044  0.0113297047 -0.3438762168
## 2022-02-28 -5.558223e-02  0.0263230079  0.025150449 -0.1086715122 -0.0794420544
## 2022-03-31  5.588250e-02  0.0596239190 -0.069779315 -0.0152250565 -0.0518377235
## 2022-04-29 -1.021777e-01 -0.2711856801 -0.252015930 -0.0584750053 -0.6769150653
## 2022-05-31 -5.603728e-02 -0.0333130879 -0.124625819  0.0604619408  0.0365177336
## 2022-06-30 -8.493684e-02 -0.1238178226  0.039688988 -0.0776261156 -0.1213919403
## 2022-07-29  1.728044e-01  0.2394860591  0.152916927 -0.0174792506  0.2516130190
## 2022-08-31 -3.170519e-02 -0.0625299224  0.005883122 -0.1628060259 -0.0059760083
## 2022-09-30 -1.289443e-01 -0.1149865758 -0.280283597 -0.1138409069  0.0517762908
## 2022-10-31  1.039557e-01 -0.0981105335  0.162960635  0.1263292285  0.2148866613
## 2022-11-30 -3.358518e-02 -0.0593198454  0.227303083  0.1539273261  0.0457051938
## 2022-12-30 -1.304192e-01 -0.1391406409  0.062884511 -0.1076203758 -0.0354794590
## 2023-01-31  1.048293e-01  0.2051735029  0.111692437  0.0179695009  0.1823328028
## 2023-02-28  2.291843e-02 -0.0902516639 -0.055254661  0.0004922475 -0.0939461074
## 2023-03-31  1.121214e-01  0.0918019370  0.052574983 -0.0106370421  0.0699795803
## 2023-04-28  2.857495e-02  0.0206963031 -0.026957068  0.0870351173 -0.0460542977
## 2023-05-31  4.502911e-02  0.1340765703 -0.005236638  0.0299002525  0.1805874132
## 2023-06-30  9.014247e-02  0.0779864104  0.026197375  0.1885876677  0.1084198198
## 2023-07-31  1.270439e-02  0.0251489708  0.123219608 -0.0153056369 -0.0034566451
## 2023-08-31 -4.330826e-02  0.0318772770 -0.064055799  0.0608470256 -0.0121241583
## 2023-09-29 -9.285934e-02 -0.0821945627 -0.155952689  0.2030278952 -0.1384714676
## 2023-10-31 -2.573260e-03  0.0458940197 -0.025681651 -0.0236781459  0.0864351944
## 2023-11-30  1.077598e-01  0.0931972815  0.214995666  0.1256728980  0.1408733862
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             AAPL        AMZN          BA DELL        NFLX
## AAPL 0.006409116 0.003574357 0.002640537   NA 0.002946398
## AMZN 0.003574357 0.007560841 0.002739863   NA 0.006728813
## BA   0.002640537 0.002739863 0.011868820   NA 0.004294981
## DELL          NA          NA          NA   NA          NA
## NFLX 0.002946398 0.006728813 0.004294981   NA 0.018318262
# 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,]   NA
# 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 AMZN BA DELL NFLX
## [1,]   NA   NA NA   NA   NA
rowSums(component_contribution)
## [1] NA
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AAPL  AMZN    BA  DELL  NFLX
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1    NA    NA    NA    NA    NA
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL            NA
## 2 AMZN            NA
## 3 BA              NA
## 4 DELL            NA
## 5 NFLX            NA

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")

# Custom function
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)

}

asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 5
##    AAPL  AMZN    BA  DELL  NFLX
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1    NA    NA    NA    NA    NA

5 Visualizing Component Contribution

# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%

    ggplot(aes(asset, contribution)) +
    geom_col(fill = "cornflowerblue") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    
    labs(title = "Percent Contribution to Portfolio Standard Deviation",
         y = "Percent Contribution to Risk",
         x = NULL)

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

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%
    add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
    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") 

6 Rolling Component Contribution

calculate_comp_contrib_by_window <- function(asset_returns_wide_tbl,
                                             start = 1,
                                             window = 24,
                                             weights) {

    # 1 Define start date
    start_date <- rownames(asset_returns_wide_tbl)[start]

    # 2 Define end date
    end_date <- rownames(asset_returns_wide_tbl)[start + window]

    # 3 Subset df
    df_subset <- asset_returns_wide_tbl %>%

        rownames_to_column(var = "date") %>%

        filter(date >= start_date & date < end_date) %>%

        column_to_rownames(var = "date")

    # 4 Calculate component contribution
    component_percentages <-df_subset %>%
        calculate_component_contribution(w = weights)

    # 5 Add end date to df
    component_percentages %>%

        mutate(date = ymd(end_date)) %>%
        select(date, everything())

}


# Check the custom function
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 1, window = 24,
                                                            w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 6
##   date        AAPL  AMZN    BA  DELL  NFLX
##   <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-01-30    NA    NA    NA    NA    NA
asset_returns_wide_tbl %>% calculate_comp_contrib_by_window(start = 2, window = 24,
                                                            w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 6
##   date        AAPL  AMZN    BA  DELL  NFLX
##   <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-02-27    NA    NA    NA    NA    NA
dump(list = c("calculate_component_contribution",
              "calculate_comp_contrib_by_window"),
     file = "../00_scripts/calculate_comp_contrib_to_portfolio_volatility.R")
# Iterate the custom function
w <- c(0.25,0.25,0.2,0.2,0.1)
window <- 24

rolling_comp_contrib_tbl <- 1:(nrow(asset_returns_wide_tbl) - window) %>%

    map_df(.x = ., .f = ~calculate_comp_contrib_by_window(asset_returns_wide_tbl,
                                                          start = .x,
                                                          weights = w,
                                                          window = window))
rolling_comp_contrib_tbl
## # A tibble: 107 × 6
##    date        AAPL  AMZN    BA  DELL  NFLX
##    <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
##  1 2015-01-30    NA    NA    NA    NA    NA
##  2 2015-02-27    NA    NA    NA    NA    NA
##  3 2015-03-31    NA    NA    NA    NA    NA
##  4 2015-04-30    NA    NA    NA    NA    NA
##  5 2015-05-29    NA    NA    NA    NA    NA
##  6 2015-06-30    NA    NA    NA    NA    NA
##  7 2015-07-31    NA    NA    NA    NA    NA
##  8 2015-08-31    NA    NA    NA    NA    NA
##  9 2015-09-30    NA    NA    NA    NA    NA
## 10 2015-10-30    NA    NA    NA    NA    NA
## # ℹ 97 more rows
# Figure 10.3 Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%

    # Transform data to long form
    pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%

    # Plot
    ggplot(aes(date, contribution, color = asset)) +
    geom_line() +

    scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
    scale_y_continuous(labels = scales::percent_format()) +

    annotate(geom = "text",
             x = as.Date("2016-07-01"),
             y = 0.03,
             color = "red", size = 5,
             label = str_glue("AGG dips below zero sometimes, indicating
                              it reduces the portfolio volatility."))

# Figure 10.4 Stacked Component Contribution ggplot ----
rolling_comp_contrib_tbl %>%

    # Transform data to long form
    pivot_longer(cols = -date, names_to = "asset", values_to = "contribution") %>%

    # Plot
    ggplot(aes(date, contribution, fill = asset)) +
    geom_area() +

    scale_x_date(breaks = scales::pretty_breaks(n = 7)) +
    scale_y_continuous(labels = scales::percent_format()) +

    annotate(geom = "text",
             x = as.Date("2016-07-01"),
             y = 0.08,
             color = "red", size = 5,
             label = str_glue("AGG dips below zero sometimes, indicating
                              it reduces the portfolio volatility."))

In my portfolio the largest contributors to volatility are AAPL and AMZN with 25 percent compared to the 20 Percent from BA and DELL close behind. I do not think my portfolios risk is concreted on one asset where four out of the five have fairly close volatility where none are extremly risky but do have have a risk factor like any stock you invest in.