# 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.0062315597 -0.0029355739 0.0366063075 0.052133425 4.992330e-02
## 2013-02-28 0.0058912656 -0.0231051224 -0.0129692952 0.016175454 1.267792e-02
## 2013-03-28 0.0009849785 -0.0102351250 0.0129692952 0.040257887 3.726827e-02
## 2013-04-30 0.0096389715 0.0120851313 0.0489676360 0.001222668 1.902988e-02
## 2013-05-31 -0.0202134504 -0.0494834844 -0.0306555133 0.041976280 2.333515e-02
## 2013-06-28 -0.0157788145 -0.0547283783 -0.0271443833 -0.001403140 -1.343411e-02
## 2013-07-31 0.0026881925 0.0131592640 0.0518605214 0.063541452 5.038621e-02
## 2013-08-30 -0.0082983833 -0.0257054930 -0.0197464620 -0.034743374 -3.045147e-02
## 2013-09-30 0.0111444584 0.0695891495 0.0753384984 0.063873651 3.115610e-02
## 2013-10-31 0.0082914650 0.0408609511 0.0320817953 0.034233939 4.526646e-02
## 2013-11-29 -0.0025098479 -0.0025939141 0.0054495420 0.041661263 2.920672e-02
## 2013-12-31 -0.0055829028 -0.0040743618 0.0215278218 0.012892288 2.559638e-02
## 2014-01-31 0.0152919871 -0.0903225597 -0.0534131516 -0.035775655 -3.588484e-02
## 2014-02-28 0.0037569195 0.0332204781 0.0595049358 0.045257743 4.451060e-02
## 2014-03-31 -0.0014819224 0.0380217618 -0.0046023351 0.013315115 8.261119e-03
## 2014-04-30 0.0081826288 0.0077728584 0.0165294453 -0.023184522 6.927373e-03
## 2014-05-30 0.0117217301 0.0290913005 0.0158283476 0.006205787 2.294138e-02
## 2014-06-30 -0.0005750546 0.0237338286 0.0091651849 0.037718483 2.043469e-02
## 2014-07-31 -0.0025122014 0.0135553549 -0.0263795571 -0.052009450 -1.352883e-02
## 2014-08-29 0.0114305858 0.0279044760 0.0018002703 0.043657972 3.870474e-02
## 2014-09-30 -0.0061671477 -0.0808564801 -0.0395983383 -0.061260624 -1.389192e-02
## 2014-10-31 0.0105846436 0.0140966785 -0.0026548111 0.068874906 2.327787e-02
## 2014-11-28 0.0065485197 -0.0155416376 0.0006253098 0.004773717 2.710086e-02
## 2014-12-31 0.0014746488 -0.0404419977 -0.0407468101 0.025295981 -2.539398e-03
## 2015-01-30 0.0203153619 -0.0068957367 0.0062265051 -0.054627963 -3.007716e-02
## 2015-02-27 -0.0089883646 0.0431361387 0.0614506173 0.056914480 5.468180e-02
## 2015-03-31 0.0037402468 -0.0150861354 -0.0143887061 0.010156314 -1.583036e-02
## 2015-04-30 -0.0032332062 0.0662811892 0.0358165038 -0.018417745 9.786126e-03
## 2015-05-29 -0.0043828532 -0.0419109062 0.0019527235 0.007509875 1.277396e-02
## 2015-06-30 -0.0108255356 -0.0297464936 -0.0316787473 0.004171596 -2.052101e-02
## 2015-07-31 0.0085843915 -0.0651783054 0.0201144807 -0.027375363 2.233780e-02
## 2015-08-31 -0.0033641746 -0.0925123356 -0.0771523151 -0.047268579 -6.288686e-02
## 2015-09-30 0.0080815575 -0.0318249917 -0.0451951440 -0.038464635 -2.584724e-02
## 2015-10-30 0.0006858259 0.0618082373 0.0640259730 0.063589715 8.163524e-02
## 2015-11-30 -0.0038986042 -0.0255604047 -0.0075559615 0.024415033 3.648362e-03
## 2015-12-31 -0.0019182733 -0.0389469627 -0.0235950314 -0.052156758 -1.743357e-02
## 2016-01-29 0.0123298231 -0.0516367468 -0.0567576480 -0.060306947 -5.106857e-02
## 2016-02-29 0.0088314878 -0.0082116050 -0.0339140811 0.020605291 -8.264855e-04
## 2016-03-31 0.0087083502 0.1218790519 0.0637458653 0.089910330 6.510016e-02
## 2016-04-29 0.0025468022 0.0040791494 0.0219750966 0.021044159 3.933651e-03
## 2016-05-31 0.0001353141 -0.0376285104 -0.0008561299 0.004397116 1.686838e-02
## 2016-06-30 0.0191665853 0.0445824046 -0.0244915362 0.008292252 3.469968e-03
## 2016-07-29 0.0054293616 0.0524421561 0.0390001969 0.049348594 3.582191e-02
## 2016-08-31 -0.0021555914 0.0087984760 0.0053268628 0.011260829 1.196736e-03
## 2016-09-30 0.0005152169 0.0248729368 0.0132793210 0.008614807 5.805519e-05
## 2016-10-31 -0.0082050636 -0.0083121911 -0.0224038293 -0.038134859 -1.748913e-02
## 2016-11-30 -0.0259894471 -0.0451618353 -0.0179743129 0.125246493 3.617615e-02
## 2016-12-30 0.0025380701 -0.0025299462 0.0267027738 0.031491798 2.006901e-02
## 2017-01-31 0.0021259016 0.0644312752 0.0323818741 -0.012144031 1.773654e-02
## 2017-02-28 0.0064380200 0.0172579591 0.0118363126 0.013428747 3.853927e-02
## 2017-03-31 -0.0005524442 0.0361888723 0.0318057133 -0.006532834 1.249307e-03
## 2017-04-28 0.0090286533 0.0168666252 0.0239520670 0.005107834 9.877235e-03
## 2017-05-31 0.0068476066 0.0280597656 0.0348103366 -0.022862872 1.401416e-02
## 2017-06-30 -0.0001826117 0.0092238738 0.0029559985 0.029152056 6.354689e-03
## 2017-07-31 0.0033345536 0.0565946436 0.0261877980 0.007481129 2.034579e-02
## 2017-08-31 0.0093689104 0.0232436697 -0.0004482935 -0.027564427 2.913587e-03
## 2017-09-29 -0.0057318345 -0.0004462957 0.0233427767 0.082321368 1.994914e-02
## 2017-10-31 0.0009780063 0.0322785714 0.0166536644 0.005916203 2.329054e-02
## 2017-11-30 -0.0014843110 -0.0038969521 0.0068701724 0.036913291 3.010800e-02
## 2017-12-29 0.0047402027 0.0369252062 0.0133981377 -0.003731357 1.205519e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398307e-05 0.0001042085 4.178275e-05 -0.0000781186 -9.031238e-06
## EEM 1.042085e-04 0.0017547090 1.039016e-03 0.0006437740 6.795433e-04
## EFA 4.178275e-05 0.0010390158 1.064236e-03 0.0006490304 6.975416e-04
## IJS -7.811860e-05 0.0006437740 6.490304e-04 0.0015654507 8.290268e-04
## SPY -9.031238e-06 0.0006795433 6.975416e-04 0.0008290268 7.408305e-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.0003874094 0.009257136 0.005815632 0.005684476 0.002330253
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.0062315597 -0.0029355739 0.0366063075 0.052133425 4.992330e-02
## 2013-02-28 0.0058912656 -0.0231051224 -0.0129692952 0.016175454 1.267792e-02
## 2013-03-28 0.0009849785 -0.0102351250 0.0129692952 0.040257887 3.726827e-02
## 2013-04-30 0.0096389715 0.0120851313 0.0489676360 0.001222668 1.902988e-02
## 2013-05-31 -0.0202134504 -0.0494834844 -0.0306555133 0.041976280 2.333515e-02
## 2013-06-28 -0.0157788145 -0.0547283783 -0.0271443833 -0.001403140 -1.343411e-02
## 2013-07-31 0.0026881925 0.0131592640 0.0518605214 0.063541452 5.038621e-02
## 2013-08-30 -0.0082983833 -0.0257054930 -0.0197464620 -0.034743374 -3.045147e-02
## 2013-09-30 0.0111444584 0.0695891495 0.0753384984 0.063873651 3.115610e-02
## 2013-10-31 0.0082914650 0.0408609511 0.0320817953 0.034233939 4.526646e-02
## 2013-11-29 -0.0025098479 -0.0025939141 0.0054495420 0.041661263 2.920672e-02
## 2013-12-31 -0.0055829028 -0.0040743618 0.0215278218 0.012892288 2.559638e-02
## 2014-01-31 0.0152919871 -0.0903225597 -0.0534131516 -0.035775655 -3.588484e-02
## 2014-02-28 0.0037569195 0.0332204781 0.0595049358 0.045257743 4.451060e-02
## 2014-03-31 -0.0014819224 0.0380217618 -0.0046023351 0.013315115 8.261119e-03
## 2014-04-30 0.0081826288 0.0077728584 0.0165294453 -0.023184522 6.927373e-03
## 2014-05-30 0.0117217301 0.0290913005 0.0158283476 0.006205787 2.294138e-02
## 2014-06-30 -0.0005750546 0.0237338286 0.0091651849 0.037718483 2.043469e-02
## 2014-07-31 -0.0025122014 0.0135553549 -0.0263795571 -0.052009450 -1.352883e-02
## 2014-08-29 0.0114305858 0.0279044760 0.0018002703 0.043657972 3.870474e-02
## 2014-09-30 -0.0061671477 -0.0808564801 -0.0395983383 -0.061260624 -1.389192e-02
## 2014-10-31 0.0105846436 0.0140966785 -0.0026548111 0.068874906 2.327787e-02
## 2014-11-28 0.0065485197 -0.0155416376 0.0006253098 0.004773717 2.710086e-02
## 2014-12-31 0.0014746488 -0.0404419977 -0.0407468101 0.025295981 -2.539398e-03
## 2015-01-30 0.0203153619 -0.0068957367 0.0062265051 -0.054627963 -3.007716e-02
## 2015-02-27 -0.0089883646 0.0431361387 0.0614506173 0.056914480 5.468180e-02
## 2015-03-31 0.0037402468 -0.0150861354 -0.0143887061 0.010156314 -1.583036e-02
## 2015-04-30 -0.0032332062 0.0662811892 0.0358165038 -0.018417745 9.786126e-03
## 2015-05-29 -0.0043828532 -0.0419109062 0.0019527235 0.007509875 1.277396e-02
## 2015-06-30 -0.0108255356 -0.0297464936 -0.0316787473 0.004171596 -2.052101e-02
## 2015-07-31 0.0085843915 -0.0651783054 0.0201144807 -0.027375363 2.233780e-02
## 2015-08-31 -0.0033641746 -0.0925123356 -0.0771523151 -0.047268579 -6.288686e-02
## 2015-09-30 0.0080815575 -0.0318249917 -0.0451951440 -0.038464635 -2.584724e-02
## 2015-10-30 0.0006858259 0.0618082373 0.0640259730 0.063589715 8.163524e-02
## 2015-11-30 -0.0038986042 -0.0255604047 -0.0075559615 0.024415033 3.648362e-03
## 2015-12-31 -0.0019182733 -0.0389469627 -0.0235950314 -0.052156758 -1.743357e-02
## 2016-01-29 0.0123298231 -0.0516367468 -0.0567576480 -0.060306947 -5.106857e-02
## 2016-02-29 0.0088314878 -0.0082116050 -0.0339140811 0.020605291 -8.264855e-04
## 2016-03-31 0.0087083502 0.1218790519 0.0637458653 0.089910330 6.510016e-02
## 2016-04-29 0.0025468022 0.0040791494 0.0219750966 0.021044159 3.933651e-03
## 2016-05-31 0.0001353141 -0.0376285104 -0.0008561299 0.004397116 1.686838e-02
## 2016-06-30 0.0191665853 0.0445824046 -0.0244915362 0.008292252 3.469968e-03
## 2016-07-29 0.0054293616 0.0524421561 0.0390001969 0.049348594 3.582191e-02
## 2016-08-31 -0.0021555914 0.0087984760 0.0053268628 0.011260829 1.196736e-03
## 2016-09-30 0.0005152169 0.0248729368 0.0132793210 0.008614807 5.805519e-05
## 2016-10-31 -0.0082050636 -0.0083121911 -0.0224038293 -0.038134859 -1.748913e-02
## 2016-11-30 -0.0259894471 -0.0451618353 -0.0179743129 0.125246493 3.617615e-02
## 2016-12-30 0.0025380701 -0.0025299462 0.0267027738 0.031491798 2.006901e-02
## 2017-01-31 0.0021259016 0.0644312752 0.0323818741 -0.012144031 1.773654e-02
## 2017-02-28 0.0064380200 0.0172579591 0.0118363126 0.013428747 3.853927e-02
## 2017-03-31 -0.0005524442 0.0361888723 0.0318057133 -0.006532834 1.249307e-03
## 2017-04-28 0.0090286533 0.0168666252 0.0239520670 0.005107834 9.877235e-03
## 2017-05-31 0.0068476066 0.0280597656 0.0348103366 -0.022862872 1.401416e-02
## 2017-06-30 -0.0001826117 0.0092238738 0.0029559985 0.029152056 6.354689e-03
## 2017-07-31 0.0033345536 0.0565946436 0.0261877980 0.007481129 2.034579e-02
## 2017-08-31 0.0093689104 0.0232436697 -0.0004482935 -0.027564427 2.913587e-03
## 2017-09-29 -0.0057318345 -0.0004462957 0.0233427767 0.082321368 1.994914e-02
## 2017-10-31 0.0009780063 0.0322785714 0.0166536644 0.005916203 2.329054e-02
## 2017-11-30 -0.0014843110 -0.0038969521 0.0068701724 0.036913291 3.010800e-02
## 2017-12-29 0.0047402027 0.0369252062 0.0133981377 -0.003731357 1.205519e-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)