The goal of this analysis is to make sure a portfolio’s volatility (risk) is not concentrated in one or a few assets. To do that we calculate the component contribution to portfolio standard deviation: how much of the portfolio’s total risk each asset is responsible for.
The outline of this code along:
The five assets are the same ones used in the book. They are listed in alphabetical order so that the order matches the weights we assign later:
We download prices starting on 2012-12-31 so that we can compute a return for January 2013, and end on 2017-12-31. That gives 60 monthly returns per asset.
tickers <- c("AGG", "EEM", "EFA", "IJS", "SPY")
prices <- tq_get(tickers,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
prices
## # A tibble: 6,300 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AGG 2012-12-31 111. 111. 111. 111. 905400 76.1
## 2 AGG 2013-01-02 111. 111 111. 111. 1137300 76.0
## 3 AGG 2013-01-03 111. 111. 111. 111. 1762900 75.8
## 4 AGG 2013-01-04 111. 111. 111. 111. 1286800 75.9
## 5 AGG 2013-01-07 111. 111. 111. 111. 1854400 75.8
## 6 AGG 2013-01-08 111. 111. 111. 111. 1784500 75.9
## 7 AGG 2013-01-09 111. 111. 111. 111. 1346900 75.8
## 8 AGG 2013-01-10 111. 111. 111. 111. 1128700 75.8
## 9 AGG 2013-01-11 111. 111. 111. 111. 1120000 75.9
## 10 AGG 2013-01-14 111. 111. 111. 111. 870200 76.0
## # ℹ 6,290 more rows
We convert the adjusted daily prices to monthly log returns, one
asset at a time (group_by(symbol)). The first row for each
asset is December 2012, which has no earlier price to compare with, so
we drop it.
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
ungroup() %>%
rename(asset = symbol, returns = monthly.returns) %>%
filter(date >= "2013-01-01")
asset_returns_tbl
## # A tibble: 300 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AGG 2013-01-31 -0.00623
## 2 AGG 2013-02-28 0.00589
## 3 AGG 2013-03-28 0.000986
## 4 AGG 2013-04-30 0.00964
## 5 AGG 2013-05-31 -0.0202
## 6 AGG 2013-06-28 -0.0158
## 7 AGG 2013-07-31 0.00269
## 8 AGG 2013-08-30 -0.00830
## 9 AGG 2013-09-30 0.0111
## 10 AGG 2013-10-31 0.00829
## # ℹ 290 more rows
asset_returns_tbl has three columns (asset,
date, returns) and 300 rows (5 assets x 60
months).
To calculate covariance we need one column per asset
and one row per date. pivot_wider() turns
the long table into a wide table, and column_to_rownames()
moves the date column into the row names, because the
cov() function only wants the numeric returns.
asset_returns_wide_tbl <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns)
asset_returns_wide_tbl # 60 rows, a date column, and one column per asset
## # A tibble: 60 × 6
## date AGG EEM EFA IJS SPY
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013-01-31 -0.00623 -0.00294 0.0366 0.0521 0.0499
## 2 2013-02-28 0.00589 -0.0231 -0.0130 0.0162 0.0127
## 3 2013-03-28 0.000986 -0.0102 0.0130 0.0403 0.0373
## 4 2013-04-30 0.00964 0.0121 0.0490 0.00122 0.0190
## 5 2013-05-31 -0.0202 -0.0495 -0.0307 0.0420 0.0233
## 6 2013-06-28 -0.0158 -0.0547 -0.0271 -0.00140 -0.0134
## 7 2013-07-31 0.00269 0.0132 0.0519 0.0635 0.0504
## 8 2013-08-30 -0.00830 -0.0257 -0.0197 -0.0347 -0.0305
## 9 2013-09-30 0.0111 0.0696 0.0753 0.0639 0.0312
## 10 2013-10-31 0.00829 0.0409 0.0321 0.0342 0.0453
## # ℹ 50 more rows
asset_returns_wide_tbl <- asset_returns_wide_tbl %>%
column_to_rownames(var = "date")
head(asset_returns_wide_tbl)
## AGG EEM EFA IJS SPY
## 2013-01-31 -0.0062311968 -0.002935638 0.03660611 0.052133347 0.04992326
## 2013-02-28 0.0058910811 -0.023105449 -0.01296968 0.016175982 0.01267802
## 2013-03-28 0.0009855156 -0.010234909 0.01296968 0.040257995 0.03726794
## 2013-04-30 0.0096391892 0.012084933 0.04896769 0.001222202 0.01903046
## 2013-05-31 -0.0202143623 -0.049483457 -0.03065539 0.041976538 0.02333560
## 2013-06-28 -0.0157785230 -0.054728427 -0.02714469 -0.001402978 -0.01343444
Covariance describes how two assets move together. A positive covariance means the two assets tend to move in the same direction. A negative covariance means they tend to move in opposite directions. The diagonal of the matrix holds each asset’s variance (its covariance with itself), and the square root of a variance is a standard deviation.
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AGG EEM EFA IJS SPY
## AGG 7.398425e-05 0.0001042117 4.178243e-05 -7.811869e-05 -9.031123e-06
## EEM 1.042117e-04 0.0017547105 1.039017e-03 6.437712e-04 6.795425e-04
## EFA 4.178243e-05 0.0010390170 1.064237e-03 6.490287e-04 6.975403e-04
## IJS -7.811869e-05 0.0006437712 6.490287e-04 1.565451e-03 8.290258e-04
## SPY -9.031123e-06 0.0006795425 6.975403e-04 8.290258e-04 7.408280e-04
w holds the weight of each asset, in the same order as
the columns above: AGG, EEM, EFA, IJS, SPY. The weights add up to 1
(100%).
w <- c(0.25, 0.25, 0.20, 0.20, 0.10)
sum(w)
## [1] 1
The portfolio standard deviation collapses the whole covariance
matrix into a single number, taking the weights into account. It uses
matrix multiplication (%*%): the weights as a 1 x 5 row,
times the 5 x 5 covariance matrix, times the weights as a 5 x 1 column.
The result is a 1 x 1 matrix, a single number.
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.02347491
The formula below is almost the same as the portfolio standard
deviation. The difference is that the last step is a simple
multiplication (*) by the weights instead of a matrix
multiplication. That keeps one number per asset instead
of collapsing everything into one number.
If we call the whole expression inside the square root x
and the portfolio standard deviation y, then
y = sqrt(x), so y^2 = x, so
y = x / y. That is why we divide by the portfolio standard
deviation. In other words, component contribution is the portfolio
standard deviation split up by asset.
component_contribution <- (w %*% covariance_matrix * w) / sd_portfolio[1, 1]
component_contribution
## AGG EEM EFA IJS SPY
## [1,] 0.0003874202 0.009257144 0.005815631 0.005684466 0.002330249
Check: the contributions of the five assets should add up to the portfolio standard deviation.
rowSums(component_contribution)
## [1] 0.02347491
sd_portfolio
## [,1]
## [1,] 0.02347491
Dividing by the portfolio standard deviation gives each asset’s share of the total risk. These shares add up to 100%.
component_percentages <- component_contribution / sd_portfolio[1, 1]
component_percentages
## AGG EEM EFA IJS SPY
## [1,] 0.01650358 0.394342 0.2477381 0.2421507 0.09926552
rowSums(component_percentages)
## [1] 1
We can turn the 1 x 5 result into a table with two columns
(asset and contribution):
component_percentages %>%
as_tibble() %>%
pivot_longer(cols = everything(),
names_to = "asset",
values_to = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AGG 0.0165
## 2 EEM 0.394
## 3 EFA 0.248
## 4 IJS 0.242
## 5 SPY 0.0993
The calculation above takes many lines. We can put it inside a
function so we do not have to write all that code each time. The
function has two inputs: .data (the wide table of returns)
and w (the weights). The covariance matrix is calculated
inside the function. We do not print the intermediate objects, we only
return the final component percentages.
calculate_component_contribution <- function(.data, w) {
# Covariance matrix of the asset returns
covariance_matrix <- cov(.data)
# Portfolio standard deviation (matrix multiplication)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
# Component contribution (simple multiplication by the weights)
component_contribution <- (w %*% covariance_matrix * w) / sd_portfolio[1, 1]
# Component contribution as a percentage of portfolio risk
component_percentages <- component_contribution / sd_portfolio[1, 1]
return(component_percentages)
}
Now test the function. The weights must add up to 1, and there must be one weight per asset (we have five assets, so five weights).
asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.20, 0.20, 0.10))
## AGG EEM EFA IJS SPY
## [1,] 0.01650358 0.394342 0.2477381 0.2421507 0.09926552
This is the same result we got step by step above.
The function returns a 1 x 5 table. To plot it we need a long table
with two columns, asset and contribution.
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.20, 0.20, 0.10)) %>%
as_tibble() %>%
pivot_longer(cols = AGG:SPY,
names_to = "asset",
values_to = "contribution")
plot_data
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 AGG 0.0165
## 2 EEM 0.394
## 3 EFA 0.248
## 4 IJS 0.242
## 5 SPY 0.0993
plot_data %>%
ggplot(aes(x = asset, y = contribution)) +
geom_col(fill = "cornflowerblue") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
labs(title = "Percent Contribution to Portfolio Standard Deviation",
x = "Asset",
y = "Contribution to volatility") +
theme(plot.title = element_text(hjust = 0.5))
The chart shows which assets are responsible for most of the portfolio’s volatility. In this portfolio, EEM (emerging markets) is the largest contributor, and AGG (bonds) is the smallest.
It is also useful to see each asset’s weight next to
its contribution to risk. We add a weight column, then
transform both columns into one long column (value) with a
type column that says which is which.
plot_data_2 <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.20, 0.20, 0.10)) %>%
as_tibble() %>%
pivot_longer(cols = AGG:SPY,
names_to = "asset",
values_to = "contribution") %>%
add_column(weight = c(0.25, 0.25, 0.20, 0.20, 0.10)) %>%
pivot_longer(cols = c(contribution, weight),
names_to = "type",
values_to = "value")
plot_data_2
## # A tibble: 10 × 3
## asset type value
## <chr> <chr> <dbl>
## 1 AGG contribution 0.0165
## 2 AGG weight 0.25
## 3 EEM contribution 0.394
## 4 EEM weight 0.25
## 5 EFA contribution 0.248
## 6 EFA weight 0.2
## 7 IJS contribution 0.242
## 8 IJS weight 0.2
## 9 SPY contribution 0.0993
## 10 SPY weight 0.1
fill = type gives each type its own color, and
position = "dodge" puts the bars side by side instead of
stacking them. theme_tq() and scale_fill_tq()
come from the tidyquant package.
plot_data_2 %>%
ggplot(aes(x = asset, y = value, fill = type)) +
geom_col(position = "dodge") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme_tq() +
scale_fill_tq() +
labs(title = "Percent Contribution to Volatility and Weight",
x = NULL,
y = "Percent") +
theme(plot.title = element_text(hjust = 0.5))
AGG and EEM each make up 25% of the portfolio, but AGG contributes very little to the portfolio’s volatility, while EEM contributes much more. AGG acts as a damper on the portfolio’s volatility: bonds have low variance and tend to move differently from stocks. An asset whose contribution is much larger than its weight is adding more risk than its size suggests.