# 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.0062308952 -0.0029355222 0.0366064087 0.052133440 0.0499232443
## 2013-02-28 0.0058908798 -0.0231053278 -0.0129695837 0.016174997 0.0126782097
## 2013-03-28 0.0009850145 -0.0102350862 0.0129695837 0.040258329 0.0372674269
## 2013-04-30 0.0096391950 0.0120847545 0.0489675020 0.001222202 0.0190304606
## 2013-05-31 -0.0202140705 -0.0494834007 -0.0306554905 0.041976546 0.0233354257
## 2013-06-28 -0.0157783220 -0.0547283650 -0.0271443095 -0.001403078 -0.0134342042
## 2013-07-31 0.0026881639 0.0131598215 0.0518601225 0.063541670 0.0503856511
## 2013-08-30 -0.0082983238 -0.0257057832 -0.0197464238 -0.034743741 -0.0304513126
## 2013-09-30 0.0111439857 0.0695890650 0.0753385468 0.063873945 0.0311560388
## 2013-10-31 0.0082923146 0.0408611644 0.0320820774 0.034233810 0.0452665724
## 2013-11-29 -0.0025100653 -0.0025939984 0.0054493777 0.041661149 0.0292070012
## 2013-12-31 -0.0055827767 -0.0040745851 0.0215280565 0.012892114 0.0255961541
## 2014-01-31 0.0152912122 -0.0903225790 -0.0534133484 -0.035775253 -0.0358850688
## 2014-02-28 0.0037568765 0.0332204266 0.0595050034 0.045257557 0.0445105890
## 2014-03-31 -0.0014818715 0.0380220280 -0.0046024059 0.013315179 0.0082614064
## 2014-04-30 0.0081833934 0.0077727909 0.0165293642 -0.023184266 0.0069278679
## 2014-05-30 0.0117220342 0.0290909825 0.0158284550 0.006205341 0.0229408762
## 2014-06-30 -0.0005759260 0.0237340295 0.0091653921 0.037718564 0.0204345460
## 2014-07-31 -0.0025122743 0.0135556500 -0.0263798718 -0.052008997 -0.0135286657
## 2014-08-29 0.0114312464 0.0279046974 0.0018006137 0.043657454 0.0387047155
## 2014-09-30 -0.0061672651 -0.0808568613 -0.0395985139 -0.061260318 -0.0138923575
## 2014-10-31 0.0105837638 0.0140963562 -0.0026549203 0.068874722 0.0232781099
## 2014-11-28 0.0065490384 -0.0155410501 0.0006254062 0.004773720 0.0271015614
## 2014-12-31 0.0014753241 -0.0404422641 -0.0407470190 0.025295750 -0.0025400505
## 2015-01-30 0.0203153087 -0.0068958965 0.0062267119 -0.054628024 -0.0300766244
## 2015-02-27 -0.0089884968 0.0431362020 0.0614504550 0.056914883 0.0546809439
## 2015-03-31 0.0037403613 -0.0150861317 -0.0143885668 0.010156172 -0.0158299264
## 2015-04-30 -0.0032327530 0.0662812785 0.0358164078 -0.018417592 0.0097858681
## 2015-05-29 -0.0043838981 -0.0419108476 0.0019527879 0.007509793 0.0127741015
## 2015-06-30 -0.0108255216 -0.0297468695 -0.0316789376 0.004171368 -0.0205210334
## 2015-07-31 0.0085847588 -0.0651780382 0.0201145440 -0.027375188 0.0223377870
## 2015-08-31 -0.0033641785 -0.0925122097 -0.0771524325 -0.047268796 -0.0628864870
## 2015-09-30 0.0080819951 -0.0318251684 -0.0451949024 -0.038464606 -0.0258474315
## 2015-10-30 0.0006846744 0.0618082191 0.0640258586 0.063589975 0.0816351482
## 2015-11-30 -0.0038975743 -0.0255603149 -0.0075558350 0.024415019 0.0036483536
## 2015-12-31 -0.0019188513 -0.0389472337 -0.0235950054 -0.052156795 -0.0174336682
## 2016-01-29 0.0123296844 -0.0516366227 -0.0567579406 -0.060306669 -0.0510681610
## 2016-02-29 0.0088314978 -0.0082115780 -0.0339139816 0.020605165 -0.0008266308
## 2016-03-31 0.0087089570 0.1218792115 0.0637458404 0.089909950 0.0651001460
## 2016-04-29 0.0025454210 0.0040790654 0.0219752986 0.021044541 0.0039335264
## 2016-05-31 0.0001357972 -0.0376284066 -0.0008562143 0.004397079 0.0168685774
## 2016-06-30 0.0191673296 0.0445822655 -0.0244915306 0.008292316 0.0034698391
## 2016-07-29 0.0054296007 0.0524422156 0.0390003784 0.049348431 0.0358221585
## 2016-08-31 -0.0021566269 0.0087985240 0.0053268332 0.011260896 0.0011967448
## 2016-09-30 0.0005164151 0.0248730233 0.0132789220 0.008614708 0.0000578254
## 2016-10-31 -0.0082051834 -0.0083125450 -0.0224037110 -0.038134774 -0.0174889174
## 2016-11-30 -0.0259896093 -0.0451619654 -0.0179742272 0.125246300 0.0361758998
## 2016-12-30 0.0025375273 -0.0025296962 0.0267027148 0.031491668 0.0200691013
## 2017-01-31 0.0021265656 0.0644315291 0.0323818393 -0.012143675 0.0177364427
## 2017-02-28 0.0064376261 0.0172577381 0.0118362989 0.013428635 0.0385394000
## 2017-03-31 -0.0005526804 0.0361890304 0.0318060644 -0.006533238 0.0012490104
## 2017-04-28 0.0090291152 0.0168667164 0.0239520535 0.005108333 0.0098773354
## 2017-05-31 0.0068474921 0.0280596833 0.0348103786 -0.022863036 0.0140143215
## 2017-06-30 -0.0001824910 0.0092237863 0.0029557372 0.029151594 0.0063544967
## 2017-07-31 0.0033340878 0.0565946380 0.0261879043 0.007481600 0.0203459826
## 2017-08-31 0.0093689433 0.0232436405 -0.0004481261 -0.027564509 0.0029132389
## 2017-09-29 -0.0057319911 -0.0004463429 0.0233425171 0.082321671 0.0199491970
## 2017-10-31 0.0009781437 0.0322784770 0.0166538869 0.005916108 0.0232907405
## 2017-11-30 -0.0014845060 -0.0038970340 0.0068699458 0.036913465 0.0301082815
## 2017-12-29 0.0047403271 0.0369254424 0.0133983610 -0.003730947 0.0120548077
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398425e-05 0.0001042113 4.178293e-05 -7.812055e-05 -9.030810e-06
## EEM 1.042113e-04 0.0017547123 1.039019e-03 6.437749e-04 6.795434e-04
## EFA 4.178293e-05 0.0010390185 1.064238e-03 6.490308e-04 6.975409e-04
## IJS -7.812055e-05 0.0006437749 6.490308e-04 1.565448e-03 8.290242e-04
## SPY -9.030810e-06 0.0006795434 6.975409e-04 8.290242e-04 7.408269e-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.02347492
# 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.0003874165 0.009257155 0.005815638 0.005684464 0.002330248
rowSums(component_contribution)
## [1] 0.02347492
# 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.0062308952 -0.0029355222 0.0366064087 0.052133440 0.0499232443
## 2013-02-28 0.0058908798 -0.0231053278 -0.0129695837 0.016174997 0.0126782097
## 2013-03-28 0.0009850145 -0.0102350862 0.0129695837 0.040258329 0.0372674269
## 2013-04-30 0.0096391950 0.0120847545 0.0489675020 0.001222202 0.0190304606
## 2013-05-31 -0.0202140705 -0.0494834007 -0.0306554905 0.041976546 0.0233354257
## 2013-06-28 -0.0157783220 -0.0547283650 -0.0271443095 -0.001403078 -0.0134342042
## 2013-07-31 0.0026881639 0.0131598215 0.0518601225 0.063541670 0.0503856511
## 2013-08-30 -0.0082983238 -0.0257057832 -0.0197464238 -0.034743741 -0.0304513126
## 2013-09-30 0.0111439857 0.0695890650 0.0753385468 0.063873945 0.0311560388
## 2013-10-31 0.0082923146 0.0408611644 0.0320820774 0.034233810 0.0452665724
## 2013-11-29 -0.0025100653 -0.0025939984 0.0054493777 0.041661149 0.0292070012
## 2013-12-31 -0.0055827767 -0.0040745851 0.0215280565 0.012892114 0.0255961541
## 2014-01-31 0.0152912122 -0.0903225790 -0.0534133484 -0.035775253 -0.0358850688
## 2014-02-28 0.0037568765 0.0332204266 0.0595050034 0.045257557 0.0445105890
## 2014-03-31 -0.0014818715 0.0380220280 -0.0046024059 0.013315179 0.0082614064
## 2014-04-30 0.0081833934 0.0077727909 0.0165293642 -0.023184266 0.0069278679
## 2014-05-30 0.0117220342 0.0290909825 0.0158284550 0.006205341 0.0229408762
## 2014-06-30 -0.0005759260 0.0237340295 0.0091653921 0.037718564 0.0204345460
## 2014-07-31 -0.0025122743 0.0135556500 -0.0263798718 -0.052008997 -0.0135286657
## 2014-08-29 0.0114312464 0.0279046974 0.0018006137 0.043657454 0.0387047155
## 2014-09-30 -0.0061672651 -0.0808568613 -0.0395985139 -0.061260318 -0.0138923575
## 2014-10-31 0.0105837638 0.0140963562 -0.0026549203 0.068874722 0.0232781099
## 2014-11-28 0.0065490384 -0.0155410501 0.0006254062 0.004773720 0.0271015614
## 2014-12-31 0.0014753241 -0.0404422641 -0.0407470190 0.025295750 -0.0025400505
## 2015-01-30 0.0203153087 -0.0068958965 0.0062267119 -0.054628024 -0.0300766244
## 2015-02-27 -0.0089884968 0.0431362020 0.0614504550 0.056914883 0.0546809439
## 2015-03-31 0.0037403613 -0.0150861317 -0.0143885668 0.010156172 -0.0158299264
## 2015-04-30 -0.0032327530 0.0662812785 0.0358164078 -0.018417592 0.0097858681
## 2015-05-29 -0.0043838981 -0.0419108476 0.0019527879 0.007509793 0.0127741015
## 2015-06-30 -0.0108255216 -0.0297468695 -0.0316789376 0.004171368 -0.0205210334
## 2015-07-31 0.0085847588 -0.0651780382 0.0201145440 -0.027375188 0.0223377870
## 2015-08-31 -0.0033641785 -0.0925122097 -0.0771524325 -0.047268796 -0.0628864870
## 2015-09-30 0.0080819951 -0.0318251684 -0.0451949024 -0.038464606 -0.0258474315
## 2015-10-30 0.0006846744 0.0618082191 0.0640258586 0.063589975 0.0816351482
## 2015-11-30 -0.0038975743 -0.0255603149 -0.0075558350 0.024415019 0.0036483536
## 2015-12-31 -0.0019188513 -0.0389472337 -0.0235950054 -0.052156795 -0.0174336682
## 2016-01-29 0.0123296844 -0.0516366227 -0.0567579406 -0.060306669 -0.0510681610
## 2016-02-29 0.0088314978 -0.0082115780 -0.0339139816 0.020605165 -0.0008266308
## 2016-03-31 0.0087089570 0.1218792115 0.0637458404 0.089909950 0.0651001460
## 2016-04-29 0.0025454210 0.0040790654 0.0219752986 0.021044541 0.0039335264
## 2016-05-31 0.0001357972 -0.0376284066 -0.0008562143 0.004397079 0.0168685774
## 2016-06-30 0.0191673296 0.0445822655 -0.0244915306 0.008292316 0.0034698391
## 2016-07-29 0.0054296007 0.0524422156 0.0390003784 0.049348431 0.0358221585
## 2016-08-31 -0.0021566269 0.0087985240 0.0053268332 0.011260896 0.0011967448
## 2016-09-30 0.0005164151 0.0248730233 0.0132789220 0.008614708 0.0000578254
## 2016-10-31 -0.0082051834 -0.0083125450 -0.0224037110 -0.038134774 -0.0174889174
## 2016-11-30 -0.0259896093 -0.0451619654 -0.0179742272 0.125246300 0.0361758998
## 2016-12-30 0.0025375273 -0.0025296962 0.0267027148 0.031491668 0.0200691013
## 2017-01-31 0.0021265656 0.0644315291 0.0323818393 -0.012143675 0.0177364427
## 2017-02-28 0.0064376261 0.0172577381 0.0118362989 0.013428635 0.0385394000
## 2017-03-31 -0.0005526804 0.0361890304 0.0318060644 -0.006533238 0.0012490104
## 2017-04-28 0.0090291152 0.0168667164 0.0239520535 0.005108333 0.0098773354
## 2017-05-31 0.0068474921 0.0280596833 0.0348103786 -0.022863036 0.0140143215
## 2017-06-30 -0.0001824910 0.0092237863 0.0029557372 0.029151594 0.0063544967
## 2017-07-31 0.0033340878 0.0565946380 0.0261879043 0.007481600 0.0203459826
## 2017-08-31 0.0093689433 0.0232436405 -0.0004481261 -0.027564509 0.0029132389
## 2017-09-29 -0.0057319911 -0.0004463429 0.0233425171 0.082321671 0.0199491970
## 2017-10-31 0.0009781437 0.0322784770 0.0166538869 0.005916108 0.0232907405
## 2017-11-30 -0.0014845060 -0.0038970340 0.0068699458 0.036913465 0.0301082815
## 2017-12-29 0.0047403271 0.0369254424 0.0133983610 -0.003730947 0.0120548077
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, using pivot longer
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")
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, using pivot longer
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add Weight
add_column(weight = c(.25, .25, .2, .2, .1)) %>%
# Transforming to long table
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)