# 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.0062302871 -0.0029359047 0.0366063041 0.052133200 4.992258e-02
## 2013-02-28 0.0058912025 -0.0231052356 -0.0129696618 0.016175614 1.267858e-02
## 2013-03-28 0.0009840015 -0.0102348976 0.0129696618 0.040257920 3.726786e-02
## 2013-04-30 0.0096393894 0.0120846781 0.0489678045 0.001222682 1.903012e-02
## 2013-05-31 -0.0202144468 -0.0494832642 -0.0306558616 0.041976232 2.333528e-02
## 2013-06-28 -0.0157773076 -0.0547285730 -0.0271442026 -0.001403003 -1.343412e-02
## 2013-07-31 0.0026876697 0.0131601342 0.0518602560 0.063541391 5.038590e-02
## 2013-08-30 -0.0082983777 -0.0257057324 -0.0197462881 -0.034743707 -3.045155e-02
## 2013-09-30 0.0111435176 0.0695887138 0.0753384984 0.063873778 3.115611e-02
## 2013-10-31 0.0082923147 0.0408614106 0.0320817953 0.034234151 4.526646e-02
## 2013-11-29 -0.0025095513 -0.0025941412 0.0054493836 0.041661124 2.920681e-02
## 2013-12-31 -0.0055834081 -0.0040743613 0.0215282128 0.012892217 2.559631e-02
## 2014-01-31 0.0152915423 -0.0903226742 -0.0534133024 -0.035775718 -3.588413e-02
## 2014-02-28 0.0037571044 0.0332204781 0.0595051622 0.045257512 4.451010e-02
## 2014-03-31 -0.0014816528 0.0380219951 -0.0046024885 0.013315590 8.261037e-03
## 2014-04-30 0.0081834237 0.0077726251 0.0165291383 -0.023184199 6.927567e-03
## 2014-05-30 0.0117212410 0.0290913005 0.0158285748 0.006205060 2.294114e-02
## 2014-06-30 -0.0005757314 0.0237337188 0.0091653328 0.037718728 2.043487e-02
## 2014-07-31 -0.0025118242 0.0135554647 -0.0263796274 -0.052009104 -1.352853e-02
## 2014-08-29 0.0114309374 0.0279046866 0.0018004223 0.043657538 3.870463e-02
## 2014-09-30 -0.0061674683 -0.0808566908 -0.0395988010 -0.061260349 -1.389224e-02
## 2014-10-31 0.0105843523 0.0140964533 -0.0026548115 0.068874970 2.327761e-02
## 2014-11-28 0.0065484984 -0.0155414124 0.0006254686 0.004773570 2.710152e-02
## 2014-12-31 0.0014755843 -0.0404419977 -0.0407468928 0.025295965 -2.539875e-03
## 2015-01-30 0.0203153307 -0.0068956168 0.0062267519 -0.054628091 -3.007699e-02
## 2015-02-27 -0.0089880583 0.0431359040 0.0614505303 0.056914632 5.468184e-02
## 2015-03-31 0.0037399490 -0.0150859040 -0.0143888617 0.010156304 -1.583005e-02
## 2015-04-30 -0.0032332530 0.0662810726 0.0358165822 -0.018417676 9.785473e-03
## 2015-05-29 -0.0043834654 -0.0419109062 0.0019527989 0.007509954 1.277455e-02
## 2015-06-30 -0.0108254179 -0.0297468452 -0.0316789006 0.004171234 -2.052136e-02
## 2015-07-31 0.0085841319 -0.0651782666 0.0201144059 -0.027375276 2.233786e-02
## 2015-08-31 -0.0033633802 -0.0925120915 -0.0771524922 -0.047268261 -6.288647e-02
## 2015-09-30 0.0080815170 -0.0318249940 -0.0451948142 -0.038464871 -2.584726e-02
## 2015-10-30 0.0006853483 0.0618085079 0.0640258921 0.063589723 8.163500e-02
## 2015-11-30 -0.0038980689 -0.0255606045 -0.0075558806 0.024415234 3.648220e-03
## 2015-12-31 -0.0019193720 -0.0389471047 -0.0235950314 -0.052156956 -1.743365e-02
## 2016-01-29 0.0123302753 -0.0516366796 -0.0567579130 -0.060306943 -5.106844e-02
## 2016-02-29 0.0088314052 -0.0082115302 -0.0339139989 0.020605230 -8.264234e-04
## 2016-03-31 0.0087091057 0.1218788516 0.0637459623 0.089910429 6.510043e-02
## 2016-04-29 0.0025459919 0.0040792832 0.0219750146 0.021044220 3.933239e-03
## 2016-05-31 0.0001357112 -0.0376284439 -0.0008560461 0.004397100 1.686870e-02
## 2016-06-30 0.0191665447 0.0445824706 -0.0244913662 0.008292264 3.469734e-03
## 2016-07-29 0.0054298198 0.0524423407 0.0390000281 0.049348251 3.582198e-02
## 2016-08-31 -0.0021564333 0.0087981012 0.0053268633 0.011261008 1.196862e-03
## 2016-09-30 0.0005161153 0.0248730610 0.0132790784 0.008614859 5.803865e-05
## 2016-10-31 -0.0082054612 -0.0083120689 -0.0224035875 -0.038134750 -1.748908e-02
## 2016-11-30 -0.0259897323 -0.0451618935 -0.0179743989 0.125246472 3.617596e-02
## 2016-12-30 0.0025381533 -0.0025300101 0.0267027783 0.031491721 2.006889e-02
## 2017-01-31 0.0021261782 0.0644312752 0.0323819590 -0.012143845 1.773664e-02
## 2017-02-28 0.0064377549 0.0172579591 0.0118363924 0.013428914 3.853938e-02
## 2017-03-31 -0.0005533261 0.0361888723 0.0318057133 -0.006533628 1.248925e-03
## 2017-04-28 0.0090299038 0.0168666252 0.0239522160 0.005108216 9.877368e-03
## 2017-05-31 0.0068475203 0.0280597656 0.0348103315 -0.022863006 1.401429e-02
## 2017-06-30 -0.0001829327 0.0092238738 0.0029557828 0.029152257 6.354682e-03
## 2017-07-31 0.0033346327 0.0565945416 0.0261879397 0.007480775 2.034564e-02
## 2017-08-31 0.0093691925 0.0232437716 -0.0004484333 -0.027564235 2.913630e-03
## 2017-09-29 -0.0057326478 -0.0004460965 0.0233429833 0.082321689 1.994907e-02
## 2017-10-31 0.0009782093 0.0322782757 0.0166535277 0.005916270 2.329063e-02
## 2017-11-30 -0.0014840174 -0.0038969524 0.0068700389 0.036913008 3.010798e-02
## 2017-12-29 0.0047402862 0.0369253964 0.0133983369 -0.003731197 1.205532e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398371e-05 0.0001042100 4.178209e-05 -7.812042e-05 -9.031420e-06
## EEM 1.042100e-04 0.0017547091 1.039017e-03 6.437741e-04 6.795412e-04
## EFA 4.178209e-05 0.0010390174 1.064239e-03 6.490300e-04 6.975398e-04
## IJS -7.812042e-05 0.0006437741 6.490300e-04 1.565450e-03 8.290234e-04
## SPY -9.031420e-06 0.0006795412 6.975398e-04 8.290234e-04 7.408267e-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.0234749
# 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.0003874096 0.009257143 0.005815637 0.005684469 0.002330245
rowSums(component_contribution)
## [1] 0.0234749
# 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.0062302871 -0.0029359047 0.0366063041 0.052133200 4.992258e-02
## 2013-02-28 0.0058912025 -0.0231052356 -0.0129696618 0.016175614 1.267858e-02
## 2013-03-28 0.0009840015 -0.0102348976 0.0129696618 0.040257920 3.726786e-02
## 2013-04-30 0.0096393894 0.0120846781 0.0489678045 0.001222682 1.903012e-02
## 2013-05-31 -0.0202144468 -0.0494832642 -0.0306558616 0.041976232 2.333528e-02
## 2013-06-28 -0.0157773076 -0.0547285730 -0.0271442026 -0.001403003 -1.343412e-02
## 2013-07-31 0.0026876697 0.0131601342 0.0518602560 0.063541391 5.038590e-02
## 2013-08-30 -0.0082983777 -0.0257057324 -0.0197462881 -0.034743707 -3.045155e-02
## 2013-09-30 0.0111435176 0.0695887138 0.0753384984 0.063873778 3.115611e-02
## 2013-10-31 0.0082923147 0.0408614106 0.0320817953 0.034234151 4.526646e-02
## 2013-11-29 -0.0025095513 -0.0025941412 0.0054493836 0.041661124 2.920681e-02
## 2013-12-31 -0.0055834081 -0.0040743613 0.0215282128 0.012892217 2.559631e-02
## 2014-01-31 0.0152915423 -0.0903226742 -0.0534133024 -0.035775718 -3.588413e-02
## 2014-02-28 0.0037571044 0.0332204781 0.0595051622 0.045257512 4.451010e-02
## 2014-03-31 -0.0014816528 0.0380219951 -0.0046024885 0.013315590 8.261037e-03
## 2014-04-30 0.0081834237 0.0077726251 0.0165291383 -0.023184199 6.927567e-03
## 2014-05-30 0.0117212410 0.0290913005 0.0158285748 0.006205060 2.294114e-02
## 2014-06-30 -0.0005757314 0.0237337188 0.0091653328 0.037718728 2.043487e-02
## 2014-07-31 -0.0025118242 0.0135554647 -0.0263796274 -0.052009104 -1.352853e-02
## 2014-08-29 0.0114309374 0.0279046866 0.0018004223 0.043657538 3.870463e-02
## 2014-09-30 -0.0061674683 -0.0808566908 -0.0395988010 -0.061260349 -1.389224e-02
## 2014-10-31 0.0105843523 0.0140964533 -0.0026548115 0.068874970 2.327761e-02
## 2014-11-28 0.0065484984 -0.0155414124 0.0006254686 0.004773570 2.710152e-02
## 2014-12-31 0.0014755843 -0.0404419977 -0.0407468928 0.025295965 -2.539875e-03
## 2015-01-30 0.0203153307 -0.0068956168 0.0062267519 -0.054628091 -3.007699e-02
## 2015-02-27 -0.0089880583 0.0431359040 0.0614505303 0.056914632 5.468184e-02
## 2015-03-31 0.0037399490 -0.0150859040 -0.0143888617 0.010156304 -1.583005e-02
## 2015-04-30 -0.0032332530 0.0662810726 0.0358165822 -0.018417676 9.785473e-03
## 2015-05-29 -0.0043834654 -0.0419109062 0.0019527989 0.007509954 1.277455e-02
## 2015-06-30 -0.0108254179 -0.0297468452 -0.0316789006 0.004171234 -2.052136e-02
## 2015-07-31 0.0085841319 -0.0651782666 0.0201144059 -0.027375276 2.233786e-02
## 2015-08-31 -0.0033633802 -0.0925120915 -0.0771524922 -0.047268261 -6.288647e-02
## 2015-09-30 0.0080815170 -0.0318249940 -0.0451948142 -0.038464871 -2.584726e-02
## 2015-10-30 0.0006853483 0.0618085079 0.0640258921 0.063589723 8.163500e-02
## 2015-11-30 -0.0038980689 -0.0255606045 -0.0075558806 0.024415234 3.648220e-03
## 2015-12-31 -0.0019193720 -0.0389471047 -0.0235950314 -0.052156956 -1.743365e-02
## 2016-01-29 0.0123302753 -0.0516366796 -0.0567579130 -0.060306943 -5.106844e-02
## 2016-02-29 0.0088314052 -0.0082115302 -0.0339139989 0.020605230 -8.264234e-04
## 2016-03-31 0.0087091057 0.1218788516 0.0637459623 0.089910429 6.510043e-02
## 2016-04-29 0.0025459919 0.0040792832 0.0219750146 0.021044220 3.933239e-03
## 2016-05-31 0.0001357112 -0.0376284439 -0.0008560461 0.004397100 1.686870e-02
## 2016-06-30 0.0191665447 0.0445824706 -0.0244913662 0.008292264 3.469734e-03
## 2016-07-29 0.0054298198 0.0524423407 0.0390000281 0.049348251 3.582198e-02
## 2016-08-31 -0.0021564333 0.0087981012 0.0053268633 0.011261008 1.196862e-03
## 2016-09-30 0.0005161153 0.0248730610 0.0132790784 0.008614859 5.803865e-05
## 2016-10-31 -0.0082054612 -0.0083120689 -0.0224035875 -0.038134750 -1.748908e-02
## 2016-11-30 -0.0259897323 -0.0451618935 -0.0179743989 0.125246472 3.617596e-02
## 2016-12-30 0.0025381533 -0.0025300101 0.0267027783 0.031491721 2.006889e-02
## 2017-01-31 0.0021261782 0.0644312752 0.0323819590 -0.012143845 1.773664e-02
## 2017-02-28 0.0064377549 0.0172579591 0.0118363924 0.013428914 3.853938e-02
## 2017-03-31 -0.0005533261 0.0361888723 0.0318057133 -0.006533628 1.248925e-03
## 2017-04-28 0.0090299038 0.0168666252 0.0239522160 0.005108216 9.877368e-03
## 2017-05-31 0.0068475203 0.0280597656 0.0348103315 -0.022863006 1.401429e-02
## 2017-06-30 -0.0001829327 0.0092238738 0.0029557828 0.029152257 6.354682e-03
## 2017-07-31 0.0033346327 0.0565945416 0.0261879397 0.007480775 2.034564e-02
## 2017-08-31 0.0093691925 0.0232437716 -0.0004484333 -0.027564235 2.913630e-03
## 2017-09-29 -0.0057326478 -0.0004460965 0.0233429833 0.082321689 1.994907e-02
## 2017-10-31 0.0009782093 0.0322782757 0.0166535277 0.005916270 2.329063e-02
## 2017-11-30 -0.0014840174 -0.0038969524 0.0068700389 0.036913008 3.010798e-02
## 2017-12-29 0.0047402862 0.0369253964 0.0133983369 -0.003731197 1.205532e-02
calculate_component_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_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
Column chart of component contribution
plot_data <-asset_returns_wide_tbl %>%
calculate_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 Porfolio Volatility")
Column Chart of Component Contribution and Weight
plot_data <-asset_returns_wide_tbl %>%
calculate_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 Porfolio Volatility and Weight",
y = "Percent",
x = NULL)