# 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("TSLA", "DELL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2024-06-26")
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
## DELL TSLA
## 2016-09-30 0.067499656 -0.038366372
## 2016-10-31 0.026629724 -0.031364583
## 2016-11-30 0.087147009 -0.043041267
## 2016-12-30 0.025985210 0.120665178
## 2017-01-31 0.136188453 0.164624916
## 2017-02-28 0.007906191 -0.007730364
## 2017-03-31 0.009249951 0.107278727
## 2017-04-28 0.046200802 0.120916212
## 2017-05-31 0.033409647 0.082295892
## 2017-06-30 -0.127067307 0.058654468
## 2017-07-31 0.050417538 -0.111459860
## 2017-08-31 0.153461438 0.095543446
## 2017-09-29 0.029974495 -0.042474144
## 2017-10-31 0.069536859 -0.028457409
## 2017-11-30 -0.056284749 -0.070862541
## 2017-12-29 0.038118899 0.008061928
## 2018-01-31 -0.125409152 0.129254571
## 2018-02-28 0.035485625 -0.032266877
## 2018-03-29 -0.014644378 -0.253920408
## 2018-04-30 -0.019865462 0.099254576
## 2018-05-31 0.116776314 -0.031698139
## 2018-06-29 0.047454925 0.186043257
## 2018-07-31 0.089727080 -0.140021491
## 2018-08-31 0.038692604 0.011737389
## 2018-09-28 0.009829723 -0.130439038
## 2018-10-31 -0.071813611 0.242170576
## 2018-11-30 0.154387872 0.038271580
## 2018-12-31 -0.178243370 -0.051761952
## 2019-01-31 -0.005745827 -0.080628789
## 2019-02-28 0.138714467 0.041032981
## 2019-03-29 0.050137129 -0.133656413
## 2019-04-30 0.138523997 -0.159123803
## 2019-05-31 -0.123977157 -0.253945372
## 2019-06-28 -0.158919824 0.188012109
## 2019-07-31 0.128053836 0.078092373
## 2019-08-30 -0.113786220 -0.068516948
## 2019-09-30 0.006383733 0.065449565
## 2019-10-31 0.019666532 0.268061253
## 2019-11-29 -0.086856508 0.046592176
## 2019-12-31 0.058085901 0.237359743
## 2020-01-31 -0.052328424 0.441578342
## 2020-02-28 -0.186801501 0.026424253
## 2020-03-31 -0.022748102 -0.242781458
## 2020-04-30 0.076398933 0.400209535
## 2020-05-29 0.150832401 0.065730500
## 2020-06-30 0.101444729 0.257108657
## 2020-07-31 0.085265447 0.281420674
## 2020-08-31 0.099359089 0.554719320
## 2020-09-30 0.024072238 -0.149762306
## 2020-10-30 -0.116270016 -0.100371771
## 2020-11-30 0.135872540 0.380308519
## 2020-12-31 0.059883132 0.217730753
## 2021-01-29 -0.005472777 0.117343701
## 2021-02-26 0.106361505 -0.161038203
## 2021-03-31 0.083727052 -0.011269836
## 2021-04-30 0.109289127 0.060292565
## 2021-05-28 0.003147813 -0.126372343
## 2021-06-30 0.010387924 0.083547955
## 2021-07-30 -0.031078932 0.010973846
## 2021-08-31 0.008656156 0.068224268
## 2021-09-30 0.065333373 0.052632612
## 2021-10-29 0.055614047 0.362230202
## 2021-11-30 0.012875294 0.027237848
## 2021-12-31 -0.005326674 -0.079968440
## 2022-01-31 0.011329602 -0.120597475
## 2022-02-28 -0.108671411 -0.073397011
## 2022-03-31 -0.015225199 0.213504290
## 2022-04-29 -0.058475098 -0.213125289
## 2022-05-31 0.060461950 -0.138340062
## 2022-06-30 -0.077626063 -0.118657126
## 2022-07-29 -0.017479137 0.280480149
## 2022-08-31 -0.162806216 -0.075250271
## 2022-09-30 -0.113840926 -0.038313992
## 2022-10-31 0.126329306 -0.153346760
## 2022-11-30 0.153927563 -0.155866121
## 2022-12-30 -0.107620629 -0.457813194
## 2023-01-31 0.017969579 0.340915767
## 2023-02-28 0.000492308 0.171904973
## 2023-03-31 -0.010637087 0.008471140
## 2023-04-28 0.087035129 -0.233183710
## 2023-05-31 0.029900224 0.216021889
## 2023-06-30 0.188587655 0.249689452
## 2023-07-31 -0.015305626 0.021391609
## 2023-08-31 0.060847087 -0.035588260
## 2023-09-29 0.203027792 -0.030929027
## 2023-10-31 -0.023678037 -0.219831983
## 2023-11-30 0.125672859 0.178463656
## 2023-12-29 0.008269328 0.034390133
## 2024-01-31 0.084560546 -0.282704160
## 2024-02-29 0.132897861 0.075015304
## 2024-03-28 0.186871339 -0.138383423
## 2024-04-30 0.092148135 0.041724969
## 2024-05-31 0.113065022 -0.028782134
## 2024-06-25 0.005644747 0.050745660
## 2013-01-31 NA 0.102078114
## 2013-02-28 NA -0.074128640
## 2013-03-28 NA 0.084208138
## 2013-04-30 NA 0.354111531
## 2013-05-31 NA 0.593716684
## 2013-06-28 NA 0.093672163
## 2013-07-31 NA 0.223739522
## 2013-08-30 NA 0.229971642
## 2013-09-30 NA 0.134706620
## 2013-10-31 NA -0.189806595
## 2013-11-29 NA -0.228409405
## 2013-12-31 NA 0.167108541
## 2014-01-31 NA 0.187261714
## 2014-02-28 NA 0.299722785
## 2014-03-31 NA -0.160783242
## 2014-04-30 NA -0.002690122
## 2014-05-30 NA -0.000577422
## 2014-06-30 NA 0.144457224
## 2014-07-31 NA -0.072372676
## 2014-08-29 NA 0.188794007
## 2014-09-30 NA -0.105566477
## 2014-10-31 NA -0.004046477
## 2014-11-28 NA 0.011599801
## 2014-12-31 NA -0.094774519
## 2015-01-30 NA -0.088365289
## 2015-02-27 NA -0.001277808
## 2015-03-31 NA -0.074350051
## 2015-04-30 NA 0.180226808
## 2015-05-29 NA 0.103899574
## 2015-06-30 NA 0.067300935
## 2015-07-31 NA -0.007896616
## 2015-08-31 NA -0.066366250
## 2015-09-30 NA -0.002653519
## 2015-10-30 NA -0.182659777
## 2015-11-30 NA 0.106828586
## 2015-12-31 NA 0.041471519
## 2016-01-29 NA -0.227360626
## 2016-02-29 NA 0.003810669
## 2016-03-31 NA 0.179948109
## 2016-04-29 NA 0.046721797
## 2016-05-31 NA -0.075597968
## 2016-06-30 NA -0.050296440
## 2016-07-29 NA 0.100785334
## 2016-08-31 NA -0.102058091
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## DELL TSLA
## DELL NA NA
## TSLA NA 0.02919988
# 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,] NA
# 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
## DELL TSLA
## [1,] NA NA
rowSums(component_contribution)
## [1] NA
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 2
## DELL TSLA
## <dbl> <dbl>
## 1 NA NA
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 2 × 2
## asset contribution
## <chr> <dbl>
## 1 DELL NA
## 2 TSLA NA
# 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
## DELL TSLA
## 2016-09-30 0.067499656 -0.038366372
## 2016-10-31 0.026629724 -0.031364583
## 2016-11-30 0.087147009 -0.043041267
## 2016-12-30 0.025985210 0.120665178
## 2017-01-31 0.136188453 0.164624916
## 2017-02-28 0.007906191 -0.007730364
## 2017-03-31 0.009249951 0.107278727
## 2017-04-28 0.046200802 0.120916212
## 2017-05-31 0.033409647 0.082295892
## 2017-06-30 -0.127067307 0.058654468
## 2017-07-31 0.050417538 -0.111459860
## 2017-08-31 0.153461438 0.095543446
## 2017-09-29 0.029974495 -0.042474144
## 2017-10-31 0.069536859 -0.028457409
## 2017-11-30 -0.056284749 -0.070862541
## 2017-12-29 0.038118899 0.008061928
## 2018-01-31 -0.125409152 0.129254571
## 2018-02-28 0.035485625 -0.032266877
## 2018-03-29 -0.014644378 -0.253920408
## 2018-04-30 -0.019865462 0.099254576
## 2018-05-31 0.116776314 -0.031698139
## 2018-06-29 0.047454925 0.186043257
## 2018-07-31 0.089727080 -0.140021491
## 2018-08-31 0.038692604 0.011737389
## 2018-09-28 0.009829723 -0.130439038
## 2018-10-31 -0.071813611 0.242170576
## 2018-11-30 0.154387872 0.038271580
## 2018-12-31 -0.178243370 -0.051761952
## 2019-01-31 -0.005745827 -0.080628789
## 2019-02-28 0.138714467 0.041032981
## 2019-03-29 0.050137129 -0.133656413
## 2019-04-30 0.138523997 -0.159123803
## 2019-05-31 -0.123977157 -0.253945372
## 2019-06-28 -0.158919824 0.188012109
## 2019-07-31 0.128053836 0.078092373
## 2019-08-30 -0.113786220 -0.068516948
## 2019-09-30 0.006383733 0.065449565
## 2019-10-31 0.019666532 0.268061253
## 2019-11-29 -0.086856508 0.046592176
## 2019-12-31 0.058085901 0.237359743
## 2020-01-31 -0.052328424 0.441578342
## 2020-02-28 -0.186801501 0.026424253
## 2020-03-31 -0.022748102 -0.242781458
## 2020-04-30 0.076398933 0.400209535
## 2020-05-29 0.150832401 0.065730500
## 2020-06-30 0.101444729 0.257108657
## 2020-07-31 0.085265447 0.281420674
## 2020-08-31 0.099359089 0.554719320
## 2020-09-30 0.024072238 -0.149762306
## 2020-10-30 -0.116270016 -0.100371771
## 2020-11-30 0.135872540 0.380308519
## 2020-12-31 0.059883132 0.217730753
## 2021-01-29 -0.005472777 0.117343701
## 2021-02-26 0.106361505 -0.161038203
## 2021-03-31 0.083727052 -0.011269836
## 2021-04-30 0.109289127 0.060292565
## 2021-05-28 0.003147813 -0.126372343
## 2021-06-30 0.010387924 0.083547955
## 2021-07-30 -0.031078932 0.010973846
## 2021-08-31 0.008656156 0.068224268
## 2021-09-30 0.065333373 0.052632612
## 2021-10-29 0.055614047 0.362230202
## 2021-11-30 0.012875294 0.027237848
## 2021-12-31 -0.005326674 -0.079968440
## 2022-01-31 0.011329602 -0.120597475
## 2022-02-28 -0.108671411 -0.073397011
## 2022-03-31 -0.015225199 0.213504290
## 2022-04-29 -0.058475098 -0.213125289
## 2022-05-31 0.060461950 -0.138340062
## 2022-06-30 -0.077626063 -0.118657126
## 2022-07-29 -0.017479137 0.280480149
## 2022-08-31 -0.162806216 -0.075250271
## 2022-09-30 -0.113840926 -0.038313992
## 2022-10-31 0.126329306 -0.153346760
## 2022-11-30 0.153927563 -0.155866121
## 2022-12-30 -0.107620629 -0.457813194
## 2023-01-31 0.017969579 0.340915767
## 2023-02-28 0.000492308 0.171904973
## 2023-03-31 -0.010637087 0.008471140
## 2023-04-28 0.087035129 -0.233183710
## 2023-05-31 0.029900224 0.216021889
## 2023-06-30 0.188587655 0.249689452
## 2023-07-31 -0.015305626 0.021391609
## 2023-08-31 0.060847087 -0.035588260
## 2023-09-29 0.203027792 -0.030929027
## 2023-10-31 -0.023678037 -0.219831983
## 2023-11-30 0.125672859 0.178463656
## 2023-12-29 0.008269328 0.034390133
## 2024-01-31 0.084560546 -0.282704160
## 2024-02-29 0.132897861 0.075015304
## 2024-03-28 0.186871339 -0.138383423
## 2024-04-30 0.092148135 0.041724969
## 2024-05-31 0.113065022 -0.028782134
## 2024-06-25 0.005644747 0.050745660
## 2013-01-31 NA 0.102078114
## 2013-02-28 NA -0.074128640
## 2013-03-28 NA 0.084208138
## 2013-04-30 NA 0.354111531
## 2013-05-31 NA 0.593716684
## 2013-06-28 NA 0.093672163
## 2013-07-31 NA 0.223739522
## 2013-08-30 NA 0.229971642
## 2013-09-30 NA 0.134706620
## 2013-10-31 NA -0.189806595
## 2013-11-29 NA -0.228409405
## 2013-12-31 NA 0.167108541
## 2014-01-31 NA 0.187261714
## 2014-02-28 NA 0.299722785
## 2014-03-31 NA -0.160783242
## 2014-04-30 NA -0.002690122
## 2014-05-30 NA -0.000577422
## 2014-06-30 NA 0.144457224
## 2014-07-31 NA -0.072372676
## 2014-08-29 NA 0.188794007
## 2014-09-30 NA -0.105566477
## 2014-10-31 NA -0.004046477
## 2014-11-28 NA 0.011599801
## 2014-12-31 NA -0.094774519
## 2015-01-30 NA -0.088365289
## 2015-02-27 NA -0.001277808
## 2015-03-31 NA -0.074350051
## 2015-04-30 NA 0.180226808
## 2015-05-29 NA 0.103899574
## 2015-06-30 NA 0.067300935
## 2015-07-31 NA -0.007896616
## 2015-08-31 NA -0.066366250
## 2015-09-30 NA -0.002653519
## 2015-10-30 NA -0.182659777
## 2015-11-30 NA 0.106828586
## 2015-12-31 NA 0.041471519
## 2016-01-29 NA -0.227360626
## 2016-02-29 NA 0.003810669
## 2016-03-31 NA 0.179948109
## 2016-04-29 NA 0.046721797
## 2016-05-31 NA -0.075597968
## 2016-06-30 NA -0.050296440
## 2016-07-29 NA 0.100785334
## 2016-08-31 NA -0.102058091
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
## DELL TSLA
## <dbl> <dbl>
## 1 NA NA
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?
It seems that Tesla and Dell contribute equally to the portfolio volatility. It is not concentrated with one asset.