# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(readr)

# Time series
library(lubridate)
library(tibbletime)

# modeling
library(broom)

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

Chpppse your stocks from 2012-12-31 to present.

symbols <- c("TM", "SBUX", "AEO", "BBW")
prices <- tq_get(x = symbols, 
                 get = "stock.prices", 
                 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
##                     AEO          BBW         SBUX           TM
## 2013-01-31 -0.014735225  0.070736560  0.045383822  0.022479976
## 2013-02-28  0.022989984  0.194442947 -0.019139215  0.073073783
## 2013-03-28 -0.100643401  0.079115486  0.037571433  0.012690493
## 2013-04-30  0.039323704 -0.061204527  0.066073926  0.124945384
## 2013-05-31  0.017329206  0.251533463  0.040473717  0.010690636
## 2013-06-28 -0.073502847 -0.071515640  0.036848584  0.026112948
## 2013-07-31  0.072855159  0.158143631  0.084553010  0.010224308
## 2013-08-30 -0.305490565  0.013966741 -0.008016812 -0.009147471
## 2013-09-30 -0.024973601 -0.032420136  0.087519776  0.067274580
## 2013-10-31  0.101851766  0.118737601  0.051650311  0.010798346
## 2013-11-29  0.049128502  0.164996811  0.008261262 -0.034349294
## 2013-12-31 -0.113259415 -0.205235820 -0.038415322 -0.025348458
## 2014-01-31 -0.062318644  0.145217756 -0.097328170 -0.060522173
## 2014-02-28  0.071306016 -0.120363577  0.001524368  0.004694489
## 2014-03-31 -0.161308336  0.218481673  0.033535268 -0.002836033
## 2014-04-30 -0.057158841  0.152814557 -0.038338116 -0.040490032
## 2014-05-30 -0.074507120  0.225635947  0.040101446  0.042348303
## 2014-06-30  0.055536312 -0.051068642  0.054986562  0.056293703
## 2014-07-31 -0.051199718 -0.236139276  0.003869202 -0.014054590
## 2014-08-29  0.278257117  0.211895554  0.005031488 -0.031598307
## 2014-09-30  0.039328842  0.003062814 -0.030667615  0.038375710
## 2014-10-31 -0.120627924  0.259183592  0.001324538  0.031985334
## 2014-11-28  0.091275503  0.194051710  0.076233662  0.014561806
## 2014-12-31 -0.006130463 -0.023599779  0.010290177  0.018905634
## 2015-01-30  0.011461521  0.025541650  0.064653250  0.026502698
## 2015-02-27  0.064137733  0.049667291  0.069516013  0.049362736
## 2015-03-31  0.131860097 -0.097851572  0.012859319  0.047003472
## 2015-04-30 -0.063589115 -0.064097385  0.046021165 -0.006238475
## 2015-05-29  0.028502659 -0.135781799  0.050062987 -0.008161688
## 2015-06-30  0.050621151 -0.006234415  0.031447687 -0.030483853
## 2015-07-31  0.037599439  0.087376087  0.077313460 -0.002170598
## 2015-08-31 -0.041996381  0.116189859 -0.054435535 -0.119902004
## 2015-09-30 -0.085196987 -0.036896864  0.038193939  0.004496951
## 2015-10-30 -0.014658821 -0.193929061  0.096045241  0.044525927
## 2015-11-30  0.018800923 -0.190581908 -0.015805879  0.014572742
## 2015-12-31  0.003493139 -0.049412395 -0.022402809 -0.011153411
## 2016-01-29 -0.057082546  0.066374988  0.012251924 -0.024767756
## 2016-02-29  0.041477343  0.087775589 -0.039776376 -0.142389767
## 2016-03-31  0.088375823 -0.094680117  0.025274732  0.039814379
## 2016-04-29 -0.144833349  0.013761711 -0.059881766 -0.044426136
## 2016-05-31  0.088873097  0.049621463 -0.020626301  0.016674485
## 2016-06-30  0.018372345 -0.030816739  0.039823047 -0.033631574
## 2016-07-29  0.125591022  0.015527033  0.016150506  0.109850888
## 2016-08-31  0.034013490 -0.161268164 -0.028377713  0.077806929
## 2016-09-30 -0.037367377 -0.113052859 -0.037877179 -0.023531100
## 2016-10-31 -0.039606378  0.264737427 -0.019961714 -0.003452393
## 2016-11-30 -0.028573544  0.061060308  0.092935577  0.024511198
## 2016-12-30 -0.080401824 -0.042711144 -0.043182315 -0.011284232
## 2017-01-31 -0.003963320 -0.136132189 -0.005418103 -0.012449158
## 2017-02-28  0.047812779 -0.282141939  0.033938681 -0.022718307
## 2017-03-31 -0.121971509 -0.022347169  0.026376840 -0.040858808
## 2017-04-28  0.013774922  0.161388201  0.028199196 -0.004428900
## 2017-05-31 -0.203118238  0.033100057  0.061537481 -0.005563798
## 2017-06-30  0.046717718 -0.028303793 -0.086997003 -0.023712104
## 2017-07-31 -0.007366402 -0.084838954 -0.077159883  0.071820077
## 2017-08-31  0.009247585 -0.042559545  0.020674964  0.001239988
## 2017-09-29  0.179528317 -0.005449588 -0.021185400  0.053340188
## 2017-10-31 -0.084525668 -0.159630179  0.020820551  0.039730536
## 2017-11-30  0.211089596  0.050010407  0.058198506  0.018695091
## 2017-12-29  0.163400065  0.115069360 -0.006767812  0.006548103
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(.3, .25, .25, .2))
## # A tibble: 1 × 4
##     AEO   BBW  SBUX    TM
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.408 0.448 0.089 0.055

6 Plot: Column Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.3, .25, .25, .2)) %>%
    
    # Transform to long form
    pivot_longer(cols = everything(),names_to = "Asset", values_to = "Contribution") %>%
    
    # Add weight
    add_column(weight = c(.3, .25, .25, .2)) %>%
    
    # 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 is the largest contributor to the portfolio volatility? Do you think your portfolio risk is concentrated in any one asset?

BBW is the largest contributor to the portfolio volatility. The portfolio risk is most likely concentrated in the BBW stock. This is because the Skewness Comparison graph shows the portfolio is most similar to BBW.