# 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.0062311938 -0.0029356149 0.0366062071 0.052132890 4.992312e-02
## 2013-02-28 0.0058911491 -0.0231052718 -0.0129692988 0.016175296 1.267823e-02
## 2013-03-28 0.0009846267 -0.0102349807 0.0129692988 0.040258214 3.726804e-02
## 2013-04-30 0.0096394590 0.0120845659 0.0489679629 0.001222503 1.903027e-02
## 2013-05-31 -0.0202137559 -0.0494833281 -0.0306557129 0.041976150 2.333483e-02
## 2013-06-28 -0.0157784203 -0.0547283624 -0.0271442996 -0.001402875 -1.343410e-02
## 2013-07-31 0.0026879274 0.0131597634 0.0518602591 0.063541614 5.038575e-02
## 2013-08-30 -0.0082981024 -0.0257056700 -0.0197465446 -0.034743794 -3.045118e-02
## 2013-09-30 0.0111437569 0.0695889000 0.0753385995 0.063873903 3.115581e-02
## 2013-10-31 0.0082919677 0.0408614731 0.0320816661 0.034233899 4.526690e-02
## 2013-11-29 -0.0025098434 -0.0025942668 0.0054497972 0.041661323 2.920683e-02
## 2013-12-31 -0.0055833727 -0.0040744756 0.0215279453 0.012891974 2.559586e-02
## 2014-01-31 0.0152916452 -0.0903224525 -0.0534132453 -0.035775315 -3.588430e-02
## 2014-02-28 0.0037573509 0.0332204476 0.0595049838 0.045257432 4.451039e-02
## 2014-03-31 -0.0014815444 0.0380218609 -0.0046024237 0.013315164 8.261372e-03
## 2014-04-30 0.0081827068 0.0077727660 0.0165293809 -0.023184109 6.927500e-03
## 2014-05-30 0.0117219368 0.0290909984 0.0158283807 0.006205324 2.294118e-02
## 2014-06-30 -0.0005757119 0.0237339392 0.0091655176 0.037718692 2.043452e-02
## 2014-07-31 -0.0025121844 0.0135557339 -0.0263799557 -0.052009333 -1.352858e-02
## 2014-08-29 0.0114310779 0.0279045096 0.0018005836 0.043657633 3.870484e-02
## 2014-09-30 -0.0061681053 -0.0808568044 -0.0395986345 -0.061260472 -1.389256e-02
## 2014-10-31 0.0105853577 0.0140966491 -0.0026546487 0.068874806 2.327807e-02
## 2014-11-28 0.0065486076 -0.0155414839 0.0006251483 0.004774007 2.710101e-02
## 2014-12-31 0.0014745513 -0.0404419447 -0.0407467496 0.025295475 -2.539391e-03
## 2015-01-30 0.0203157653 -0.0068958195 0.0062266515 -0.054627727 -3.007737e-02
## 2015-02-27 -0.0089882366 0.0431361518 0.0614504471 0.056914519 5.468185e-02
## 2015-03-31 0.0037401380 -0.0150863650 -0.0143885547 0.010156103 -1.583028e-02
## 2015-04-30 -0.0032333579 0.0662812915 0.0358163054 -0.018417512 9.785978e-03
## 2015-05-29 -0.0043836810 -0.0419110348 0.0019528441 0.007510072 1.277407e-02
## 2015-06-30 -0.0108250229 -0.0297465862 -0.0316789216 0.004171341 -2.052115e-02
## 2015-07-31 0.0085842899 -0.0651780054 0.0201145590 -0.027375405 2.233794e-02
## 2015-08-31 -0.0033637686 -0.0925125820 -0.0771524288 -0.047268445 -6.288667e-02
## 2015-09-30 0.0080819523 -0.0318247067 -0.0451950749 -0.038464611 -2.584727e-02
## 2015-10-30 0.0006850480 0.0618083092 0.0640260980 0.063589665 8.163521e-02
## 2015-11-30 -0.0038983052 -0.0255606226 -0.0075558630 0.024415189 3.648134e-03
## 2015-12-31 -0.0019186899 -0.0389470367 -0.0235949324 -0.052157053 -1.743346e-02
## 2016-01-29 0.0123295753 -0.0516366466 -0.0567579292 -0.060306943 -5.106853e-02
## 2016-02-29 0.0088320249 -0.0082114300 -0.0339139210 0.020605353 -8.261283e-04
## 2016-03-31 0.0087085888 0.1218788614 0.0637456380 0.089910556 6.509991e-02
## 2016-04-29 0.0025460543 0.0040793342 0.0219751258 0.021044115 3.933460e-03
## 2016-05-31 0.0001355821 -0.0376285480 -0.0008561489 0.004397040 1.686870e-02
## 2016-06-30 0.0191668948 0.0445823791 -0.0244914495 0.008292351 3.469841e-03
## 2016-07-29 0.0054293384 0.0524423965 0.0390002318 0.049348444 3.582171e-02
## 2016-08-31 -0.0021557030 0.0087987423 0.0053268061 0.011260998 1.197078e-03
## 2016-09-30 0.0005155519 0.0248726899 0.0132792447 0.008614733 5.796024e-05
## 2016-10-31 -0.0082048919 -0.0083126000 -0.0224036216 -0.038134756 -1.748909e-02
## 2016-11-30 -0.0259896866 -0.0451615541 -0.0179744997 0.125246265 3.617591e-02
## 2016-12-30 0.0025377956 -0.0025300701 0.0267028663 0.031492019 2.006913e-02
## 2017-01-31 0.0021260874 0.0644312746 0.0323817590 -0.012144235 1.773659e-02
## 2017-02-28 0.0064378146 0.0172581706 0.0118365242 0.013428817 3.853930e-02
## 2017-03-31 -0.0005524628 0.0361890071 0.0318056037 -0.006533221 1.249100e-03
## 2017-04-28 0.0090290975 0.0168659566 0.0239522302 0.005108111 9.877365e-03
## 2017-05-31 0.0068472617 0.0280601914 0.0348102875 -0.022862715 1.401392e-02
## 2017-06-30 -0.0001825845 0.0092236754 0.0029558483 0.029151873 6.354856e-03
## 2017-07-31 0.0033347298 0.0565944551 0.0261879254 0.007481106 2.034571e-02
## 2017-08-31 0.0093693012 0.0232439220 -0.0004482983 -0.027564554 2.913602e-03
## 2017-09-29 -0.0057328166 -0.0004461971 0.0233427221 0.082321936 1.994909e-02
## 2017-10-31 0.0009779872 0.0322782484 0.0166538934 0.005915878 2.329066e-02
## 2017-11-30 -0.0014839301 -0.0038969126 0.0068697764 0.036913503 3.010794e-02
## 2017-12-29 0.0047405756 0.0369254493 0.0133983353 -0.003731344 1.205502e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398457e-05 0.0001042109 4.178171e-05 -7.811982e-05 -9.032008e-06
## EEM 1.042109e-04 0.0017547095 1.039017e-03 6.437747e-04 6.795427e-04
## EFA 4.178171e-05 0.0010390166 1.064238e-03 6.490309e-04 6.975400e-04
## IJS -7.811982e-05 0.0006437747 6.490309e-04 1.565450e-03 8.290244e-04
## SPY -9.032008e-06 0.0006795427 6.975400e-04 8.290244e-04 7.408275e-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.0003874141 0.009257145 0.005815633 0.005684472 0.002330247
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.0062311938 -0.0029356149 0.0366062071 0.052132890 4.992312e-02
## 2013-02-28 0.0058911491 -0.0231052718 -0.0129692988 0.016175296 1.267823e-02
## 2013-03-28 0.0009846267 -0.0102349807 0.0129692988 0.040258214 3.726804e-02
## 2013-04-30 0.0096394590 0.0120845659 0.0489679629 0.001222503 1.903027e-02
## 2013-05-31 -0.0202137559 -0.0494833281 -0.0306557129 0.041976150 2.333483e-02
## 2013-06-28 -0.0157784203 -0.0547283624 -0.0271442996 -0.001402875 -1.343410e-02
## 2013-07-31 0.0026879274 0.0131597634 0.0518602591 0.063541614 5.038575e-02
## 2013-08-30 -0.0082981024 -0.0257056700 -0.0197465446 -0.034743794 -3.045118e-02
## 2013-09-30 0.0111437569 0.0695889000 0.0753385995 0.063873903 3.115581e-02
## 2013-10-31 0.0082919677 0.0408614731 0.0320816661 0.034233899 4.526690e-02
## 2013-11-29 -0.0025098434 -0.0025942668 0.0054497972 0.041661323 2.920683e-02
## 2013-12-31 -0.0055833727 -0.0040744756 0.0215279453 0.012891974 2.559586e-02
## 2014-01-31 0.0152916452 -0.0903224525 -0.0534132453 -0.035775315 -3.588430e-02
## 2014-02-28 0.0037573509 0.0332204476 0.0595049838 0.045257432 4.451039e-02
## 2014-03-31 -0.0014815444 0.0380218609 -0.0046024237 0.013315164 8.261372e-03
## 2014-04-30 0.0081827068 0.0077727660 0.0165293809 -0.023184109 6.927500e-03
## 2014-05-30 0.0117219368 0.0290909984 0.0158283807 0.006205324 2.294118e-02
## 2014-06-30 -0.0005757119 0.0237339392 0.0091655176 0.037718692 2.043452e-02
## 2014-07-31 -0.0025121844 0.0135557339 -0.0263799557 -0.052009333 -1.352858e-02
## 2014-08-29 0.0114310779 0.0279045096 0.0018005836 0.043657633 3.870484e-02
## 2014-09-30 -0.0061681053 -0.0808568044 -0.0395986345 -0.061260472 -1.389256e-02
## 2014-10-31 0.0105853577 0.0140966491 -0.0026546487 0.068874806 2.327807e-02
## 2014-11-28 0.0065486076 -0.0155414839 0.0006251483 0.004774007 2.710101e-02
## 2014-12-31 0.0014745513 -0.0404419447 -0.0407467496 0.025295475 -2.539391e-03
## 2015-01-30 0.0203157653 -0.0068958195 0.0062266515 -0.054627727 -3.007737e-02
## 2015-02-27 -0.0089882366 0.0431361518 0.0614504471 0.056914519 5.468185e-02
## 2015-03-31 0.0037401380 -0.0150863650 -0.0143885547 0.010156103 -1.583028e-02
## 2015-04-30 -0.0032333579 0.0662812915 0.0358163054 -0.018417512 9.785978e-03
## 2015-05-29 -0.0043836810 -0.0419110348 0.0019528441 0.007510072 1.277407e-02
## 2015-06-30 -0.0108250229 -0.0297465862 -0.0316789216 0.004171341 -2.052115e-02
## 2015-07-31 0.0085842899 -0.0651780054 0.0201145590 -0.027375405 2.233794e-02
## 2015-08-31 -0.0033637686 -0.0925125820 -0.0771524288 -0.047268445 -6.288667e-02
## 2015-09-30 0.0080819523 -0.0318247067 -0.0451950749 -0.038464611 -2.584727e-02
## 2015-10-30 0.0006850480 0.0618083092 0.0640260980 0.063589665 8.163521e-02
## 2015-11-30 -0.0038983052 -0.0255606226 -0.0075558630 0.024415189 3.648134e-03
## 2015-12-31 -0.0019186899 -0.0389470367 -0.0235949324 -0.052157053 -1.743346e-02
## 2016-01-29 0.0123295753 -0.0516366466 -0.0567579292 -0.060306943 -5.106853e-02
## 2016-02-29 0.0088320249 -0.0082114300 -0.0339139210 0.020605353 -8.261283e-04
## 2016-03-31 0.0087085888 0.1218788614 0.0637456380 0.089910556 6.509991e-02
## 2016-04-29 0.0025460543 0.0040793342 0.0219751258 0.021044115 3.933460e-03
## 2016-05-31 0.0001355821 -0.0376285480 -0.0008561489 0.004397040 1.686870e-02
## 2016-06-30 0.0191668948 0.0445823791 -0.0244914495 0.008292351 3.469841e-03
## 2016-07-29 0.0054293384 0.0524423965 0.0390002318 0.049348444 3.582171e-02
## 2016-08-31 -0.0021557030 0.0087987423 0.0053268061 0.011260998 1.197078e-03
## 2016-09-30 0.0005155519 0.0248726899 0.0132792447 0.008614733 5.796024e-05
## 2016-10-31 -0.0082048919 -0.0083126000 -0.0224036216 -0.038134756 -1.748909e-02
## 2016-11-30 -0.0259896866 -0.0451615541 -0.0179744997 0.125246265 3.617591e-02
## 2016-12-30 0.0025377956 -0.0025300701 0.0267028663 0.031492019 2.006913e-02
## 2017-01-31 0.0021260874 0.0644312746 0.0323817590 -0.012144235 1.773659e-02
## 2017-02-28 0.0064378146 0.0172581706 0.0118365242 0.013428817 3.853930e-02
## 2017-03-31 -0.0005524628 0.0361890071 0.0318056037 -0.006533221 1.249100e-03
## 2017-04-28 0.0090290975 0.0168659566 0.0239522302 0.005108111 9.877365e-03
## 2017-05-31 0.0068472617 0.0280601914 0.0348102875 -0.022862715 1.401392e-02
## 2017-06-30 -0.0001825845 0.0092236754 0.0029558483 0.029151873 6.354856e-03
## 2017-07-31 0.0033347298 0.0565944551 0.0261879254 0.007481106 2.034571e-02
## 2017-08-31 0.0093693012 0.0232439220 -0.0004482983 -0.027564554 2.913602e-03
## 2017-09-29 -0.0057328166 -0.0004461971 0.0233427221 0.082321936 1.994909e-02
## 2017-10-31 0.0009779872 0.0322782484 0.0166538934 0.005915878 2.329066e-02
## 2017-11-30 -0.0014839301 -0.0038969126 0.0068697764 0.036913503 3.010794e-02
## 2017-12-29 0.0047405756 0.0369254493 0.0133983353 -0.003731344 1.205502e-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(0.25, 0.25, 0.2, 0.2, 0.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 ## 6 Rolling Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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)