# 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("NXPI", "IFX.DE", "AMD", "ON")

prices <- tq_get(
  x = symbols,
  get = "stock.prices",
  from = "2012-12-31",
  to = "2026-11-20"
)

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
  group_by(symbol) %>%
    
  tq_transmute(
    select       = adjusted,
    mutate_fun   = periodReturn,
    period       = "monthly",
    type         = "arithmetic",
    col_rename   = "returns") %>%
    
  slice(-1) %>%
    
  ungroup() %>%
    
  rename(asset = symbol)

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) %>%
    
    drop_na() %>%
    
    column_to_rownames(var = "date")

# create the function
calculate_component_contribution <- function(.data, w) {
    
    # covariance of asset returns
    covariance_matrix <- cov(.data)
    
    # portfolio standard deviation
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
    
    # component contribution
    component_contribution <-
        (t(w) %*% covariance_matrix * w) / sd_portfolio[1, 1]
    
    # component contribution as a proportion
    component_percentages <-
        (component_contribution / sd_portfolio[1, 1]) %>%
        round(3) %>%
        as_tibble()
    
    return(component_percentages)
}

# calculate contributions using four equal weights
plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.25, .25, .25, .25)) %>%
    
    # transform to long form
    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",
         x = "Asset",
         y = "Contribution")

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- asset_returns_wide_tbl %>%
    
    calculate_component_contribution(w = c(.25, .25, .25, .25)) %>%
    
    # transform to long form
    pivot_longer(cols = everything(),
                 names_to = "asset",
                 values_to = "contribution") %>%
    
    # add weights
    add_column(weight = c(.25, .25, .25, .25)) %>%
    
    # 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_tq() +
    theme(plot.title = element_text(hjust = 0.5)) +
    
    labs(title = "Percent contribution to portfolio volatility and weight",
         x = "Asset",
         y = "Percent")

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?

AMD is the largest contributor to my portfolio’s volatility at about 32%, even though it only makes up 25% of the portfolio. The risk is somewhat tilted toward AMD, but it is not heavily concentrated in one asset since the other stocks contribute about 20%–26% each. However, all four stocks are in the semiconductor industry, so the portfolio is concentrated in one industry.