# 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("NKE", "TSLA")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-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
## NKE TSLA
## 2013-01-31 0.0463880642 0.102078114
## 2013-02-28 0.0114288519 -0.074128640
## 2013-03-28 0.0802404209 0.084208138
## 2013-04-30 0.0749065937 0.354111531
## 2013-05-31 -0.0276355444 0.593716684
## 2013-06-28 0.0322354411 0.093672163
## 2013-07-31 -0.0120065207 0.223739522
## 2013-08-30 0.0017286441 0.229971642
## 2013-09-30 0.1452424925 0.134706620
## 2013-10-31 0.0420545130 -0.189806595
## 2013-11-29 0.0436480295 -0.228409405
## 2013-12-31 -0.0032097202 0.167108541
## 2014-01-31 -0.0764779613 0.187261714
## 2014-02-28 0.0752103995 0.299722785
## 2014-03-31 -0.0583764690 -0.160783242
## 2014-04-30 -0.0123970401 -0.002690122
## 2014-05-30 0.0560134096 -0.000577422
## 2014-06-30 0.0082871886 0.144457224
## 2014-07-31 -0.0054307831 -0.072372676
## 2014-08-29 0.0212580292 0.188794007
## 2014-09-30 0.1271458928 -0.105566477
## 2014-10-31 0.0413955680 -0.004046477
## 2014-11-28 0.0657681396 0.011599801
## 2014-12-31 -0.0292646137 -0.094774519
## 2015-01-30 -0.0414067309 -0.088365289
## 2015-02-27 0.0543413734 -0.001277808
## 2015-03-31 0.0325172334 -0.074350051
## 2015-04-30 -0.0149624928 0.180226808
## 2015-05-29 0.0309560111 0.103899574
## 2015-06-30 0.0605844674 0.067300935
## 2015-07-31 0.0645269596 -0.007896616
## 2015-08-31 -0.0305791219 -0.066366250
## 2015-09-30 0.0982087642 -0.002653519
## 2015-10-30 0.0634857817 -0.182659777
## 2015-11-30 0.0094945087 0.106828586
## 2015-12-31 -0.0541857698 0.041471519
## 2016-01-29 -0.0078711116 -0.227360626
## 2016-02-29 -0.0067961771 0.003810669
## 2016-03-31 0.0006247821 0.179948109
## 2016-04-29 -0.0420290011 0.046721797
## 2016-05-31 -0.0651951971 -0.075597968
## 2016-06-30 0.0025549671 -0.050296440
## 2016-07-29 0.0054200736 0.100785334
## 2016-08-31 0.0378337871 -0.102058091
## 2016-09-30 -0.0877709966 -0.038366372
## 2016-10-31 -0.0480497210 -0.031364583
## 2016-11-30 -0.0021944442 -0.043041267
## 2016-12-30 0.0186662169 0.120665178
## 2017-01-31 0.0399165958 0.164624916
## 2017-02-28 0.0774510868 -0.007730364
## 2017-03-31 -0.0222249596 0.107278727
## 2017-04-28 -0.0057583292 0.120916212
## 2017-05-31 -0.0446567571 0.082295892
## 2017-06-30 0.1108366156 0.058654468
## 2017-07-31 0.0008470262 -0.111459860
## 2017-08-31 -0.1082532233 0.095543446
## 2017-09-29 -0.0183457349 -0.042474144
## 2017-10-31 0.0587962343 -0.028457409
## 2017-11-30 0.0941690827 -0.070862541
## 2017-12-29 0.0379615936 0.008061928
## 2018-01-31 0.0867716108 0.129254571
## 2018-02-28 -0.0175974761 -0.032266877
## 2018-03-29 -0.0058207426 -0.253920408
## 2018-04-30 0.0289271401 0.099254576
## 2018-05-31 0.0486579844 -0.031698139
## 2018-06-29 0.1069234237 0.186043257
## 2018-07-31 -0.0353825127 -0.140021491
## 2018-08-31 0.0689794084 0.011737389
## 2018-09-28 0.0301963272 -0.130439038
## 2018-10-31 -0.1213303491 0.242170576
## 2018-11-30 0.0040291905 0.038271580
## 2018-12-31 -0.0131314390 -0.051761952
## 2019-01-31 0.0992992834 -0.080628789
## 2019-02-28 0.0459481748 0.041032981
## 2019-03-29 -0.0153195203 -0.133656413
## 2019-04-30 0.0420893172 -0.159123803
## 2019-05-31 -0.1270014213 -0.253945372
## 2019-06-28 0.0845992737 0.188012109
## 2019-07-31 0.0244745856 0.078092373
## 2019-08-30 -0.0153645103 -0.068516948
## 2019-09-30 0.1056918918 0.065449565
## 2019-10-31 -0.0476459982 0.268061253
## 2019-11-29 0.0456629192 0.046592176
## 2019-12-31 0.0803308266 0.237359743
## 2020-01-31 -0.0507166206 0.441578342
## 2020-02-28 -0.0718009462 0.026424253
## 2020-03-31 -0.0771938448 -0.242781458
## 2020-04-30 0.0522718367 0.400209535
## 2020-05-29 0.1253848781 0.065730500
## 2020-06-30 -0.0053908101 0.257108657
## 2020-07-31 -0.0044977259 0.281420674
## 2020-08-31 0.1387491134 0.554719320
## 2020-09-30 0.1151081769 -0.149762306
## 2020-10-30 -0.0444662812 -0.100371771
## 2020-11-30 0.1148918404 0.380308519
## 2020-12-31 0.0510475985 0.217730753
## 2021-01-29 -0.0573120914 0.117343701
## 2021-02-26 0.0108993961 -0.161038203
## 2021-03-31 -0.0141223615 -0.011269836
## 2021-04-30 -0.0020336910 0.060292565
## 2021-05-28 0.0305594010 -0.126372343
## 2021-06-30 0.1240977557 0.083547955
## 2021-07-30 0.0809137151 0.010973846
## 2021-08-31 -0.0150257075 0.068224268
## 2021-09-30 -0.1260497260 0.052632612
## 2021-10-29 0.1414100282 0.362230202
## 2021-11-30 0.0115890529 0.027237848
## 2021-12-31 -0.0135064421 -0.079968440
## 2022-01-31 -0.1183305607 -0.120597475
## 2022-02-28 -0.0809942149 -0.073397011
## 2022-03-31 -0.0124014953 0.213504290
## 2022-04-29 -0.0760992821 -0.213125289
## 2022-05-31 -0.0480486078 -0.138340062
## 2022-06-30 -0.1484559787 -0.118657126
## 2022-07-29 0.1173045643 0.280480149
## 2022-08-31 -0.0765609457 -0.075250271
## 2022-09-30 -0.2445216701 -0.038313992
## 2022-10-31 0.1088673616 -0.153346760
## 2022-11-30 0.1685054672 -0.155866121
## 2022-12-30 0.0676659076 -0.457813194
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## NKE TSLA
## NKE 0.004910847 0.002117178
## TSLA 0.002117178 0.029457675
# 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(.5, .5)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.09823808
# 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
## NKE TSLA
## [1,] 0.01788519 0.08035289
rowSums(component_contribution)
## [1] 0.09823808
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 2
## NKE TSLA
## <dbl> <dbl>
## 1 0.182 0.818
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 2 × 2
## asset contribution
## <chr> <dbl>
## 1 NKE 0.182
## 2 TSLA 0.818
# 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
## NKE TSLA
## 2013-01-31 0.0463880642 0.102078114
## 2013-02-28 0.0114288519 -0.074128640
## 2013-03-28 0.0802404209 0.084208138
## 2013-04-30 0.0749065937 0.354111531
## 2013-05-31 -0.0276355444 0.593716684
## 2013-06-28 0.0322354411 0.093672163
## 2013-07-31 -0.0120065207 0.223739522
## 2013-08-30 0.0017286441 0.229971642
## 2013-09-30 0.1452424925 0.134706620
## 2013-10-31 0.0420545130 -0.189806595
## 2013-11-29 0.0436480295 -0.228409405
## 2013-12-31 -0.0032097202 0.167108541
## 2014-01-31 -0.0764779613 0.187261714
## 2014-02-28 0.0752103995 0.299722785
## 2014-03-31 -0.0583764690 -0.160783242
## 2014-04-30 -0.0123970401 -0.002690122
## 2014-05-30 0.0560134096 -0.000577422
## 2014-06-30 0.0082871886 0.144457224
## 2014-07-31 -0.0054307831 -0.072372676
## 2014-08-29 0.0212580292 0.188794007
## 2014-09-30 0.1271458928 -0.105566477
## 2014-10-31 0.0413955680 -0.004046477
## 2014-11-28 0.0657681396 0.011599801
## 2014-12-31 -0.0292646137 -0.094774519
## 2015-01-30 -0.0414067309 -0.088365289
## 2015-02-27 0.0543413734 -0.001277808
## 2015-03-31 0.0325172334 -0.074350051
## 2015-04-30 -0.0149624928 0.180226808
## 2015-05-29 0.0309560111 0.103899574
## 2015-06-30 0.0605844674 0.067300935
## 2015-07-31 0.0645269596 -0.007896616
## 2015-08-31 -0.0305791219 -0.066366250
## 2015-09-30 0.0982087642 -0.002653519
## 2015-10-30 0.0634857817 -0.182659777
## 2015-11-30 0.0094945087 0.106828586
## 2015-12-31 -0.0541857698 0.041471519
## 2016-01-29 -0.0078711116 -0.227360626
## 2016-02-29 -0.0067961771 0.003810669
## 2016-03-31 0.0006247821 0.179948109
## 2016-04-29 -0.0420290011 0.046721797
## 2016-05-31 -0.0651951971 -0.075597968
## 2016-06-30 0.0025549671 -0.050296440
## 2016-07-29 0.0054200736 0.100785334
## 2016-08-31 0.0378337871 -0.102058091
## 2016-09-30 -0.0877709966 -0.038366372
## 2016-10-31 -0.0480497210 -0.031364583
## 2016-11-30 -0.0021944442 -0.043041267
## 2016-12-30 0.0186662169 0.120665178
## 2017-01-31 0.0399165958 0.164624916
## 2017-02-28 0.0774510868 -0.007730364
## 2017-03-31 -0.0222249596 0.107278727
## 2017-04-28 -0.0057583292 0.120916212
## 2017-05-31 -0.0446567571 0.082295892
## 2017-06-30 0.1108366156 0.058654468
## 2017-07-31 0.0008470262 -0.111459860
## 2017-08-31 -0.1082532233 0.095543446
## 2017-09-29 -0.0183457349 -0.042474144
## 2017-10-31 0.0587962343 -0.028457409
## 2017-11-30 0.0941690827 -0.070862541
## 2017-12-29 0.0379615936 0.008061928
## 2018-01-31 0.0867716108 0.129254571
## 2018-02-28 -0.0175974761 -0.032266877
## 2018-03-29 -0.0058207426 -0.253920408
## 2018-04-30 0.0289271401 0.099254576
## 2018-05-31 0.0486579844 -0.031698139
## 2018-06-29 0.1069234237 0.186043257
## 2018-07-31 -0.0353825127 -0.140021491
## 2018-08-31 0.0689794084 0.011737389
## 2018-09-28 0.0301963272 -0.130439038
## 2018-10-31 -0.1213303491 0.242170576
## 2018-11-30 0.0040291905 0.038271580
## 2018-12-31 -0.0131314390 -0.051761952
## 2019-01-31 0.0992992834 -0.080628789
## 2019-02-28 0.0459481748 0.041032981
## 2019-03-29 -0.0153195203 -0.133656413
## 2019-04-30 0.0420893172 -0.159123803
## 2019-05-31 -0.1270014213 -0.253945372
## 2019-06-28 0.0845992737 0.188012109
## 2019-07-31 0.0244745856 0.078092373
## 2019-08-30 -0.0153645103 -0.068516948
## 2019-09-30 0.1056918918 0.065449565
## 2019-10-31 -0.0476459982 0.268061253
## 2019-11-29 0.0456629192 0.046592176
## 2019-12-31 0.0803308266 0.237359743
## 2020-01-31 -0.0507166206 0.441578342
## 2020-02-28 -0.0718009462 0.026424253
## 2020-03-31 -0.0771938448 -0.242781458
## 2020-04-30 0.0522718367 0.400209535
## 2020-05-29 0.1253848781 0.065730500
## 2020-06-30 -0.0053908101 0.257108657
## 2020-07-31 -0.0044977259 0.281420674
## 2020-08-31 0.1387491134 0.554719320
## 2020-09-30 0.1151081769 -0.149762306
## 2020-10-30 -0.0444662812 -0.100371771
## 2020-11-30 0.1148918404 0.380308519
## 2020-12-31 0.0510475985 0.217730753
## 2021-01-29 -0.0573120914 0.117343701
## 2021-02-26 0.0108993961 -0.161038203
## 2021-03-31 -0.0141223615 -0.011269836
## 2021-04-30 -0.0020336910 0.060292565
## 2021-05-28 0.0305594010 -0.126372343
## 2021-06-30 0.1240977557 0.083547955
## 2021-07-30 0.0809137151 0.010973846
## 2021-08-31 -0.0150257075 0.068224268
## 2021-09-30 -0.1260497260 0.052632612
## 2021-10-29 0.1414100282 0.362230202
## 2021-11-30 0.0115890529 0.027237848
## 2021-12-31 -0.0135064421 -0.079968440
## 2022-01-31 -0.1183305607 -0.120597475
## 2022-02-28 -0.0809942149 -0.073397011
## 2022-03-31 -0.0124014953 0.213504290
## 2022-04-29 -0.0760992821 -0.213125289
## 2022-05-31 -0.0480486078 -0.138340062
## 2022-06-30 -0.1484559787 -0.118657126
## 2022-07-29 0.1173045643 0.280480149
## 2022-08-31 -0.0765609457 -0.075250271
## 2022-09-30 -0.2445216701 -0.038313992
## 2022-10-31 0.1088673616 -0.153346760
## 2022-11-30 0.1685054672 -0.155866121
## 2022-12-30 0.0676659076 -0.457813194
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# 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
# 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(.5, .5))
## # A tibble: 1 × 2
## NKE TSLA
## <dbl> <dbl>
## 1 0.182 0.818
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.5, .5)) %>%
# Transform to long from
pivot_longer(cols = everything() ,names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.5, .5)) %>%
# Transfrom 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?
Tesla is the largest contributor to the portfolios volatility, sitting at over 80%. When I compare Tesla to Nike as their contribution is just under 20% I would say that my risk is concentrated on Tesla being the more risky asset.