# 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.0062315603 -0.0029355739 0.0366063983 0.052133092 4.992318e-02
## 2013-02-28 0.0058916369 -0.0231052356 -0.0129696618 0.016175140 1.267804e-02
## 2013-03-28 0.0009841449 -0.0102347832 0.0129696618 0.040258712 3.726857e-02
## 2013-04-30 0.0096398939 0.0120845638 0.0489677181 0.001222466 1.902941e-02
## 2013-05-31 -0.0202143788 -0.0494831455 -0.0306555079 0.041976272 2.333550e-02
## 2013-06-28 -0.0157781580 -0.0547284410 -0.0271445615 -0.001403431 -1.343435e-02
## 2013-07-31 0.0026881923 0.0131595742 0.0518603476 0.063541367 5.038611e-02
## 2013-08-30 -0.0082990517 -0.0257052962 -0.0197462881 -0.034743192 -3.045148e-02
## 2013-09-30 0.0111447483 0.0695885868 0.0753384984 0.063873562 3.115611e-02
## 2013-10-31 0.0082915611 0.0408612969 0.0320817157 0.034234198 4.526679e-02
## 2013-11-29 -0.0025101304 -0.0025941415 0.0054494632 0.041661174 2.920692e-02
## 2013-12-31 -0.0055828109 -0.0040743618 0.0215283678 0.012892044 2.559607e-02
## 2014-01-31 0.0152914343 -0.0903226224 -0.0534136210 -0.035775325 -3.588433e-02
## 2014-02-28 0.0037570157 0.0332207224 0.0595053258 0.045257415 4.450989e-02
## 2014-03-31 -0.0014810877 0.0380214635 -0.0046026433 0.013315354 8.261514e-03
## 2014-04-30 0.0081829973 0.0077727436 0.0165292931 -0.023184196 6.927372e-03
## 2014-05-30 0.0117214527 0.0290914195 0.0158284250 0.006205141 2.294118e-02
## 2014-06-30 -0.0005759655 0.0237338312 0.0091654826 0.037718879 2.043469e-02
## 2014-07-31 -0.0025113815 0.0135554647 -0.0263800086 -0.052009441 -1.352873e-02
## 2014-08-29 0.0114305858 0.0279045813 0.0018007274 0.043657809 3.870501e-02
## 2014-09-30 -0.0061678745 -0.0808566996 -0.0395984874 -0.061260624 -1.389256e-02
## 2014-10-31 0.0105847412 0.0140966801 -0.0026550491 0.068875294 2.327770e-02
## 2014-11-28 0.0065489703 -0.0155411819 0.0006253893 0.004773484 2.710167e-02
## 2014-12-31 0.0014750949 -0.0404422216 -0.0407466482 0.025295676 -2.539749e-03
## 2015-01-30 0.0203150070 -0.0068958558 0.0062264224 -0.054628051 -3.007707e-02
## 2015-02-27 -0.0089879245 0.0431361387 0.0614506173 0.056914868 5.468171e-02
## 2015-03-31 0.0037399819 -0.0150860189 -0.0143886278 0.010156312 -1.583045e-02
## 2015-04-30 -0.0032326771 0.0662809635 0.0358163499 -0.018417742 9.786299e-03
## 2015-05-29 -0.0043839127 -0.0419109109 0.0019528745 0.007509724 1.277379e-02
## 2015-06-30 -0.0108251825 -0.0297464970 -0.0316788228 0.004171297 -2.052119e-02
## 2015-07-31 0.0085842148 -0.0651781257 0.0201144044 -0.027375218 2.233806e-02
## 2015-08-31 -0.0033638194 -0.0925123982 -0.0771524861 -0.047268506 -6.288668e-02
## 2015-09-30 0.0080820866 -0.0318250626 -0.0451949829 -0.038464387 -2.584714e-02
## 2015-10-30 0.0006848549 0.0618083747 0.0640261402 0.063589784 8.163496e-02
## 2015-11-30 -0.0038981627 -0.0255604713 -0.0075558793 0.024414797 3.648448e-03
## 2015-12-31 -0.0019187169 -0.0389471047 -0.0235951944 -0.052156920 -1.743357e-02
## 2016-01-29 0.0123300904 -0.0516367544 -0.0567577363 -0.060306785 -5.106875e-02
## 2016-02-29 0.0088314017 -0.0082115308 -0.0339139014 0.020605123 -8.260274e-04
## 2016-03-31 0.0087091270 0.1218792608 0.0637457739 0.089910652 6.510006e-02
## 2016-04-29 0.0025455119 0.0040790824 0.0219750127 0.021043855 3.933393e-03
## 2016-05-31 0.0001358296 -0.0376285078 -0.0008559620 0.004397416 1.686872e-02
## 2016-06-30 0.0191663357 0.0445824016 -0.0244915341 0.008292028 3.469465e-03
## 2016-07-29 0.0054301183 0.0524419648 0.0390001936 0.049348598 3.582232e-02
## 2016-08-31 -0.0021564307 0.0087986634 0.0053266155 0.011260899 1.196655e-03
## 2016-09-30 0.0005163086 0.0248731170 0.0132790795 0.008614669 5.797444e-05
## 2016-10-31 -0.0082053127 -0.0083123112 -0.0224034232 -0.038134721 -1.748905e-02
## 2016-11-30 -0.0259900463 -0.0451618296 -0.0179745666 0.125246556 3.617598e-02
## 2016-12-30 0.0025384174 -0.0025300099 0.0267031098 0.031491488 2.006901e-02
## 2017-01-31 0.0021260744 0.0644313313 0.0323817917 -0.012143598 1.773662e-02
## 2017-02-28 0.0064381902 0.0172579571 0.0118363126 0.013428622 3.853926e-02
## 2017-03-31 -0.0005533896 0.0361888681 0.0318056370 -0.006533081 1.248940e-03
## 2017-04-28 0.0090292543 0.0168666233 0.0239522178 0.005107958 9.877456e-03
## 2017-05-31 0.0068475214 0.0280596536 0.0348101901 -0.022862685 1.401430e-02
## 2017-06-30 -0.0001831195 0.0092237659 0.0029559987 0.029151746 6.354546e-03
## 2017-07-31 0.0033348083 0.0565947515 0.0261880794 0.007481494 2.034572e-02
## 2017-08-31 0.0093691635 0.0232435701 -0.0004484332 -0.027564859 2.913657e-03
## 2017-09-29 -0.0057318345 -0.0004461961 0.0233427068 0.082321848 1.994901e-02
## 2017-10-31 0.0009778384 0.0322784749 0.0166537987 0.005916087 2.329075e-02
## 2017-11-30 -0.0014839749 -0.0038969524 0.0068697044 0.036913176 3.010800e-02
## 2017-12-29 0.0047404530 0.0369253964 0.0133987346 -0.003731136 1.205512e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398475e-05 0.0001042135 4.178435e-05 -7.811947e-05 -9.030995e-06
## EEM 1.042135e-04 0.0017547093 1.039017e-03 6.437763e-04 6.795436e-04
## EFA 4.178435e-05 0.0010390175 1.064240e-03 6.490318e-04 6.975416e-04
## IJS -7.811947e-05 0.0006437763 6.490318e-04 1.565452e-03 8.290262e-04
## SPY -9.030995e-06 0.0006795436 6.975416e-04 8.290262e-04 7.408300e-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.02347494
# 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.0003874286 0.009257147 0.005815639 0.005684475 0.00233025
rowSums(component_contribution)
## [1] 0.02347494
# 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.0062315603 -0.0029355739 0.0366063983 0.052133092 4.992318e-02
## 2013-02-28 0.0058916369 -0.0231052356 -0.0129696618 0.016175140 1.267804e-02
## 2013-03-28 0.0009841449 -0.0102347832 0.0129696618 0.040258712 3.726857e-02
## 2013-04-30 0.0096398939 0.0120845638 0.0489677181 0.001222466 1.902941e-02
## 2013-05-31 -0.0202143788 -0.0494831455 -0.0306555079 0.041976272 2.333550e-02
## 2013-06-28 -0.0157781580 -0.0547284410 -0.0271445615 -0.001403431 -1.343435e-02
## 2013-07-31 0.0026881923 0.0131595742 0.0518603476 0.063541367 5.038611e-02
## 2013-08-30 -0.0082990517 -0.0257052962 -0.0197462881 -0.034743192 -3.045148e-02
## 2013-09-30 0.0111447483 0.0695885868 0.0753384984 0.063873562 3.115611e-02
## 2013-10-31 0.0082915611 0.0408612969 0.0320817157 0.034234198 4.526679e-02
## 2013-11-29 -0.0025101304 -0.0025941415 0.0054494632 0.041661174 2.920692e-02
## 2013-12-31 -0.0055828109 -0.0040743618 0.0215283678 0.012892044 2.559607e-02
## 2014-01-31 0.0152914343 -0.0903226224 -0.0534136210 -0.035775325 -3.588433e-02
## 2014-02-28 0.0037570157 0.0332207224 0.0595053258 0.045257415 4.450989e-02
## 2014-03-31 -0.0014810877 0.0380214635 -0.0046026433 0.013315354 8.261514e-03
## 2014-04-30 0.0081829973 0.0077727436 0.0165292931 -0.023184196 6.927372e-03
## 2014-05-30 0.0117214527 0.0290914195 0.0158284250 0.006205141 2.294118e-02
## 2014-06-30 -0.0005759655 0.0237338312 0.0091654826 0.037718879 2.043469e-02
## 2014-07-31 -0.0025113815 0.0135554647 -0.0263800086 -0.052009441 -1.352873e-02
## 2014-08-29 0.0114305858 0.0279045813 0.0018007274 0.043657809 3.870501e-02
## 2014-09-30 -0.0061678745 -0.0808566996 -0.0395984874 -0.061260624 -1.389256e-02
## 2014-10-31 0.0105847412 0.0140966801 -0.0026550491 0.068875294 2.327770e-02
## 2014-11-28 0.0065489703 -0.0155411819 0.0006253893 0.004773484 2.710167e-02
## 2014-12-31 0.0014750949 -0.0404422216 -0.0407466482 0.025295676 -2.539749e-03
## 2015-01-30 0.0203150070 -0.0068958558 0.0062264224 -0.054628051 -3.007707e-02
## 2015-02-27 -0.0089879245 0.0431361387 0.0614506173 0.056914868 5.468171e-02
## 2015-03-31 0.0037399819 -0.0150860189 -0.0143886278 0.010156312 -1.583045e-02
## 2015-04-30 -0.0032326771 0.0662809635 0.0358163499 -0.018417742 9.786299e-03
## 2015-05-29 -0.0043839127 -0.0419109109 0.0019528745 0.007509724 1.277379e-02
## 2015-06-30 -0.0108251825 -0.0297464970 -0.0316788228 0.004171297 -2.052119e-02
## 2015-07-31 0.0085842148 -0.0651781257 0.0201144044 -0.027375218 2.233806e-02
## 2015-08-31 -0.0033638194 -0.0925123982 -0.0771524861 -0.047268506 -6.288668e-02
## 2015-09-30 0.0080820866 -0.0318250626 -0.0451949829 -0.038464387 -2.584714e-02
## 2015-10-30 0.0006848549 0.0618083747 0.0640261402 0.063589784 8.163496e-02
## 2015-11-30 -0.0038981627 -0.0255604713 -0.0075558793 0.024414797 3.648448e-03
## 2015-12-31 -0.0019187169 -0.0389471047 -0.0235951944 -0.052156920 -1.743357e-02
## 2016-01-29 0.0123300904 -0.0516367544 -0.0567577363 -0.060306785 -5.106875e-02
## 2016-02-29 0.0088314017 -0.0082115308 -0.0339139014 0.020605123 -8.260274e-04
## 2016-03-31 0.0087091270 0.1218792608 0.0637457739 0.089910652 6.510006e-02
## 2016-04-29 0.0025455119 0.0040790824 0.0219750127 0.021043855 3.933393e-03
## 2016-05-31 0.0001358296 -0.0376285078 -0.0008559620 0.004397416 1.686872e-02
## 2016-06-30 0.0191663357 0.0445824016 -0.0244915341 0.008292028 3.469465e-03
## 2016-07-29 0.0054301183 0.0524419648 0.0390001936 0.049348598 3.582232e-02
## 2016-08-31 -0.0021564307 0.0087986634 0.0053266155 0.011260899 1.196655e-03
## 2016-09-30 0.0005163086 0.0248731170 0.0132790795 0.008614669 5.797444e-05
## 2016-10-31 -0.0082053127 -0.0083123112 -0.0224034232 -0.038134721 -1.748905e-02
## 2016-11-30 -0.0259900463 -0.0451618296 -0.0179745666 0.125246556 3.617598e-02
## 2016-12-30 0.0025384174 -0.0025300099 0.0267031098 0.031491488 2.006901e-02
## 2017-01-31 0.0021260744 0.0644313313 0.0323817917 -0.012143598 1.773662e-02
## 2017-02-28 0.0064381902 0.0172579571 0.0118363126 0.013428622 3.853926e-02
## 2017-03-31 -0.0005533896 0.0361888681 0.0318056370 -0.006533081 1.248940e-03
## 2017-04-28 0.0090292543 0.0168666233 0.0239522178 0.005107958 9.877456e-03
## 2017-05-31 0.0068475214 0.0280596536 0.0348101901 -0.022862685 1.401430e-02
## 2017-06-30 -0.0001831195 0.0092237659 0.0029559987 0.029151746 6.354546e-03
## 2017-07-31 0.0033348083 0.0565947515 0.0261880794 0.007481494 2.034572e-02
## 2017-08-31 0.0093691635 0.0232435701 -0.0004484332 -0.027564859 2.913657e-03
## 2017-09-29 -0.0057318345 -0.0004461961 0.0233427068 0.082321848 1.994901e-02
## 2017-10-31 0.0009778384 0.0322784749 0.0166537987 0.005916087 2.329075e-02
## 2017-11-30 -0.0014839749 -0.0038969524 0.0068697044 0.036913176 3.010800e-02
## 2017-12-29 0.0047404530 0.0369253964 0.0133987346 -0.003731136 1.205512e-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
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
return(component_percentages)
}
asset_returns_wide_tbl %>% calculate_component_contribution(w = c(.25,.25,.20,.20,.10))
## # 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,.20,.20,.10)) %>%
# transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contributions")
plot_data %>%
ggplot(aes(x = Asset, y = Contributions)) +
geom_col(fill = "steelblue") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = .5)) +
labs(title = "Percent Contribution to Portfolio Standerd Deveiation")
column chart of component contribution and weight
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(.25,.25,.20,.20,.10)) %>%
# transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contributions") %>%
# add wights
add_column(weight = c(.25,.25,.20,.20,.10)) %>%
# transform to long
pivot_longer(cols = c(Contributions, 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 = .5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio volitlity and weight",
y = "Percent",
x = NULL)