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.

# Load packages

# Core
library(tidyverse)
library(tidyquant)
symbols <- c("AMZN", "TGT", "WMT", "COST")

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

head(asset_returns_wide_tbl)
##                    AMZN         COST          TGT         WMT
## 2013-01-31  0.056679940  0.035911258  0.020739842  0.02489617
## 2013-02-28 -0.004643502 -0.007647305  0.047067448  0.01179542
## 2013-03-28  0.008365416  0.046488848  0.083604047  0.06207374
## 2013-04-30 -0.048750750  0.021628192  0.030359741  0.03789345
## 2013-05-31  0.058868625  0.013793576 -0.009961326 -0.03177995
## 2013-06-28  0.031050751  0.008537312 -0.009251450 -0.00468769

Covariance of asset returns:

covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
##              AMZN        COST         TGT          WMT
## AMZN 0.0070364591 0.002145031 0.001652237 0.0008318722
## COST 0.0021450311 0.003113359 0.001942073 0.0015318607
## TGT  0.0016522370 0.001942073 0.006769442 0.0017756490
## WMT  0.0008318722 0.001531861 0.001775649 0.0027411689

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.3, 0.25, 0.25, 0.2)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##            [,1]
## [1,] 0.05102478

Component contribution: similar to the formula for sd_portfolio, but a mathematical trick to summarize the same portfolio standard deviation by asset instead of as a single number.

component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
component_contribution
##            AMZN      COST        TGT         WMT
## [1,] 0.01897095 0.0108464 0.01483926 0.006368172
rowSums(component_contribution)
## [1] 0.05102478

Component contribution in percentage:

component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 4
##    AMZN  COST   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.372 0.213 0.291 0.125
component_percentages %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 4 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AMZN         0.372
## 2 COST         0.213
## 3 TGT          0.291
## 4 WMT          0.125

4 Function for Component Contribution

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
##    AMZN  COST   TGT   WMT
##   <dbl> <dbl> <dbl> <dbl>
## 1 0.372 0.213 0.291 0.125

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

6 Interpretation

Answer:

The main contributor to my portfolio’s volatility is AMZN. Its contribution bar is the highest, at around 37%. My portfolio’s risk is concentrated in AMZN because its contribution is significantly higher than its portfolio weight. TGT also adds a noticeable share of risk (about 29%), but still much less than AMZN. Overall, AMZN is the primary driver of my portfolio’s volatility.