# 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.0062309943 -0.0029354067 0.0366063083 0.052133230 0.0499228583
## 2013-02-28 0.0058906779 -0.0231053278 -0.0129694856 0.016175330 0.0126783416
## 2013-03-28 0.0009851147 -0.0102348468 0.0129694856 0.040258438 0.0372676809
## 2013-04-30 0.0096385983 0.0120845151 0.0489676863 0.001222828 0.0190305206
## 2013-05-31 -0.0202135741 -0.0494833386 -0.0306555798 0.041976121 0.0233351310
## 2013-06-28 -0.0157785278 -0.0547283615 -0.0271445021 -0.001403078 -0.0134344452
## 2013-07-31 0.0026873432 0.0131598854 0.0518603128 0.063541000 0.0503859006
## 2013-08-30 -0.0082974008 -0.0257057134 -0.0197462328 -0.034742979 -0.0304508534
## 2013-09-30 0.0111439869 0.0695888036 0.0753384385 0.063873196 0.0311553537
## 2013-10-31 0.0082917064 0.0408613454 0.0320813924 0.034234178 0.0452668084
## 2013-11-29 -0.0025098636 -0.0025941174 0.0054499717 0.041661153 0.0292067977
## 2013-12-31 -0.0055825748 -0.0040744055 0.0215279720 0.012892199 0.0255960625
## 2014-01-31 0.0152916200 -0.0903227587 -0.0534134356 -0.035775253 -0.0358843458
## 2014-02-28 0.0037570770 0.0332208705 0.0595052550 0.045257227 0.0445101743
## 2014-03-31 -0.0014820721 0.0380213399 -0.0046025703 0.013315428 0.0082613075
## 2014-04-30 0.0081834924 0.0077727928 0.0165294454 -0.023184268 0.0069278699
## 2014-05-30 0.0117216376 0.0290912248 0.0158283738 0.006205176 0.0229409806
## 2014-06-30 -0.0005760247 0.0237341444 0.0091652337 0.037719133 0.0204350278
## 2014-07-31 -0.0025113852 0.0135553083 -0.0263797134 -0.052009653 -0.0135291462
## 2014-08-29 0.0114307509 0.0279048138 0.0018006137 0.043657871 0.0387048161
## 2014-09-30 -0.0061681497 -0.0808568108 -0.0395984295 -0.061260142 -0.0138924533
## 2014-10-31 0.0105844534 0.0140962981 -0.0026550894 0.068874625 0.0232780219
## 2014-11-28 0.0065490390 -0.0155411118 0.0006254908 0.004773799 0.0271016587
## 2014-12-31 0.0014748415 -0.0404418976 -0.0407469309 0.025295511 -0.0025399608
## 2015-01-30 0.0203156041 -0.0068959580 0.0062266237 -0.054627778 -0.0300769986
## 2015-02-27 -0.0089882130 0.0431362568 0.0614503726 0.056914094 0.0546819356
## 2015-03-31 0.0037399809 -0.0150863121 -0.0143884844 0.010156867 -0.0158307217
## 2015-04-30 -0.0032325634 0.0662812785 0.0358164884 -0.018417902 0.0097862245
## 2015-05-29 -0.0043838990 -0.0419109667 0.0019527073 0.007510182 0.0127741004
## 2015-06-30 -0.0108256206 -0.0297465050 -0.0316788545 0.004171135 -0.0205210316
## 2015-07-31 0.0085847613 -0.0651780216 0.0201145423 -0.027375267 0.0223376979
## 2015-08-31 -0.0033633122 -0.0925126871 -0.0771526897 -0.047268384 -0.0628867655
## 2015-09-30 0.0080815125 -0.0318248046 -0.0451947266 -0.038464766 -0.0258472483
## 2015-10-30 0.0006852475 0.0618084193 0.0640258586 0.063589640 0.0816354190
## 2015-11-30 -0.0038980512 -0.0255607350 -0.0075557480 0.024415339 0.0036480031
## 2015-12-31 -0.0019190431 -0.0389470134 -0.0235951814 -0.052156953 -0.0174331372
## 2016-01-29 0.0123295895 -0.0516365365 -0.0567577573 -0.060306846 -0.0510686106
## 2016-02-29 0.0088316868 -0.0082118918 -0.0339139784 0.020605082 -0.0008262559
## 2016-03-31 0.0087085832 0.1218792904 0.0637456515 0.089910528 0.0650999526
## 2016-04-29 0.0025465379 0.0040791350 0.0219752111 0.021044224 0.0039335261
## 2016-05-31 0.0001349600 -0.0376284039 -0.0008560354 0.004396692 0.0168684039
## 2016-06-30 0.0191668734 0.0445821933 -0.0244917141 0.008292472 0.0034700966
## 2016-07-29 0.0054301477 0.0524423468 0.0390005619 0.049348296 0.0358216562
## 2016-08-31 -0.0021570815 0.0087984579 0.0053267454 0.011261333 0.0011969104
## 2016-09-30 0.0005166879 0.0248730851 0.0132792696 0.008614421 0.0000578254
## 2016-10-31 -0.0082051842 -0.0083124160 -0.0224037937 -0.038134560 -0.0174888362
## 2016-11-30 -0.0259901760 -0.0451621543 -0.0179745848 0.125246169 0.0361761460
## 2016-12-30 0.0025387486 -0.0025298301 0.0267029830 0.031491926 0.0200690187
## 2017-01-31 0.0021252537 0.0644315333 0.0323819215 -0.012143673 0.0177361290
## 2017-02-28 0.0064385611 0.0172579864 0.0118362129 0.013428443 0.0385397093
## 2017-03-31 -0.0005532387 0.0361886662 0.0318058176 -0.006532855 0.0012488602
## 2017-04-28 0.0090295797 0.0168665435 0.0239522957 0.005107505 0.0098774104
## 2017-05-31 0.0068473083 0.0280601489 0.0348102992 -0.022862592 0.0140141017
## 2017-06-30 -0.0001826743 0.0092234465 0.0029558137 0.029151912 0.0063545709
## 2017-07-31 0.0033343627 0.0565945437 0.0261879023 0.007481598 0.0203460568
## 2017-08-31 0.0093693956 0.0232438564 -0.0004483498 -0.027564630 0.0029135236
## 2017-09-29 -0.0057328074 -0.0004461343 0.0233428119 0.082321661 0.0199487745
## 2017-10-31 0.0009783259 0.0322784737 0.0166534546 0.005915754 0.0232908134
## 2017-11-30 -0.0014834139 -0.0038967294 0.0068703747 0.036913360 0.0301083516
## 2017-12-29 0.0047396886 0.0369251345 0.0133981485 -0.003731177 0.0120546779
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398430e-05 0.0001042103 4.178224e-05 -7.812176e-05 -9.031837e-06
## EEM 1.042103e-04 0.0017547131 1.039019e-03 6.437740e-04 6.795446e-04
## EFA 4.178224e-05 0.0010390188 1.064238e-03 6.490284e-04 6.975400e-04
## IJS -7.812176e-05 0.0006437740 6.490284e-04 1.565444e-03 8.290218e-04
## SPY -9.031837e-06 0.0006795446 6.975400e-04 8.290218e-04 7.408286e-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.000387409 0.009257161 0.005815637 0.005684451 0.002330248
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.0062309943 -0.0029354067 0.0366063083 0.052133230 0.0499228583
## 2013-02-28 0.0058906779 -0.0231053278 -0.0129694856 0.016175330 0.0126783416
## 2013-03-28 0.0009851147 -0.0102348468 0.0129694856 0.040258438 0.0372676809
## 2013-04-30 0.0096385983 0.0120845151 0.0489676863 0.001222828 0.0190305206
## 2013-05-31 -0.0202135741 -0.0494833386 -0.0306555798 0.041976121 0.0233351310
## 2013-06-28 -0.0157785278 -0.0547283615 -0.0271445021 -0.001403078 -0.0134344452
## 2013-07-31 0.0026873432 0.0131598854 0.0518603128 0.063541000 0.0503859006
## 2013-08-30 -0.0082974008 -0.0257057134 -0.0197462328 -0.034742979 -0.0304508534
## 2013-09-30 0.0111439869 0.0695888036 0.0753384385 0.063873196 0.0311553537
## 2013-10-31 0.0082917064 0.0408613454 0.0320813924 0.034234178 0.0452668084
## 2013-11-29 -0.0025098636 -0.0025941174 0.0054499717 0.041661153 0.0292067977
## 2013-12-31 -0.0055825748 -0.0040744055 0.0215279720 0.012892199 0.0255960625
## 2014-01-31 0.0152916200 -0.0903227587 -0.0534134356 -0.035775253 -0.0358843458
## 2014-02-28 0.0037570770 0.0332208705 0.0595052550 0.045257227 0.0445101743
## 2014-03-31 -0.0014820721 0.0380213399 -0.0046025703 0.013315428 0.0082613075
## 2014-04-30 0.0081834924 0.0077727928 0.0165294454 -0.023184268 0.0069278699
## 2014-05-30 0.0117216376 0.0290912248 0.0158283738 0.006205176 0.0229409806
## 2014-06-30 -0.0005760247 0.0237341444 0.0091652337 0.037719133 0.0204350278
## 2014-07-31 -0.0025113852 0.0135553083 -0.0263797134 -0.052009653 -0.0135291462
## 2014-08-29 0.0114307509 0.0279048138 0.0018006137 0.043657871 0.0387048161
## 2014-09-30 -0.0061681497 -0.0808568108 -0.0395984295 -0.061260142 -0.0138924533
## 2014-10-31 0.0105844534 0.0140962981 -0.0026550894 0.068874625 0.0232780219
## 2014-11-28 0.0065490390 -0.0155411118 0.0006254908 0.004773799 0.0271016587
## 2014-12-31 0.0014748415 -0.0404418976 -0.0407469309 0.025295511 -0.0025399608
## 2015-01-30 0.0203156041 -0.0068959580 0.0062266237 -0.054627778 -0.0300769986
## 2015-02-27 -0.0089882130 0.0431362568 0.0614503726 0.056914094 0.0546819356
## 2015-03-31 0.0037399809 -0.0150863121 -0.0143884844 0.010156867 -0.0158307217
## 2015-04-30 -0.0032325634 0.0662812785 0.0358164884 -0.018417902 0.0097862245
## 2015-05-29 -0.0043838990 -0.0419109667 0.0019527073 0.007510182 0.0127741004
## 2015-06-30 -0.0108256206 -0.0297465050 -0.0316788545 0.004171135 -0.0205210316
## 2015-07-31 0.0085847613 -0.0651780216 0.0201145423 -0.027375267 0.0223376979
## 2015-08-31 -0.0033633122 -0.0925126871 -0.0771526897 -0.047268384 -0.0628867655
## 2015-09-30 0.0080815125 -0.0318248046 -0.0451947266 -0.038464766 -0.0258472483
## 2015-10-30 0.0006852475 0.0618084193 0.0640258586 0.063589640 0.0816354190
## 2015-11-30 -0.0038980512 -0.0255607350 -0.0075557480 0.024415339 0.0036480031
## 2015-12-31 -0.0019190431 -0.0389470134 -0.0235951814 -0.052156953 -0.0174331372
## 2016-01-29 0.0123295895 -0.0516365365 -0.0567577573 -0.060306846 -0.0510686106
## 2016-02-29 0.0088316868 -0.0082118918 -0.0339139784 0.020605082 -0.0008262559
## 2016-03-31 0.0087085832 0.1218792904 0.0637456515 0.089910528 0.0650999526
## 2016-04-29 0.0025465379 0.0040791350 0.0219752111 0.021044224 0.0039335261
## 2016-05-31 0.0001349600 -0.0376284039 -0.0008560354 0.004396692 0.0168684039
## 2016-06-30 0.0191668734 0.0445821933 -0.0244917141 0.008292472 0.0034700966
## 2016-07-29 0.0054301477 0.0524423468 0.0390005619 0.049348296 0.0358216562
## 2016-08-31 -0.0021570815 0.0087984579 0.0053267454 0.011261333 0.0011969104
## 2016-09-30 0.0005166879 0.0248730851 0.0132792696 0.008614421 0.0000578254
## 2016-10-31 -0.0082051842 -0.0083124160 -0.0224037937 -0.038134560 -0.0174888362
## 2016-11-30 -0.0259901760 -0.0451621543 -0.0179745848 0.125246169 0.0361761460
## 2016-12-30 0.0025387486 -0.0025298301 0.0267029830 0.031491926 0.0200690187
## 2017-01-31 0.0021252537 0.0644315333 0.0323819215 -0.012143673 0.0177361290
## 2017-02-28 0.0064385611 0.0172579864 0.0118362129 0.013428443 0.0385397093
## 2017-03-31 -0.0005532387 0.0361886662 0.0318058176 -0.006532855 0.0012488602
## 2017-04-28 0.0090295797 0.0168665435 0.0239522957 0.005107505 0.0098774104
## 2017-05-31 0.0068473083 0.0280601489 0.0348102992 -0.022862592 0.0140141017
## 2017-06-30 -0.0001826743 0.0092234465 0.0029558137 0.029151912 0.0063545709
## 2017-07-31 0.0033343627 0.0565945437 0.0261879023 0.007481598 0.0203460568
## 2017-08-31 0.0093693956 0.0232438564 -0.0004483498 -0.027564630 0.0029135236
## 2017-09-29 -0.0057328074 -0.0004461343 0.0233428119 0.082321661 0.0199487745
## 2017-10-31 0.0009783259 0.0322784737 0.0166534546 0.005915754 0.0232908134
## 2017-11-30 -0.0014834139 -0.0038967294 0.0068703747 0.036913360 0.0301083516
## 2017-12-29 0.0047396886 0.0369251345 0.0133981485 -0.003731177 0.0120546779
cal_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 %>% cal_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.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
plot_data <- asset_returns_wide_tbl %>%
cal_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")
plot_data <- asset_returns_wide_tbl %>%
cal_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_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
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)) +
theme(plot.title = element_text(hjust = 0.5)) +
theme_tq()
labs (title = "Percent Contribution to Portfolio Volatility and weight", y = "Percent", x = NULL)
## <ggplot2::labels> List of 3
## $ y : chr "Percent"
## $ x : NULL
## $ title: chr "Percent Contribution to Portfolio Volatility and weight"