# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibble)
# 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("LULU", "NFLX", "TSLA")
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
## LULU NFLX TSLA
## 2013-01-31 -0.099648621 0.579217838 0.102078031
## 2013-02-28 -0.028667850 0.129468358 -0.074128613
## 2013-03-28 -0.072675014 0.006360086 0.084208141
## 2013-04-30 0.199678726 0.132375029 0.354111527
## 2013-05-31 0.021827566 0.046038160 0.593716693
## 2013-06-28 -0.172219789 -0.069356053 0.093672182
## 2013-07-31 0.060283297 0.146848896 0.223739545
## 2013-08-30 0.018090317 0.149523807 0.229971572
## 2013-09-30 0.031814882 0.085363333 0.134706682
## 2013-10-31 -0.056828694 0.042020457 -0.189806650
## 2013-11-29 0.009077290 0.126045634 -0.228409431
## 2013-12-31 -0.166441463 0.006458101 0.167108548
## 2014-01-31 -0.256166339 0.105976904 0.187261770
## 2014-02-28 0.096324451 0.084967328 0.299722757
## 2014-03-31 0.044322103 -0.235772615 -0.160783192
## 2014-04-30 -0.135407489 -0.089040664 -0.002690159
## 2014-05-30 -0.028712197 0.260398879 -0.000577395
## 2014-06-30 -0.097598276 0.053062782 0.144457218
## 2014-07-31 -0.050929282 -0.041427369 -0.072372711
## 2014-08-29 0.037249178 0.122147224 0.188794049
## 2014-09-30 0.050779716 -0.056990909 -0.105566506
## 2014-10-31 -0.008606221 -0.138642145 -0.004046457
## 2014-11-28 0.145850093 -0.125081715 0.011599756
## 2014-12-31 0.146443150 -0.014472737 -0.094774520
## 2015-01-30 0.171689820 0.257187515 -0.088365243
## 2015-02-27 0.032672998 0.072268007 -0.001277805
## 2015-03-31 -0.066761991 -0.130782761 -0.074350086
## 2015-04-30 -0.005953300 0.289324645 0.180226844
## 2015-05-29 -0.062403748 0.114579340 0.103899535
## 2015-06-30 0.088153643 0.051346185 0.067300966
## 2015-07-31 -0.038082035 0.197231491 -0.007896618
## 2015-08-31 0.018129305 0.006278917 -0.066366266
## 2015-09-30 -0.234100082 -0.107942856 -0.002653542
## 2015-10-30 -0.029655629 0.048393444 -0.182659742
## 2015-11-30 -0.027839679 0.129220163 0.106828579
## 2015-12-31 0.092797635 -0.075337494 0.041471546
## 2016-01-29 0.168021184 -0.219478323 -0.227360646
## 2016-02-29 0.010577022 0.016950560 0.003810669
## 2016-03-31 0.076394063 0.090226757 0.179948112
## 2016-04-29 -0.032420609 -0.127082269 0.046721799
## 2016-05-31 -0.007964569 0.130402555 -0.075597977
## 2016-06-30 0.127322736 -0.114425090 -0.050296472
## 2016-07-29 0.050040152 -0.002517409 0.100785361
## 2016-08-31 -0.014790098 0.065736403 -0.102058076
## 2016-09-30 -0.226875536 0.011224669 -0.038366402
## 2016-10-31 -0.063118299 0.236709156 -0.031364578
## 2016-11-30 -0.004551793 -0.065099285 -0.043041257
## 2016-12-30 0.131357532 0.056493450 0.120665160
## 2017-01-31 0.038042384 0.128033700 0.164624944
## 2017-02-28 -0.033896443 0.010041082 -0.007730394
## 2017-03-31 -0.229638753 0.039185485 0.107278734
## 2017-04-28 0.002503149 0.029267774 0.120916240
## 2017-05-31 -0.074433469 0.068984174 0.082295867
## 2017-06-30 0.212019098 -0.087485368 0.058654469
## 2017-07-31 0.032481646 0.195442598 -0.111459838
## 2017-08-31 -0.068656876 -0.039009334 0.095543417
## 2017-09-29 0.078504418 0.037301402 -0.042474117
## 2017-10-31 -0.011958805 0.079877199 -0.028457431
## 2017-11-30 0.084895681 -0.046100666 -0.070862536
## 2017-12-29 0.160149003 0.023081622 0.008061927
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## LULU NFLX TSLA
## LULU 0.0113251360 -0.001187046 -0.0004300193
## NFLX -0.0011870458 0.017786495 0.0049890795
## TSLA -0.0004300193 0.004989080 0.0209573545
# 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.35, 0.35, 0.3)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.07822348
# 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
## LULU NFLX TSLA
## [1,] 0.01529929 0.03269205 0.03023214
rowSums(component_contribution)
## [1] 0.07822348
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 3
## LULU NFLX TSLA
## <dbl> <dbl> <dbl>
## 1 0.196 0.418 0.386
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
## asset contribution
## <chr> <dbl>
## 1 LULU 0.196
## 2 NFLX 0.418
## 3 TSLA 0.386
# 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
## LULU NFLX TSLA
## 2013-01-31 -0.099648621 0.579217838 0.102078031
## 2013-02-28 -0.028667850 0.129468358 -0.074128613
## 2013-03-28 -0.072675014 0.006360086 0.084208141
## 2013-04-30 0.199678726 0.132375029 0.354111527
## 2013-05-31 0.021827566 0.046038160 0.593716693
## 2013-06-28 -0.172219789 -0.069356053 0.093672182
## 2013-07-31 0.060283297 0.146848896 0.223739545
## 2013-08-30 0.018090317 0.149523807 0.229971572
## 2013-09-30 0.031814882 0.085363333 0.134706682
## 2013-10-31 -0.056828694 0.042020457 -0.189806650
## 2013-11-29 0.009077290 0.126045634 -0.228409431
## 2013-12-31 -0.166441463 0.006458101 0.167108548
## 2014-01-31 -0.256166339 0.105976904 0.187261770
## 2014-02-28 0.096324451 0.084967328 0.299722757
## 2014-03-31 0.044322103 -0.235772615 -0.160783192
## 2014-04-30 -0.135407489 -0.089040664 -0.002690159
## 2014-05-30 -0.028712197 0.260398879 -0.000577395
## 2014-06-30 -0.097598276 0.053062782 0.144457218
## 2014-07-31 -0.050929282 -0.041427369 -0.072372711
## 2014-08-29 0.037249178 0.122147224 0.188794049
## 2014-09-30 0.050779716 -0.056990909 -0.105566506
## 2014-10-31 -0.008606221 -0.138642145 -0.004046457
## 2014-11-28 0.145850093 -0.125081715 0.011599756
## 2014-12-31 0.146443150 -0.014472737 -0.094774520
## 2015-01-30 0.171689820 0.257187515 -0.088365243
## 2015-02-27 0.032672998 0.072268007 -0.001277805
## 2015-03-31 -0.066761991 -0.130782761 -0.074350086
## 2015-04-30 -0.005953300 0.289324645 0.180226844
## 2015-05-29 -0.062403748 0.114579340 0.103899535
## 2015-06-30 0.088153643 0.051346185 0.067300966
## 2015-07-31 -0.038082035 0.197231491 -0.007896618
## 2015-08-31 0.018129305 0.006278917 -0.066366266
## 2015-09-30 -0.234100082 -0.107942856 -0.002653542
## 2015-10-30 -0.029655629 0.048393444 -0.182659742
## 2015-11-30 -0.027839679 0.129220163 0.106828579
## 2015-12-31 0.092797635 -0.075337494 0.041471546
## 2016-01-29 0.168021184 -0.219478323 -0.227360646
## 2016-02-29 0.010577022 0.016950560 0.003810669
## 2016-03-31 0.076394063 0.090226757 0.179948112
## 2016-04-29 -0.032420609 -0.127082269 0.046721799
## 2016-05-31 -0.007964569 0.130402555 -0.075597977
## 2016-06-30 0.127322736 -0.114425090 -0.050296472
## 2016-07-29 0.050040152 -0.002517409 0.100785361
## 2016-08-31 -0.014790098 0.065736403 -0.102058076
## 2016-09-30 -0.226875536 0.011224669 -0.038366402
## 2016-10-31 -0.063118299 0.236709156 -0.031364578
## 2016-11-30 -0.004551793 -0.065099285 -0.043041257
## 2016-12-30 0.131357532 0.056493450 0.120665160
## 2017-01-31 0.038042384 0.128033700 0.164624944
## 2017-02-28 -0.033896443 0.010041082 -0.007730394
## 2017-03-31 -0.229638753 0.039185485 0.107278734
## 2017-04-28 0.002503149 0.029267774 0.120916240
## 2017-05-31 -0.074433469 0.068984174 0.082295867
## 2017-06-30 0.212019098 -0.087485368 0.058654469
## 2017-07-31 0.032481646 0.195442598 -0.111459838
## 2017-08-31 -0.068656876 -0.039009334 0.095543417
## 2017-09-29 0.078504418 0.037301402 -0.042474117
## 2017-10-31 -0.011958805 0.079877199 -0.028457431
## 2017-11-30 0.084895681 -0.046100666 -0.070862536
## 2017-12-29 0.160149003 0.023081622 0.008061927
calculate_component_contribution <- function(.data, w) {
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
# 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(.35, .35, .30))
## # A tibble: 1 × 3
## LULU NFLX TSLA
## <dbl> <dbl> <dbl>
## 1 0.196 0.418 0.386
Column Chart of Component Contribution
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.35, .35, .30)) %>%
#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(.35, .35, .30)) %>%
#Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
#add weights
add_column(weight = c(.35, .35, .30)) %>%
#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)
Which of the assets in your portfolio the largest contributor to the portfolio volatility? Do you think your portfolio risk is concentrated in any one asset?
# The largest contributor in my portfolio is Netflix and this also has the most volatility. The other two stocks I have chosen around the same as Netflix, I think my portfolio is balanced in this area.