# 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.0062310941 -0.0029355995 0.0366062193 0.052133210 4.992283e-02
## 2013-02-28 0.0058911537 -0.0231048858 -0.0129694754 0.016175451 1.267817e-02
## 2013-03-28 0.0009850083 -0.0102352292 0.0129694754 0.040258193 3.726788e-02
## 2013-04-30 0.0096388117 0.0120846606 0.0489678042 0.001222435 1.903035e-02
## 2013-05-31 -0.0202141848 -0.0494834075 -0.0306556819 0.041976390 2.333525e-02
## 2013-06-28 -0.0157779382 -0.0547284119 -0.0271445305 -0.001402804 -1.343419e-02
## 2013-07-31 0.0026874078 0.0131598630 0.0518603108 0.063541317 5.038615e-02
## 2013-08-30 -0.0082977817 -0.0257055117 -0.0197460608 -0.034744047 -3.045179e-02
## 2013-09-30 0.0111438358 0.0695885854 0.0753384280 0.063874095 3.115615e-02
## 2013-10-31 0.0082921922 0.0408614187 0.0320815751 0.034233730 4.526662e-02
## 2013-11-29 -0.0025099125 -0.0025941931 0.0054496820 0.041661507 2.920700e-02
## 2013-12-31 -0.0055828449 -0.0040742395 0.0215280892 0.012892130 2.559605e-02
## 2014-01-31 0.0152914900 -0.0903224080 -0.0534133526 -0.035775734 -3.588465e-02
## 2014-02-28 0.0037565529 0.0332201667 0.0595050873 0.045257607 4.451035e-02
## 2014-03-31 -0.0014810730 0.0380218252 -0.0046025730 0.013315233 8.261002e-03
## 2014-04-30 0.0081825877 0.0077727927 0.0165292653 -0.023183865 6.928150e-03
## 2014-05-30 0.0117220303 0.0290913076 0.0158286006 0.006205285 2.294097e-02
## 2014-06-30 -0.0005759348 0.0237337140 0.0091653391 0.037718647 2.043435e-02
## 2014-07-31 -0.0025116809 0.0135558000 -0.0263798134 -0.052009577 -1.352860e-02
## 2014-08-29 0.0114304310 0.0279044964 0.0018004767 0.043657946 3.870496e-02
## 2014-09-30 -0.0061669748 -0.0808566622 -0.0395985398 -0.061260479 -1.389227e-02
## 2014-10-31 0.0105840791 0.0140964557 -0.0026549338 0.068874884 2.327780e-02
## 2014-11-28 0.0065491742 -0.0155414430 0.0006254932 0.004773730 2.710149e-02
## 2014-12-31 0.0014743222 -0.0404418783 -0.0407465589 0.025295752 -2.539913e-03
## 2015-01-30 0.0203157272 -0.0068958802 0.0062263652 -0.054628124 -3.007737e-02
## 2015-02-27 -0.0089882999 0.0431362036 0.0614505830 0.056914690 5.468197e-02
## 2015-03-31 0.0037402586 -0.0150863608 -0.0143886313 0.010156390 -1.583006e-02
## 2015-04-30 -0.0032334180 0.0662814118 0.0358164176 -0.018417572 9.785785e-03
## 2015-05-29 -0.0043828924 -0.0419109218 0.0019526336 0.007509632 1.277408e-02
## 2015-06-30 -0.0108257834 -0.0297466418 -0.0316788424 0.004171436 -2.052089e-02
## 2015-07-31 0.0085846836 -0.0651782668 0.0201143381 -0.027375350 2.233784e-02
## 2015-08-31 -0.0033636913 -0.0925122625 -0.0771523474 -0.047268437 -6.288680e-02
## 2015-09-30 0.0080808856 -0.0318250291 -0.0451948496 -0.038464675 -2.584716e-02
## 2015-10-30 0.0006860038 0.0618082789 0.0640259951 0.063590090 8.163490e-02
## 2015-11-30 -0.0038984426 -0.0255604049 -0.0075558957 0.024415031 3.648508e-03
## 2015-12-31 -0.0019187160 -0.0389469911 -0.0235950520 -0.052157166 -1.743350e-02
## 2016-01-29 0.0123301079 -0.0516367692 -0.0567579739 -0.060306903 -5.106887e-02
## 2016-02-29 0.0088311325 -0.0082117694 -0.0339136488 0.020605217 -8.261510e-04
## 2016-03-31 0.0087085984 0.1218793200 0.0637456621 0.089910235 6.510008e-02
## 2016-04-29 0.0025464758 0.0040789675 0.0219750163 0.021044401 3.933516e-03
## 2016-05-31 0.0001355991 -0.0376284665 -0.0008560851 0.004396951 1.686876e-02
## 2016-06-30 0.0191667141 0.0445824275 -0.0244914386 0.008292475 3.469421e-03
## 2016-07-29 0.0054296816 0.0524422949 0.0390002385 0.049348284 3.582192e-02
## 2016-08-31 -0.0021564405 0.0087984061 0.0053267596 0.011261132 1.197056e-03
## 2016-09-30 0.0005161298 0.0248730429 0.0132789122 0.008614665 5.827063e-05
## 2016-10-31 -0.0082051906 -0.0083124516 -0.0224035861 -0.038134742 -1.748922e-02
## 2016-11-30 -0.0259895293 -0.0451616955 -0.0179742290 0.125246199 3.617591e-02
## 2016-12-30 0.0025376060 -0.0025300490 0.0267029003 0.031492122 2.006898e-02
## 2017-01-31 0.0021262175 0.0644316066 0.0323819089 -0.012144335 1.773641e-02
## 2017-02-28 0.0064381402 0.0172578949 0.0118363492 0.013429087 3.853956e-02
## 2017-03-31 -0.0005530387 0.0361887350 0.0318057497 -0.006532995 1.248899e-03
## 2017-04-28 0.0090295236 0.0168666395 0.0239520970 0.005107514 9.877230e-03
## 2017-05-31 0.0068474445 0.0280600081 0.0348102211 -0.022862550 1.401457e-02
## 2017-06-30 -0.0001830930 0.0092234371 0.0029558521 0.029151768 6.354577e-03
## 2017-07-31 0.0033344935 0.0565946395 0.0261881000 0.007481405 2.034566e-02
## 2017-08-31 0.0093693318 0.0232438163 -0.0004483592 -0.027564590 2.913569e-03
## 2017-09-29 -0.0057321771 -0.0004462596 0.0233428112 0.082322151 1.994893e-02
## 2017-10-31 0.0009780892 0.0322785967 0.0166535124 0.005915399 2.329082e-02
## 2017-11-30 -0.0014838847 -0.0038973163 0.0068700315 0.036913833 3.010808e-02
## 2017-12-29 0.0047401615 0.0369257143 0.0133984801 -0.003731752 1.205501e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398321e-05 0.0001042088 4.178213e-05 -7.812254e-05 -9.031522e-06
## EEM 1.042088e-04 0.0017547112 1.039016e-03 6.437740e-04 6.795429e-04
## EFA 4.178213e-05 0.0010390162 1.064236e-03 6.490318e-04 6.975416e-04
## IJS -7.812254e-05 0.0006437740 6.490318e-04 1.565455e-03 8.290264e-04
## SPY -9.031522e-06 0.0006795429 6.975416e-04 8.290264e-04 7.408301e-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.02347491
# 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.0003874007 0.009257144 0.005815634 0.005684476 0.002330252
rowSums(component_contribution)
## [1] 0.02347491
# 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.0062310941 -0.0029355995 0.0366062193 0.052133210 4.992283e-02
## 2013-02-28 0.0058911537 -0.0231048858 -0.0129694754 0.016175451 1.267817e-02
## 2013-03-28 0.0009850083 -0.0102352292 0.0129694754 0.040258193 3.726788e-02
## 2013-04-30 0.0096388117 0.0120846606 0.0489678042 0.001222435 1.903035e-02
## 2013-05-31 -0.0202141848 -0.0494834075 -0.0306556819 0.041976390 2.333525e-02
## 2013-06-28 -0.0157779382 -0.0547284119 -0.0271445305 -0.001402804 -1.343419e-02
## 2013-07-31 0.0026874078 0.0131598630 0.0518603108 0.063541317 5.038615e-02
## 2013-08-30 -0.0082977817 -0.0257055117 -0.0197460608 -0.034744047 -3.045179e-02
## 2013-09-30 0.0111438358 0.0695885854 0.0753384280 0.063874095 3.115615e-02
## 2013-10-31 0.0082921922 0.0408614187 0.0320815751 0.034233730 4.526662e-02
## 2013-11-29 -0.0025099125 -0.0025941931 0.0054496820 0.041661507 2.920700e-02
## 2013-12-31 -0.0055828449 -0.0040742395 0.0215280892 0.012892130 2.559605e-02
## 2014-01-31 0.0152914900 -0.0903224080 -0.0534133526 -0.035775734 -3.588465e-02
## 2014-02-28 0.0037565529 0.0332201667 0.0595050873 0.045257607 4.451035e-02
## 2014-03-31 -0.0014810730 0.0380218252 -0.0046025730 0.013315233 8.261002e-03
## 2014-04-30 0.0081825877 0.0077727927 0.0165292653 -0.023183865 6.928150e-03
## 2014-05-30 0.0117220303 0.0290913076 0.0158286006 0.006205285 2.294097e-02
## 2014-06-30 -0.0005759348 0.0237337140 0.0091653391 0.037718647 2.043435e-02
## 2014-07-31 -0.0025116809 0.0135558000 -0.0263798134 -0.052009577 -1.352860e-02
## 2014-08-29 0.0114304310 0.0279044964 0.0018004767 0.043657946 3.870496e-02
## 2014-09-30 -0.0061669748 -0.0808566622 -0.0395985398 -0.061260479 -1.389227e-02
## 2014-10-31 0.0105840791 0.0140964557 -0.0026549338 0.068874884 2.327780e-02
## 2014-11-28 0.0065491742 -0.0155414430 0.0006254932 0.004773730 2.710149e-02
## 2014-12-31 0.0014743222 -0.0404418783 -0.0407465589 0.025295752 -2.539913e-03
## 2015-01-30 0.0203157272 -0.0068958802 0.0062263652 -0.054628124 -3.007737e-02
## 2015-02-27 -0.0089882999 0.0431362036 0.0614505830 0.056914690 5.468197e-02
## 2015-03-31 0.0037402586 -0.0150863608 -0.0143886313 0.010156390 -1.583006e-02
## 2015-04-30 -0.0032334180 0.0662814118 0.0358164176 -0.018417572 9.785785e-03
## 2015-05-29 -0.0043828924 -0.0419109218 0.0019526336 0.007509632 1.277408e-02
## 2015-06-30 -0.0108257834 -0.0297466418 -0.0316788424 0.004171436 -2.052089e-02
## 2015-07-31 0.0085846836 -0.0651782668 0.0201143381 -0.027375350 2.233784e-02
## 2015-08-31 -0.0033636913 -0.0925122625 -0.0771523474 -0.047268437 -6.288680e-02
## 2015-09-30 0.0080808856 -0.0318250291 -0.0451948496 -0.038464675 -2.584716e-02
## 2015-10-30 0.0006860038 0.0618082789 0.0640259951 0.063590090 8.163490e-02
## 2015-11-30 -0.0038984426 -0.0255604049 -0.0075558957 0.024415031 3.648508e-03
## 2015-12-31 -0.0019187160 -0.0389469911 -0.0235950520 -0.052157166 -1.743350e-02
## 2016-01-29 0.0123301079 -0.0516367692 -0.0567579739 -0.060306903 -5.106887e-02
## 2016-02-29 0.0088311325 -0.0082117694 -0.0339136488 0.020605217 -8.261510e-04
## 2016-03-31 0.0087085984 0.1218793200 0.0637456621 0.089910235 6.510008e-02
## 2016-04-29 0.0025464758 0.0040789675 0.0219750163 0.021044401 3.933516e-03
## 2016-05-31 0.0001355991 -0.0376284665 -0.0008560851 0.004396951 1.686876e-02
## 2016-06-30 0.0191667141 0.0445824275 -0.0244914386 0.008292475 3.469421e-03
## 2016-07-29 0.0054296816 0.0524422949 0.0390002385 0.049348284 3.582192e-02
## 2016-08-31 -0.0021564405 0.0087984061 0.0053267596 0.011261132 1.197056e-03
## 2016-09-30 0.0005161298 0.0248730429 0.0132789122 0.008614665 5.827063e-05
## 2016-10-31 -0.0082051906 -0.0083124516 -0.0224035861 -0.038134742 -1.748922e-02
## 2016-11-30 -0.0259895293 -0.0451616955 -0.0179742290 0.125246199 3.617591e-02
## 2016-12-30 0.0025376060 -0.0025300490 0.0267029003 0.031492122 2.006898e-02
## 2017-01-31 0.0021262175 0.0644316066 0.0323819089 -0.012144335 1.773641e-02
## 2017-02-28 0.0064381402 0.0172578949 0.0118363492 0.013429087 3.853956e-02
## 2017-03-31 -0.0005530387 0.0361887350 0.0318057497 -0.006532995 1.248899e-03
## 2017-04-28 0.0090295236 0.0168666395 0.0239520970 0.005107514 9.877230e-03
## 2017-05-31 0.0068474445 0.0280600081 0.0348102211 -0.022862550 1.401457e-02
## 2017-06-30 -0.0001830930 0.0092234371 0.0029558521 0.029151768 6.354577e-03
## 2017-07-31 0.0033344935 0.0565946395 0.0261881000 0.007481405 2.034566e-02
## 2017-08-31 0.0093693318 0.0232438163 -0.0004483592 -0.027564590 2.913569e-03
## 2017-09-29 -0.0057321771 -0.0004462596 0.0233428112 0.082322151 1.994893e-02
## 2017-10-31 0.0009780892 0.0322785967 0.0166535124 0.005915399 2.329082e-02
## 2017-11-30 -0.0014838847 -0.0038973163 0.0068700315 0.036913833 3.010808e-02
## 2017-12-29 0.0047401615 0.0369257143 0.0133984801 -0.003731752 1.205501e-02
calculate_component_contribution <- function(asset_returns_wide_tbl, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# Standard deviation of portfolio
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
# Component contribution
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()
component_percentages
return(component_percentages) %>%
as_tibble() %>%
gather(key - "asset", value = "contribution")
}
asset_returns_wide_tbl %>% calculate_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
# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
gather(key = "asset", value = "contribution") %>%
ggplot(aes(asset, contribution)) +
geom_col(fill = "cornflowerblue") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
labs(title = "Percent Contribution to Portfolio Standard Deviation",
y = "Percent Contribution to Risk",
x = NULL)
# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
gather(key = "asset", value = "contribution") %>%
add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%
ggplot(aes(asset, value, fill = type)) +
geom_col(position = "dodge") +
theme(plot.title = element_text(hjust = 0.5)) +
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 = "asset")