# 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.
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"))
Refresh your memory on covariance with this video. Click this link Refresh your memory on matrix multiplication. Click this link
# 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.0062304940 -0.0029354071 0.0366064159 0.052133219 0.0499235181
## 2013-02-28 0.0058909802 -0.0231050935 -0.0129693901 0.016175435 0.0126775118
## 2013-03-28 0.0009851146 -0.0102350850 0.0129693901 0.040258007 0.0372684339
## 2013-04-30 0.0096388952 0.0120847531 0.0489676956 0.001222724 0.0190300356
## 2013-05-31 -0.0202133634 -0.0494835190 -0.0306556806 0.041975929 0.0233357232
## 2013-06-28 -0.0157786211 -0.0547286931 -0.0271442170 -0.001403078 -0.0134347971
## 2013-07-31 0.0026880604 0.0131602791 0.0518603128 0.063541776 0.0503859006
## 2013-08-30 -0.0082984256 -0.0257055141 -0.0197465165 -0.034743452 -0.0304512031
## 2013-09-30 0.0111442916 0.0695886663 0.0753387222 0.063873476 0.0311560424
## 2013-10-31 0.0082911946 0.0408612834 0.0320815623 0.034234172 0.0452664695
## 2013-11-29 -0.0025093547 -0.0025942367 0.0054498019 0.041661146 0.0292070074
## 2013-12-31 -0.0055830837 -0.0040742862 0.0215279720 0.012892197 0.0255964661
## 2014-01-31 0.0152918216 -0.0903227587 -0.0534133484 -0.035775247 -0.0358846412
## 2014-02-28 0.0037569758 0.0332206168 0.0595050856 0.045257550 0.0445099578
## 2014-03-31 -0.0014820720 0.0380218988 -0.0046024881 0.013314932 0.0082614072
## 2014-04-30 0.0081831923 0.0077725481 0.0165293642 -0.023184352 0.0069276689
## 2014-05-30 0.0117214427 0.0290910466 0.0158284550 0.006205425 0.0229411757
## 2014-06-30 -0.0005751373 0.0237342621 0.0091654713 0.037718647 0.0204344504
## 2014-07-31 -0.0025123727 0.0135556484 -0.0263798696 -0.052009334 -0.0135286670
## 2014-08-29 0.0114316363 0.0279044737 0.0018005324 0.043658113 0.0387046260
## 2014-09-30 -0.0061682457 -0.0808568108 -0.0395987672 -0.061260555 -0.0138919821
## 2014-10-31 0.0105849369 0.0140964160 -0.0026547516 0.068874636 0.0232778286
## 2014-11-28 0.0065486472 -0.0155412297 0.0006253216 0.004773720 0.0271012044
## 2014-12-31 0.0014747444 -0.0404420222 -0.0407466735 0.025295827 -0.0025394209
## 2015-01-30 0.0203152199 -0.0068956450 0.0062264480 -0.054627774 -0.0300771760
## 2015-02-27 -0.0089879274 0.0431360084 0.0614506250 0.056914555 0.0546815790
## 2015-03-31 0.0037397899 -0.0150862520 -0.0143888162 0.010156248 -0.0158302794
## 2015-04-30 -0.0032328497 0.0662812785 0.0358165749 -0.018417747 0.0097858690
## 2015-05-29 -0.0043836127 -0.0419109667 0.0019525465 0.007509871 0.0127744519
## 2015-06-30 -0.0108253300 -0.0297469345 -0.0316786131 0.004171368 -0.0205212062
## 2015-07-31 0.0085845667 -0.0651776577 0.0201143795 -0.027375029 0.0223377850
## 2015-08-31 -0.0033641792 -0.0925126935 -0.0771523511 -0.047268539 -0.0628867598
## 2015-09-30 0.0080816143 -0.0318248810 -0.0451949944 -0.038464503 -0.0258469601
## 2015-10-30 0.0006857256 0.0618083585 0.0640262095 0.063589375 0.0816350380
## 2015-11-30 -0.0038984356 -0.0255603828 -0.0075560069 0.024415575 0.0036481780
## 2015-12-31 -0.0019190442 -0.0389471565 -0.0235950033 -0.052157353 -0.0174334902
## 2016-01-29 0.0123299761 -0.0516367714 -0.0567579354 -0.060307117 -0.0510686199
## 2016-02-29 0.0088317825 -0.0082116569 -0.0339138809 0.020605610 -0.0008264435
## 2016-03-31 0.0087087705 0.1218791507 0.0637455540 0.089910116 0.0651005033
## 2016-04-29 0.0025465377 0.0040790660 0.0219751216 0.021044462 0.0039330880
## 2016-05-31 0.0001352390 -0.0376281951 -0.0008558564 0.004397156 0.0168689245
## 2016-06-30 0.0191667751 0.0445821242 -0.0244918954 0.008292162 0.0034694956
## 2016-07-29 0.0054297832 0.0524421535 0.0390006536 0.049348215 0.0358220818
## 2016-08-31 -0.0021562632 0.0087985902 0.0053265698 0.011261404 0.0011969929
## 2016-09-30 0.0005159604 0.0248730249 0.0132792719 0.008614563 0.0000578254
## 2016-10-31 -0.0082056424 -0.0083124176 -0.0224037090 -0.038134994 -0.0174888333
## 2016-11-30 -0.0259896237 -0.0451615611 -0.0179744060 0.125246252 0.0361758157
## 2016-12-30 0.0025382793 -0.0025298962 0.0267028050 0.031491928 0.0200688630
## 2017-01-31 0.0021262843 0.0644312609 0.0323820093 -0.012143739 0.0177368372
## 2017-02-28 0.0064378127 0.0172579235 0.0118361289 0.013428445 0.0385390936
## 2017-03-31 -0.0005526804 0.0361888450 0.0318058203 -0.006532856 0.0012489355
## 2017-04-28 0.0090287454 0.0168663647 0.0239523771 0.005108014 0.0098771890
## 2017-05-31 0.0068475856 0.0280601489 0.0348100690 -0.022862779 0.0140146197
## 2017-06-30 -0.0001822162 0.0092235594 0.0029560439 0.029151590 0.0063547883
## 2017-07-31 0.0033344528 0.0565946442 0.0261879768 0.007481474 0.0203455502
## 2017-08-31 0.0093684867 0.0232435387 -0.0004484989 -0.027564637 0.0029135951
## 2017-09-29 -0.0057324460 -0.0004460301 0.0233429593 0.082321800 0.0199491245
## 2017-10-31 0.0009785077 0.0322784737 0.0166534534 0.005916226 0.0232906708
## 2017-11-30 -0.0014843241 -0.0038971349 0.0068700184 0.036913233 0.0301082154
## 2017-12-29 0.0047402361 0.0369256378 0.0133985033 -0.003731062 0.0120547432
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398384e-05 0.0001042132 4.178523e-05 -7.811402e-05 -9.028359e-06
## EEM 1.042132e-04 0.0017547113 1.039020e-03 6.437749e-04 6.795441e-04
## EFA 4.178523e-05 0.0010390195 1.064240e-03 6.490325e-04 6.975413e-04
## IJS -7.811402e-05 0.0006437749 6.490325e-04 1.565448e-03 8.290240e-04
## SPY -9.028359e-06 0.0006795441 6.975413e-04 8.290240e-04 7.408294e-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.02347495
# 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.0003874413 0.009257147 0.005815643 0.005684473 0.002330249
rowSums(component_contribution)
## [1] 0.02347495
# 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.0062304940 -0.0029354071 0.0366064159 0.052133219 0.0499235181
## 2013-02-28 0.0058909802 -0.0231050935 -0.0129693901 0.016175435 0.0126775118
## 2013-03-28 0.0009851146 -0.0102350850 0.0129693901 0.040258007 0.0372684339
## 2013-04-30 0.0096388952 0.0120847531 0.0489676956 0.001222724 0.0190300356
## 2013-05-31 -0.0202133634 -0.0494835190 -0.0306556806 0.041975929 0.0233357232
## 2013-06-28 -0.0157786211 -0.0547286931 -0.0271442170 -0.001403078 -0.0134347971
## 2013-07-31 0.0026880604 0.0131602791 0.0518603128 0.063541776 0.0503859006
## 2013-08-30 -0.0082984256 -0.0257055141 -0.0197465165 -0.034743452 -0.0304512031
## 2013-09-30 0.0111442916 0.0695886663 0.0753387222 0.063873476 0.0311560424
## 2013-10-31 0.0082911946 0.0408612834 0.0320815623 0.034234172 0.0452664695
## 2013-11-29 -0.0025093547 -0.0025942367 0.0054498019 0.041661146 0.0292070074
## 2013-12-31 -0.0055830837 -0.0040742862 0.0215279720 0.012892197 0.0255964661
## 2014-01-31 0.0152918216 -0.0903227587 -0.0534133484 -0.035775247 -0.0358846412
## 2014-02-28 0.0037569758 0.0332206168 0.0595050856 0.045257550 0.0445099578
## 2014-03-31 -0.0014820720 0.0380218988 -0.0046024881 0.013314932 0.0082614072
## 2014-04-30 0.0081831923 0.0077725481 0.0165293642 -0.023184352 0.0069276689
## 2014-05-30 0.0117214427 0.0290910466 0.0158284550 0.006205425 0.0229411757
## 2014-06-30 -0.0005751373 0.0237342621 0.0091654713 0.037718647 0.0204344504
## 2014-07-31 -0.0025123727 0.0135556484 -0.0263798696 -0.052009334 -0.0135286670
## 2014-08-29 0.0114316363 0.0279044737 0.0018005324 0.043658113 0.0387046260
## 2014-09-30 -0.0061682457 -0.0808568108 -0.0395987672 -0.061260555 -0.0138919821
## 2014-10-31 0.0105849369 0.0140964160 -0.0026547516 0.068874636 0.0232778286
## 2014-11-28 0.0065486472 -0.0155412297 0.0006253216 0.004773720 0.0271012044
## 2014-12-31 0.0014747444 -0.0404420222 -0.0407466735 0.025295827 -0.0025394209
## 2015-01-30 0.0203152199 -0.0068956450 0.0062264480 -0.054627774 -0.0300771760
## 2015-02-27 -0.0089879274 0.0431360084 0.0614506250 0.056914555 0.0546815790
## 2015-03-31 0.0037397899 -0.0150862520 -0.0143888162 0.010156248 -0.0158302794
## 2015-04-30 -0.0032328497 0.0662812785 0.0358165749 -0.018417747 0.0097858690
## 2015-05-29 -0.0043836127 -0.0419109667 0.0019525465 0.007509871 0.0127744519
## 2015-06-30 -0.0108253300 -0.0297469345 -0.0316786131 0.004171368 -0.0205212062
## 2015-07-31 0.0085845667 -0.0651776577 0.0201143795 -0.027375029 0.0223377850
## 2015-08-31 -0.0033641792 -0.0925126935 -0.0771523511 -0.047268539 -0.0628867598
## 2015-09-30 0.0080816143 -0.0318248810 -0.0451949944 -0.038464503 -0.0258469601
## 2015-10-30 0.0006857256 0.0618083585 0.0640262095 0.063589375 0.0816350380
## 2015-11-30 -0.0038984356 -0.0255603828 -0.0075560069 0.024415575 0.0036481780
## 2015-12-31 -0.0019190442 -0.0389471565 -0.0235950033 -0.052157353 -0.0174334902
## 2016-01-29 0.0123299761 -0.0516367714 -0.0567579354 -0.060307117 -0.0510686199
## 2016-02-29 0.0088317825 -0.0082116569 -0.0339138809 0.020605610 -0.0008264435
## 2016-03-31 0.0087087705 0.1218791507 0.0637455540 0.089910116 0.0651005033
## 2016-04-29 0.0025465377 0.0040790660 0.0219751216 0.021044462 0.0039330880
## 2016-05-31 0.0001352390 -0.0376281951 -0.0008558564 0.004397156 0.0168689245
## 2016-06-30 0.0191667751 0.0445821242 -0.0244918954 0.008292162 0.0034694956
## 2016-07-29 0.0054297832 0.0524421535 0.0390006536 0.049348215 0.0358220818
## 2016-08-31 -0.0021562632 0.0087985902 0.0053265698 0.011261404 0.0011969929
## 2016-09-30 0.0005159604 0.0248730249 0.0132792719 0.008614563 0.0000578254
## 2016-10-31 -0.0082056424 -0.0083124176 -0.0224037090 -0.038134994 -0.0174888333
## 2016-11-30 -0.0259896237 -0.0451615611 -0.0179744060 0.125246252 0.0361758157
## 2016-12-30 0.0025382793 -0.0025298962 0.0267028050 0.031491928 0.0200688630
## 2017-01-31 0.0021262843 0.0644312609 0.0323820093 -0.012143739 0.0177368372
## 2017-02-28 0.0064378127 0.0172579235 0.0118361289 0.013428445 0.0385390936
## 2017-03-31 -0.0005526804 0.0361888450 0.0318058203 -0.006532856 0.0012489355
## 2017-04-28 0.0090287454 0.0168663647 0.0239523771 0.005108014 0.0098771890
## 2017-05-31 0.0068475856 0.0280601489 0.0348100690 -0.022862779 0.0140146197
## 2017-06-30 -0.0001822162 0.0092235594 0.0029560439 0.029151590 0.0063547883
## 2017-07-31 0.0033344528 0.0565946442 0.0261879768 0.007481474 0.0203455502
## 2017-08-31 0.0093684867 0.0232435387 -0.0004484989 -0.027564637 0.0029135951
## 2017-09-29 -0.0057324460 -0.0004460301 0.0233429593 0.082321800 0.0199491245
## 2017-10-31 0.0009785077 0.0322784737 0.0166534534 0.005916226 0.0232906708
## 2017-11-30 -0.0014843241 -0.0038971349 0.0068700184 0.036913233 0.0301082154
## 2017-12-29 0.0047402361 0.0369256378 0.0133985033 -0.003731062 0.0120547432
calculate_componet_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_componet_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
Column Chart of Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_componet_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")
Column Chart of Component Contribution and weight
plot_data <- asset_returns_wide_tbl %>%
calculate_componet_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)