# 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.0062312606 -0.002935486 0.0366060287 0.052132817 4.992308e-02
## 2013-02-28 0.0058915318 -0.023105002 -0.0129691922 0.016175755 1.267794e-02
## 2013-03-28 0.0009843653 -0.010235113 0.0129691922 0.040257696 3.726806e-02
## 2013-04-30 0.0096393343 0.012084777 0.0489678978 0.001222316 1.903036e-02
## 2013-05-31 -0.0202143774 -0.049483463 -0.0306554981 0.041976504 2.333503e-02
## 2013-06-28 -0.0157778893 -0.054728215 -0.0271445255 -0.001402802 -1.343435e-02
## 2013-07-31 0.0026877619 0.013159669 0.0518601219 0.063541488 5.038618e-02
## 2013-08-30 -0.0082982137 -0.025705836 -0.0197461523 -0.034743163 -3.045175e-02
## 2013-09-30 0.0111437645 0.069588968 0.0753385194 0.063873096 3.115662e-02
## 2013-10-31 0.0082921828 0.040861180 0.0320815751 0.034234090 4.526623e-02
## 2013-11-29 -0.0025104504 -0.002594076 0.0054497637 0.041661216 2.920710e-02
## 2013-12-31 -0.0055825383 -0.004074240 0.0215280075 0.012892027 2.559588e-02
## 2014-01-31 0.0152916098 -0.090322665 -0.0534133526 -0.035775520 -3.588485e-02
## 2014-02-28 0.0037568570 0.033220362 0.0595053257 0.045257298 4.451080e-02
## 2014-03-31 -0.0014808518 0.038021947 -0.0046027316 0.013315796 8.261759e-03
## 2014-04-30 0.0081822763 0.007772733 0.0165293425 -0.023184513 6.927276e-03
## 2014-05-30 0.0117216729 0.029091077 0.0158283663 0.006205381 2.294102e-02
## 2014-06-30 -0.0005757126 0.023733945 0.0091654929 0.037718651 2.043473e-02
## 2014-07-31 -0.0025123242 0.013555689 -0.0263798899 -0.052009275 -1.352870e-02
## 2014-08-29 0.0114310695 0.027904608 0.0018005552 0.043657707 3.870464e-02
## 2014-09-30 -0.0061676354 -0.080856897 -0.0395985366 -0.061260360 -1.389239e-02
## 2014-10-31 0.0105853559 0.014096575 -0.0026547698 0.068874813 2.327787e-02
## 2014-11-28 0.0065483191 -0.015541210 0.0006251658 0.004773682 2.710134e-02
## 2014-12-31 0.0014750290 -0.040442057 -0.0407466475 0.025295765 -2.539905e-03
## 2015-01-30 0.0203153204 -0.006895881 0.0062263662 -0.054627793 -3.007705e-02
## 2015-02-27 -0.0089886101 0.043136147 0.0614506727 0.056914449 5.468211e-02
## 2015-03-31 0.0037399307 -0.015086123 -0.0143885516 0.010156502 -1.583053e-02
## 2015-04-30 -0.0032321232 0.066281292 0.0358164176 -0.018417523 9.786001e-03
## 2015-05-29 -0.0043839964 -0.041910922 0.0019525558 0.007509826 1.277413e-02
## 2015-06-30 -0.0108251405 -0.029746702 -0.0316786040 0.004171139 -2.052125e-02
## 2015-07-31 0.0085838110 -0.065178078 0.0201144923 -0.027375419 2.233809e-02
## 2015-08-31 -0.0033631564 -0.092512532 -0.0771526622 -0.047268276 -6.288675e-02
## 2015-09-30 0.0080816088 -0.031824888 -0.0451947606 -0.038464412 -2.584703e-02
## 2015-10-30 0.0006852472 0.061808142 0.0640258227 0.063589561 8.163484e-02
## 2015-11-30 -0.0038980795 -0.025560058 -0.0075558963 0.024414982 3.648621e-03
## 2015-12-31 -0.0019187584 -0.038947274 -0.0235949679 -0.052157019 -1.743352e-02
## 2016-01-29 0.0123297356 -0.051636620 -0.0567578828 -0.060306535 -5.106844e-02
## 2016-02-29 0.0088319078 -0.008211691 -0.0339136456 0.020605156 -8.264550e-04
## 2016-03-31 0.0087081563 0.121879165 0.0637455679 0.089910076 6.509956e-02
## 2016-04-29 0.0025462211 0.004079036 0.0219750163 0.021044203 3.933884e-03
## 2016-05-31 0.0001360336 -0.037628322 -0.0008561716 0.004397318 1.686868e-02
## 2016-06-30 0.0191666282 0.044582079 -0.0244914407 0.008292392 3.469825e-03
## 2016-07-29 0.0054298918 0.052442366 0.0390002419 0.049348406 3.582173e-02
## 2016-08-31 -0.0021571298 0.008798534 0.0053269298 0.011260876 1.197008e-03
## 2016-09-30 0.0005165109 0.024872917 0.0132789949 0.008614633 5.810952e-05
## 2016-10-31 -0.0082052694 -0.008312139 -0.0224035823 -0.038134799 -1.748938e-02
## 2016-11-30 -0.0259895177 -0.045162143 -0.0179744003 0.125246356 3.617608e-02
## 2016-12-30 0.0025381141 -0.002529786 0.0267027305 0.031491749 2.006908e-02
## 2017-01-31 0.0021259590 0.064431479 0.0323820788 -0.012144064 1.773648e-02
## 2017-02-28 0.0064378127 0.017257775 0.0118364304 0.013428739 3.853923e-02
## 2017-03-31 -0.0005531133 0.036188800 0.0318058258 -0.006532617 1.249445e-03
## 2017-04-28 0.0090293551 0.016866527 0.0239521701 0.005108050 9.877170e-03
## 2017-05-31 0.0068481995 0.028060014 0.0348102132 -0.022862890 1.401439e-02
## 2017-06-30 -0.0001836687 0.009223882 0.0029557035 0.029151647 6.354442e-03
## 2017-07-31 0.0033343616 0.056594313 0.0261878819 0.007481170 2.034570e-02
## 2017-08-31 0.0093696189 0.023243921 -0.0004482871 -0.027564338 2.913594e-03
## 2017-09-29 -0.0057322731 -0.000446055 0.0233428128 0.082321706 1.994900e-02
## 2017-10-31 0.0009780167 0.032278293 0.0166538599 0.005915824 2.329086e-02
## 2017-11-30 -0.0014842837 -0.003897118 0.0068698920 0.036913551 3.010797e-02
## 2017-12-29 0.0047411006 0.036925615 0.0133981388 -0.003731056 1.205493e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398398e-05 0.0001042106 4.178069e-05 -7.812158e-05 -9.033210e-06
## EEM 1.042106e-04 0.0017547112 1.039017e-03 6.437719e-04 6.795438e-04
## EFA 4.178069e-05 0.0010390173 1.064236e-03 6.490270e-04 6.975422e-04
## IJS -7.812158e-05 0.0006437719 6.490270e-04 1.565443e-03 8.290227e-04
## SPY -9.033210e-06 0.0006795438 6.975422e-04 8.290227e-04 7.408293e-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.02347489
# 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.0003874048 0.009257155 0.005815631 0.005684448 0.00233025
rowSums(component_contribution)
## [1] 0.02347489
# 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.0062312606 -0.002935486 0.0366060287 0.052132817 4.992308e-02
## 2013-02-28 0.0058915318 -0.023105002 -0.0129691922 0.016175755 1.267794e-02
## 2013-03-28 0.0009843653 -0.010235113 0.0129691922 0.040257696 3.726806e-02
## 2013-04-30 0.0096393343 0.012084777 0.0489678978 0.001222316 1.903036e-02
## 2013-05-31 -0.0202143774 -0.049483463 -0.0306554981 0.041976504 2.333503e-02
## 2013-06-28 -0.0157778893 -0.054728215 -0.0271445255 -0.001402802 -1.343435e-02
## 2013-07-31 0.0026877619 0.013159669 0.0518601219 0.063541488 5.038618e-02
## 2013-08-30 -0.0082982137 -0.025705836 -0.0197461523 -0.034743163 -3.045175e-02
## 2013-09-30 0.0111437645 0.069588968 0.0753385194 0.063873096 3.115662e-02
## 2013-10-31 0.0082921828 0.040861180 0.0320815751 0.034234090 4.526623e-02
## 2013-11-29 -0.0025104504 -0.002594076 0.0054497637 0.041661216 2.920710e-02
## 2013-12-31 -0.0055825383 -0.004074240 0.0215280075 0.012892027 2.559588e-02
## 2014-01-31 0.0152916098 -0.090322665 -0.0534133526 -0.035775520 -3.588485e-02
## 2014-02-28 0.0037568570 0.033220362 0.0595053257 0.045257298 4.451080e-02
## 2014-03-31 -0.0014808518 0.038021947 -0.0046027316 0.013315796 8.261759e-03
## 2014-04-30 0.0081822763 0.007772733 0.0165293425 -0.023184513 6.927276e-03
## 2014-05-30 0.0117216729 0.029091077 0.0158283663 0.006205381 2.294102e-02
## 2014-06-30 -0.0005757126 0.023733945 0.0091654929 0.037718651 2.043473e-02
## 2014-07-31 -0.0025123242 0.013555689 -0.0263798899 -0.052009275 -1.352870e-02
## 2014-08-29 0.0114310695 0.027904608 0.0018005552 0.043657707 3.870464e-02
## 2014-09-30 -0.0061676354 -0.080856897 -0.0395985366 -0.061260360 -1.389239e-02
## 2014-10-31 0.0105853559 0.014096575 -0.0026547698 0.068874813 2.327787e-02
## 2014-11-28 0.0065483191 -0.015541210 0.0006251658 0.004773682 2.710134e-02
## 2014-12-31 0.0014750290 -0.040442057 -0.0407466475 0.025295765 -2.539905e-03
## 2015-01-30 0.0203153204 -0.006895881 0.0062263662 -0.054627793 -3.007705e-02
## 2015-02-27 -0.0089886101 0.043136147 0.0614506727 0.056914449 5.468211e-02
## 2015-03-31 0.0037399307 -0.015086123 -0.0143885516 0.010156502 -1.583053e-02
## 2015-04-30 -0.0032321232 0.066281292 0.0358164176 -0.018417523 9.786001e-03
## 2015-05-29 -0.0043839964 -0.041910922 0.0019525558 0.007509826 1.277413e-02
## 2015-06-30 -0.0108251405 -0.029746702 -0.0316786040 0.004171139 -2.052125e-02
## 2015-07-31 0.0085838110 -0.065178078 0.0201144923 -0.027375419 2.233809e-02
## 2015-08-31 -0.0033631564 -0.092512532 -0.0771526622 -0.047268276 -6.288675e-02
## 2015-09-30 0.0080816088 -0.031824888 -0.0451947606 -0.038464412 -2.584703e-02
## 2015-10-30 0.0006852472 0.061808142 0.0640258227 0.063589561 8.163484e-02
## 2015-11-30 -0.0038980795 -0.025560058 -0.0075558963 0.024414982 3.648621e-03
## 2015-12-31 -0.0019187584 -0.038947274 -0.0235949679 -0.052157019 -1.743352e-02
## 2016-01-29 0.0123297356 -0.051636620 -0.0567578828 -0.060306535 -5.106844e-02
## 2016-02-29 0.0088319078 -0.008211691 -0.0339136456 0.020605156 -8.264550e-04
## 2016-03-31 0.0087081563 0.121879165 0.0637455679 0.089910076 6.509956e-02
## 2016-04-29 0.0025462211 0.004079036 0.0219750163 0.021044203 3.933884e-03
## 2016-05-31 0.0001360336 -0.037628322 -0.0008561716 0.004397318 1.686868e-02
## 2016-06-30 0.0191666282 0.044582079 -0.0244914407 0.008292392 3.469825e-03
## 2016-07-29 0.0054298918 0.052442366 0.0390002419 0.049348406 3.582173e-02
## 2016-08-31 -0.0021571298 0.008798534 0.0053269298 0.011260876 1.197008e-03
## 2016-09-30 0.0005165109 0.024872917 0.0132789949 0.008614633 5.810952e-05
## 2016-10-31 -0.0082052694 -0.008312139 -0.0224035823 -0.038134799 -1.748938e-02
## 2016-11-30 -0.0259895177 -0.045162143 -0.0179744003 0.125246356 3.617608e-02
## 2016-12-30 0.0025381141 -0.002529786 0.0267027305 0.031491749 2.006908e-02
## 2017-01-31 0.0021259590 0.064431479 0.0323820788 -0.012144064 1.773648e-02
## 2017-02-28 0.0064378127 0.017257775 0.0118364304 0.013428739 3.853923e-02
## 2017-03-31 -0.0005531133 0.036188800 0.0318058258 -0.006532617 1.249445e-03
## 2017-04-28 0.0090293551 0.016866527 0.0239521701 0.005108050 9.877170e-03
## 2017-05-31 0.0068481995 0.028060014 0.0348102132 -0.022862890 1.401439e-02
## 2017-06-30 -0.0001836687 0.009223882 0.0029557035 0.029151647 6.354442e-03
## 2017-07-31 0.0033343616 0.056594313 0.0261878819 0.007481170 2.034570e-02
## 2017-08-31 0.0093696189 0.023243921 -0.0004482871 -0.027564338 2.913594e-03
## 2017-09-29 -0.0057322731 -0.000446055 0.0233428128 0.082321706 1.994900e-02
## 2017-10-31 0.0009780167 0.032278293 0.0166538599 0.005915824 2.329086e-02
## 2017-11-30 -0.0014842837 -0.003897118 0.0068698920 0.036913551 3.010797e-02
## 2017-12-29 0.0047411006 0.036925615 0.0133981388 -0.003731056 1.205493e-02
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
# 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
# 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 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
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)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio Volatility and Weight",
y = "Percent",
x = NULL)