# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
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("MELI", "NVDA", "AFL", "TTD", "GOOG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2016-12-31",
to = "2023-03-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
## AFL GOOG MELI NVDA TTD
## 2017-02-28 0.0393267318 0.0326201762 0.128779974 -0.071874646 0.352857823
## 2017-03-31 0.0009674931 0.0076841317 0.003550835 0.070843314 -0.125005215
## 2017-04-28 0.0334041322 0.0880996911 0.079245809 -0.043434073 0.002680967
## 2017-05-31 0.0124932275 0.0629878581 0.183842027 0.326022518 0.387000274
## 2017-06-30 0.0300560311 -0.0599349707 -0.091609946 0.001453523 -0.093112596
## 2017-07-31 0.0262989137 0.0236740767 0.139442995 0.117044719 0.061903342
## 2017-08-31 0.0399074368 0.0094447148 -0.109638246 0.042639236 -0.006398215
## 2017-09-29 -0.0141519888 0.0208389789 0.002339236 0.053601385 0.149474048
## 2017-10-31 0.0302541942 0.0582525579 -0.074627910 0.145700343 0.069242122
## 2017-11-30 0.0490772342 0.0046809143 0.135350383 -0.029244769 -0.293768519
## 2017-12-29 0.0015961376 0.0241716956 0.134682876 -0.036583592 -0.071918829
## 2018-01-31 0.0047733381 0.1115967979 0.207190370 0.239240573 0.058396804
## 2018-02-28 0.0134975298 -0.0573515384 0.002244947 -0.014959128 0.148832461
## 2018-03-29 -0.0154199355 -0.0683058032 -0.084902341 -0.043969064 -0.125589825
## 2018-04-30 0.0405294188 -0.0141135901 -0.048227823 -0.029312688 0.030759445
## 2018-05-31 -0.0055553600 0.0643892025 -0.155058997 0.115145096 0.513713769
## 2018-06-29 -0.0463299950 0.0278664423 0.027470550 -0.062544729 0.092297664
## 2018-07-31 0.0786470092 0.0871652022 0.137258625 0.033048520 -0.106545771
## 2018-08-31 -0.0008932449 0.0007637357 -0.001459173 0.137075582 0.520362545
## 2018-09-28 0.0177905965 -0.0205011202 -0.005681842 0.001210560 0.061702003
## 2018-10-31 -0.0888091014 -0.1028991926 -0.048041483 -0.287373740 -0.200037700
## 2018-11-30 0.0658935087 0.0162678420 0.081260411 -0.253667397 0.142345129
## 2018-12-31 -0.0039431806 -0.0552430740 -0.183885395 -0.202283144 -0.204883762
## 2019-01-31 0.0459014135 0.0750917624 0.217493315 0.073974145 0.206497062
## 2019-02-28 0.0352360550 0.0031748503 0.231438741 0.071593754 0.325336735
## 2019-03-29 0.0173494846 0.0465716130 0.101357223 0.151870178 0.002073378
## 2019-04-30 0.0075712094 0.0128463483 -0.047575680 0.007987533 0.112317864
## 2019-05-31 0.0232864189 -0.0740704511 0.164174109 -0.288679698 -0.107982692
## 2019-06-28 0.0661818964 -0.0208014127 0.069808167 0.192591437 0.136030606
## 2019-07-31 -0.0403964242 0.1183225218 0.015650727 0.026972408 0.144951756
## 2019-08-30 -0.0427214090 -0.0237704621 -0.044118284 -0.006208081 -0.068935868
## 2019-09-30 0.0417622160 0.0256754913 -0.075736762 0.038414633 -0.270350716
## 2019-10-31 0.0159275447 0.0331681677 -0.055404446 0.143946763 0.068263965
## 2019-11-29 0.0360901318 0.0349733697 0.107279970 0.076031806 0.271136585
## 2019-12-31 -0.0360165236 0.0242708227 -0.014993539 0.082162781 -0.013610815
## 2020-01-31 -0.0254632198 0.0701849208 0.147740896 0.004790776 0.035545106
## 2020-02-28 -0.1799183397 -0.0684586737 -0.073479279 0.133626893 0.064972653
## 2020-03-31 -0.2243108878 -0.1413300016 -0.231792513 -0.024248276 -0.397662780
## 2020-04-30 0.0839886784 0.1482720197 0.177558409 0.103279376 0.416047962
## 2020-05-29 -0.0128032541 0.0578074081 0.378137501 0.194461902 0.062853837
## 2020-06-30 -0.0121380672 -0.0107722406 0.146223974 0.068216718 0.265992042
## 2020-07-31 -0.0128495635 0.0478934714 0.131777393 0.111189502 0.104592644
## 2020-08-31 0.0283689611 0.0971010186 0.038352668 0.231105196 0.064314175
## 2020-09-30 0.0008255825 -0.1061508537 -0.076543178 0.011895777 0.074989066
## 2020-10-30 -0.0683054795 0.0980591037 0.114707232 -0.076501496 0.087908953
## 2020-11-30 0.2642068328 0.0826848099 0.246439067 0.066921684 0.464194153
## 2020-12-31 0.0122173328 -0.0050446895 0.075543497 -0.025899842 -0.117722046
## 2021-01-29 0.0158398294 0.0467581828 0.060393569 -0.005010640 -0.044691820
## 2021-02-26 0.0653102196 0.1039617233 -0.082794922 0.054292869 0.050157543
## 2021-03-31 0.0664422008 0.0154771554 -0.106825994 -0.026723055 -0.211803691
## 2021-04-30 0.0486225867 0.1527899325 0.064982481 0.117297821 0.112575928
## 2021-05-28 0.0593160505 0.0005973520 -0.145193303 0.079071109 -0.215133884
## 2021-06-30 -0.0547536116 0.0385416919 0.136761863 0.208332021 0.274089959
## 2021-07-30 0.0246653910 0.0760718620 0.006979044 -0.025494004 0.057151268
## 2021-08-31 0.0358418860 0.0730045001 0.174326618 0.138204222 -0.022969655
## 2021-09-30 -0.0836808761 -0.0875715239 -0.106137237 -0.077484763 -0.129879635
## 2021-10-29 0.0291136738 0.1066948595 -0.125705548 0.210396091 0.063515605
## 2021-11-30 0.0144677430 -0.0400331883 -0.220114745 0.245338520 0.322510901
## 2021-12-31 0.0755713667 0.0155159060 0.126302414 -0.105149790 -0.120930501
## 2022-01-31 0.0731288337 -0.0640854511 -0.174879688 -0.183267285 -0.275965706
## 2022-02-28 -0.0216772624 -0.0059684383 -0.004790385 -0.004133316 0.204506753
## 2022-03-31 0.0526101524 0.0346686656 0.054267571 0.112575926 -0.208685749
## 2022-04-29 -0.1170068276 -0.1944949821 -0.200240139 -0.386065585 -0.161542587
## 2022-05-31 0.0629923292 -0.0081002690 -0.214227244 0.006717068 -0.123975782
## 2022-06-30 -0.0904844280 -0.0417810349 -0.210238570 -0.208219470 -0.217157665
## 2022-07-29 0.0349853886 0.0643327907 0.245038519 0.180792227 0.071615379
## 2022-08-31 0.0427808021 -0.0663691584 0.049918351 -0.185089266 0.331698974
## 2022-09-30 -0.0557140424 -0.1268136085 -0.032774962 -0.217576810 -0.048192273
## 2022-10-31 0.1471613308 -0.0156179261 0.085445686 0.106044067 -0.115359160
## 2022-11-30 0.1054538863 0.0692744700 0.032055386 0.226461995 -0.020877642
## 2022-12-30 0.0001391143 -0.1339679520 -0.095445530 -0.146693635 -0.151054787
## 2023-01-31 0.0214528693 0.1182712856 0.333897851 0.290330090 0.123048328
## 2023-02-28 -0.0696419501 -0.1007318766 0.031905291 0.172531591 0.098711202
## 2023-03-30 -0.0583070738 0.1151463315 0.037643216 0.165250472 0.071206306
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AFL GOOG MELI NVDA TTD
## AFL 0.004428827 0.002051137 0.002919884 0.001843240 0.002755357
## GOOG 0.002051137 0.005257723 0.005067509 0.006030606 0.005404455
## MELI 0.002919884 0.005067509 0.018123917 0.006659639 0.010102859
## NVDA 0.001843240 0.006030606 0.006659639 0.020514457 0.011485429
## TTD 0.002755357 0.005404455 0.010102859 0.011485429 0.038359322
# 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.25, 0.25, 0.2, 0.2, 0.1)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.07862154
# 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
## AFL GOOG MELI NVDA TTD
## [1,] 0.009056524 0.0145866 0.02025868 0.0217544 0.01296533
rowSums(component_contribution)
## [1] 0.07862154
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## AFL GOOG MELI NVDA TTD
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.115 0.186 0.258 0.277 0.165
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AFL 0.115
## 2 GOOG 0.186
## 3 MELI 0.258
## 4 NVDA 0.277
## 5 TTD 0.165
# 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
## AFL GOOG MELI NVDA TTD
## 2017-02-28 0.0393267318 0.0326201762 0.128779974 -0.071874646 0.352857823
## 2017-03-31 0.0009674931 0.0076841317 0.003550835 0.070843314 -0.125005215
## 2017-04-28 0.0334041322 0.0880996911 0.079245809 -0.043434073 0.002680967
## 2017-05-31 0.0124932275 0.0629878581 0.183842027 0.326022518 0.387000274
## 2017-06-30 0.0300560311 -0.0599349707 -0.091609946 0.001453523 -0.093112596
## 2017-07-31 0.0262989137 0.0236740767 0.139442995 0.117044719 0.061903342
## 2017-08-31 0.0399074368 0.0094447148 -0.109638246 0.042639236 -0.006398215
## 2017-09-29 -0.0141519888 0.0208389789 0.002339236 0.053601385 0.149474048
## 2017-10-31 0.0302541942 0.0582525579 -0.074627910 0.145700343 0.069242122
## 2017-11-30 0.0490772342 0.0046809143 0.135350383 -0.029244769 -0.293768519
## 2017-12-29 0.0015961376 0.0241716956 0.134682876 -0.036583592 -0.071918829
## 2018-01-31 0.0047733381 0.1115967979 0.207190370 0.239240573 0.058396804
## 2018-02-28 0.0134975298 -0.0573515384 0.002244947 -0.014959128 0.148832461
## 2018-03-29 -0.0154199355 -0.0683058032 -0.084902341 -0.043969064 -0.125589825
## 2018-04-30 0.0405294188 -0.0141135901 -0.048227823 -0.029312688 0.030759445
## 2018-05-31 -0.0055553600 0.0643892025 -0.155058997 0.115145096 0.513713769
## 2018-06-29 -0.0463299950 0.0278664423 0.027470550 -0.062544729 0.092297664
## 2018-07-31 0.0786470092 0.0871652022 0.137258625 0.033048520 -0.106545771
## 2018-08-31 -0.0008932449 0.0007637357 -0.001459173 0.137075582 0.520362545
## 2018-09-28 0.0177905965 -0.0205011202 -0.005681842 0.001210560 0.061702003
## 2018-10-31 -0.0888091014 -0.1028991926 -0.048041483 -0.287373740 -0.200037700
## 2018-11-30 0.0658935087 0.0162678420 0.081260411 -0.253667397 0.142345129
## 2018-12-31 -0.0039431806 -0.0552430740 -0.183885395 -0.202283144 -0.204883762
## 2019-01-31 0.0459014135 0.0750917624 0.217493315 0.073974145 0.206497062
## 2019-02-28 0.0352360550 0.0031748503 0.231438741 0.071593754 0.325336735
## 2019-03-29 0.0173494846 0.0465716130 0.101357223 0.151870178 0.002073378
## 2019-04-30 0.0075712094 0.0128463483 -0.047575680 0.007987533 0.112317864
## 2019-05-31 0.0232864189 -0.0740704511 0.164174109 -0.288679698 -0.107982692
## 2019-06-28 0.0661818964 -0.0208014127 0.069808167 0.192591437 0.136030606
## 2019-07-31 -0.0403964242 0.1183225218 0.015650727 0.026972408 0.144951756
## 2019-08-30 -0.0427214090 -0.0237704621 -0.044118284 -0.006208081 -0.068935868
## 2019-09-30 0.0417622160 0.0256754913 -0.075736762 0.038414633 -0.270350716
## 2019-10-31 0.0159275447 0.0331681677 -0.055404446 0.143946763 0.068263965
## 2019-11-29 0.0360901318 0.0349733697 0.107279970 0.076031806 0.271136585
## 2019-12-31 -0.0360165236 0.0242708227 -0.014993539 0.082162781 -0.013610815
## 2020-01-31 -0.0254632198 0.0701849208 0.147740896 0.004790776 0.035545106
## 2020-02-28 -0.1799183397 -0.0684586737 -0.073479279 0.133626893 0.064972653
## 2020-03-31 -0.2243108878 -0.1413300016 -0.231792513 -0.024248276 -0.397662780
## 2020-04-30 0.0839886784 0.1482720197 0.177558409 0.103279376 0.416047962
## 2020-05-29 -0.0128032541 0.0578074081 0.378137501 0.194461902 0.062853837
## 2020-06-30 -0.0121380672 -0.0107722406 0.146223974 0.068216718 0.265992042
## 2020-07-31 -0.0128495635 0.0478934714 0.131777393 0.111189502 0.104592644
## 2020-08-31 0.0283689611 0.0971010186 0.038352668 0.231105196 0.064314175
## 2020-09-30 0.0008255825 -0.1061508537 -0.076543178 0.011895777 0.074989066
## 2020-10-30 -0.0683054795 0.0980591037 0.114707232 -0.076501496 0.087908953
## 2020-11-30 0.2642068328 0.0826848099 0.246439067 0.066921684 0.464194153
## 2020-12-31 0.0122173328 -0.0050446895 0.075543497 -0.025899842 -0.117722046
## 2021-01-29 0.0158398294 0.0467581828 0.060393569 -0.005010640 -0.044691820
## 2021-02-26 0.0653102196 0.1039617233 -0.082794922 0.054292869 0.050157543
## 2021-03-31 0.0664422008 0.0154771554 -0.106825994 -0.026723055 -0.211803691
## 2021-04-30 0.0486225867 0.1527899325 0.064982481 0.117297821 0.112575928
## 2021-05-28 0.0593160505 0.0005973520 -0.145193303 0.079071109 -0.215133884
## 2021-06-30 -0.0547536116 0.0385416919 0.136761863 0.208332021 0.274089959
## 2021-07-30 0.0246653910 0.0760718620 0.006979044 -0.025494004 0.057151268
## 2021-08-31 0.0358418860 0.0730045001 0.174326618 0.138204222 -0.022969655
## 2021-09-30 -0.0836808761 -0.0875715239 -0.106137237 -0.077484763 -0.129879635
## 2021-10-29 0.0291136738 0.1066948595 -0.125705548 0.210396091 0.063515605
## 2021-11-30 0.0144677430 -0.0400331883 -0.220114745 0.245338520 0.322510901
## 2021-12-31 0.0755713667 0.0155159060 0.126302414 -0.105149790 -0.120930501
## 2022-01-31 0.0731288337 -0.0640854511 -0.174879688 -0.183267285 -0.275965706
## 2022-02-28 -0.0216772624 -0.0059684383 -0.004790385 -0.004133316 0.204506753
## 2022-03-31 0.0526101524 0.0346686656 0.054267571 0.112575926 -0.208685749
## 2022-04-29 -0.1170068276 -0.1944949821 -0.200240139 -0.386065585 -0.161542587
## 2022-05-31 0.0629923292 -0.0081002690 -0.214227244 0.006717068 -0.123975782
## 2022-06-30 -0.0904844280 -0.0417810349 -0.210238570 -0.208219470 -0.217157665
## 2022-07-29 0.0349853886 0.0643327907 0.245038519 0.180792227 0.071615379
## 2022-08-31 0.0427808021 -0.0663691584 0.049918351 -0.185089266 0.331698974
## 2022-09-30 -0.0557140424 -0.1268136085 -0.032774962 -0.217576810 -0.048192273
## 2022-10-31 0.1471613308 -0.0156179261 0.085445686 0.106044067 -0.115359160
## 2022-11-30 0.1054538863 0.0692744700 0.032055386 0.226461995 -0.020877642
## 2022-12-30 0.0001391143 -0.1339679520 -0.095445530 -0.146693635 -0.151054787
## 2023-01-31 0.0214528693 0.1182712856 0.333897851 0.290330090 0.123048328
## 2023-02-28 -0.0696419501 -0.1007318766 0.031905291 0.172531591 0.098711202
## 2023-03-30 -0.0583070738 0.1151463315 0.037643216 0.165250472 0.071206306
calculate_component_contribution <- function(.data, w) {
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 in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
return(component_percentages)
}
asset_return_wide_tbl %>% calculate_component_contribution(w - c(.25, .25, .2,.2, .1))
## # A tibble: 1 × 5
## AFL GOOG MELI NVDA TTD
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NaN NaN NaN NaN NaN
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.25, .25, .2,.2, .1)) %>%
#Transform to long from
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.25, .25, .2,.2, .1)) %>%
# 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 Potfolio 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?
The largest contributor to my portfolio volatility is NVDA with over 25% contribution. I think the risk of my portfolio is mostly concentrated to NVDA followed closely by MELI as these are the top to contributors to my portfolio.