# 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.0062317479 -0.0029354630 0.0366061226 0.052133425 4.992285e-02
## 2013-02-28 0.0058914537 -0.0231053436 -0.0129692045 0.016175559 1.267873e-02
## 2013-03-28 0.0009856270 -0.0102348964 0.0129692045 0.040257984 3.726735e-02
## 2013-04-30 0.0096388762 0.0120847897 0.0489679861 0.001222567 1.903048e-02
## 2013-05-31 -0.0202143806 -0.0494836088 -0.0306555943 0.041976373 2.333503e-02
## 2013-06-28 -0.0157782545 -0.0547283290 -0.0271443784 -0.001403043 -1.343435e-02
## 2013-07-31 0.0026880032 0.0131596995 0.0518602514 0.063540979 5.038589e-02
## 2013-08-30 -0.0082979076 -0.0257055501 -0.0197463751 -0.034743097 -3.045115e-02
## 2013-09-30 0.0111436053 0.0695888407 0.0753384162 0.063873645 3.115633e-02
## 2013-10-31 0.0082925019 0.0408612969 0.0320817183 0.034234449 4.526635e-02
## 2013-11-29 -0.0025104110 -0.0025940275 0.0054497013 0.041660911 2.920682e-02
## 2013-12-31 -0.0055832820 -0.0040742469 0.0215281319 0.012891880 2.559637e-02
## 2014-01-31 0.0152923681 -0.0903229138 -0.0534132163 -0.035775325 -3.588484e-02
## 2014-02-28 0.0037565492 0.0332204216 0.0595049987 0.045257495 4.451040e-02
## 2014-03-31 -0.0014817374 0.0380217103 -0.0046025659 0.013315037 8.261218e-03
## 2014-04-30 0.0081832767 0.0077728602 0.0165291395 -0.023184040 6.927568e-03
## 2014-05-30 0.0117220911 0.0290913071 0.0158285011 0.006205222 2.294128e-02
## 2014-06-30 -0.0005761474 0.0237336143 0.0091656312 0.037718802 2.043459e-02
## 2014-07-31 -0.0025116546 0.0135556857 -0.0263800046 -0.052009446 -1.352883e-02
## 2014-08-29 0.0114304944 0.0279050056 0.0018005749 0.043657891 3.870502e-02
## 2014-09-30 -0.0061673294 -0.0808567872 -0.0395985666 -0.061260292 -1.389265e-02
## 2014-10-31 0.0105841062 0.0140964517 -0.0026548905 0.068874574 2.327806e-02
## 2014-11-28 0.0065487923 -0.0155414106 0.0006253892 0.004773872 2.710140e-02
## 2014-12-31 0.0014755412 -0.0404421120 -0.0407467275 0.025295751 -2.539573e-03
## 2015-01-30 0.0203147413 -0.0068959765 0.0062264224 -0.054627967 -3.007743e-02
## 2015-02-27 -0.0089880135 0.0431363785 0.0614505401 0.056914634 5.468216e-02
## 2015-03-31 0.0037402461 -0.0150862520 -0.0143885505 0.010156164 -1.583027e-02
## 2015-04-30 -0.0032326768 0.0662813058 0.0358164255 -0.018417443 9.785431e-03
## 2015-05-29 -0.0043840893 -0.0419107925 0.0019527235 0.007509648 1.277465e-02
## 2015-06-30 -0.0108251835 -0.0297469590 -0.0316788252 0.004171446 -2.052162e-02
## 2015-07-31 0.0085847478 -0.0651780164 0.0201145586 -0.027375521 2.233806e-02
## 2015-08-31 -0.0033644410 -0.0925122045 -0.0771527273 -0.047268030 -6.288669e-02
## 2015-09-30 0.0080818231 -0.0318250603 -0.0451947318 -0.038464709 -2.584695e-02
## 2015-10-30 0.0006851199 0.0618082373 0.0640258921 0.063589469 8.163495e-02
## 2015-11-30 -0.0038978977 -0.0255604047 -0.0075557990 0.024415418 3.648362e-03
## 2015-12-31 -0.0019184505 -0.0389469627 -0.0235951129 -0.052157227 -1.743365e-02
## 2016-01-29 0.0123292094 -0.0516366720 -0.0567577363 -0.060306614 -5.106848e-02
## 2016-02-29 0.0088321884 -0.0082116798 -0.0339138100 0.020605119 -8.263021e-04
## 2016-03-31 0.0087084356 0.1218791187 0.0637455968 0.089910637 6.510006e-02
## 2016-04-29 0.0025462862 0.0040790827 0.0219752662 0.021043927 3.933394e-03
## 2016-05-31 0.0001351423 -0.0376283033 -0.0008562977 0.004396966 1.686880e-02
## 2016-06-30 0.0191672693 0.0445821975 -0.0244914523 0.008292550 3.469716e-03
## 2016-07-29 0.0054294445 0.0524420308 0.0390001969 0.049348301 3.582182e-02
## 2016-08-31 -0.0021560949 0.0087987255 0.0053267805 0.011261109 1.196897e-03
## 2016-09-30 0.0005156368 0.0248728732 0.0132794033 0.008614667 5.797444e-05
## 2016-10-31 -0.0082048082 -0.0083121295 -0.0224037462 -0.038134857 -1.748905e-02
## 2016-11-30 -0.0259895228 -0.0451619574 -0.0179746496 0.125246484 3.617606e-02
## 2016-12-30 0.0025378093 -0.0025300102 0.0267031098 0.031491796 2.006893e-02
## 2017-01-31 0.0021262473 0.0644314594 0.0323819512 -0.012144030 1.773662e-02
## 2017-02-28 0.0064377595 0.0172579571 0.0118362320 0.013428623 3.853934e-02
## 2017-03-31 -0.0005529599 0.0361887542 0.0318056345 -0.006533082 1.249013e-03
## 2017-04-28 0.0090290824 0.0168667372 0.0239521415 0.005108020 9.877164e-03
## 2017-05-31 0.0068475214 0.0280597625 0.0348104060 -0.022862749 1.401423e-02
## 2017-06-30 -0.0001825271 0.0092236570 0.0029559263 0.029151933 6.354975e-03
## 2017-07-31 0.0033342159 0.0565946495 0.0261877962 0.007481251 2.034572e-02
## 2017-08-31 0.0093689963 0.0232437716 -0.0004482935 -0.027564552 2.913587e-03
## 2017-09-29 -0.0057320035 -0.0004462957 0.0233427751 0.082321608 1.994894e-02
## 2017-10-31 0.0009785104 0.0322784749 0.0166537304 0.005916088 2.329075e-02
## 2017-11-30 -0.0014847311 -0.0038969524 0.0068701047 0.036913180 3.010800e-02
## 2017-12-29 0.0047404549 0.0369253964 0.0133981368 -0.003731026 1.205519e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398392e-05 0.0001042089 4.178037e-05 -7.812212e-05 -9.032446e-06
## EEM 1.042089e-04 0.0017547114 1.039017e-03 6.437733e-04 6.795436e-04
## EFA 4.178037e-05 0.0010390174 1.064237e-03 6.490286e-04 6.975410e-04
## IJS -7.812212e-05 0.0006437733 6.490286e-04 1.565447e-03 8.290241e-04
## SPY -9.032446e-06 0.0006795436 6.975410e-04 8.290241e-04 7.408295e-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.02347489
# 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.0003873992 0.009257152 0.005815632 0.005684459 0.00233025
rowSums(component_contribution)
## [1] 0.02347489
# 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.0062317479 -0.0029354630 0.0366061226 0.052133425 4.992285e-02
## 2013-02-28 0.0058914537 -0.0231053436 -0.0129692045 0.016175559 1.267873e-02
## 2013-03-28 0.0009856270 -0.0102348964 0.0129692045 0.040257984 3.726735e-02
## 2013-04-30 0.0096388762 0.0120847897 0.0489679861 0.001222567 1.903048e-02
## 2013-05-31 -0.0202143806 -0.0494836088 -0.0306555943 0.041976373 2.333503e-02
## 2013-06-28 -0.0157782545 -0.0547283290 -0.0271443784 -0.001403043 -1.343435e-02
## 2013-07-31 0.0026880032 0.0131596995 0.0518602514 0.063540979 5.038589e-02
## 2013-08-30 -0.0082979076 -0.0257055501 -0.0197463751 -0.034743097 -3.045115e-02
## 2013-09-30 0.0111436053 0.0695888407 0.0753384162 0.063873645 3.115633e-02
## 2013-10-31 0.0082925019 0.0408612969 0.0320817183 0.034234449 4.526635e-02
## 2013-11-29 -0.0025104110 -0.0025940275 0.0054497013 0.041660911 2.920682e-02
## 2013-12-31 -0.0055832820 -0.0040742469 0.0215281319 0.012891880 2.559637e-02
## 2014-01-31 0.0152923681 -0.0903229138 -0.0534132163 -0.035775325 -3.588484e-02
## 2014-02-28 0.0037565492 0.0332204216 0.0595049987 0.045257495 4.451040e-02
## 2014-03-31 -0.0014817374 0.0380217103 -0.0046025659 0.013315037 8.261218e-03
## 2014-04-30 0.0081832767 0.0077728602 0.0165291395 -0.023184040 6.927568e-03
## 2014-05-30 0.0117220911 0.0290913071 0.0158285011 0.006205222 2.294128e-02
## 2014-06-30 -0.0005761474 0.0237336143 0.0091656312 0.037718802 2.043459e-02
## 2014-07-31 -0.0025116546 0.0135556857 -0.0263800046 -0.052009446 -1.352883e-02
## 2014-08-29 0.0114304944 0.0279050056 0.0018005749 0.043657891 3.870502e-02
## 2014-09-30 -0.0061673294 -0.0808567872 -0.0395985666 -0.061260292 -1.389265e-02
## 2014-10-31 0.0105841062 0.0140964517 -0.0026548905 0.068874574 2.327806e-02
## 2014-11-28 0.0065487923 -0.0155414106 0.0006253892 0.004773872 2.710140e-02
## 2014-12-31 0.0014755412 -0.0404421120 -0.0407467275 0.025295751 -2.539573e-03
## 2015-01-30 0.0203147413 -0.0068959765 0.0062264224 -0.054627967 -3.007743e-02
## 2015-02-27 -0.0089880135 0.0431363785 0.0614505401 0.056914634 5.468216e-02
## 2015-03-31 0.0037402461 -0.0150862520 -0.0143885505 0.010156164 -1.583027e-02
## 2015-04-30 -0.0032326768 0.0662813058 0.0358164255 -0.018417443 9.785431e-03
## 2015-05-29 -0.0043840893 -0.0419107925 0.0019527235 0.007509648 1.277465e-02
## 2015-06-30 -0.0108251835 -0.0297469590 -0.0316788252 0.004171446 -2.052162e-02
## 2015-07-31 0.0085847478 -0.0651780164 0.0201145586 -0.027375521 2.233806e-02
## 2015-08-31 -0.0033644410 -0.0925122045 -0.0771527273 -0.047268030 -6.288669e-02
## 2015-09-30 0.0080818231 -0.0318250603 -0.0451947318 -0.038464709 -2.584695e-02
## 2015-10-30 0.0006851199 0.0618082373 0.0640258921 0.063589469 8.163495e-02
## 2015-11-30 -0.0038978977 -0.0255604047 -0.0075557990 0.024415418 3.648362e-03
## 2015-12-31 -0.0019184505 -0.0389469627 -0.0235951129 -0.052157227 -1.743365e-02
## 2016-01-29 0.0123292094 -0.0516366720 -0.0567577363 -0.060306614 -5.106848e-02
## 2016-02-29 0.0088321884 -0.0082116798 -0.0339138100 0.020605119 -8.263021e-04
## 2016-03-31 0.0087084356 0.1218791187 0.0637455968 0.089910637 6.510006e-02
## 2016-04-29 0.0025462862 0.0040790827 0.0219752662 0.021043927 3.933394e-03
## 2016-05-31 0.0001351423 -0.0376283033 -0.0008562977 0.004396966 1.686880e-02
## 2016-06-30 0.0191672693 0.0445821975 -0.0244914523 0.008292550 3.469716e-03
## 2016-07-29 0.0054294445 0.0524420308 0.0390001969 0.049348301 3.582182e-02
## 2016-08-31 -0.0021560949 0.0087987255 0.0053267805 0.011261109 1.196897e-03
## 2016-09-30 0.0005156368 0.0248728732 0.0132794033 0.008614667 5.797444e-05
## 2016-10-31 -0.0082048082 -0.0083121295 -0.0224037462 -0.038134857 -1.748905e-02
## 2016-11-30 -0.0259895228 -0.0451619574 -0.0179746496 0.125246484 3.617606e-02
## 2016-12-30 0.0025378093 -0.0025300102 0.0267031098 0.031491796 2.006893e-02
## 2017-01-31 0.0021262473 0.0644314594 0.0323819512 -0.012144030 1.773662e-02
## 2017-02-28 0.0064377595 0.0172579571 0.0118362320 0.013428623 3.853934e-02
## 2017-03-31 -0.0005529599 0.0361887542 0.0318056345 -0.006533082 1.249013e-03
## 2017-04-28 0.0090290824 0.0168667372 0.0239521415 0.005108020 9.877164e-03
## 2017-05-31 0.0068475214 0.0280597625 0.0348104060 -0.022862749 1.401423e-02
## 2017-06-30 -0.0001825271 0.0092236570 0.0029559263 0.029151933 6.354975e-03
## 2017-07-31 0.0033342159 0.0565946495 0.0261877962 0.007481251 2.034572e-02
## 2017-08-31 0.0093689963 0.0232437716 -0.0004482935 -0.027564552 2.913587e-03
## 2017-09-29 -0.0057320035 -0.0004462957 0.0233427751 0.082321608 1.994894e-02
## 2017-10-31 0.0009785104 0.0322784749 0.0166537304 0.005916088 2.329075e-02
## 2017-11-30 -0.0014847311 -0.0038969524 0.0068701047 0.036913180 3.010800e-02
## 2017-12-29 0.0047404549 0.0369253964 0.0133981368 -0.003731026 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 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 = .5))
labs(title = "Percent Contriibution tp {prtfolio Volatility")
## $title
## [1] "Percent Contriibution tp {prtfolio Volatility"
##
## attr(,"class")
## [1] "labels"
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 Weight
add_column(weight = c(.25, .25, .2, .2, .1)) %>%
# Tranform 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 = .5)) +
theme_tq() +
labs(title = "Percent Contriibution to Portfolio Volatility and Weight",
y = "Percent",
x = NULL)