# 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.0062309206 -0.0029358265 0.0366063129 0.052133712 4.992290e-02
## 2013-02-28 0.0058910204 -0.0231048911 -0.0129695690 0.016175110 1.267882e-02
## 2013-03-28 0.0009849864 -0.0102349968 0.0129695690 0.040258323 3.726774e-02
## 2013-04-30 0.0096400601 0.0120845446 0.0489678889 0.001222724 1.903030e-02
## 2013-05-31 -0.0202145587 -0.0494829869 -0.0306555845 0.041975886 2.333491e-02
## 2013-06-28 -0.0157787032 -0.0547287165 -0.0271448062 -0.001403097 -1.343399e-02
## 2013-07-31 0.0026877241 0.0131598630 0.0518604004 0.063541408 5.038583e-02
## 2013-08-30 -0.0082977289 -0.0257057724 -0.0197463333 -0.034743173 -3.045141e-02
## 2013-09-30 0.0111438856 0.0695887852 0.0753386109 0.063873382 3.115605e-02
## 2013-10-31 0.0082923923 0.0408613628 0.0320814930 0.034234436 4.526635e-02
## 2013-11-29 -0.0025099115 -0.0025937254 0.0054497641 0.041661036 2.920731e-02
## 2013-12-31 -0.0055838102 -0.0040747080 0.0215280092 0.012892025 2.559608e-02
## 2014-01-31 0.0152920400 -0.0903224834 -0.0534133570 -0.035775599 -3.588442e-02
## 2014-02-28 0.0037570079 0.0332204840 0.0595050922 0.045257537 4.451017e-02
## 2014-03-31 -0.0014816378 0.0380217008 -0.0046024936 0.013315391 8.261362e-03
## 2014-04-30 0.0081832228 0.0077727927 0.0165294223 -0.023184269 6.927376e-03
## 2014-05-30 0.0117219820 0.0290910768 0.0158282891 0.006205136 2.294112e-02
## 2014-06-30 -0.0005759637 0.0237342830 0.0091653405 0.037718811 2.043492e-02
## 2014-07-31 -0.0025122243 0.0135553507 -0.0263795816 -0.052009519 -1.352889e-02
## 2014-08-29 0.0114303097 0.0279046076 0.0018004766 0.043658109 3.870482e-02
## 2014-09-30 -0.0061670037 -0.0808565450 -0.0395986182 -0.061260765 -1.389267e-02
## 2014-10-31 0.0105843902 0.0140963385 -0.0026548519 0.068874588 2.327814e-02
## 2014-11-28 0.0065492287 -0.0155414430 0.0006253295 0.004774153 2.710152e-02
## 2014-12-31 0.0014744425 -0.0404421228 -0.0407466475 0.025295610 -2.539727e-03
## 2015-01-30 0.0203158246 -0.0068958203 0.0062264509 -0.054627476 -3.007741e-02
## 2015-02-27 -0.0089884694 0.0431363883 0.0614507473 0.056914207 5.468211e-02
## 2015-03-31 0.0037405832 -0.0150863009 -0.0143889533 0.010156728 -1.583035e-02
## 2015-04-30 -0.0032327384 0.0662812399 0.0358165040 -0.018417902 9.785737e-03
## 2015-05-29 -0.0043836790 -0.0419109266 0.0019529451 0.007509903 1.277456e-02
## 2015-06-30 -0.0108260223 -0.0297466454 -0.0316790783 0.004171442 -2.052159e-02
## 2015-07-31 0.0085851645 -0.0651779537 0.0201147332 -0.027375568 2.233791e-02
## 2015-08-31 -0.0033642859 -0.0925122438 -0.0771525772 -0.047268436 -6.288639e-02
## 2015-09-30 0.0080807775 -0.0318252405 -0.0451947567 -0.038464839 -2.584740e-02
## 2015-10-30 0.0006860849 0.0618082789 0.0640259007 0.063589911 8.163494e-02
## 2015-11-30 -0.0038980275 -0.0255602647 -0.0075559791 0.024415374 3.648707e-03
## 2015-12-31 -0.0019189348 -0.0389472043 -0.0235949659 -0.052157170 -1.743387e-02
## 2016-01-29 0.0123300367 -0.0516369267 -0.0567577867 -0.060307052 -5.106845e-02
## 2016-02-29 0.0088313869 -0.0082114616 -0.0339140163 0.020605420 -8.262703e-04
## 2016-03-31 0.0087086672 0.1218791055 0.0637457564 0.089910402 6.510017e-02
## 2016-04-29 0.0025465417 0.0040792410 0.0219751893 0.021044276 3.933019e-03
## 2016-05-31 0.0001350846 -0.0376285322 -0.0008561715 0.004397090 1.686894e-02
## 2016-06-30 0.0191669356 0.0445825600 -0.0244917026 0.008292241 3.469487e-03
## 2016-07-29 0.0054297915 0.0524420273 0.0390003306 0.049348341 3.582223e-02
## 2016-08-31 -0.0021563198 0.0087984705 0.0053267601 0.011261091 1.196845e-03
## 2016-09-30 0.0005160165 0.0248729807 0.0132793321 0.008614563 5.802803e-05
## 2016-10-31 -0.0082054357 -0.0083120758 -0.0224039211 -0.038134656 -1.748905e-02
## 2016-11-30 -0.0259894309 -0.0451618779 -0.0179742290 0.125246412 3.617607e-02
## 2016-12-30 0.0025375592 -0.0025299829 0.0267029003 0.031491994 2.006900e-02
## 2017-01-31 0.0021267576 0.0644313476 0.0323818267 -0.012144123 1.773609e-02
## 2017-02-28 0.0064377273 0.0172577141 0.0118364314 0.013428797 3.853954e-02
## 2017-03-31 -0.0005531445 0.0361889775 0.0318055923 -0.006533116 1.249445e-03
## 2017-04-28 0.0090292649 0.0168664096 0.0239523312 0.005107801 9.877097e-03
## 2017-05-31 0.0068473207 0.0280601262 0.0348103668 -0.022862453 1.401432e-02
## 2017-06-30 -0.0001827520 0.0092236596 0.0029557775 0.029151707 6.354730e-03
## 2017-07-31 0.0033343223 0.0565944241 0.0261878079 0.007481415 2.034556e-02
## 2017-08-31 0.0093693937 0.0232437164 -0.0004482150 -0.027564707 2.913523e-03
## 2017-09-29 -0.0057322074 -0.0004460551 0.0233426703 0.082321590 1.994920e-02
## 2017-10-31 0.0009779709 0.0322783985 0.0166536532 0.005916056 2.329059e-02
## 2017-11-30 -0.0014836909 -0.0038970188 0.0068700315 0.036913212 3.010817e-02
## 2017-12-29 0.0047402021 0.0369254231 0.0133982085 -0.003731057 1.205473e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398560e-05 0.0001042119 4.178576e-05 -7.812075e-05 -9.030161e-06
## EEM 1.042119e-04 0.0017547095 1.039017e-03 6.437760e-04 6.795420e-04
## EFA 4.178576e-05 0.0010390172 1.064240e-03 6.490316e-04 6.975414e-04
## IJS -7.812075e-05 0.0006437760 6.490316e-04 1.565450e-03 8.290271e-04
## SPY -9.030161e-06 0.0006795420 6.975414e-04 8.290271e-04 7.408295e-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.0003874277 0.009257142 0.005815643 0.005684471 0.00233025
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.0062309206 -0.0029358265 0.0366063129 0.052133712 4.992290e-02
## 2013-02-28 0.0058910204 -0.0231048911 -0.0129695690 0.016175110 1.267882e-02
## 2013-03-28 0.0009849864 -0.0102349968 0.0129695690 0.040258323 3.726774e-02
## 2013-04-30 0.0096400601 0.0120845446 0.0489678889 0.001222724 1.903030e-02
## 2013-05-31 -0.0202145587 -0.0494829869 -0.0306555845 0.041975886 2.333491e-02
## 2013-06-28 -0.0157787032 -0.0547287165 -0.0271448062 -0.001403097 -1.343399e-02
## 2013-07-31 0.0026877241 0.0131598630 0.0518604004 0.063541408 5.038583e-02
## 2013-08-30 -0.0082977289 -0.0257057724 -0.0197463333 -0.034743173 -3.045141e-02
## 2013-09-30 0.0111438856 0.0695887852 0.0753386109 0.063873382 3.115605e-02
## 2013-10-31 0.0082923923 0.0408613628 0.0320814930 0.034234436 4.526635e-02
## 2013-11-29 -0.0025099115 -0.0025937254 0.0054497641 0.041661036 2.920731e-02
## 2013-12-31 -0.0055838102 -0.0040747080 0.0215280092 0.012892025 2.559608e-02
## 2014-01-31 0.0152920400 -0.0903224834 -0.0534133570 -0.035775599 -3.588442e-02
## 2014-02-28 0.0037570079 0.0332204840 0.0595050922 0.045257537 4.451017e-02
## 2014-03-31 -0.0014816378 0.0380217008 -0.0046024936 0.013315391 8.261362e-03
## 2014-04-30 0.0081832228 0.0077727927 0.0165294223 -0.023184269 6.927376e-03
## 2014-05-30 0.0117219820 0.0290910768 0.0158282891 0.006205136 2.294112e-02
## 2014-06-30 -0.0005759637 0.0237342830 0.0091653405 0.037718811 2.043492e-02
## 2014-07-31 -0.0025122243 0.0135553507 -0.0263795816 -0.052009519 -1.352889e-02
## 2014-08-29 0.0114303097 0.0279046076 0.0018004766 0.043658109 3.870482e-02
## 2014-09-30 -0.0061670037 -0.0808565450 -0.0395986182 -0.061260765 -1.389267e-02
## 2014-10-31 0.0105843902 0.0140963385 -0.0026548519 0.068874588 2.327814e-02
## 2014-11-28 0.0065492287 -0.0155414430 0.0006253295 0.004774153 2.710152e-02
## 2014-12-31 0.0014744425 -0.0404421228 -0.0407466475 0.025295610 -2.539727e-03
## 2015-01-30 0.0203158246 -0.0068958203 0.0062264509 -0.054627476 -3.007741e-02
## 2015-02-27 -0.0089884694 0.0431363883 0.0614507473 0.056914207 5.468211e-02
## 2015-03-31 0.0037405832 -0.0150863009 -0.0143889533 0.010156728 -1.583035e-02
## 2015-04-30 -0.0032327384 0.0662812399 0.0358165040 -0.018417902 9.785737e-03
## 2015-05-29 -0.0043836790 -0.0419109266 0.0019529451 0.007509903 1.277456e-02
## 2015-06-30 -0.0108260223 -0.0297466454 -0.0316790783 0.004171442 -2.052159e-02
## 2015-07-31 0.0085851645 -0.0651779537 0.0201147332 -0.027375568 2.233791e-02
## 2015-08-31 -0.0033642859 -0.0925122438 -0.0771525772 -0.047268436 -6.288639e-02
## 2015-09-30 0.0080807775 -0.0318252405 -0.0451947567 -0.038464839 -2.584740e-02
## 2015-10-30 0.0006860849 0.0618082789 0.0640259007 0.063589911 8.163494e-02
## 2015-11-30 -0.0038980275 -0.0255602647 -0.0075559791 0.024415374 3.648707e-03
## 2015-12-31 -0.0019189348 -0.0389472043 -0.0235949659 -0.052157170 -1.743387e-02
## 2016-01-29 0.0123300367 -0.0516369267 -0.0567577867 -0.060307052 -5.106845e-02
## 2016-02-29 0.0088313869 -0.0082114616 -0.0339140163 0.020605420 -8.262703e-04
## 2016-03-31 0.0087086672 0.1218791055 0.0637457564 0.089910402 6.510017e-02
## 2016-04-29 0.0025465417 0.0040792410 0.0219751893 0.021044276 3.933019e-03
## 2016-05-31 0.0001350846 -0.0376285322 -0.0008561715 0.004397090 1.686894e-02
## 2016-06-30 0.0191669356 0.0445825600 -0.0244917026 0.008292241 3.469487e-03
## 2016-07-29 0.0054297915 0.0524420273 0.0390003306 0.049348341 3.582223e-02
## 2016-08-31 -0.0021563198 0.0087984705 0.0053267601 0.011261091 1.196845e-03
## 2016-09-30 0.0005160165 0.0248729807 0.0132793321 0.008614563 5.802803e-05
## 2016-10-31 -0.0082054357 -0.0083120758 -0.0224039211 -0.038134656 -1.748905e-02
## 2016-11-30 -0.0259894309 -0.0451618779 -0.0179742290 0.125246412 3.617607e-02
## 2016-12-30 0.0025375592 -0.0025299829 0.0267029003 0.031491994 2.006900e-02
## 2017-01-31 0.0021267576 0.0644313476 0.0323818267 -0.012144123 1.773609e-02
## 2017-02-28 0.0064377273 0.0172577141 0.0118364314 0.013428797 3.853954e-02
## 2017-03-31 -0.0005531445 0.0361889775 0.0318055923 -0.006533116 1.249445e-03
## 2017-04-28 0.0090292649 0.0168664096 0.0239523312 0.005107801 9.877097e-03
## 2017-05-31 0.0068473207 0.0280601262 0.0348103668 -0.022862453 1.401432e-02
## 2017-06-30 -0.0001827520 0.0092236596 0.0029557775 0.029151707 6.354730e-03
## 2017-07-31 0.0033343223 0.0565944241 0.0261878079 0.007481415 2.034556e-02
## 2017-08-31 0.0093693937 0.0232437164 -0.0004482150 -0.027564707 2.913523e-03
## 2017-09-29 -0.0057322074 -0.0004460551 0.0233426703 0.082321590 1.994920e-02
## 2017-10-31 0.0009779709 0.0322783985 0.0166536532 0.005916056 2.329059e-02
## 2017-11-30 -0.0014836909 -0.0038970188 0.0068700315 0.036913212 3.010817e-02
## 2017-12-29 0.0047402021 0.0369254231 0.0133982085 -0.003731057 1.205473e-02
calculate_component_contribution <- function(.data, w) { 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]
rowSums(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,.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 and Weight
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution (w = c(.25, .25,.2, .2, .1)) %>%
# Transform to long form
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(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)