# Load packages
# Core
library(tidyverse)
library(tidyquant)
Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.
Choose your stocks from 2012-12-31 to present.
symbols <- c("TSLA", "GOOG","MSFT", "AAPL")
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"))
# 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
## AAPL GOOG MSFT TSLA
## 2013-01-31 -1.555890e-01 0.066063183 0.027328189 0.102078031
## 2013-02-28 -2.561096e-02 0.058479349 0.020915139 -0.074128613
## 2013-03-28 2.850513e-03 -0.008787848 0.028720295 0.084208141
## 2013-04-30 2.707961e-04 0.037539320 0.145776986 0.354111527
## 2013-05-31 2.217203e-02 0.055032448 0.059941136 0.593716693
## 2013-06-28 -1.258956e-01 0.010447756 -0.010368777 0.093672182
## 2013-07-31 1.321019e-01 0.008347897 -0.081394579 0.223739545
## 2013-08-30 8.044307e-02 -0.047107484 0.054854314 0.229971572
## 2013-09-30 -2.172364e-02 0.033680642 -0.003599082 0.134706682
## 2013-10-31 9.201528e-02 0.162613748 0.062037466 -0.189806650
## 2013-11-29 6.770796e-02 0.027760300 0.081562238 -0.228409431
## 2013-12-31 8.862471e-03 0.056080333 -0.019063636 0.167108548
## 2014-01-31 -1.139489e-01 0.052373779 0.011429102 0.187261770
## 2014-02-28 5.591763e-02 0.028942731 0.019815207 0.299722757
## 2014-03-31 1.975650e-02 -0.086376126 0.067617054 -0.160783192
## 2014-04-30 9.476133e-02 -0.055956216 -0.014498687 -0.002690159
## 2014-05-30 7.576528e-02 0.061185200 0.020307802 -0.000577395
## 2014-06-30 2.728575e-02 0.027116573 0.018393661 0.144457218
## 2014-07-31 2.832674e-02 -0.006417530 0.034413027 -0.072372711
## 2014-08-29 7.465145e-02 0.000000000 0.057484852 0.188794049
## 2014-09-30 -1.722041e-02 0.010026597 0.020264530 -0.105566506
## 2014-10-31 6.948904e-02 -0.032173432 0.012646477 -0.004046457
## 2014-11-28 1.007308e-01 -0.031340259 0.024438809 0.011599756
## 2014-12-31 -7.460640e-02 -0.028890933 -0.028858155 -0.094774520
## 2015-01-30 5.961183e-02 0.015307754 -0.139547088 -0.088365243
## 2015-02-27 9.601598e-02 0.043706410 0.089036423 -0.001277805
## 2015-03-31 -3.187451e-02 -0.018800219 -0.075529724 -0.074350086
## 2015-04-30 5.769940e-03 -0.016902497 0.179201290 0.180226844
## 2015-05-29 4.434103e-02 -0.009780881 -0.030804051 0.103899535
## 2015-06-30 -3.793803e-02 -0.022041136 -0.059571208 0.067300966
## 2015-07-31 -3.348090e-02 0.183918112 0.056150909 -0.007896618
## 2015-08-31 -6.848900e-02 -0.011834267 -0.063950601 -0.066366266
## 2015-09-30 -2.205775e-02 -0.016027473 0.016860671 -0.002653542
## 2015-10-30 8.011246e-02 0.155539760 0.173394737 -0.182659742
## 2015-11-30 -5.821097e-03 0.043752376 0.038685978 0.106828579
## 2015-12-31 -1.167903e-01 0.021686093 0.020578405 0.041471546
## 2016-01-29 -7.822364e-02 -0.021214941 -0.007054420 -0.227360646
## 2016-02-29 -1.288397e-03 -0.062739185 -0.072344107 0.003810669
## 2016-03-31 1.197462e-01 0.065427593 0.082036326 0.179948112
## 2016-04-29 -1.507309e-01 -0.072272671 -0.102086605 0.046721799
## 2016-05-31 6.931416e-02 0.059805126 0.067842248 -0.075597977
## 2016-06-30 -4.359655e-02 -0.061119131 -0.035138324 -0.050296472
## 2016-07-29 8.623540e-02 0.105087370 0.102267865 0.100785361
## 2016-08-31 2.337641e-02 -0.002265810 0.019880975 -0.102058076
## 2016-09-30 6.344827e-02 0.013261445 0.002433473 -0.038366402
## 2016-10-31 4.325143e-03 0.009284125 0.039487622 -0.031364578
## 2016-11-30 -2.183783e-02 -0.034361430 0.012391040 -0.043041257
## 2016-12-30 4.684075e-02 0.018015208 0.030721488 0.120665160
## 2017-01-31 4.664151e-02 0.031839794 0.039598132 0.164624944
## 2017-02-28 1.255551e-01 0.032620176 -0.004373343 -0.007730394
## 2017-03-31 4.754184e-02 0.007684132 0.028960645 0.107278734
## 2017-04-28 -6.996171e-05 0.088099691 0.038718464 0.120916240
## 2017-05-31 6.560786e-02 0.062987858 0.025672577 0.082295867
## 2017-06-30 -5.891623e-02 -0.059934971 -0.013115039 0.058654469
## 2017-07-31 3.218031e-02 0.023674077 0.053249924 -0.111459838
## 2017-08-31 1.016534e-01 0.009444715 0.033388989 0.095543417
## 2017-09-29 -6.213481e-02 0.020838979 -0.003752018 -0.042474117
## 2017-10-31 9.240373e-02 0.058252558 0.110342005 -0.028457431
## 2017-11-30 2.007504e-02 0.004680914 0.016841504 -0.070862536
## 2017-12-29 -1.536350e-02 0.024171696 0.016145450 0.008061927
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.25, 0.25))
## # A tibble: 1 × 4
## AAPL GOOG MSFT TSLA
## <dbl> <dbl> <dbl> <dbl>
## 1 0.172 0.128 0.168 0.532
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.25, 0.25)) %>%
# 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.25, 0.25)) %>%
# 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?
TSLA is the largest contributor to my portfolios volatility. It takes on over 50% of my portfolios volatility, as TSLA is prone to large swings of negative and positive returns. This portfolio consists of tech sector stocks which are proned to more volatility than that of others, so the risk is higher to start. Interestingly, TSLA and AAPL have tended to create greater risk than MSFT and GOOG which have been more stables stocks for the portfolio. I do believe that TSLA creates greater risk for my portfolio compared to my other stocks due to Elon Musk’s extreme influence and high presence on social media.