# Load packages
# Core
library(tidyverse)
library(tidyquant)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
Choose your stocks from 2012-12-31 to present.
symbols <- c("JPM", "NVDA", "LLY", "AMZN")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
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"))
# 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
## AMZN JPM LLY NVDA
## 2013-01-31 0.0566799395 0.074549220 8.489723e-02 0.000000000
## 2013-02-28 -0.0046435024 0.038975391 2.705470e-02 0.038222120
## 2013-03-28 0.0083654162 -0.030299147 3.822806e-02 0.013338704
## 2013-04-30 -0.0487507497 0.038370189 -2.514161e-02 0.070705992
## 2013-05-31 0.0588686246 0.107826577 -3.216736e-02 0.054652320
## 2013-06-28 0.0310507506 -0.033528817 -7.904062e-02 -0.030167547
## 2013-07-31 0.0813355350 0.061462658 7.809900e-02 0.028091869
## 2013-08-30 -0.0695574090 -0.097951633 -2.361967e-02 0.026270337
## 2013-09-30 0.1067688897 0.022697024 -2.103703e-02 0.053460574
## 2013-10-31 0.1521839116 0.004434294 -1.018444e-02 -0.024066335
## 2013-11-29 0.0781496860 0.104545027 1.783311e-02 0.032034407
## 2013-12-31 0.0130490386 0.021781467 1.541233e-02 0.026567318
## 2014-01-31 -0.1059765119 -0.048308542 5.734336e-02 -0.020177289
## 2014-02-28 0.0094619003 0.026031353 1.076842e-01 0.162107675
## 2014-03-31 -0.0737086161 0.066219889 -1.266156e-02 -0.025904134
## 2014-04-30 -0.1007565303 -0.074830584 4.068651e-03 0.030788725
## 2014-05-30 0.0273091844 -0.007351280 2.105865e-02 0.032886092
## 2014-06-30 0.0383836202 0.036226201 3.786377e-02 -0.024508194
## 2014-07-31 -0.0369768154 0.007833586 -1.801526e-02 -0.057729407
## 2014-08-29 0.0799468404 0.030398863 4.814345e-02 0.110059757
## 2014-09-30 -0.0502010184 0.013200682 2.009253e-02 -0.052782636
## 2014-10-31 -0.0540982347 0.010690903 2.256525e-02 0.057399364
## 2014-11-28 0.1031187277 -0.005304876 3.387705e-02 0.074852135
## 2014-12-31 -0.0872368614 0.039437967 1.269086e-02 -0.044863508
## 2015-01-30 0.1330922557 -0.134036825 4.270426e-02 -0.043319068
## 2015-02-27 0.0697992426 0.119456693 -1.862794e-02 0.142698427
## 2015-03-31 -0.0214295755 -0.011488106 3.473241e-02 -0.052582055
## 2015-04-30 0.1253212736 0.049912892 -1.079421e-02 0.058909095
## 2015-05-29 0.0175090293 0.039062357 1.002141e-01 0.001459648
## 2015-06-30 0.0112589814 0.029656407 5.654544e-02 -0.095717000
## 2015-07-31 0.2111621090 0.017814239 1.214299e-02 -0.007988052
## 2015-08-31 -0.0443525782 -0.066827373 -1.988810e-02 0.123595408
## 2015-09-30 -0.0019516837 -0.050062407 1.614082e-02 0.092151046
## 2015-10-30 0.2010808743 0.059589313 -2.565789e-02 0.140555210
## 2015-11-30 0.0602956777 0.037123453 1.206592e-02 0.115405310
## 2015-12-31 0.0165440008 -0.009796015 2.670038e-02 0.038347469
## 2016-01-29 -0.1410054620 -0.097446549 -6.319441e-02 -0.118048567
## 2016-02-29 -0.0605352209 -0.055282243 -8.710652e-02 0.071923611
## 2016-03-31 0.0717834363 0.050564954 1.387580e-04 0.127654918
## 2016-04-29 0.1053453760 0.072421350 4.772485e-02 -0.002810766
## 2016-05-31 0.0915002899 0.032228351 1.383101e-05 0.276388497
## 2016-06-30 -0.0099694639 -0.049142695 4.839025e-02 0.006188209
## 2016-07-29 0.0586021229 0.036778732 5.123645e-02 0.194443299
## 2016-08-31 0.0135476418 0.053713440 -5.766452e-02 0.073469232
## 2016-09-30 0.0848953908 -0.013573300 3.177272e-02 0.110693865
## 2016-10-31 -0.0583893058 0.046556604 -8.337061e-02 0.037805017
## 2016-11-30 -0.0509721927 0.146281619 -8.889141e-02 0.260525119
## 2016-12-30 -0.0009330556 0.073564251 9.148347e-02 0.146435715
## 2017-01-31 0.0936394059 -0.013906724 4.622919e-02 0.022602353
## 2017-02-28 0.0258446800 0.068385744 7.906113e-02 -0.071874984
## 2017-03-31 0.0479423007 -0.031157793 1.557712e-02 0.070843467
## 2017-04-28 0.0424566944 -0.003879638 -2.467507e-02 -0.043434206
## 2017-05-31 0.0725778018 -0.057360982 -2.437732e-02 0.326022562
## 2017-06-30 -0.0271286156 0.106698595 3.373424e-02 0.001453719
## 2017-07-31 0.0202278808 0.009852354 4.364646e-03 0.117044977
## 2017-08-31 -0.0072953953 -0.009962565 -1.035508e-02 0.042639172
## 2017-09-29 -0.0198260355 0.049581217 5.096112e-02 0.053601418
## 2017-10-31 0.1395154056 0.057849039 -4.299681e-02 0.145700504
## 2017-11-30 0.0626577318 0.038126619 3.871497e-02 -0.029245218
## 2017-12-29 -0.0062057845 0.022889717 -2.128869e-03 -0.036583515
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(0.25, 0.25, 0.25, 0.25))
## # A tibble: 1 × 4
## AMZN JPM LLY NVDA
## <dbl> <dbl> <dbl> <dbl>
## 1 0.333 0.203 0.083 0.381
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.25, 0.25)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(0.25, 0.25, 0.25, 0.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(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 the largest contributor to the portfolio volatility? Do you think your portfolio risk is concentrated in any one asset?
NVDA is the largest contributor to my portfolios volatility. It takes on almost 40% of my portfolios volatility. Followed by AMZN that takes on almost 35% of the portfolios volatility. This portfolio is a diversified mix of tech, financial, healthcare, and consumer cyclical stocks, each with varying levels of volatility. The tech sector, including NVDA, tends to be more volatile, inherently carrying higher risk. NVIDIA is a high-growth company, heavily reliant on emerging trends like AI, gaming, and data centers. Growth stocks often exhibit higher price fluctuations because their valuations depend on future earnings, which are uncertain and prone to rapid shifts in sentiment. In contrast, JPM and LLY have established business models, and AMZN is considered more diversified in revenue sources, which can help reduce volatility.