# 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.0062310813 -0.002935574 0.0366062100 0.052132878 4.992324e-02
## 2013-02-28 0.0058912009 -0.023105233 -0.0129694779 0.016175880 1.267810e-02
## 2013-03-28 0.0009848334 -0.010234896 0.0129694779 0.040257984 3.726767e-02
## 2013-04-30 0.0096392456 0.012084677 0.0489675453 0.001222668 1.903030e-02
## 2013-05-31 -0.0202138448 -0.049483496 -0.0306555133 0.041976078 2.333538e-02
## 2013-06-28 -0.0157784487 -0.054728016 -0.0271443833 -0.001402752 -1.343423e-02
## 2013-07-31 0.0026879115 0.013159634 0.0518602606 0.063541428 5.038587e-02
## 2013-08-30 -0.0082984921 -0.025705798 -0.0197462012 -0.034743738 -3.045102e-02
## 2013-09-30 0.0111440498 0.069588841 0.0753384162 0.063873739 3.115587e-02
## 2013-10-31 0.0082920313 0.040861183 0.0320816387 0.034234022 4.526657e-02
## 2013-11-29 -0.0025105158 -0.002593914 0.0054497017 0.041661174 2.920672e-02
## 2013-12-31 -0.0055821742 -0.004074132 0.0215281336 0.012891882 2.559617e-02
## 2014-01-31 0.0152913059 -0.090322840 -0.0534131388 -0.035775164 -3.588454e-02
## 2014-02-28 0.0037569374 0.033220355 0.0595049216 0.045257575 4.451059e-02
## 2014-03-31 -0.0014812914 0.038022056 -0.0046026436 0.013315273 8.260921e-03
## 2014-04-30 0.0081832033 0.007772509 0.0165291421 -0.023184518 6.927763e-03
## 2014-05-30 0.0117214984 0.029091191 0.0158285784 0.006205465 2.294099e-02
## 2014-06-30 -0.0005760467 0.023733724 0.0091654091 0.037718876 2.043469e-02
## 2014-07-31 -0.0025122598 0.013556009 -0.0263797076 -0.052009437 -1.352864e-02
## 2014-08-29 0.0114313080 0.027904572 0.0018005749 0.043657805 3.870492e-02
## 2014-09-30 -0.0061678908 -0.080856901 -0.0395986457 -0.061260619 -1.389274e-02
## 2014-10-31 0.0105846842 0.014096566 -0.0026548113 0.068875056 2.327779e-02
## 2014-11-28 0.0065486013 -0.015541296 0.0006252305 0.004773331 2.710158e-02
## 2014-12-31 0.0014749154 -0.040442465 -0.0407466515 0.025296135 -2.539838e-03
## 2015-01-30 0.0203158784 -0.006895499 0.0062264229 -0.054628122 -3.007681e-02
## 2015-02-27 -0.0089886336 0.043136254 0.0614505450 0.056914939 5.468197e-02
## 2015-03-31 0.0037406581 -0.015086134 -0.0143887084 0.010156236 -1.583053e-02
## 2015-04-30 -0.0032326370 0.066281182 0.0358165849 -0.018417892 9.785779e-03
## 2015-05-29 -0.0043843653 -0.041910788 0.0019527236 0.007509950 1.277422e-02
## 2015-06-30 -0.0108255046 -0.029746956 -0.0316788276 0.004171296 -2.052127e-02
## 2015-07-31 0.0085846666 -0.065178134 0.0201143312 -0.027375367 2.233798e-02
## 2015-08-31 -0.0033637353 -0.092512342 -0.0771521747 -0.047268264 -6.288642e-02
## 2015-09-30 0.0080816190 -0.031824994 -0.0451948928 -0.038464796 -2.584741e-02
## 2015-10-30 0.0006852449 0.061808242 0.0640258868 0.063589794 8.163514e-02
## 2015-11-30 -0.0038980454 -0.025560338 -0.0075558800 0.024415337 3.648277e-03
## 2015-12-31 -0.0019188111 -0.038947105 -0.0235951964 -0.052157142 -1.743339e-02
## 2016-01-29 0.0123294448 -0.051636605 -0.0567577412 -0.060306947 -5.106884e-02
## 2016-02-29 0.0088324755 -0.008211831 -0.0339139044 0.020605039 -8.262107e-04
## 2016-03-31 0.0087080528 0.121879078 0.0637458653 0.089910582 6.510041e-02
## 2016-04-29 0.0025465531 0.004079416 0.0219748450 0.021044310 3.933136e-03
## 2016-05-31 0.0001354825 -0.037628577 -0.0008558782 0.004397115 1.686872e-02
## 2016-06-30 0.0191666271 0.044582339 -0.0244913641 0.008292177 3.469800e-03
## 2016-07-29 0.0054299649 0.052442034 0.0390000248 0.049348308 3.582206e-02
## 2016-08-31 -0.0021568371 0.008798788 0.0053268628 0.011261250 1.196332e-03
## 2016-09-30 0.0005163371 0.024872631 0.0132791585 0.008614597 5.845891e-05
## 2016-10-31 -0.0082055791 -0.008312009 -0.0224037499 -0.038134931 -1.748921e-02
## 2016-11-30 -0.0259893794 -0.045161835 -0.0179743989 0.125246501 3.617606e-02
## 2016-12-30 0.0025385048 -0.002529946 0.0267029429 0.031491738 2.006917e-02
## 2017-01-31 0.0021257480 0.064431335 0.0323817146 -0.012143970 1.773653e-02
## 2017-02-28 0.0064379246 0.017258017 0.0118364721 0.013428564 3.853911e-02
## 2017-03-31 -0.0005530665 0.036188982 0.0318057896 -0.006532773 1.249233e-03
## 2017-04-28 0.0090295817 0.016866285 0.0239521397 0.005107589 9.876945e-03
## 2017-05-31 0.0068470655 0.028059986 0.0348101875 -0.022862441 1.401452e-02
## 2017-06-30 -0.0001825937 0.009223657 0.0029560702 0.029151811 6.354404e-03
## 2017-07-31 0.0033343112 0.056594548 0.0261878661 0.007481677 2.034600e-02
## 2017-08-31 0.0093689965 0.023243973 -0.0004485032 -0.027564918 2.913587e-03
## 2017-09-29 -0.0057321835 -0.000446495 0.0233427783 0.082321900 1.994907e-02
## 2017-10-31 0.0009779748 0.032278575 0.0166538670 0.005915972 2.329055e-02
## 2017-11-30 -0.0014839375 -0.003897049 0.0068697711 0.036913066 3.010826e-02
## 2017-12-29 0.0047408527 0.036925493 0.0133985362 -0.003731136 1.205480e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398452e-05 0.0001042102 4.178272e-05 -7.811836e-05 -9.031213e-06
## EEM 1.042102e-04 0.0017547113 1.039016e-03 6.437734e-04 6.795434e-04
## EFA 4.178272e-05 0.0010390163 1.064235e-03 6.490307e-04 6.975413e-04
## IJS -7.811836e-05 0.0006437734 6.490307e-04 1.565453e-03 8.290254e-04
## SPY -9.031213e-06 0.0006795434 6.975413e-04 8.290254e-04 7.408301e-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.02347492
# 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.0003874181 0.009257143 0.005815628 0.005684477 0.002330251
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062310813 -0.002935574 0.0366062100 0.052132878 4.992324e-02
## 2013-02-28 0.0058912009 -0.023105233 -0.0129694779 0.016175880 1.267810e-02
## 2013-03-28 0.0009848334 -0.010234896 0.0129694779 0.040257984 3.726767e-02
## 2013-04-30 0.0096392456 0.012084677 0.0489675453 0.001222668 1.903030e-02
## 2013-05-31 -0.0202138448 -0.049483496 -0.0306555133 0.041976078 2.333538e-02
## 2013-06-28 -0.0157784487 -0.054728016 -0.0271443833 -0.001402752 -1.343423e-02
## 2013-07-31 0.0026879115 0.013159634 0.0518602606 0.063541428 5.038587e-02
## 2013-08-30 -0.0082984921 -0.025705798 -0.0197462012 -0.034743738 -3.045102e-02
## 2013-09-30 0.0111440498 0.069588841 0.0753384162 0.063873739 3.115587e-02
## 2013-10-31 0.0082920313 0.040861183 0.0320816387 0.034234022 4.526657e-02
## 2013-11-29 -0.0025105158 -0.002593914 0.0054497017 0.041661174 2.920672e-02
## 2013-12-31 -0.0055821742 -0.004074132 0.0215281336 0.012891882 2.559617e-02
## 2014-01-31 0.0152913059 -0.090322840 -0.0534131388 -0.035775164 -3.588454e-02
## 2014-02-28 0.0037569374 0.033220355 0.0595049216 0.045257575 4.451059e-02
## 2014-03-31 -0.0014812914 0.038022056 -0.0046026436 0.013315273 8.260921e-03
## 2014-04-30 0.0081832033 0.007772509 0.0165291421 -0.023184518 6.927763e-03
## 2014-05-30 0.0117214984 0.029091191 0.0158285784 0.006205465 2.294099e-02
## 2014-06-30 -0.0005760467 0.023733724 0.0091654091 0.037718876 2.043469e-02
## 2014-07-31 -0.0025122598 0.013556009 -0.0263797076 -0.052009437 -1.352864e-02
## 2014-08-29 0.0114313080 0.027904572 0.0018005749 0.043657805 3.870492e-02
## 2014-09-30 -0.0061678908 -0.080856901 -0.0395986457 -0.061260619 -1.389274e-02
## 2014-10-31 0.0105846842 0.014096566 -0.0026548113 0.068875056 2.327779e-02
## 2014-11-28 0.0065486013 -0.015541296 0.0006252305 0.004773331 2.710158e-02
## 2014-12-31 0.0014749154 -0.040442465 -0.0407466515 0.025296135 -2.539838e-03
## 2015-01-30 0.0203158784 -0.006895499 0.0062264229 -0.054628122 -3.007681e-02
## 2015-02-27 -0.0089886336 0.043136254 0.0614505450 0.056914939 5.468197e-02
## 2015-03-31 0.0037406581 -0.015086134 -0.0143887084 0.010156236 -1.583053e-02
## 2015-04-30 -0.0032326370 0.066281182 0.0358165849 -0.018417892 9.785779e-03
## 2015-05-29 -0.0043843653 -0.041910788 0.0019527236 0.007509950 1.277422e-02
## 2015-06-30 -0.0108255046 -0.029746956 -0.0316788276 0.004171296 -2.052127e-02
## 2015-07-31 0.0085846666 -0.065178134 0.0201143312 -0.027375367 2.233798e-02
## 2015-08-31 -0.0033637353 -0.092512342 -0.0771521747 -0.047268264 -6.288642e-02
## 2015-09-30 0.0080816190 -0.031824994 -0.0451948928 -0.038464796 -2.584741e-02
## 2015-10-30 0.0006852449 0.061808242 0.0640258868 0.063589794 8.163514e-02
## 2015-11-30 -0.0038980454 -0.025560338 -0.0075558800 0.024415337 3.648277e-03
## 2015-12-31 -0.0019188111 -0.038947105 -0.0235951964 -0.052157142 -1.743339e-02
## 2016-01-29 0.0123294448 -0.051636605 -0.0567577412 -0.060306947 -5.106884e-02
## 2016-02-29 0.0088324755 -0.008211831 -0.0339139044 0.020605039 -8.262107e-04
## 2016-03-31 0.0087080528 0.121879078 0.0637458653 0.089910582 6.510041e-02
## 2016-04-29 0.0025465531 0.004079416 0.0219748450 0.021044310 3.933136e-03
## 2016-05-31 0.0001354825 -0.037628577 -0.0008558782 0.004397115 1.686872e-02
## 2016-06-30 0.0191666271 0.044582339 -0.0244913641 0.008292177 3.469800e-03
## 2016-07-29 0.0054299649 0.052442034 0.0390000248 0.049348308 3.582206e-02
## 2016-08-31 -0.0021568371 0.008798788 0.0053268628 0.011261250 1.196332e-03
## 2016-09-30 0.0005163371 0.024872631 0.0132791585 0.008614597 5.845891e-05
## 2016-10-31 -0.0082055791 -0.008312009 -0.0224037499 -0.038134931 -1.748921e-02
## 2016-11-30 -0.0259893794 -0.045161835 -0.0179743989 0.125246501 3.617606e-02
## 2016-12-30 0.0025385048 -0.002529946 0.0267029429 0.031491738 2.006917e-02
## 2017-01-31 0.0021257480 0.064431335 0.0323817146 -0.012143970 1.773653e-02
## 2017-02-28 0.0064379246 0.017258017 0.0118364721 0.013428564 3.853911e-02
## 2017-03-31 -0.0005530665 0.036188982 0.0318057896 -0.006532773 1.249233e-03
## 2017-04-28 0.0090295817 0.016866285 0.0239521397 0.005107589 9.876945e-03
## 2017-05-31 0.0068470655 0.028059986 0.0348101875 -0.022862441 1.401452e-02
## 2017-06-30 -0.0001825937 0.009223657 0.0029560702 0.029151811 6.354404e-03
## 2017-07-31 0.0033343112 0.056594548 0.0261878661 0.007481677 2.034600e-02
## 2017-08-31 0.0093689965 0.023243973 -0.0004485032 -0.027564918 2.913587e-03
## 2017-09-29 -0.0057321835 -0.000446495 0.0233427783 0.082321900 1.994907e-02
## 2017-10-31 0.0009779748 0.032278575 0.0166538670 0.005915972 2.329055e-02
## 2017-11-30 -0.0014839375 -0.003897049 0.0068697711 0.036913066 3.010826e-02
## 2017-12-29 0.0047408527 0.036925493 0.0133985362 -0.003731136 1.205480e-02
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(.data)
covariance_matrix
# 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()
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 Portfolio Volitility")
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 Portfolio Volitility and Weight", y = "Percent",
x = NULL)