# 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.0062316529 -0.0029353521 0.0366061192 0.052132991 4.992312e-02
## 2013-02-28 0.0058917296 -0.0231054516 -0.0129692952 0.016175459 1.267823e-02
## 2013-03-28 0.0009845152 -0.0102350096 0.0129692952 0.040258405 3.726797e-02
## 2013-04-30 0.0096391558 0.0120846767 0.0489676360 0.001222365 1.903024e-02
## 2013-05-31 -0.0202140100 -0.0494837332 -0.0306554241 0.041976187 2.333549e-02
## 2013-06-28 -0.0157777762 -0.0547280916 -0.0271443808 -0.001402752 -1.343469e-02
## 2013-07-31 0.0026868636 0.0131596377 0.0518601691 0.063541713 5.038611e-02
## 2013-08-30 -0.0082978160 -0.0257053613 -0.0197462012 -0.034743923 -3.045137e-02
## 2013-09-30 0.0111439877 0.0695883585 0.0753384984 0.063873480 3.115588e-02
## 2013-10-31 0.0082924081 0.0408617658 0.0320816361 0.034234119 4.526679e-02
## 2013-11-29 -0.0025097532 -0.0025944831 0.0054497805 0.041661431 2.920682e-02
## 2013-12-31 -0.0055834678 -0.0040741338 0.0215278201 0.012892044 2.559607e-02
## 2014-01-31 0.0152918969 -0.0903227476 -0.0534132291 -0.035775325 -3.588423e-02
## 2014-02-28 0.0037568278 0.0332207265 0.0595050128 0.045257415 4.450969e-02
## 2014-03-31 -0.0014818300 0.0380217013 -0.0046026443 0.013315433 8.261713e-03
## 2014-04-30 0.0081832760 0.0077728584 0.0165295253 -0.023184356 6.927275e-03
## 2014-05-30 0.0117217258 0.0290914129 0.0158283500 0.006204740 2.294147e-02
## 2014-06-30 -0.0005759654 0.0237332771 0.0091656318 0.037719284 2.043487e-02
## 2014-07-31 -0.0025114722 0.0135560106 -0.0263800066 -0.052009282 -1.352864e-02
## 2014-08-29 0.0114304934 0.0279045754 0.0018005750 0.043657571 3.870436e-02
## 2014-09-30 -0.0061675106 -0.0808566819 -0.0395985696 -0.061260302 -1.389228e-02
## 2014-10-31 0.0105842869 0.0140964517 -0.0026548907 0.068874895 2.327806e-02
## 2014-11-28 0.0065488810 -0.0155415250 0.0006255479 0.004773331 2.710104e-02
## 2014-12-31 0.0014753626 -0.0404418786 -0.0407468068 0.025295911 -2.539750e-03
## 2015-01-30 0.0203149161 -0.0068956759 0.0062264224 -0.054627821 -3.007699e-02
## 2015-02-27 -0.0089882764 0.0431359589 0.0614506173 0.056914489 5.468164e-02
## 2015-03-31 0.0037405978 -0.0150862520 -0.0143887845 0.010156612 -1.582993e-02
## 2015-04-30 -0.0032335572 0.0662813058 0.0358165822 -0.018417818 9.785778e-03
## 2015-05-29 -0.0043837380 -0.0419109062 0.0019527235 0.007509875 1.277431e-02
## 2015-06-30 -0.0108252759 -0.0297467280 -0.0316787473 0.004171446 -2.052136e-02
## 2015-07-31 0.0085844848 -0.0651783838 0.0201142517 -0.027375211 2.233797e-02
## 2015-08-31 -0.0033631079 -0.0925118170 -0.0771522510 -0.047268414 -6.288677e-02
## 2015-09-30 0.0080814642 -0.0318251976 -0.0451947203 -0.038464709 -2.584723e-02
## 2015-10-30 0.0006852079 0.0618084371 0.0640255524 0.063589548 8.163497e-02
## 2015-11-30 -0.0038985160 -0.0255606045 -0.0075556366 0.024415109 3.648790e-03
## 2015-12-31 -0.0019187171 -0.0389471757 -0.0235951109 -0.052156593 -1.743382e-02
## 2016-01-29 0.0123294778 -0.0516366086 -0.0567579081 -0.060307018 -5.106866e-02
## 2016-02-29 0.0088318423 -0.0082114548 -0.0339139044 0.020605203 -8.258442e-04
## 2016-03-31 0.0087089570 0.1218788430 0.0637457796 0.089910399 6.509987e-02
## 2016-04-29 0.0025462862 0.0040792165 0.0219750146 0.021043930 3.933564e-03
## 2016-05-31 0.0001354860 -0.0376285129 -0.0008559621 0.004397565 1.686846e-02
## 2016-06-30 0.0191668414 0.0445826057 -0.0244915362 0.008291952 3.469716e-03
## 2016-07-29 0.0054296126 0.0524420240 0.0390002796 0.049348449 3.582182e-02
## 2016-08-31 -0.0021565147 0.0087987244 0.0053269447 0.011261039 1.196817e-03
## 2016-09-30 0.0005164766 0.0248726884 0.0132789939 0.008614667 5.821668e-05
## 2016-10-31 -0.0082052273 -0.0083119468 -0.0224037499 -0.038135003 -1.748921e-02
## 2016-11-30 -0.0259897812 -0.0451620157 -0.0179744835 0.125246573 3.617638e-02
## 2016-12-30 0.0025379830 -0.0025300101 0.0267029452 0.031492108 2.006885e-02
## 2017-01-31 0.0021261608 0.0644311551 0.0323818767 -0.012144152 1.773646e-02
## 2017-02-28 0.0064380178 0.0172581973 0.0118364711 0.013428807 3.853912e-02
## 2017-03-31 -0.0005533897 0.0361890959 0.0318056345 -0.006533204 1.249380e-03
## 2017-04-28 0.0090292551 0.0168662836 0.0239522905 0.005107774 9.877380e-03
## 2017-05-31 0.0068473527 0.0280596567 0.0348101850 -0.022862689 1.401415e-02
## 2017-06-30 -0.0001823579 0.0092240905 0.0029559265 0.029151813 6.354546e-03
## 2017-07-31 0.0033344693 0.0565944338 0.0261877980 0.007481738 2.034593e-02
## 2017-08-31 0.0093693290 0.0232435724 -0.0004482935 -0.027565043 2.913239e-03
## 2017-09-29 -0.0057320847 -0.0004458973 0.0233428450 0.082322140 1.994901e-02
## 2017-10-31 0.0009778383 0.0322781792 0.0166535960 0.005915857 2.329095e-02
## 2017-11-30 -0.0014843111 -0.0038966623 0.0068701057 0.036913066 3.010806e-02
## 2017-12-29 0.0047404541 0.0369252960 0.0133982702 -0.003730915 1.205493e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398382e-05 0.0001042076 4.177867e-05 -7.812271e-05 -9.034227e-06
## EEM 1.042076e-04 0.0017547073 1.039014e-03 6.437702e-04 6.795403e-04
## EFA 4.177867e-05 0.0010390142 1.064235e-03 6.490294e-04 6.975387e-04
## IJS -7.812271e-05 0.0006437702 6.490294e-04 1.565452e-03 8.290252e-04
## SPY -9.034227e-06 0.0006795403 6.975387e-04 8.290252e-04 7.408274e-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.02347486
# 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.0003873891 0.009257133 0.005815625 0.005684469 0.002330246
rowSums(component_contribution)
## [1] 0.02347486
# 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.0062316529 -0.0029353521 0.0366061192 0.052132991 4.992312e-02
## 2013-02-28 0.0058917296 -0.0231054516 -0.0129692952 0.016175459 1.267823e-02
## 2013-03-28 0.0009845152 -0.0102350096 0.0129692952 0.040258405 3.726797e-02
## 2013-04-30 0.0096391558 0.0120846767 0.0489676360 0.001222365 1.903024e-02
## 2013-05-31 -0.0202140100 -0.0494837332 -0.0306554241 0.041976187 2.333549e-02
## 2013-06-28 -0.0157777762 -0.0547280916 -0.0271443808 -0.001402752 -1.343469e-02
## 2013-07-31 0.0026868636 0.0131596377 0.0518601691 0.063541713 5.038611e-02
## 2013-08-30 -0.0082978160 -0.0257053613 -0.0197462012 -0.034743923 -3.045137e-02
## 2013-09-30 0.0111439877 0.0695883585 0.0753384984 0.063873480 3.115588e-02
## 2013-10-31 0.0082924081 0.0408617658 0.0320816361 0.034234119 4.526679e-02
## 2013-11-29 -0.0025097532 -0.0025944831 0.0054497805 0.041661431 2.920682e-02
## 2013-12-31 -0.0055834678 -0.0040741338 0.0215278201 0.012892044 2.559607e-02
## 2014-01-31 0.0152918969 -0.0903227476 -0.0534132291 -0.035775325 -3.588423e-02
## 2014-02-28 0.0037568278 0.0332207265 0.0595050128 0.045257415 4.450969e-02
## 2014-03-31 -0.0014818300 0.0380217013 -0.0046026443 0.013315433 8.261713e-03
## 2014-04-30 0.0081832760 0.0077728584 0.0165295253 -0.023184356 6.927275e-03
## 2014-05-30 0.0117217258 0.0290914129 0.0158283500 0.006204740 2.294147e-02
## 2014-06-30 -0.0005759654 0.0237332771 0.0091656318 0.037719284 2.043487e-02
## 2014-07-31 -0.0025114722 0.0135560106 -0.0263800066 -0.052009282 -1.352864e-02
## 2014-08-29 0.0114304934 0.0279045754 0.0018005750 0.043657571 3.870436e-02
## 2014-09-30 -0.0061675106 -0.0808566819 -0.0395985696 -0.061260302 -1.389228e-02
## 2014-10-31 0.0105842869 0.0140964517 -0.0026548907 0.068874895 2.327806e-02
## 2014-11-28 0.0065488810 -0.0155415250 0.0006255479 0.004773331 2.710104e-02
## 2014-12-31 0.0014753626 -0.0404418786 -0.0407468068 0.025295911 -2.539750e-03
## 2015-01-30 0.0203149161 -0.0068956759 0.0062264224 -0.054627821 -3.007699e-02
## 2015-02-27 -0.0089882764 0.0431359589 0.0614506173 0.056914489 5.468164e-02
## 2015-03-31 0.0037405978 -0.0150862520 -0.0143887845 0.010156612 -1.582993e-02
## 2015-04-30 -0.0032335572 0.0662813058 0.0358165822 -0.018417818 9.785778e-03
## 2015-05-29 -0.0043837380 -0.0419109062 0.0019527235 0.007509875 1.277431e-02
## 2015-06-30 -0.0108252759 -0.0297467280 -0.0316787473 0.004171446 -2.052136e-02
## 2015-07-31 0.0085844848 -0.0651783838 0.0201142517 -0.027375211 2.233797e-02
## 2015-08-31 -0.0033631079 -0.0925118170 -0.0771522510 -0.047268414 -6.288677e-02
## 2015-09-30 0.0080814642 -0.0318251976 -0.0451947203 -0.038464709 -2.584723e-02
## 2015-10-30 0.0006852079 0.0618084371 0.0640255524 0.063589548 8.163497e-02
## 2015-11-30 -0.0038985160 -0.0255606045 -0.0075556366 0.024415109 3.648790e-03
## 2015-12-31 -0.0019187171 -0.0389471757 -0.0235951109 -0.052156593 -1.743382e-02
## 2016-01-29 0.0123294778 -0.0516366086 -0.0567579081 -0.060307018 -5.106866e-02
## 2016-02-29 0.0088318423 -0.0082114548 -0.0339139044 0.020605203 -8.258442e-04
## 2016-03-31 0.0087089570 0.1218788430 0.0637457796 0.089910399 6.509987e-02
## 2016-04-29 0.0025462862 0.0040792165 0.0219750146 0.021043930 3.933564e-03
## 2016-05-31 0.0001354860 -0.0376285129 -0.0008559621 0.004397565 1.686846e-02
## 2016-06-30 0.0191668414 0.0445826057 -0.0244915362 0.008291952 3.469716e-03
## 2016-07-29 0.0054296126 0.0524420240 0.0390002796 0.049348449 3.582182e-02
## 2016-08-31 -0.0021565147 0.0087987244 0.0053269447 0.011261039 1.196817e-03
## 2016-09-30 0.0005164766 0.0248726884 0.0132789939 0.008614667 5.821668e-05
## 2016-10-31 -0.0082052273 -0.0083119468 -0.0224037499 -0.038135003 -1.748921e-02
## 2016-11-30 -0.0259897812 -0.0451620157 -0.0179744835 0.125246573 3.617638e-02
## 2016-12-30 0.0025379830 -0.0025300101 0.0267029452 0.031492108 2.006885e-02
## 2017-01-31 0.0021261608 0.0644311551 0.0323818767 -0.012144152 1.773646e-02
## 2017-02-28 0.0064380178 0.0172581973 0.0118364711 0.013428807 3.853912e-02
## 2017-03-31 -0.0005533897 0.0361890959 0.0318056345 -0.006533204 1.249380e-03
## 2017-04-28 0.0090292551 0.0168662836 0.0239522905 0.005107774 9.877380e-03
## 2017-05-31 0.0068473527 0.0280596567 0.0348101850 -0.022862689 1.401415e-02
## 2017-06-30 -0.0001823579 0.0092240905 0.0029559265 0.029151813 6.354546e-03
## 2017-07-31 0.0033344693 0.0565944338 0.0261877980 0.007481738 2.034593e-02
## 2017-08-31 0.0093693290 0.0232435724 -0.0004482935 -0.027565043 2.913239e-03
## 2017-09-29 -0.0057320847 -0.0004458973 0.0233428450 0.082322140 1.994901e-02
## 2017-10-31 0.0009778383 0.0322781792 0.0166535960 0.005915857 2.329095e-02
## 2017-11-30 -0.0014843111 -0.0038966623 0.0068701057 0.036913066 3.010806e-02
## 2017-12-29 0.0047404541 0.0369252960 0.0133982702 -0.003730915 1.205493e-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)