# Load packages
# Core
library(tidyverse)
library(tidyquant)
Collect individual returns into a portfolio by assigning a weight to each stock
five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG”
from 2012-12-31 to 2017-12-31
symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
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
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062312757 -0.0029353766 0.0366062683 0.052133493 4.992291e-02
## 2013-02-28 0.0058912302 -0.0231050789 -0.0129695813 0.016175635 1.267818e-02
## 2013-03-28 0.0009853225 -0.0102352563 0.0129695813 0.040257938 3.726763e-02
## 2013-04-30 0.0096388055 0.0120847586 0.0489678212 0.001222614 1.903053e-02
## 2013-05-31 -0.0202136810 -0.0494834727 -0.0306558318 0.041976329 2.333539e-02
## 2013-06-28 -0.0157786409 -0.0547279997 -0.0271442993 -0.001402985 -1.343462e-02
## 2013-07-31 0.0026879189 0.0131591220 0.0518603677 0.063541281 5.038573e-02
## 2013-08-30 -0.0082977407 -0.0257054659 -0.0197462410 -0.034743533 -3.045151e-02
## 2013-09-30 0.0111434923 0.0695888181 0.0753385048 0.063873653 3.115633e-02
## 2013-10-31 0.0082921584 0.0408616094 0.0320816681 0.034234302 4.526647e-02
## 2013-11-29 -0.0025100682 -0.0025943204 0.0054494506 0.041661011 2.920714e-02
## 2013-12-31 -0.0055827129 -0.0040740563 0.0215281791 0.012892094 2.559568e-02
## 2014-01-31 0.0152913519 -0.0903231061 -0.0534132369 -0.035775204 -3.588412e-02
## 2014-02-28 0.0037575362 0.0332206363 0.0595051200 0.045257431 4.451044e-02
## 2014-03-31 -0.0014819737 0.0380218620 -0.0046027333 0.013315248 8.261183e-03
## 2014-04-30 0.0081828895 0.0077727556 0.0165294498 -0.023184349 6.927010e-03
## 2014-05-30 0.0117217509 0.0290911900 0.0158285223 0.006205651 2.294164e-02
## 2014-06-30 -0.0005752759 0.0237338661 0.0091650895 0.037718502 2.043472e-02
## 2014-07-31 -0.0025126938 0.0135556986 -0.0263796603 -0.052009570 -1.352884e-02
## 2014-08-29 0.0114312420 0.0279046064 0.0018004102 0.043658098 3.870474e-02
## 2014-09-30 -0.0061676840 -0.0808568750 -0.0395983133 -0.061260437 -1.389231e-02
## 2014-10-31 0.0105849968 0.0140966941 -0.0026549065 0.068874503 2.327794e-02
## 2014-11-28 0.0065483015 -0.0155413077 0.0006255358 0.004773735 2.710131e-02
## 2014-12-31 0.0014751631 -0.0404421868 -0.0407468846 0.025295933 -2.539882e-03
## 2015-01-30 0.0203150132 -0.0068957397 0.0062264483 -0.054627878 -3.007685e-02
## 2015-02-27 -0.0089881489 0.0431360353 0.0614504891 0.056914620 5.468175e-02
## 2015-03-31 0.0037402863 -0.0150862344 -0.0143887820 0.010156275 -1.583022e-02
## 2015-04-30 -0.0032326252 0.0662814221 0.0358166031 -0.018417765 9.785923e-03
## 2015-05-29 -0.0043838746 -0.0419109871 0.0019527033 0.007510021 1.277413e-02
## 2015-06-30 -0.0108257909 -0.0297467198 -0.0316787437 0.004171401 -2.052125e-02
## 2015-07-31 0.0085847496 -0.0651783072 0.0201143772 -0.027375334 2.233761e-02
## 2015-08-31 -0.0033640968 -0.0925121891 -0.0771524311 -0.047268431 -6.288658e-02
## 2015-09-30 0.0080817391 -0.0318249694 -0.0451948440 -0.038464760 -2.584703e-02
## 2015-10-30 0.0006856285 0.0618084723 0.0640259413 0.063589762 8.163498e-02
## 2015-11-30 -0.0038983778 -0.0255605440 -0.0075559480 0.024415234 3.648612e-03
## 2015-12-31 -0.0019186138 -0.0389470315 -0.0235948720 -0.052156892 -1.743350e-02
## 2016-01-29 0.0123296532 -0.0516367193 -0.0567578901 -0.060306846 -5.106891e-02
## 2016-02-29 0.0088319268 -0.0082115858 -0.0339138171 0.020605065 -8.262119e-04
## 2016-03-31 0.0087084221 0.1218790109 0.0637456071 0.089910215 6.510003e-02
## 2016-04-29 0.0025462471 0.0040791614 0.0219750690 0.021044415 3.933581e-03
## 2016-05-31 0.0001355428 -0.0376285621 -0.0008560276 0.004396808 1.686837e-02
## 2016-06-30 0.0191667040 0.0445823593 -0.0244916585 0.008292575 3.470132e-03
## 2016-07-29 0.0054297093 0.0524424917 0.0390003361 0.049348286 3.582176e-02
## 2016-08-31 -0.0021562448 0.0087983409 0.0053269932 0.011261075 1.196951e-03
## 2016-09-30 0.0005156333 0.0248729848 0.0132789612 0.008614676 5.799761e-05
## 2016-10-31 -0.0082049007 -0.0083121393 -0.0224038310 -0.038134898 -1.748908e-02
## 2016-11-30 -0.0259896259 -0.0451619762 -0.0179743762 0.125246743 3.617624e-02
## 2016-12-30 0.0025381835 -0.0025298836 0.0267029082 0.031491329 2.006877e-02
## 2017-01-31 0.0021259760 0.0644313492 0.0323820620 -0.012143670 1.773643e-02
## 2017-02-28 0.0064378625 0.0172578478 0.0118364521 0.013428563 3.853949e-02
## 2017-03-31 -0.0005528840 0.0361888431 0.0318055250 -0.006532688 1.248928e-03
## 2017-04-28 0.0090294639 0.0168665520 0.0239523250 0.005107721 9.877421e-03
## 2017-05-31 0.0068474937 0.0280599501 0.0348101849 -0.022862847 1.401413e-02
## 2017-06-30 -0.0001827913 0.0092237243 0.0029559203 0.029151768 6.354760e-03
## 2017-07-31 0.0033343542 0.0565944225 0.0261877968 0.007481577 2.034567e-02
## 2017-08-31 0.0093691796 0.0232439074 -0.0004482412 -0.027564568 2.913588e-03
## 2017-09-29 -0.0057322178 -0.0004462721 0.0233426361 0.082321903 1.994922e-02
## 2017-10-31 0.0009779632 0.0322785151 0.0166538329 0.005915794 2.329058e-02
## 2017-11-30 -0.0014838854 -0.0038970436 0.0068699324 0.036913279 3.010800e-02
## 2017-12-29 0.0047400736 0.0369254672 0.0133984228 -0.003731152 1.205494e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398362e-05 0.0001042106 4.178346e-05 -7.811749e-05 -9.029039e-06
## EEM 1.042106e-04 0.0017547132 1.039017e-03 6.437730e-04 6.795428e-04
## EFA 4.178346e-05 0.0010390166 1.064237e-03 6.490285e-04 6.975398e-04
## IJS -7.811749e-05 0.0006437730 6.490285e-04 1.565450e-03 8.290253e-04
## SPY -9.029039e-06 0.0006795428 6.975398e-04 8.290253e-04 7.408275e-04
# 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.02347492
# 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
## AGG EEM EFA IJS SPY
## [1,] 0.0003874225 0.009257148 0.00581563 0.005684468 0.00233025
rowSums(component_contribution)
## [1] 0.02347492
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AGG 0.017
## 2 EEM 0.394
## 3 EFA 0.248
## 4 IJS 0.242
## 5 SPY 0.099
# 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
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062312757 -0.0029353766 0.0366062683 0.052133493 4.992291e-02
## 2013-02-28 0.0058912302 -0.0231050789 -0.0129695813 0.016175635 1.267818e-02
## 2013-03-28 0.0009853225 -0.0102352563 0.0129695813 0.040257938 3.726763e-02
## 2013-04-30 0.0096388055 0.0120847586 0.0489678212 0.001222614 1.903053e-02
## 2013-05-31 -0.0202136810 -0.0494834727 -0.0306558318 0.041976329 2.333539e-02
## 2013-06-28 -0.0157786409 -0.0547279997 -0.0271442993 -0.001402985 -1.343462e-02
## 2013-07-31 0.0026879189 0.0131591220 0.0518603677 0.063541281 5.038573e-02
## 2013-08-30 -0.0082977407 -0.0257054659 -0.0197462410 -0.034743533 -3.045151e-02
## 2013-09-30 0.0111434923 0.0695888181 0.0753385048 0.063873653 3.115633e-02
## 2013-10-31 0.0082921584 0.0408616094 0.0320816681 0.034234302 4.526647e-02
## 2013-11-29 -0.0025100682 -0.0025943204 0.0054494506 0.041661011 2.920714e-02
## 2013-12-31 -0.0055827129 -0.0040740563 0.0215281791 0.012892094 2.559568e-02
## 2014-01-31 0.0152913519 -0.0903231061 -0.0534132369 -0.035775204 -3.588412e-02
## 2014-02-28 0.0037575362 0.0332206363 0.0595051200 0.045257431 4.451044e-02
## 2014-03-31 -0.0014819737 0.0380218620 -0.0046027333 0.013315248 8.261183e-03
## 2014-04-30 0.0081828895 0.0077727556 0.0165294498 -0.023184349 6.927010e-03
## 2014-05-30 0.0117217509 0.0290911900 0.0158285223 0.006205651 2.294164e-02
## 2014-06-30 -0.0005752759 0.0237338661 0.0091650895 0.037718502 2.043472e-02
## 2014-07-31 -0.0025126938 0.0135556986 -0.0263796603 -0.052009570 -1.352884e-02
## 2014-08-29 0.0114312420 0.0279046064 0.0018004102 0.043658098 3.870474e-02
## 2014-09-30 -0.0061676840 -0.0808568750 -0.0395983133 -0.061260437 -1.389231e-02
## 2014-10-31 0.0105849968 0.0140966941 -0.0026549065 0.068874503 2.327794e-02
## 2014-11-28 0.0065483015 -0.0155413077 0.0006255358 0.004773735 2.710131e-02
## 2014-12-31 0.0014751631 -0.0404421868 -0.0407468846 0.025295933 -2.539882e-03
## 2015-01-30 0.0203150132 -0.0068957397 0.0062264483 -0.054627878 -3.007685e-02
## 2015-02-27 -0.0089881489 0.0431360353 0.0614504891 0.056914620 5.468175e-02
## 2015-03-31 0.0037402863 -0.0150862344 -0.0143887820 0.010156275 -1.583022e-02
## 2015-04-30 -0.0032326252 0.0662814221 0.0358166031 -0.018417765 9.785923e-03
## 2015-05-29 -0.0043838746 -0.0419109871 0.0019527033 0.007510021 1.277413e-02
## 2015-06-30 -0.0108257909 -0.0297467198 -0.0316787437 0.004171401 -2.052125e-02
## 2015-07-31 0.0085847496 -0.0651783072 0.0201143772 -0.027375334 2.233761e-02
## 2015-08-31 -0.0033640968 -0.0925121891 -0.0771524311 -0.047268431 -6.288658e-02
## 2015-09-30 0.0080817391 -0.0318249694 -0.0451948440 -0.038464760 -2.584703e-02
## 2015-10-30 0.0006856285 0.0618084723 0.0640259413 0.063589762 8.163498e-02
## 2015-11-30 -0.0038983778 -0.0255605440 -0.0075559480 0.024415234 3.648612e-03
## 2015-12-31 -0.0019186138 -0.0389470315 -0.0235948720 -0.052156892 -1.743350e-02
## 2016-01-29 0.0123296532 -0.0516367193 -0.0567578901 -0.060306846 -5.106891e-02
## 2016-02-29 0.0088319268 -0.0082115858 -0.0339138171 0.020605065 -8.262119e-04
## 2016-03-31 0.0087084221 0.1218790109 0.0637456071 0.089910215 6.510003e-02
## 2016-04-29 0.0025462471 0.0040791614 0.0219750690 0.021044415 3.933581e-03
## 2016-05-31 0.0001355428 -0.0376285621 -0.0008560276 0.004396808 1.686837e-02
## 2016-06-30 0.0191667040 0.0445823593 -0.0244916585 0.008292575 3.470132e-03
## 2016-07-29 0.0054297093 0.0524424917 0.0390003361 0.049348286 3.582176e-02
## 2016-08-31 -0.0021562448 0.0087983409 0.0053269932 0.011261075 1.196951e-03
## 2016-09-30 0.0005156333 0.0248729848 0.0132789612 0.008614676 5.799761e-05
## 2016-10-31 -0.0082049007 -0.0083121393 -0.0224038310 -0.038134898 -1.748908e-02
## 2016-11-30 -0.0259896259 -0.0451619762 -0.0179743762 0.125246743 3.617624e-02
## 2016-12-30 0.0025381835 -0.0025298836 0.0267029082 0.031491329 2.006877e-02
## 2017-01-31 0.0021259760 0.0644313492 0.0323820620 -0.012143670 1.773643e-02
## 2017-02-28 0.0064378625 0.0172578478 0.0118364521 0.013428563 3.853949e-02
## 2017-03-31 -0.0005528840 0.0361888431 0.0318055250 -0.006532688 1.248928e-03
## 2017-04-28 0.0090294639 0.0168665520 0.0239523250 0.005107721 9.877421e-03
## 2017-05-31 0.0068474937 0.0280599501 0.0348101849 -0.022862847 1.401413e-02
## 2017-06-30 -0.0001827913 0.0092237243 0.0029559203 0.029151768 6.354760e-03
## 2017-07-31 0.0033343542 0.0565944225 0.0261877968 0.007481577 2.034567e-02
## 2017-08-31 0.0093691796 0.0232439074 -0.0004482412 -0.027564568 2.913588e-03
## 2017-09-29 -0.0057322178 -0.0004462721 0.0233426361 0.082321903 1.994922e-02
## 2017-10-31 0.0009779632 0.0322785151 0.0166538329 0.005915794 2.329058e-02
## 2017-11-30 -0.0014838854 -0.0038970436 0.0068699324 0.036913279 3.010800e-02
## 2017-12-29 0.0047400736 0.0369254672 0.0133984228 -0.003731152 1.205494e-02
cal_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
# 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
# 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
rowSums(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 %>% cal_component_contribution(w = c(.25, .25, .2, .2, .1))
## # A tibble: 1 × 5
## AGG EEM EFA IJS SPY
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.017 0.394 0.248 0.242 0.099
plot_data <- asset_returns_wide_tbl %>%
cal_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
#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")
plot_data <- asset_returns_wide_tbl %>%
cal_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
#Transform to long form
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 Portfolio Volatility and Weight",
y = "percent",
x = NULL)