# 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.0062313944 -0.0029356431 0.0366062975 0.052133618 4.992319e-02
## 2013-02-28 0.0058911870 -0.0231048248 -0.0129692976 0.016175211 1.267793e-02
## 2013-03-28 0.0009849471 -0.0102351212 0.0129692976 0.040258000 3.726828e-02
## 2013-04-30 0.0096389084 0.0120845930 0.0489677005 0.001222503 1.902936e-02
## 2013-05-31 -0.0202134078 -0.0494833224 -0.0306556296 0.041976249 2.333585e-02
## 2013-06-28 -0.0157786236 -0.0547285436 -0.0271442109 -0.001402777 -1.343443e-02
## 2013-07-31 0.0026878088 0.0131599496 0.0518601076 0.063541140 5.038618e-02
## 2013-08-30 -0.0082977748 -0.0257058570 -0.0197463931 -0.034743254 -3.045162e-02
## 2013-09-30 0.0111435496 0.0695888451 0.0753386814 0.063873551 3.115624e-02
## 2013-10-31 0.0082922064 0.0408615063 0.0320816635 0.034234141 4.526628e-02
## 2013-11-29 -0.0025097964 -0.0025941817 0.0054497179 0.041660922 2.920724e-02
## 2013-12-31 -0.0055831705 -0.0040740470 0.0215279453 0.012892142 2.559615e-02
## 2014-01-31 0.0152915793 -0.0903230688 -0.0534132453 -0.035775084 -3.588439e-02
## 2014-02-28 0.0037569381 0.0332205445 0.0595049838 0.045257141 4.451018e-02
## 2014-03-31 -0.0014815112 0.0380219517 -0.0046026549 0.013315306 8.261085e-03
## 2014-04-30 0.0081835763 0.0077726504 0.0165296121 -0.023184109 6.927694e-03
## 2014-05-30 0.0117216242 0.0290911140 0.0158283807 0.006205324 2.294127e-02
## 2014-06-30 -0.0005762010 0.0237337198 0.0091652403 0.037718535 2.043424e-02
## 2014-07-31 -0.0025119009 0.0135558451 -0.0263796025 -0.052009176 -1.352831e-02
## 2014-08-29 0.0114306548 0.0279046178 0.0018002804 0.043657851 3.870440e-02
## 2014-09-30 -0.0061675253 -0.0808568044 -0.0395984072 -0.061260606 -1.389212e-02
## 2014-10-31 0.0105845187 0.0140967334 -0.0026548858 0.068874860 2.327798e-02
## 2014-11-28 0.0065489038 -0.0155415682 0.0006253854 0.004773791 2.710153e-02
## 2014-12-31 0.0014750330 -0.0404420637 -0.0407467496 0.025295706 -2.540074e-03
## 2015-01-30 0.0203154369 -0.0068954611 0.0062265697 -0.054627960 -3.007711e-02
## 2015-02-27 -0.0089888415 0.0431356829 0.0614505865 0.056914600 5.468226e-02
## 2015-03-31 0.0037407160 -0.0150860192 -0.0143887489 0.010156537 -1.583061e-02
## 2015-04-30 -0.0032330575 0.0662813930 0.0358164419 -0.018417868 9.786065e-03
## 2015-05-29 -0.0043835034 -0.0419110539 0.0019527690 0.007510071 1.277406e-02
## 2015-06-30 -0.0108258315 -0.0297465802 -0.0316789046 0.004171341 -2.052132e-02
## 2015-07-31 0.0085850533 -0.0651783352 0.0201144652 -0.027375325 2.233802e-02
## 2015-08-31 -0.0033641788 -0.0925122172 -0.0771522768 -0.047268437 -6.288658e-02
## 2015-09-30 0.0080815251 -0.0318248759 -0.0451947314 -0.038464775 -2.584735e-02
## 2015-10-30 0.0006856085 0.0618083050 0.0640258955 0.063589825 8.163495e-02
## 2015-11-30 -0.0038982457 -0.0255604844 -0.0075561459 0.024415107 3.648548e-03
## 2015-12-31 -0.0019189840 -0.0389471705 -0.0235949565 -0.052157131 -1.743354e-02
## 2016-01-29 0.0123302026 -0.0516366428 -0.0567577631 -0.060307008 -5.106879e-02
## 2016-02-29 0.0088309883 -0.0082117307 -0.0339139210 0.020605333 -8.262158e-04
## 2016-03-31 0.0087089964 0.1218791540 0.0637454672 0.089910583 6.510001e-02
## 2016-04-29 0.0025465661 0.0040791679 0.0219752130 0.021044174 3.933711e-03
## 2016-05-31 0.0001354430 -0.0376286552 -0.0008559818 0.004397249 1.686853e-02
## 2016-06-30 0.0191665192 0.0445826519 -0.0244915330 0.008292066 3.469921e-03
## 2016-07-29 0.0054298247 0.0524418925 0.0390001700 0.049348448 3.582194e-02
## 2016-08-31 -0.0021562179 0.0087987151 0.0053268678 0.011260928 1.196610e-03
## 2016-09-30 0.0005161556 0.0248729131 0.0132791638 0.008614752 5.780091e-05
## 2016-10-31 -0.0082053062 -0.0083123579 -0.0224036234 -0.038134925 -1.748878e-02
## 2016-11-30 -0.0259898848 -0.0451617456 -0.0179745854 0.125246364 3.617599e-02
## 2016-12-30 0.0025378768 -0.0025300066 0.0267031167 0.031491978 2.006913e-02
## 2017-01-31 0.0021264319 0.0644316425 0.0323817564 -0.012143875 1.773659e-02
## 2017-02-28 0.0064378729 0.0172576947 0.0118363664 0.013428690 3.853902e-02
## 2017-03-31 -0.0005528926 0.0361890439 0.0318056821 -0.006532922 1.249175e-03
## 2017-04-28 0.0090292593 0.0168662675 0.0239523044 0.005107813 9.877366e-03
## 2017-05-31 0.0068472204 0.0280600796 0.0348101417 -0.022862475 1.401406e-02
## 2017-06-30 -0.0001826264 0.0092237832 0.0029559199 0.029151400 6.354788e-03
## 2017-07-31 0.0033344101 0.0565943473 0.0261879949 0.007481802 2.034578e-02
## 2017-08-31 0.0093693928 0.0232439220 -0.0004485071 -0.027564953 2.913602e-03
## 2017-09-29 -0.0057322330 -0.0004462966 0.0233429293 0.082321537 1.994902e-02
## 2017-10-31 0.0009779679 0.0322784443 0.0166537083 0.005916432 2.329060e-02
## 2017-11-30 -0.0014840876 -0.0038970090 0.0068698934 0.036913188 3.010813e-02
## 2017-12-29 0.0047402631 0.0369254493 0.0133984009 -0.003731035 1.205515e-02
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398437e-05 0.0001042103 4.178261e-05 -7.812152e-05 -9.031834e-06
## EEM 1.042103e-04 0.0017547132 1.039015e-03 6.437741e-04 6.795419e-04
## EFA 4.178261e-05 0.0010390150 1.064235e-03 6.490286e-04 6.975393e-04
## IJS -7.812152e-05 0.0006437741 6.490286e-04 1.565448e-03 8.290249e-04
## SPY -9.031834e-06 0.0006795419 6.975393e-04 8.290249e-04 7.408294e-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.0003874105 0.009257152 0.005815626 0.005684463 0.002330248
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
# 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.0062313944 -0.0029356431 0.0366062975 0.052133618 4.992319e-02
## 2013-02-28 0.0058911870 -0.0231048248 -0.0129692976 0.016175211 1.267793e-02
## 2013-03-28 0.0009849471 -0.0102351212 0.0129692976 0.040258000 3.726828e-02
## 2013-04-30 0.0096389084 0.0120845930 0.0489677005 0.001222503 1.902936e-02
## 2013-05-31 -0.0202134078 -0.0494833224 -0.0306556296 0.041976249 2.333585e-02
## 2013-06-28 -0.0157786236 -0.0547285436 -0.0271442109 -0.001402777 -1.343443e-02
## 2013-07-31 0.0026878088 0.0131599496 0.0518601076 0.063541140 5.038618e-02
## 2013-08-30 -0.0082977748 -0.0257058570 -0.0197463931 -0.034743254 -3.045162e-02
## 2013-09-30 0.0111435496 0.0695888451 0.0753386814 0.063873551 3.115624e-02
## 2013-10-31 0.0082922064 0.0408615063 0.0320816635 0.034234141 4.526628e-02
## 2013-11-29 -0.0025097964 -0.0025941817 0.0054497179 0.041660922 2.920724e-02
## 2013-12-31 -0.0055831705 -0.0040740470 0.0215279453 0.012892142 2.559615e-02
## 2014-01-31 0.0152915793 -0.0903230688 -0.0534132453 -0.035775084 -3.588439e-02
## 2014-02-28 0.0037569381 0.0332205445 0.0595049838 0.045257141 4.451018e-02
## 2014-03-31 -0.0014815112 0.0380219517 -0.0046026549 0.013315306 8.261085e-03
## 2014-04-30 0.0081835763 0.0077726504 0.0165296121 -0.023184109 6.927694e-03
## 2014-05-30 0.0117216242 0.0290911140 0.0158283807 0.006205324 2.294127e-02
## 2014-06-30 -0.0005762010 0.0237337198 0.0091652403 0.037718535 2.043424e-02
## 2014-07-31 -0.0025119009 0.0135558451 -0.0263796025 -0.052009176 -1.352831e-02
## 2014-08-29 0.0114306548 0.0279046178 0.0018002804 0.043657851 3.870440e-02
## 2014-09-30 -0.0061675253 -0.0808568044 -0.0395984072 -0.061260606 -1.389212e-02
## 2014-10-31 0.0105845187 0.0140967334 -0.0026548858 0.068874860 2.327798e-02
## 2014-11-28 0.0065489038 -0.0155415682 0.0006253854 0.004773791 2.710153e-02
## 2014-12-31 0.0014750330 -0.0404420637 -0.0407467496 0.025295706 -2.540074e-03
## 2015-01-30 0.0203154369 -0.0068954611 0.0062265697 -0.054627960 -3.007711e-02
## 2015-02-27 -0.0089888415 0.0431356829 0.0614505865 0.056914600 5.468226e-02
## 2015-03-31 0.0037407160 -0.0150860192 -0.0143887489 0.010156537 -1.583061e-02
## 2015-04-30 -0.0032330575 0.0662813930 0.0358164419 -0.018417868 9.786065e-03
## 2015-05-29 -0.0043835034 -0.0419110539 0.0019527690 0.007510071 1.277406e-02
## 2015-06-30 -0.0108258315 -0.0297465802 -0.0316789046 0.004171341 -2.052132e-02
## 2015-07-31 0.0085850533 -0.0651783352 0.0201144652 -0.027375325 2.233802e-02
## 2015-08-31 -0.0033641788 -0.0925122172 -0.0771522768 -0.047268437 -6.288658e-02
## 2015-09-30 0.0080815251 -0.0318248759 -0.0451947314 -0.038464775 -2.584735e-02
## 2015-10-30 0.0006856085 0.0618083050 0.0640258955 0.063589825 8.163495e-02
## 2015-11-30 -0.0038982457 -0.0255604844 -0.0075561459 0.024415107 3.648548e-03
## 2015-12-31 -0.0019189840 -0.0389471705 -0.0235949565 -0.052157131 -1.743354e-02
## 2016-01-29 0.0123302026 -0.0516366428 -0.0567577631 -0.060307008 -5.106879e-02
## 2016-02-29 0.0088309883 -0.0082117307 -0.0339139210 0.020605333 -8.262158e-04
## 2016-03-31 0.0087089964 0.1218791540 0.0637454672 0.089910583 6.510001e-02
## 2016-04-29 0.0025465661 0.0040791679 0.0219752130 0.021044174 3.933711e-03
## 2016-05-31 0.0001354430 -0.0376286552 -0.0008559818 0.004397249 1.686853e-02
## 2016-06-30 0.0191665192 0.0445826519 -0.0244915330 0.008292066 3.469921e-03
## 2016-07-29 0.0054298247 0.0524418925 0.0390001700 0.049348448 3.582194e-02
## 2016-08-31 -0.0021562179 0.0087987151 0.0053268678 0.011260928 1.196610e-03
## 2016-09-30 0.0005161556 0.0248729131 0.0132791638 0.008614752 5.780091e-05
## 2016-10-31 -0.0082053062 -0.0083123579 -0.0224036234 -0.038134925 -1.748878e-02
## 2016-11-30 -0.0259898848 -0.0451617456 -0.0179745854 0.125246364 3.617599e-02
## 2016-12-30 0.0025378768 -0.0025300066 0.0267031167 0.031491978 2.006913e-02
## 2017-01-31 0.0021264319 0.0644316425 0.0323817564 -0.012143875 1.773659e-02
## 2017-02-28 0.0064378729 0.0172576947 0.0118363664 0.013428690 3.853902e-02
## 2017-03-31 -0.0005528926 0.0361890439 0.0318056821 -0.006532922 1.249175e-03
## 2017-04-28 0.0090292593 0.0168662675 0.0239523044 0.005107813 9.877366e-03
## 2017-05-31 0.0068472204 0.0280600796 0.0348101417 -0.022862475 1.401406e-02
## 2017-06-30 -0.0001826264 0.0092237832 0.0029559199 0.029151400 6.354788e-03
## 2017-07-31 0.0033344101 0.0565943473 0.0261879949 0.007481802 2.034578e-02
## 2017-08-31 0.0093693928 0.0232439220 -0.0004485071 -0.027564953 2.913602e-03
## 2017-09-29 -0.0057322330 -0.0004462966 0.0233429293 0.082321537 1.994902e-02
## 2017-10-31 0.0009779679 0.0322784443 0.0166537083 0.005916432 2.329060e-02
## 2017-11-30 -0.0014840876 -0.0038970090 0.0068698934 0.036913188 3.010813e-02
## 2017-12-29 0.0047402631 0.0369254493 0.0133984009 -0.003731035 1.205515e-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(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
Column Chart of Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.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(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(0.25, 0.25, 0.2, 0.2, 0.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)