# 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.0062307258 -0.0029353524 0.0366061192 0.052132873 0.0499233630
## 2013-02-28 0.0058914510 -0.0231054542 -0.0129693872 0.016175352 0.0126776058
## 2013-03-28 0.0009848857 -0.0102348964 0.0129693872 0.040258202 0.0372682190
## 2013-04-30 0.0096391540 0.0120847897 0.0489677225 0.001222567 0.0190304746
## 2013-05-31 -0.0202140063 -0.0494832528 -0.0306555997 0.041976381 0.0233350304
## 2013-06-28 -0.0157781535 -0.0547283716 -0.0271442001 -0.001403140 -0.0134342277
## 2013-07-31 0.0026874330 0.0131595099 0.0518601644 0.063541452 0.0503855427
## 2013-08-30 -0.0082979084 -0.0257057373 -0.0197462881 -0.034743279 -0.0304506932
## 2013-09-30 0.0111437955 0.0695889042 0.0753385807 0.063873645 0.0311556529
## 2013-10-31 0.0082922198 0.0408612969 0.0320816335 0.034233936 0.0452664705
## 2013-11-29 -0.0025097534 -0.0025940275 0.0054493840 0.041661260 0.0292070353
## 2013-12-31 -0.0055832793 -0.0040744757 0.0215284470 0.012892287 0.0255958743
## 2014-01-31 0.0152914300 -0.0903224971 -0.0534132939 -0.035775568 -0.0358842365
## 2014-02-28 0.0037572002 0.0332205972 0.0595048446 0.045257495 0.0445105949
## 2014-03-31 -0.0014819229 0.0380216967 -0.0046025666 0.013315353 0.0082609211
## 2014-04-30 0.0081832767 0.0077727418 0.0165295989 -0.023184276 0.0069275683
## 2014-05-30 0.0117217269 0.0290913005 0.0158282715 0.006205302 0.0229409933
## 2014-06-30 -0.0005755099 0.0237337188 0.0091654077 0.037718719 0.0204348790
## 2014-07-31 -0.0025125672 0.0135553564 -0.0263799323 -0.052009441 -0.0135288294
## 2014-08-29 0.0114306823 0.0279047949 0.0018004228 0.043657965 0.0387049257
## 2014-09-30 -0.0061670597 -0.0808568050 -0.0395982591 -0.061260614 -0.0138922824
## 2014-10-31 0.0105842879 0.0140966801 -0.0026547315 0.068874740 0.0232777867
## 2014-11-28 0.0065484350 -0.0155414106 0.0006249924 0.004773717 0.0271010460
## 2014-12-31 0.0014755417 -0.0404418739 -0.0407464862 0.025295906 -0.0025393099
## 2015-01-30 0.0203149232 -0.0068958549 0.0062263398 -0.054628046 -0.0300775258
## 2015-02-27 -0.0089879269 0.0431360188 0.0614506173 0.056914489 0.0546820799
## 2015-03-31 0.0037403343 -0.0150862520 -0.0143887845 0.010156612 -0.0158300164
## 2015-04-30 -0.0032332056 0.0662813058 0.0358164310 -0.018417742 0.0097856043
## 2015-05-29 -0.0043831180 -0.0419110200 0.0019530255 0.007509874 0.0127745632
## 2015-06-30 -0.0108260735 -0.0297464970 -0.0316790540 0.004171446 -0.0205213526
## 2015-07-31 0.0085849291 -0.0651783759 0.0201144838 -0.027375209 0.0223376295
## 2015-08-31 -0.0033638188 -0.0925120794 -0.0771524922 -0.047268733 -0.0628865129
## 2015-09-30 0.0080817320 -0.0318249895 -0.0451948142 -0.038464719 -0.0258473239
## 2015-10-30 0.0006849433 0.0618082331 0.0640261348 0.063589642 0.0816349709
## 2015-11-30 -0.0038975430 -0.0255604713 -0.0075560418 0.024415497 0.0036485340
## 2015-12-31 -0.0019196921 -0.0389471047 -0.0235951129 -0.052157146 -0.0174335669
## 2016-01-29 0.0123299205 -0.0516365300 -0.0567578247 -0.060306952 -0.0510687528
## 2016-02-29 0.0088321022 -0.0082117552 -0.0339139044 0.020605376 -0.0008260275
## 2016-03-31 0.0087090401 0.1218791941 0.0637457796 0.089910483 0.0651000610
## 2016-04-29 0.0025461133 0.0040791492 0.0219750985 0.021044156 0.0039336501
## 2016-05-31 0.0001351423 -0.0376285768 -0.0008560460 0.004397340 0.0168682928
## 2016-06-30 0.0191671801 0.0445823386 -0.0244913641 0.008292175 0.0034697168
## 2016-07-29 0.0054292759 0.0524423474 0.0390000248 0.049348085 0.0358222349
## 2016-08-31 -0.0021561789 0.0087984749 0.0053268628 0.011260971 0.0011966550
## 2016-09-30 0.0005156369 0.0248728126 0.0132791585 0.008614738 0.0000578937
## 2016-10-31 -0.0082051475 -0.0083121911 -0.0224039160 -0.038134721 -0.0174889707
## 2016-11-30 -0.0259891864 -0.0451618353 -0.0179742328 0.125246239 0.0361759841
## 2016-12-30 0.0025381561 -0.0025298821 0.0267029429 0.031491560 0.0200690924
## 2017-01-31 0.0021259009 0.0644312712 0.0323817146 -0.012143476 0.0177366154
## 2017-02-28 0.0064375882 0.0172580171 0.0118364721 0.013428623 0.0385394840
## 2017-03-31 -0.0005528741 0.0361889820 0.0318057133 -0.006532958 0.0012489397
## 2017-04-28 0.0090292543 0.0168663974 0.0239523650 0.005107897 0.0098772357
## 2017-05-31 0.0068478598 0.0280597656 0.0348101105 -0.022862812 0.0140143003
## 2017-06-30 -0.0001830348 0.0092239816 0.0029559982 0.029151752 0.0063546176
## 2017-07-31 0.0033343852 0.0565945357 0.0261879359 0.007481617 0.0203459279
## 2017-08-31 0.0093690799 0.0232435701 -0.0004485731 -0.027564673 0.0029133778
## 2017-09-29 -0.0057320030 -0.0004460965 0.0233429150 0.082321723 0.0199488714
## 2017-10-31 0.0009780905 0.0322783753 0.0166537304 0.005916202 0.0232909521
## 2017-11-30 -0.0014843112 -0.0038968556 0.0068700380 0.036913172 0.0301078707
## 2017-12-29 0.0047404545 0.0369252062 0.0133982693 -0.003731246 0.0120551244
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398343e-05 0.0001042083 0.0000417817 -7.811791e-05 -9.031111e-06
## EEM 1.042083e-04 0.0017547095 0.0010390153 6.437762e-04 6.795418e-04
## EFA 4.178170e-05 0.0010390153 0.0010642366 6.490313e-04 6.975402e-04
## IJS -7.811791e-05 0.0006437762 0.0006490313 1.565449e-03 8.290250e-04
## SPY -9.031111e-06 0.0006795418 0.0006975402 8.290250e-04 7.408280e-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.02347491
# 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.0003874091 0.009257139 0.00581563 0.00568448 0.002330248
rowSums(component_contribution)
## [1] 0.02347491
# 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.0062307258 -0.0029353524 0.0366061192 0.052132873 0.0499233630
## 2013-02-28 0.0058914510 -0.0231054542 -0.0129693872 0.016175352 0.0126776058
## 2013-03-28 0.0009848857 -0.0102348964 0.0129693872 0.040258202 0.0372682190
## 2013-04-30 0.0096391540 0.0120847897 0.0489677225 0.001222567 0.0190304746
## 2013-05-31 -0.0202140063 -0.0494832528 -0.0306555997 0.041976381 0.0233350304
## 2013-06-28 -0.0157781535 -0.0547283716 -0.0271442001 -0.001403140 -0.0134342277
## 2013-07-31 0.0026874330 0.0131595099 0.0518601644 0.063541452 0.0503855427
## 2013-08-30 -0.0082979084 -0.0257057373 -0.0197462881 -0.034743279 -0.0304506932
## 2013-09-30 0.0111437955 0.0695889042 0.0753385807 0.063873645 0.0311556529
## 2013-10-31 0.0082922198 0.0408612969 0.0320816335 0.034233936 0.0452664705
## 2013-11-29 -0.0025097534 -0.0025940275 0.0054493840 0.041661260 0.0292070353
## 2013-12-31 -0.0055832793 -0.0040744757 0.0215284470 0.012892287 0.0255958743
## 2014-01-31 0.0152914300 -0.0903224971 -0.0534132939 -0.035775568 -0.0358842365
## 2014-02-28 0.0037572002 0.0332205972 0.0595048446 0.045257495 0.0445105949
## 2014-03-31 -0.0014819229 0.0380216967 -0.0046025666 0.013315353 0.0082609211
## 2014-04-30 0.0081832767 0.0077727418 0.0165295989 -0.023184276 0.0069275683
## 2014-05-30 0.0117217269 0.0290913005 0.0158282715 0.006205302 0.0229409933
## 2014-06-30 -0.0005755099 0.0237337188 0.0091654077 0.037718719 0.0204348790
## 2014-07-31 -0.0025125672 0.0135553564 -0.0263799323 -0.052009441 -0.0135288294
## 2014-08-29 0.0114306823 0.0279047949 0.0018004228 0.043657965 0.0387049257
## 2014-09-30 -0.0061670597 -0.0808568050 -0.0395982591 -0.061260614 -0.0138922824
## 2014-10-31 0.0105842879 0.0140966801 -0.0026547315 0.068874740 0.0232777867
## 2014-11-28 0.0065484350 -0.0155414106 0.0006249924 0.004773717 0.0271010460
## 2014-12-31 0.0014755417 -0.0404418739 -0.0407464862 0.025295906 -0.0025393099
## 2015-01-30 0.0203149232 -0.0068958549 0.0062263398 -0.054628046 -0.0300775258
## 2015-02-27 -0.0089879269 0.0431360188 0.0614506173 0.056914489 0.0546820799
## 2015-03-31 0.0037403343 -0.0150862520 -0.0143887845 0.010156612 -0.0158300164
## 2015-04-30 -0.0032332056 0.0662813058 0.0358164310 -0.018417742 0.0097856043
## 2015-05-29 -0.0043831180 -0.0419110200 0.0019530255 0.007509874 0.0127745632
## 2015-06-30 -0.0108260735 -0.0297464970 -0.0316790540 0.004171446 -0.0205213526
## 2015-07-31 0.0085849291 -0.0651783759 0.0201144838 -0.027375209 0.0223376295
## 2015-08-31 -0.0033638188 -0.0925120794 -0.0771524922 -0.047268733 -0.0628865129
## 2015-09-30 0.0080817320 -0.0318249895 -0.0451948142 -0.038464719 -0.0258473239
## 2015-10-30 0.0006849433 0.0618082331 0.0640261348 0.063589642 0.0816349709
## 2015-11-30 -0.0038975430 -0.0255604713 -0.0075560418 0.024415497 0.0036485340
## 2015-12-31 -0.0019196921 -0.0389471047 -0.0235951129 -0.052157146 -0.0174335669
## 2016-01-29 0.0123299205 -0.0516365300 -0.0567578247 -0.060306952 -0.0510687528
## 2016-02-29 0.0088321022 -0.0082117552 -0.0339139044 0.020605376 -0.0008260275
## 2016-03-31 0.0087090401 0.1218791941 0.0637457796 0.089910483 0.0651000610
## 2016-04-29 0.0025461133 0.0040791492 0.0219750985 0.021044156 0.0039336501
## 2016-05-31 0.0001351423 -0.0376285768 -0.0008560460 0.004397340 0.0168682928
## 2016-06-30 0.0191671801 0.0445823386 -0.0244913641 0.008292175 0.0034697168
## 2016-07-29 0.0054292759 0.0524423474 0.0390000248 0.049348085 0.0358222349
## 2016-08-31 -0.0021561789 0.0087984749 0.0053268628 0.011260971 0.0011966550
## 2016-09-30 0.0005156369 0.0248728126 0.0132791585 0.008614738 0.0000578937
## 2016-10-31 -0.0082051475 -0.0083121911 -0.0224039160 -0.038134721 -0.0174889707
## 2016-11-30 -0.0259891864 -0.0451618353 -0.0179742328 0.125246239 0.0361759841
## 2016-12-30 0.0025381561 -0.0025298821 0.0267029429 0.031491560 0.0200690924
## 2017-01-31 0.0021259009 0.0644312712 0.0323817146 -0.012143476 0.0177366154
## 2017-02-28 0.0064375882 0.0172580171 0.0118364721 0.013428623 0.0385394840
## 2017-03-31 -0.0005528741 0.0361889820 0.0318057133 -0.006532958 0.0012489397
## 2017-04-28 0.0090292543 0.0168663974 0.0239523650 0.005107897 0.0098772357
## 2017-05-31 0.0068478598 0.0280597656 0.0348101105 -0.022862812 0.0140143003
## 2017-06-30 -0.0001830348 0.0092239816 0.0029559982 0.029151752 0.0063546176
## 2017-07-31 0.0033343852 0.0565945357 0.0261879359 0.007481617 0.0203459279
## 2017-08-31 0.0093690799 0.0232435701 -0.0004485731 -0.027564673 0.0029133778
## 2017-09-29 -0.0057320030 -0.0004460965 0.0233429150 0.082321723 0.0199488714
## 2017-10-31 0.0009780905 0.0322783753 0.0166537304 0.005916202 0.0232909521
## 2017-11-30 -0.0014843112 -0.0038968556 0.0068700380 0.036913172 0.0301078707
## 2017-12-29 0.0047404545 0.0369252062 0.0133982693 -0.003731246 0.0120551244
calulate_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 %>% calulate_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 %>%
calulate_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")
Column Chart of Component Contribution and Weight
plot_data <- asset_returns_wide_tbl %>%
calulate_component_contribution(w = c(.25, .25, .2, .2, .1)) %>%
# Transform to long form
pivot_longer(cols = everything(),names_to = "Asset", values_to = "Contribution") %>%
# Add weight
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)