# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
library(slider)
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.0062306933 -0.0029358694 0.0366066095 0.052132998 0.0499232542
## 2013-02-28 0.0058910793 -0.0231048620 -0.0129695837 0.016175547 0.0126781486
## 2013-03-28 0.0009848136 -0.0102353842 0.0129695837 0.040258220 0.0372681094
## 2013-04-30 0.0096394909 0.0120851705 0.0489675941 0.001222307 0.0190300922
## 2013-05-31 -0.0202144656 -0.0494835130 -0.0306554876 0.041976942 0.0233351310
## 2013-06-28 -0.0157778057 -0.0547281611 -0.0271443068 -0.001403678 -0.0134346830
## 2013-07-31 0.0026875463 0.0131592991 0.0518600248 0.063541582 0.0503868169
## 2013-08-30 -0.0082981168 -0.0257052567 -0.0197464238 -0.034743748 -0.0304518815
## 2013-09-30 0.0111436763 0.0695886088 0.0753386345 0.063873957 0.0311559294
## 2013-10-31 0.0082921124 0.0408614074 0.0320816500 0.034233993 0.0452664745
## 2013-11-29 -0.0025101678 -0.0025941174 0.0054498019 0.041660811 0.0292073252
## 2013-12-31 -0.0055829836 -0.0040744653 0.0215279720 0.012892787 0.0255960519
## 2014-01-31 0.0152915239 -0.0903226988 -0.0534132612 -0.035775673 -0.0358846487
## 2014-02-28 0.0037573797 0.0332208071 0.0595047519 0.045257478 0.0445107778
## 2014-03-31 -0.0014822734 0.0380216475 -0.0046022416 0.013315181 0.0082606986
## 2014-04-30 0.0081840932 0.0077727303 0.0165291206 -0.023184103 0.0069279691
## 2014-05-30 0.0117211412 0.0290911608 0.0158288584 0.006205424 0.0229408785
## 2014-06-30 -0.0005751372 0.0237336819 0.0091651531 0.037718481 0.0204346435
## 2014-07-31 -0.0025122733 0.0135558798 -0.0263797926 -0.052009165 -0.0135287626
## 2014-08-29 0.0114303621 0.0279044768 0.0018004513 0.043657703 0.0387051852
## 2014-09-30 -0.0061667763 -0.0808565212 -0.0395983516 -0.061260399 -0.0138926359
## 2014-10-31 0.0105844451 0.0140960010 -0.0026549203 0.068874882 0.0232778307
## 2014-11-28 0.0065485505 -0.0155409341 0.0006251523 0.004773720 0.0271012967
## 2014-12-31 0.0014749376 -0.0404422691 -0.0407465889 0.025295591 -0.0025396912
## 2015-01-30 0.0203157856 -0.0068955836 0.0062265356 -0.054628106 -0.0300771843
## 2015-02-27 -0.0089892579 0.0431359536 0.0614505374 0.056914810 0.0546819455
## 2015-03-31 0.0037406483 -0.0150861327 -0.0143888162 0.010156480 -0.0158302780
## 2015-04-30 -0.0032330399 0.0662814538 0.0358166556 -0.018417590 0.0097858681
## 2015-05-29 -0.0043834211 -0.0419110809 0.0019526268 0.007509714 0.0127745381
## 2015-06-30 -0.0108260060 -0.0297467504 -0.0316789402 0.004171445 -0.0205213809
## 2015-07-31 0.0085849550 -0.0651779727 0.0201147084 -0.027375342 0.0223376107
## 2015-08-31 -0.0033636977 -0.0925124907 -0.0771526018 -0.047268546 -0.0628864927
## 2015-09-30 0.0080815156 -0.0318248787 -0.0451947225 -0.038464423 -0.0258468624
## 2015-10-30 0.0006853433 0.0618081449 0.0640259392 0.063589381 0.0816347598
## 2015-11-30 -0.0038981482 -0.0255604580 -0.0075558337 0.024415260 0.0036482658
## 2015-12-31 -0.0019187556 -0.0389468676 -0.0235950903 -0.052156874 -0.0174335792
## 2016-01-29 0.0123297804 -0.0516369240 -0.0567579354 -0.060306935 -0.0510687183
## 2016-02-29 0.0088313097 -0.0082114207 -0.0339138809 0.020605171 -0.0008258813
## 2016-03-31 0.0087089587 0.1218789229 0.0637455540 0.089910210 0.0650998648
## 2016-04-29 0.0025462586 0.0040794143 0.0219753006 0.021044308 0.0039336139
## 2016-05-31 0.0001352391 -0.0376284735 -0.0008561248 0.004397157 0.0168684039
## 2016-06-30 0.0191673278 0.0445822624 -0.0244916223 0.008292547 0.0034700109
## 2016-07-29 0.0054288743 0.0524422121 0.0390003819 0.049348573 0.0358219074
## 2016-08-31 -0.0021558098 0.0087985884 0.0053269214 0.011260965 0.0011968276
## 2016-09-30 0.0005165059 0.0248726397 0.0132790952 0.008614276 0.0000578254
## 2016-10-31 -0.0082054561 -0.0083122274 -0.0224037957 -0.038134631 -0.0174890029
## 2016-11-30 -0.0259901736 -0.0451616949 -0.0179743158 0.125246300 0.0361763111
## 2016-12-30 0.0025380916 -0.0025298965 0.0267026270 0.031491668 0.0200687787
## 2017-01-31 0.0021260975 0.0644315208 0.0323820971 -0.012143868 0.0177364440
## 2017-02-28 0.0064378151 0.0172577360 0.0118362129 0.013428765 0.0385394780
## 2017-03-31 -0.0005525875 0.0361888472 0.0318058176 -0.006532855 0.0012490854
## 2017-04-28 0.0090292091 0.0168663071 0.0239521368 0.005107250 0.0098772604
## 2017-05-31 0.0068471264 0.0280600382 0.0348103814 -0.022862143 0.0140141017
## 2017-06-30 -0.0001822162 0.0092236744 0.0029557374 0.029152095 0.0063545709
## 2017-07-31 0.0033346363 0.0565947572 0.0261879062 0.007481407 0.0203458428
## 2017-08-31 0.0093687589 0.0232437473 -0.0004482007 -0.027564883 0.0029135953
## 2017-09-29 -0.0057318081 -0.0004464472 0.0233428119 0.082321725 0.0199491956
## 2017-10-31 0.0009775980 0.0322787833 0.0166536696 0.005915754 0.0232905346
## 2017-11-30 -0.0014834139 -0.0038970332 0.0068699463 0.036913360 0.0301081534
## 2017-12-29 0.0047395074 0.0369253373 0.0133982918 -0.003730948 0.0120550720
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398471e-05 0.0001042077 4.178213e-05 -7.812278e-05 -9.032968e-06
## EEM 1.042077e-04 0.0017547086 1.039016e-03 6.437716e-04 6.795418e-04
## EFA 4.178213e-05 0.0010390162 1.064237e-03 6.490302e-04 6.975413e-04
## IJS -7.812278e-05 0.0006437716 6.490302e-04 1.565449e-03 8.290276e-04
## SPY -9.032968e-06 0.0006795418 6.975413e-04 8.290276e-04 7.408312e-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.0003874 0.009257135 0.005815637 0.005684464 0.002330252
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
# Create a custom function
component_contribution <- function(returns, weights) {
covariance_matrix <- cov(returns)
portfolio_sd <- sqrt(t(weights) %*% covariance_matrix %*% weights)
contribution <- (weights * (covariance_matrix %*% weights)) / portfolio_sd[1,1]
return(as.numeric(contribution))
}
# Calculate component contribution
component_contribution(asset_returns_wide_tbl, w)
## [1] 0.000387400 0.009257135 0.005815637 0.005684464 0.002330252
# Calculate component contribution percentages
component_percentages <- component_contribution(asset_returns_wide_tbl, w) / sd_portfolio[1,1]
# Create a data frame
component_tbl <- tibble(
asset = symbols,
contribution = component_percentages
)
component_tbl
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 SPY 0.0165
## 2 EFA 0.394
## 3 IJS 0.248
## 4 EEM 0.242
## 5 AGG 0.0993
# Create a bar chart
ggplot(component_tbl, aes(x = asset, y = contribution)) +
geom_col() +
labs(
title = "Component Contribution to Portfolio Risk",
x = "Asset",
y = "Contribution (%)"
) +
scale_y_continuous(labels = scales::percent) +
theme_minimal()
# Calculate rolling component contribution
rolling_component_contribution <- asset_returns_wide_tbl %>%
as_tibble(rownames = "date") %>%
mutate(date = as.Date(date)) %>%
arrange(date) %>%
mutate(
rolling_contribution = slide(
.x = row_number(),
.f = function(index) {
returns <- asset_returns_wide_tbl[index, ]
contribution <- component_contribution(returns, w)
as.numeric(contribution)
},
.before = 23,
.complete = TRUE
)
)
rolling_component_contribution
## # A tibble: 60 × 7
## date AGG EEM EFA IJS SPY rolling_contribution
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
## 1 2013-01-31 -0.00623 -0.00294 0.0366 0.0521 0.0499 <NULL>
## 2 2013-02-28 0.00589 -0.0231 -0.0130 0.0162 0.0127 <NULL>
## 3 2013-03-28 0.000985 -0.0102 0.0130 0.0403 0.0373 <NULL>
## 4 2013-04-30 0.00964 0.0121 0.0490 0.00122 0.0190 <NULL>
## 5 2013-05-31 -0.0202 -0.0495 -0.0307 0.0420 0.0233 <NULL>
## 6 2013-06-28 -0.0158 -0.0547 -0.0271 -0.00140 -0.0134 <NULL>
## 7 2013-07-31 0.00269 0.0132 0.0519 0.0635 0.0504 <NULL>
## 8 2013-08-30 -0.00830 -0.0257 -0.0197 -0.0347 -0.0305 <NULL>
## 9 2013-09-30 0.0111 0.0696 0.0753 0.0639 0.0312 <NULL>
## 10 2013-10-31 0.00829 0.0409 0.0321 0.0342 0.0453 <NULL>
## # ℹ 50 more rows