# 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.0062315902 -0.0029353524 0.0366063075 0.052133306 4.992260e-02
## 2013-02-28 0.0058916248 -0.0231054542 -0.0129692033 0.016175136 1.267811e-02
## 2013-03-28 0.0009853678 -0.0102348964 0.0129692033 0.040258299 3.726828e-02
## 2013-04-30 0.0096382816 0.0120847897 0.0489678089 0.001222668 1.903006e-02
## 2013-05-31 -0.0202136606 -0.0494836088 -0.0306556861 0.041976078 2.333527e-02
## 2013-06-28 -0.0157779875 -0.0547281409 -0.0271443833 -0.001402849 -1.343423e-02
## 2013-07-31 0.0026872778 0.0131596971 0.0518602606 0.063541070 5.038588e-02
## 2013-08-30 -0.0082984937 -0.0257057991 -0.0197460238 -0.034743283 -3.045114e-02
## 2013-09-30 0.0111445556 0.0695890226 0.0753384033 0.063873827 3.115610e-02
## 2013-10-31 0.0082923089 0.0408609511 0.0320815538 0.034233762 4.526614e-02
## 2013-11-29 -0.0025100792 -0.0025939141 0.0054497805 0.041661345 2.920704e-02
## 2013-12-31 -0.0055830827 -0.0040743618 0.0215277426 0.012891963 2.559628e-02
## 2014-01-31 0.0152911415 -0.0903225597 -0.0534130698 -0.035775161 -3.588433e-02
## 2014-02-28 0.0037573164 0.0332205992 0.0595050852 0.045257251 4.450999e-02
## 2014-03-31 -0.0014819090 0.0380217573 -0.0046026436 0.013315355 8.261514e-03
## 2014-04-30 0.0081834801 0.0077726260 0.0165293705 -0.023184279 6.927469e-03
## 2014-05-30 0.0117217783 0.0290913038 0.0158285748 0.006205384 2.294118e-02
## 2014-06-30 -0.0005763988 0.0237337214 0.0091654813 0.037718489 2.043450e-02
## 2014-07-31 -0.0025119273 0.0135557911 -0.0263800808 -0.052009295 -1.352836e-02
## 2014-08-29 0.0114307718 0.0279046807 0.0018006511 0.043657972 3.870409e-02
## 2014-09-30 -0.0061672033 -0.0808567872 -0.0395985666 -0.061260375 -1.389201e-02
## 2014-10-31 0.0105852492 0.0140965643 -0.0026548111 0.068874657 2.327806e-02
## 2014-11-28 0.0065479072 -0.0155412945 0.0006253098 0.004773949 2.710131e-02
## 2014-12-31 0.0014755967 -0.0404423407 -0.0407468101 0.025295524 -2.539925e-03
## 2015-01-30 0.0203147826 -0.0068956168 0.0062263408 -0.054627975 -3.007690e-02
## 2015-02-27 -0.0089880732 0.0431360188 0.0614507043 0.056914793 5.468172e-02
## 2015-03-31 0.0037398657 -0.0150862520 -0.0143886289 0.010156387 -1.583037e-02
## 2015-04-30 -0.0032327405 0.0662813058 0.0358165794 -0.018417742 9.786386e-03
## 2015-05-29 -0.0043834671 -0.0419107925 0.0019527233 0.007509799 1.277387e-02
## 2015-06-30 -0.0108252494 -0.0297469590 -0.0316789785 0.004171297 -2.052118e-02
## 2015-07-31 0.0085842861 -0.0651778287 0.0201146365 -0.027375369 2.233763e-02
## 2015-08-31 -0.0033639246 -0.0925125293 -0.0771523975 -0.047268107 -6.288652e-02
## 2015-09-30 0.0080821415 -0.0318249231 -0.0451951478 -0.038464625 -2.584723e-02
## 2015-10-30 0.0006848747 0.0618081707 0.0640259784 0.063589543 8.163514e-02
## 2015-11-30 -0.0038980433 -0.0255603382 -0.0075558806 0.024415184 3.648362e-03
## 2015-12-31 -0.0019188555 -0.0389472468 -0.0235950314 -0.052156988 -1.743339e-02
## 2016-01-29 0.0123300394 -0.0516366123 -0.0567576480 -0.060306604 -5.106847e-02
## 2016-02-29 0.0088312199 -0.0082115308 -0.0339139897 0.020604947 -8.265769e-04
## 2016-03-31 0.0087091532 0.1218789271 0.0637457739 0.089910483 6.510032e-02
## 2016-04-29 0.0025463205 0.0040794162 0.0219749289 0.021044231 3.933222e-03
## 2016-05-31 0.0001354793 -0.0376285768 -0.0008559621 0.004396890 1.686847e-02
## 2016-06-30 0.0191666894 0.0445824706 -0.0244915362 0.008292401 3.469801e-03
## 2016-07-29 0.0054298376 0.0524419648 0.0390001969 0.049348234 3.582215e-02
## 2016-08-31 -0.0021564496 0.0087986013 0.0053268628 0.011260971 1.196736e-03
## 2016-09-30 0.0005159879 0.0248729974 0.0132791585 0.008614946 5.797445e-05
## 2016-10-31 -0.0082054711 -0.0083121906 -0.0224037499 -0.038134785 -1.748889e-02
## 2016-11-30 -0.0259894573 -0.0451618325 -0.0179744835 0.125245840 3.617622e-02
## 2016-12-30 0.0025378368 -0.0025298819 0.0267029452 0.031492245 2.006893e-02
## 2017-01-31 0.0021263936 0.0644313273 0.0323817970 -0.012143907 1.773638e-02
## 2017-02-28 0.0064374296 0.0172577789 0.0118364721 0.013428869 3.853926e-02
## 2017-03-31 -0.0005527089 0.0361892139 0.0318057896 -0.006533142 1.249233e-03
## 2017-04-28 0.0090294579 0.0168661716 0.0239522142 0.005107835 9.876945e-03
## 2017-05-31 0.0068471621 0.0280598776 0.0348101850 -0.022862749 1.401430e-02
## 2017-06-30 -0.0001824196 0.0092237659 0.0029558548 0.029151933 6.354904e-03
## 2017-07-31 0.0033344871 0.0565945476 0.0261879397 0.007481433 2.034572e-02
## 2017-08-31 0.0093691955 0.0232438736 -0.0004481536 -0.027564796 2.913448e-03
## 2017-09-29 -0.0057322996 -0.0004463954 0.0233427036 0.082321785 1.994915e-02
## 2017-10-31 0.0009777832 0.0322785746 0.0166534606 0.005915973 2.329075e-02
## 2017-11-30 -0.0014840713 -0.0038969524 0.0068701061 0.036913401 3.010806e-02
## 2017-12-29 0.0047404064 0.0369254897 0.0133982711 -0.003730914 1.205525e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398320e-05 0.0001042115 4.178201e-05 -7.811769e-05 -9.031726e-06
## EEM 1.042115e-04 0.0017547103 1.039017e-03 6.437729e-04 6.795425e-04
## EFA 4.178201e-05 0.0010390167 1.064237e-03 6.490294e-04 6.975394e-04
## IJS -7.811769e-05 0.0006437729 6.490294e-04 1.565445e-03 8.290215e-04
## SPY -9.031726e-06 0.0006795425 6.975394e-04 8.290215e-04 7.408254e-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.0003874176 0.009257148 0.005815632 0.00568446 0.002330244
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
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.0062315902 -0.0029353524 0.0366063075 0.052133306 4.992260e-02
## 2013-02-28 0.0058916248 -0.0231054542 -0.0129692033 0.016175136 1.267811e-02
## 2013-03-28 0.0009853678 -0.0102348964 0.0129692033 0.040258299 3.726828e-02
## 2013-04-30 0.0096382816 0.0120847897 0.0489678089 0.001222668 1.903006e-02
## 2013-05-31 -0.0202136606 -0.0494836088 -0.0306556861 0.041976078 2.333527e-02
## 2013-06-28 -0.0157779875 -0.0547281409 -0.0271443833 -0.001402849 -1.343423e-02
## 2013-07-31 0.0026872778 0.0131596971 0.0518602606 0.063541070 5.038588e-02
## 2013-08-30 -0.0082984937 -0.0257057991 -0.0197460238 -0.034743283 -3.045114e-02
## 2013-09-30 0.0111445556 0.0695890226 0.0753384033 0.063873827 3.115610e-02
## 2013-10-31 0.0082923089 0.0408609511 0.0320815538 0.034233762 4.526614e-02
## 2013-11-29 -0.0025100792 -0.0025939141 0.0054497805 0.041661345 2.920704e-02
## 2013-12-31 -0.0055830827 -0.0040743618 0.0215277426 0.012891963 2.559628e-02
## 2014-01-31 0.0152911415 -0.0903225597 -0.0534130698 -0.035775161 -3.588433e-02
## 2014-02-28 0.0037573164 0.0332205992 0.0595050852 0.045257251 4.450999e-02
## 2014-03-31 -0.0014819090 0.0380217573 -0.0046026436 0.013315355 8.261514e-03
## 2014-04-30 0.0081834801 0.0077726260 0.0165293705 -0.023184279 6.927469e-03
## 2014-05-30 0.0117217783 0.0290913038 0.0158285748 0.006205384 2.294118e-02
## 2014-06-30 -0.0005763988 0.0237337214 0.0091654813 0.037718489 2.043450e-02
## 2014-07-31 -0.0025119273 0.0135557911 -0.0263800808 -0.052009295 -1.352836e-02
## 2014-08-29 0.0114307718 0.0279046807 0.0018006511 0.043657972 3.870409e-02
## 2014-09-30 -0.0061672033 -0.0808567872 -0.0395985666 -0.061260375 -1.389201e-02
## 2014-10-31 0.0105852492 0.0140965643 -0.0026548111 0.068874657 2.327806e-02
## 2014-11-28 0.0065479072 -0.0155412945 0.0006253098 0.004773949 2.710131e-02
## 2014-12-31 0.0014755967 -0.0404423407 -0.0407468101 0.025295524 -2.539925e-03
## 2015-01-30 0.0203147826 -0.0068956168 0.0062263408 -0.054627975 -3.007690e-02
## 2015-02-27 -0.0089880732 0.0431360188 0.0614507043 0.056914793 5.468172e-02
## 2015-03-31 0.0037398657 -0.0150862520 -0.0143886289 0.010156387 -1.583037e-02
## 2015-04-30 -0.0032327405 0.0662813058 0.0358165794 -0.018417742 9.786386e-03
## 2015-05-29 -0.0043834671 -0.0419107925 0.0019527233 0.007509799 1.277387e-02
## 2015-06-30 -0.0108252494 -0.0297469590 -0.0316789785 0.004171297 -2.052118e-02
## 2015-07-31 0.0085842861 -0.0651778287 0.0201146365 -0.027375369 2.233763e-02
## 2015-08-31 -0.0033639246 -0.0925125293 -0.0771523975 -0.047268107 -6.288652e-02
## 2015-09-30 0.0080821415 -0.0318249231 -0.0451951478 -0.038464625 -2.584723e-02
## 2015-10-30 0.0006848747 0.0618081707 0.0640259784 0.063589543 8.163514e-02
## 2015-11-30 -0.0038980433 -0.0255603382 -0.0075558806 0.024415184 3.648362e-03
## 2015-12-31 -0.0019188555 -0.0389472468 -0.0235950314 -0.052156988 -1.743339e-02
## 2016-01-29 0.0123300394 -0.0516366123 -0.0567576480 -0.060306604 -5.106847e-02
## 2016-02-29 0.0088312199 -0.0082115308 -0.0339139897 0.020604947 -8.265769e-04
## 2016-03-31 0.0087091532 0.1218789271 0.0637457739 0.089910483 6.510032e-02
## 2016-04-29 0.0025463205 0.0040794162 0.0219749289 0.021044231 3.933222e-03
## 2016-05-31 0.0001354793 -0.0376285768 -0.0008559621 0.004396890 1.686847e-02
## 2016-06-30 0.0191666894 0.0445824706 -0.0244915362 0.008292401 3.469801e-03
## 2016-07-29 0.0054298376 0.0524419648 0.0390001969 0.049348234 3.582215e-02
## 2016-08-31 -0.0021564496 0.0087986013 0.0053268628 0.011260971 1.196736e-03
## 2016-09-30 0.0005159879 0.0248729974 0.0132791585 0.008614946 5.797445e-05
## 2016-10-31 -0.0082054711 -0.0083121906 -0.0224037499 -0.038134785 -1.748889e-02
## 2016-11-30 -0.0259894573 -0.0451618325 -0.0179744835 0.125245840 3.617622e-02
## 2016-12-30 0.0025378368 -0.0025298819 0.0267029452 0.031492245 2.006893e-02
## 2017-01-31 0.0021263936 0.0644313273 0.0323817970 -0.012143907 1.773638e-02
## 2017-02-28 0.0064374296 0.0172577789 0.0118364721 0.013428869 3.853926e-02
## 2017-03-31 -0.0005527089 0.0361892139 0.0318057896 -0.006533142 1.249233e-03
## 2017-04-28 0.0090294579 0.0168661716 0.0239522142 0.005107835 9.876945e-03
## 2017-05-31 0.0068471621 0.0280598776 0.0348101850 -0.022862749 1.401430e-02
## 2017-06-30 -0.0001824196 0.0092237659 0.0029558548 0.029151933 6.354904e-03
## 2017-07-31 0.0033344871 0.0565945476 0.0261879397 0.007481433 2.034572e-02
## 2017-08-31 0.0093691955 0.0232438736 -0.0004481536 -0.027564796 2.913448e-03
## 2017-09-29 -0.0057322996 -0.0004463954 0.0233427036 0.082321785 1.994915e-02
## 2017-10-31 0.0009777832 0.0322785746 0.0166534606 0.005915973 2.329075e-02
## 2017-11-30 -0.0014840713 -0.0038969524 0.0068701061 0.036913401 3.010806e-02
## 2017-12-29 0.0047404064 0.0369254897 0.0133982711 -0.003730914 1.205525e-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 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
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")
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 Volatitily")
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 Weight
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)) +
labs(title = "Percent Contribution to Portfolio Volatitily and Weight",
y = "Percent",
x = NULL)