# 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.0062310755 -0.0029353524 0.0366063075 0.052133749 4.992320e-02
## 2013-02-28 0.0058911741 -0.0231055673 -0.0129695710 0.016174668 1.267783e-02
## 2013-03-28 0.0009852591 -0.0102346689 0.0129695710 0.040258653 3.726810e-02
## 2013-04-30 0.0096390259 0.0120847883 0.0489677225 0.001222682 1.902988e-02
## 2013-05-31 -0.0202142584 -0.0494832470 -0.0306554215 0.041976135 2.333551e-02
## 2013-06-28 -0.0157781037 -0.0547287410 -0.0271445615 -0.001403003 -1.343447e-02
## 2013-07-31 0.0026874575 0.0131596369 0.0518603476 0.063541216 5.038602e-02
## 2013-08-30 -0.0082980847 -0.0257053596 -0.0197461995 -0.034743154 -3.045132e-02
## 2013-09-30 0.0111443367 0.0695886503 0.0753384920 0.063873584 3.115578e-02
## 2013-10-31 0.0082919009 0.0408612969 0.0320815538 0.034234064 4.526668e-02
## 2013-11-29 -0.0025093654 -0.0025942554 0.0054495429 0.041661124 2.920721e-02
## 2013-12-31 -0.0055831513 -0.0040742478 0.0215282903 0.012892297 2.559599e-02
## 2014-01-31 0.0152914271 -0.0903223719 -0.0534133799 -0.035775465 -3.588474e-02
## 2014-02-28 0.0037569207 0.0332202296 0.0595051622 0.045257498 4.451061e-02
## 2014-03-31 -0.0014817233 0.0380219391 -0.0046027981 0.013315035 8.261428e-03
## 2014-04-30 0.0081824926 0.0077726260 0.0165296002 -0.023184204 6.927077e-03
## 2014-05-30 0.0117220568 0.0290910789 0.0158281228 0.006205221 2.294123e-02
## 2014-06-30 -0.0005755446 0.0237341658 0.0091656325 0.037718731 2.043487e-02
## 2014-07-31 -0.0025123098 0.0135555715 -0.0263797036 -0.052009271 -1.352891e-02
## 2014-08-29 0.0114315357 0.0279044700 0.0018002702 0.043658092 3.870491e-02
## 2014-09-30 -0.0061674463 -0.0808569191 -0.0395984935 -0.061260743 -1.389251e-02
## 2014-10-31 0.0105837723 0.0140967943 -0.0026546525 0.068874975 2.327789e-02
## 2014-11-28 0.0065492659 -0.0155415250 0.0006251511 0.004773647 2.710135e-02
## 2014-12-31 0.0014752744 -0.0404417595 -0.0407468134 0.025295890 -2.539612e-03
## 2015-01-30 0.0203146985 -0.0068959149 0.0062265877 -0.054628016 -3.007726e-02
## 2015-02-27 -0.0089879255 0.0431359639 0.0614505401 0.056914632 5.468185e-02
## 2015-03-31 0.0037404838 -0.0150859040 -0.0143887072 0.010156304 -1.583014e-02
## 2015-04-30 -0.0032335966 0.0662809635 0.0358168090 -0.018417601 9.785906e-03
## 2015-05-29 -0.0043839472 -0.0419109109 0.0019524967 0.007509804 1.277446e-02
## 2015-06-30 -0.0108249851 -0.0297466142 -0.0316788252 0.004171383 -2.052144e-02
## 2015-07-31 0.0085847731 -0.0651781336 0.0201144823 -0.027375350 2.233786e-02
## 2015-08-31 -0.0033640153 -0.0925122045 -0.0771524861 -0.047268341 -6.288674e-02
## 2015-09-30 0.0080815874 -0.0318249895 -0.0451948966 -0.038464708 -2.584708e-02
## 2015-10-30 0.0006853688 0.0618080999 0.0640258921 0.063589484 8.163492e-02
## 2015-11-30 -0.0038982389 -0.0255603382 -0.0075557175 0.024415390 3.648391e-03
## 2015-12-31 -0.0019190911 -0.0389471047 -0.0235951944 -0.052156956 -1.743365e-02
## 2016-01-29 0.0123306487 -0.0516366048 -0.0567578247 -0.060307028 -5.106863e-02
## 2016-02-29 0.0088314709 -0.0082116050 -0.0339139044 0.020605483 -8.261495e-04
## 2016-03-31 0.0087081049 0.1218789852 0.0637457796 0.089910338 6.510026e-02
## 2016-04-29 0.0025468022 0.0040792827 0.0219751824 0.021044069 3.933324e-03
## 2016-05-31 0.0001350113 -0.0376285078 -0.0008562138 0.004397100 1.686870e-02
## 2016-06-30 0.0191668088 0.0445824016 -0.0244912802 0.008292486 3.469734e-03
## 2016-07-29 0.0054296826 0.0524420901 0.0390001903 0.049348103 3.582198e-02
## 2016-08-31 -0.0021559298 0.0087987244 0.0053266973 0.011261286 1.197184e-03
## 2016-09-30 0.0005156516 0.0248726884 0.0132790773 0.008614581 5.779714e-05
## 2016-10-31 -0.0082047357 -0.0083120078 -0.0224035026 -0.038134965 -1.748924e-02
## 2016-11-30 -0.0259901428 -0.0451617629 -0.0179746496 0.125246181 3.617613e-02
## 2016-12-30 0.0025382843 -0.0025304580 0.0267030275 0.031491920 2.006896e-02
## 2017-01-31 0.0021263055 0.0644317116 0.0323817146 -0.012143600 1.773656e-02
## 2017-02-28 0.0064377107 0.0172577789 0.0118364721 0.013428548 3.853916e-02
## 2017-03-31 -0.0005532638 0.0361891001 0.0318057133 -0.006533138 1.249290e-03
## 2017-04-28 0.0090299641 0.0168662855 0.0239523650 0.005108093 9.877076e-03
## 2017-05-31 0.0068468803 0.0280598776 0.0348099666 -0.022862819 1.401443e-02
## 2017-06-30 -0.0001824719 0.0092236580 0.0029561422 0.029152071 6.354540e-03
## 2017-07-31 0.0033342260 0.0565947574 0.0261877263 0.007481198 2.034599e-02
## 2017-08-31 0.0093691094 0.0232437716 -0.0004484333 -0.027564411 2.913352e-03
## 2017-09-29 -0.0057317982 -0.0004461961 0.0233429166 0.082321389 1.994913e-02
## 2017-10-31 0.0009776176 0.0322782788 0.0166537987 0.005916157 2.329050e-02
## 2017-11-30 -0.0014838399 -0.0038965654 0.0068698379 0.036913454 3.010817e-02
## 2017-12-29 0.0047402118 0.0369251992 0.0133983378 -0.003731086 1.205507e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398395e-05 0.0001042081 0.0000417814 -7.811944e-05 -9.032206e-06
## EEM 1.042081e-04 0.0017547073 0.0010390155 6.437744e-04 6.795426e-04
## EFA 4.178140e-05 0.0010390155 0.0010642378 6.490302e-04 6.975410e-04
## IJS -7.811944e-05 0.0006437744 0.0006490302 1.565449e-03 8.290271e-04
## SPY -9.032206e-06 0.0006795426 0.0006975410 8.290271e-04 7.408313e-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.0234749
# 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.0003874051 0.009257134 0.005815633 0.005684474 0.002330252
rowSums(component_contribution)
## [1] 0.0234749
# 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.0062310755 -0.0029353524 0.0366063075 0.052133749 4.992320e-02
## 2013-02-28 0.0058911741 -0.0231055673 -0.0129695710 0.016174668 1.267783e-02
## 2013-03-28 0.0009852591 -0.0102346689 0.0129695710 0.040258653 3.726810e-02
## 2013-04-30 0.0096390259 0.0120847883 0.0489677225 0.001222682 1.902988e-02
## 2013-05-31 -0.0202142584 -0.0494832470 -0.0306554215 0.041976135 2.333551e-02
## 2013-06-28 -0.0157781037 -0.0547287410 -0.0271445615 -0.001403003 -1.343447e-02
## 2013-07-31 0.0026874575 0.0131596369 0.0518603476 0.063541216 5.038602e-02
## 2013-08-30 -0.0082980847 -0.0257053596 -0.0197461995 -0.034743154 -3.045132e-02
## 2013-09-30 0.0111443367 0.0695886503 0.0753384920 0.063873584 3.115578e-02
## 2013-10-31 0.0082919009 0.0408612969 0.0320815538 0.034234064 4.526668e-02
## 2013-11-29 -0.0025093654 -0.0025942554 0.0054495429 0.041661124 2.920721e-02
## 2013-12-31 -0.0055831513 -0.0040742478 0.0215282903 0.012892297 2.559599e-02
## 2014-01-31 0.0152914271 -0.0903223719 -0.0534133799 -0.035775465 -3.588474e-02
## 2014-02-28 0.0037569207 0.0332202296 0.0595051622 0.045257498 4.451061e-02
## 2014-03-31 -0.0014817233 0.0380219391 -0.0046027981 0.013315035 8.261428e-03
## 2014-04-30 0.0081824926 0.0077726260 0.0165296002 -0.023184204 6.927077e-03
## 2014-05-30 0.0117220568 0.0290910789 0.0158281228 0.006205221 2.294123e-02
## 2014-06-30 -0.0005755446 0.0237341658 0.0091656325 0.037718731 2.043487e-02
## 2014-07-31 -0.0025123098 0.0135555715 -0.0263797036 -0.052009271 -1.352891e-02
## 2014-08-29 0.0114315357 0.0279044700 0.0018002702 0.043658092 3.870491e-02
## 2014-09-30 -0.0061674463 -0.0808569191 -0.0395984935 -0.061260743 -1.389251e-02
## 2014-10-31 0.0105837723 0.0140967943 -0.0026546525 0.068874975 2.327789e-02
## 2014-11-28 0.0065492659 -0.0155415250 0.0006251511 0.004773647 2.710135e-02
## 2014-12-31 0.0014752744 -0.0404417595 -0.0407468134 0.025295890 -2.539612e-03
## 2015-01-30 0.0203146985 -0.0068959149 0.0062265877 -0.054628016 -3.007726e-02
## 2015-02-27 -0.0089879255 0.0431359639 0.0614505401 0.056914632 5.468185e-02
## 2015-03-31 0.0037404838 -0.0150859040 -0.0143887072 0.010156304 -1.583014e-02
## 2015-04-30 -0.0032335966 0.0662809635 0.0358168090 -0.018417601 9.785906e-03
## 2015-05-29 -0.0043839472 -0.0419109109 0.0019524967 0.007509804 1.277446e-02
## 2015-06-30 -0.0108249851 -0.0297466142 -0.0316788252 0.004171383 -2.052144e-02
## 2015-07-31 0.0085847731 -0.0651781336 0.0201144823 -0.027375350 2.233786e-02
## 2015-08-31 -0.0033640153 -0.0925122045 -0.0771524861 -0.047268341 -6.288674e-02
## 2015-09-30 0.0080815874 -0.0318249895 -0.0451948966 -0.038464708 -2.584708e-02
## 2015-10-30 0.0006853688 0.0618080999 0.0640258921 0.063589484 8.163492e-02
## 2015-11-30 -0.0038982389 -0.0255603382 -0.0075557175 0.024415390 3.648391e-03
## 2015-12-31 -0.0019190911 -0.0389471047 -0.0235951944 -0.052156956 -1.743365e-02
## 2016-01-29 0.0123306487 -0.0516366048 -0.0567578247 -0.060307028 -5.106863e-02
## 2016-02-29 0.0088314709 -0.0082116050 -0.0339139044 0.020605483 -8.261495e-04
## 2016-03-31 0.0087081049 0.1218789852 0.0637457796 0.089910338 6.510026e-02
## 2016-04-29 0.0025468022 0.0040792827 0.0219751824 0.021044069 3.933324e-03
## 2016-05-31 0.0001350113 -0.0376285078 -0.0008562138 0.004397100 1.686870e-02
## 2016-06-30 0.0191668088 0.0445824016 -0.0244912802 0.008292486 3.469734e-03
## 2016-07-29 0.0054296826 0.0524420901 0.0390001903 0.049348103 3.582198e-02
## 2016-08-31 -0.0021559298 0.0087987244 0.0053266973 0.011261286 1.197184e-03
## 2016-09-30 0.0005156516 0.0248726884 0.0132790773 0.008614581 5.779714e-05
## 2016-10-31 -0.0082047357 -0.0083120078 -0.0224035026 -0.038134965 -1.748924e-02
## 2016-11-30 -0.0259901428 -0.0451617629 -0.0179746496 0.125246181 3.617613e-02
## 2016-12-30 0.0025382843 -0.0025304580 0.0267030275 0.031491920 2.006896e-02
## 2017-01-31 0.0021263055 0.0644317116 0.0323817146 -0.012143600 1.773656e-02
## 2017-02-28 0.0064377107 0.0172577789 0.0118364721 0.013428548 3.853916e-02
## 2017-03-31 -0.0005532638 0.0361891001 0.0318057133 -0.006533138 1.249290e-03
## 2017-04-28 0.0090299641 0.0168662855 0.0239523650 0.005108093 9.877076e-03
## 2017-05-31 0.0068468803 0.0280598776 0.0348099666 -0.022862819 1.401443e-02
## 2017-06-30 -0.0001824719 0.0092236580 0.0029561422 0.029152071 6.354540e-03
## 2017-07-31 0.0033342260 0.0565947574 0.0261877263 0.007481198 2.034599e-02
## 2017-08-31 0.0093691094 0.0232437716 -0.0004484333 -0.027564411 2.913352e-03
## 2017-09-29 -0.0057317982 -0.0004461961 0.0233429166 0.082321389 1.994913e-02
## 2017-10-31 0.0009776176 0.0322782788 0.0166537987 0.005916157 2.329050e-02
## 2017-11-30 -0.0014838399 -0.0038965654 0.0068698379 0.036913454 3.010817e-02
## 2017-12-29 0.0047402118 0.0369251992 0.0133983378 -0.003731086 1.205507e-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()
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
Colum Chart of Compound 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(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
labs(title = "Percent Contribution to Portfolio Votality")
Colum Chart of Compound 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") %>%
#Add weights
add_column(weight = c(.25, .25, .2, .2, .1 )) %>%
# Transform to log
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 Votality and Weight")