# 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.0062310703 -0.0029350930 0.0366062698 0.052133686 4.992342e-02
## 2013-02-28 0.0058910689 -0.0231052854 -0.0129693233 0.016175286 1.267801e-02
## 2013-03-28 0.0009854895 -0.0102349970 0.0129693233 0.040258053 3.726814e-02
## 2013-04-30 0.0096393072 0.0120846536 0.0489679329 0.001222269 1.902985e-02
## 2013-05-31 -0.0202142694 -0.0494833093 -0.0306556320 0.041976522 2.333576e-02
## 2013-06-28 -0.0157785966 -0.0547283206 -0.0271445692 -0.001403290 -1.343455e-02
## 2013-07-31 0.0026876313 0.0131598422 0.0518603709 0.063541878 5.038635e-02
## 2013-08-30 -0.0082977809 -0.0257058403 -0.0197462980 -0.034743741 -3.045143e-02
## 2013-09-30 0.0111437361 0.0695890335 0.0753384185 0.063873984 3.115551e-02
## 2013-10-31 0.0082924548 0.0408611599 0.0320818492 0.034233646 4.526709e-02
## 2013-11-29 -0.0025105069 -0.0025938709 0.0054496382 0.041661426 2.920696e-02
## 2013-12-31 -0.0055831725 -0.0040746186 0.0215279917 0.012891998 2.559600e-02
## 2014-01-31 0.0152917985 -0.0903227464 -0.0534134224 -0.035775401 -3.588483e-02
## 2014-02-28 0.0037568560 0.0332207754 0.0595051752 0.045257346 4.451051e-02
## 2014-03-31 -0.0014813305 0.0380215019 -0.0046023124 0.013315482 8.261305e-03
## 2014-04-30 0.0081831216 0.0077727969 0.0165292059 -0.023184216 6.927463e-03
## 2014-05-30 0.0117219021 0.0290910773 0.0158282194 0.006205363 2.294118e-02
## 2014-06-30 -0.0005759825 0.0237340696 0.0091654908 0.037718504 2.043464e-02
## 2014-07-31 -0.0025124222 0.0135554643 -0.0263797850 -0.052009633 -1.352859e-02
## 2014-08-29 0.0114307630 0.0279047915 0.0018005131 0.043658139 3.870454e-02
## 2014-09-30 -0.0061673351 -0.0808568916 -0.0395986731 -0.061260322 -1.389226e-02
## 2014-10-31 0.0105849471 0.0140965415 -0.0026546542 0.068874703 2.327798e-02
## 2014-11-28 0.0065488393 -0.0155413222 0.0006252355 0.004773328 2.710113e-02
## 2014-12-31 0.0014747912 -0.0404422187 -0.0407466701 0.025296120 -2.539677e-03
## 2015-01-30 0.0203157578 -0.0068956277 0.0062265031 -0.054627823 -3.007659e-02
## 2015-02-27 -0.0089886625 0.0431362557 0.0614504485 0.056914653 5.468151e-02
## 2015-03-31 0.0037403191 -0.0150861199 -0.0143886933 0.010156131 -1.583022e-02
## 2015-04-30 -0.0032333102 0.0662811397 0.0358165769 -0.018417621 9.786066e-03
## 2015-05-29 -0.0043834166 -0.0419108998 0.0019527483 0.007510009 1.277400e-02
## 2015-06-30 -0.0108253781 -0.0297466579 -0.0316789392 0.004171395 -2.052116e-02
## 2015-07-31 0.0085845478 -0.0651782658 0.0201145890 -0.027375157 2.233773e-02
## 2015-08-31 -0.0033636943 -0.0925125037 -0.0771524538 -0.047268606 -6.288642e-02
## 2015-09-30 0.0080814237 -0.0318248686 -0.0451950173 -0.038464759 -2.584720e-02
## 2015-10-30 0.0006855144 0.0618083483 0.0640260956 0.063589750 8.163461e-02
## 2015-11-30 -0.0038983760 -0.0255601899 -0.0075559415 0.024415191 3.648509e-03
## 2015-12-31 -0.0019190861 -0.0389472916 -0.0235950350 -0.052157054 -1.743355e-02
## 2016-01-29 0.0123297267 -0.0516366854 -0.0567577476 -0.060306595 -5.106862e-02
## 2016-02-29 0.0088324313 -0.0082116151 -0.0339138891 0.020604869 -8.260927e-04
## 2016-03-31 0.0087087121 0.1218791532 0.0637457573 0.089910599 6.510026e-02
## 2016-04-29 0.0025458739 0.0040792898 0.0219749504 0.021044286 3.933452e-03
## 2016-05-31 0.0001356125 -0.0376287021 -0.0008561427 0.004396886 1.686847e-02
## 2016-06-30 0.0191664860 0.0445823354 -0.0244913881 0.008292097 3.469641e-03
## 2016-07-29 0.0054300247 0.0524423160 0.0390001962 0.049348456 3.582204e-02
## 2016-08-31 -0.0021564408 0.0087984104 0.0053269239 0.011261141 1.196879e-03
## 2016-09-30 0.0005158552 0.0248730027 0.0132791037 0.008614747 5.789918e-05
## 2016-10-31 -0.0082051791 -0.0083121374 -0.0224037236 -0.038134835 -1.748894e-02
## 2016-11-30 -0.0259894974 -0.0451618458 -0.0179745945 0.125246347 3.617603e-02
## 2016-12-30 0.0025380375 -0.0025300917 0.0267030151 0.031491872 2.006908e-02
## 2017-01-31 0.0021262875 0.0644312358 0.0323817970 -0.012143844 1.773643e-02
## 2017-02-28 0.0064375136 0.0172578955 0.0118366182 0.013428703 3.853925e-02
## 2017-03-31 -0.0005527966 0.0361892342 0.0318055175 -0.006533150 1.249205e-03
## 2017-04-28 0.0090292883 0.0168663683 0.0239523849 0.005107584 9.877109e-03
## 2017-05-31 0.0068474034 0.0280599886 0.0348100506 -0.022862503 1.401433e-02
## 2017-06-30 -0.0001826614 0.0092236117 0.0029558687 0.029151959 6.354854e-03
## 2017-07-31 0.0033345462 0.0565944936 0.0261880937 0.007481186 2.034571e-02
## 2017-08-31 0.0093692250 0.0232438852 -0.0004484189 -0.027564256 2.913533e-03
## 2017-09-29 -0.0057323664 -0.0004461534 0.0233427730 0.082321832 1.994898e-02
## 2017-10-31 0.0009779708 0.0322784833 0.0166536400 0.005915767 2.329065e-02
## 2017-11-30 -0.0014840591 -0.0038972207 0.0068700036 0.036913182 3.010823e-02
## 2017-12-29 0.0047402234 0.0369256775 0.0133983684 -0.003731205 1.205483e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398488e-05 0.0001042097 4.178223e-05 -7.812013e-05 -9.031881e-06
## EEM 1.042097e-04 0.0017547136 1.039020e-03 6.437765e-04 6.795434e-04
## EFA 4.178223e-05 0.0010390195 1.064238e-03 6.490319e-04 6.975404e-04
## IJS -7.812013e-05 0.0006437765 6.490319e-04 1.565452e-03 8.290257e-04
## SPY -9.031881e-06 0.0006795434 6.975404e-04 8.290257e-04 7.408279e-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.02347493
# 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.0003874119 0.009257156 0.005815639 0.005684477 0.002330247
rowSums(component_contribution)
## [1] 0.02347493
# 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.0062310703 -0.0029350930 0.0366062698 0.052133686 4.992342e-02
## 2013-02-28 0.0058910689 -0.0231052854 -0.0129693233 0.016175286 1.267801e-02
## 2013-03-28 0.0009854895 -0.0102349970 0.0129693233 0.040258053 3.726814e-02
## 2013-04-30 0.0096393072 0.0120846536 0.0489679329 0.001222269 1.902985e-02
## 2013-05-31 -0.0202142694 -0.0494833093 -0.0306556320 0.041976522 2.333576e-02
## 2013-06-28 -0.0157785966 -0.0547283206 -0.0271445692 -0.001403290 -1.343455e-02
## 2013-07-31 0.0026876313 0.0131598422 0.0518603709 0.063541878 5.038635e-02
## 2013-08-30 -0.0082977809 -0.0257058403 -0.0197462980 -0.034743741 -3.045143e-02
## 2013-09-30 0.0111437361 0.0695890335 0.0753384185 0.063873984 3.115551e-02
## 2013-10-31 0.0082924548 0.0408611599 0.0320818492 0.034233646 4.526709e-02
## 2013-11-29 -0.0025105069 -0.0025938709 0.0054496382 0.041661426 2.920696e-02
## 2013-12-31 -0.0055831725 -0.0040746186 0.0215279917 0.012891998 2.559600e-02
## 2014-01-31 0.0152917985 -0.0903227464 -0.0534134224 -0.035775401 -3.588483e-02
## 2014-02-28 0.0037568560 0.0332207754 0.0595051752 0.045257346 4.451051e-02
## 2014-03-31 -0.0014813305 0.0380215019 -0.0046023124 0.013315482 8.261305e-03
## 2014-04-30 0.0081831216 0.0077727969 0.0165292059 -0.023184216 6.927463e-03
## 2014-05-30 0.0117219021 0.0290910773 0.0158282194 0.006205363 2.294118e-02
## 2014-06-30 -0.0005759825 0.0237340696 0.0091654908 0.037718504 2.043464e-02
## 2014-07-31 -0.0025124222 0.0135554643 -0.0263797850 -0.052009633 -1.352859e-02
## 2014-08-29 0.0114307630 0.0279047915 0.0018005131 0.043658139 3.870454e-02
## 2014-09-30 -0.0061673351 -0.0808568916 -0.0395986731 -0.061260322 -1.389226e-02
## 2014-10-31 0.0105849471 0.0140965415 -0.0026546542 0.068874703 2.327798e-02
## 2014-11-28 0.0065488393 -0.0155413222 0.0006252355 0.004773328 2.710113e-02
## 2014-12-31 0.0014747912 -0.0404422187 -0.0407466701 0.025296120 -2.539677e-03
## 2015-01-30 0.0203157578 -0.0068956277 0.0062265031 -0.054627823 -3.007659e-02
## 2015-02-27 -0.0089886625 0.0431362557 0.0614504485 0.056914653 5.468151e-02
## 2015-03-31 0.0037403191 -0.0150861199 -0.0143886933 0.010156131 -1.583022e-02
## 2015-04-30 -0.0032333102 0.0662811397 0.0358165769 -0.018417621 9.786066e-03
## 2015-05-29 -0.0043834166 -0.0419108998 0.0019527483 0.007510009 1.277400e-02
## 2015-06-30 -0.0108253781 -0.0297466579 -0.0316789392 0.004171395 -2.052116e-02
## 2015-07-31 0.0085845478 -0.0651782658 0.0201145890 -0.027375157 2.233773e-02
## 2015-08-31 -0.0033636943 -0.0925125037 -0.0771524538 -0.047268606 -6.288642e-02
## 2015-09-30 0.0080814237 -0.0318248686 -0.0451950173 -0.038464759 -2.584720e-02
## 2015-10-30 0.0006855144 0.0618083483 0.0640260956 0.063589750 8.163461e-02
## 2015-11-30 -0.0038983760 -0.0255601899 -0.0075559415 0.024415191 3.648509e-03
## 2015-12-31 -0.0019190861 -0.0389472916 -0.0235950350 -0.052157054 -1.743355e-02
## 2016-01-29 0.0123297267 -0.0516366854 -0.0567577476 -0.060306595 -5.106862e-02
## 2016-02-29 0.0088324313 -0.0082116151 -0.0339138891 0.020604869 -8.260927e-04
## 2016-03-31 0.0087087121 0.1218791532 0.0637457573 0.089910599 6.510026e-02
## 2016-04-29 0.0025458739 0.0040792898 0.0219749504 0.021044286 3.933452e-03
## 2016-05-31 0.0001356125 -0.0376287021 -0.0008561427 0.004396886 1.686847e-02
## 2016-06-30 0.0191664860 0.0445823354 -0.0244913881 0.008292097 3.469641e-03
## 2016-07-29 0.0054300247 0.0524423160 0.0390001962 0.049348456 3.582204e-02
## 2016-08-31 -0.0021564408 0.0087984104 0.0053269239 0.011261141 1.196879e-03
## 2016-09-30 0.0005158552 0.0248730027 0.0132791037 0.008614747 5.789918e-05
## 2016-10-31 -0.0082051791 -0.0083121374 -0.0224037236 -0.038134835 -1.748894e-02
## 2016-11-30 -0.0259894974 -0.0451618458 -0.0179745945 0.125246347 3.617603e-02
## 2016-12-30 0.0025380375 -0.0025300917 0.0267030151 0.031491872 2.006908e-02
## 2017-01-31 0.0021262875 0.0644312358 0.0323817970 -0.012143844 1.773643e-02
## 2017-02-28 0.0064375136 0.0172578955 0.0118366182 0.013428703 3.853925e-02
## 2017-03-31 -0.0005527966 0.0361892342 0.0318055175 -0.006533150 1.249205e-03
## 2017-04-28 0.0090292883 0.0168663683 0.0239523849 0.005107584 9.877109e-03
## 2017-05-31 0.0068474034 0.0280599886 0.0348100506 -0.022862503 1.401433e-02
## 2017-06-30 -0.0001826614 0.0092236117 0.0029558687 0.029151959 6.354854e-03
## 2017-07-31 0.0033345462 0.0565944936 0.0261880937 0.007481186 2.034571e-02
## 2017-08-31 0.0093692250 0.0232438852 -0.0004484189 -0.027564256 2.913533e-03
## 2017-09-29 -0.0057323664 -0.0004461534 0.0233427730 0.082321832 1.994898e-02
## 2017-10-31 0.0009779708 0.0322784833 0.0166536400 0.005915767 2.329065e-02
## 2017-11-30 -0.0014840591 -0.0038972207 0.0068700036 0.036913182 3.010823e-02
## 2017-12-29 0.0047402234 0.0369256775 0.0133983684 -0.003731205 1.205483e-02
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# 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)
# 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)
}
aasset_reutrns_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 from
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(aaccuracy = 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 %>%
calculate_component_contribution(w = c(.25, .25, .2, .2, 1)) %>%
# Transform to long from
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(aaccuracy = 1)) +
scale_fill_tq() +
theme(plot.title = element_text(hjust = 0.5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight",
y = "Precent",
x = NULL)