# 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("WMT", "AMZN", "NVDA")
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
## AMZN NVDA WMT
## 2013-01-31 0.0566799395 0.000000000 0.0248964315
## 2013-02-28 -0.0046435024 0.038222015 0.0117958128
## 2013-03-28 0.0083654162 0.013338604 0.0620732354
## 2013-04-30 -0.0487507497 0.070706278 0.0378937634
## 2013-05-31 0.0588686246 0.054651957 -0.0317800543
## 2013-06-28 0.0310507506 -0.030167461 -0.0046876694
## 2013-07-31 0.0813355350 0.028092049 0.0452744790
## 2013-08-30 -0.0695574090 0.026270421 -0.0596997341
## 2013-09-30 0.1067688897 0.053460402 0.0133389516
## 2013-10-31 0.1521839116 -0.024066669 0.0370287871
## 2013-11-29 0.0781496860 0.032034741 0.0540190125
## 2013-12-31 0.0130490386 0.026567318 -0.0232520181
## 2014-01-31 -0.1059765119 -0.020177128 -0.0523039222
## 2014-02-28 0.0094619003 0.162107514 0.0002679891
## 2014-03-31 -0.0737086161 -0.025904134 0.0293257924
## 2014-04-30 -0.1007565303 0.030788520 0.0420203246
## 2014-05-30 0.0273091844 0.032886692 -0.0314095369
## 2014-06-30 0.0383836202 -0.024508387 -0.0223926882
## 2014-07-31 -0.0369768154 -0.057729681 -0.0200476968
## 2014-08-29 0.0799468404 0.110059765 0.0323258235
## 2014-09-30 -0.0502010184 -0.052782437 0.0127660375
## 2014-10-31 -0.0540982347 0.057398974 -0.0026192017
## 2014-11-28 0.1031187277 0.074852154 0.1378165409
## 2014-12-31 -0.0872368614 -0.044863456 -0.0135740758
## 2015-01-30 0.1330922557 -0.043319011 -0.0105350977
## 2015-02-27 0.0697992426 0.142698893 -0.0124328182
## 2015-03-31 -0.0214295755 -0.052582273 -0.0142313433
## 2015-04-30 0.1253212736 0.058908754 -0.0524137242
## 2015-05-29 0.0175090293 0.001460205 -0.0433511649
## 2015-06-30 0.0112589814 -0.095717334 -0.0460136566
## 2015-07-31 0.2111621090 -0.007987990 0.0146949011
## 2015-08-31 -0.0443525782 0.123595673 -0.0993584482
## 2015-09-30 -0.0019516837 0.092150819 0.0016976106
## 2015-10-30 0.2010808743 0.140555197 -0.1246694015
## 2015-11-30 0.0602956777 0.115405147 0.0275687794
## 2015-12-31 0.0165440008 0.038347176 0.0492993646
## 2016-01-29 -0.1410054620 -0.118048446 0.0793142861
## 2016-02-29 -0.0605352209 0.071923706 -0.0003011690
## 2016-03-31 0.0717834363 0.127654936 0.0392702193
## 2016-04-29 0.1053453760 -0.002810493 -0.0239368486
## 2016-05-31 0.0915002899 0.276388360 0.0641208339
## 2016-06-30 -0.0099694639 0.006187796 0.0311567384
## 2016-07-29 0.0586021229 0.194443967 -0.0006850858
## 2016-08-31 0.0135476418 0.073469214 -0.0143679118
## 2016-09-30 0.0848953908 0.110693557 0.0094735839
## 2016-10-31 -0.0583893058 0.037804815 -0.0295505885
## 2016-11-30 -0.0509721927 0.260525496 0.0058385850
## 2016-12-30 -0.0009330556 0.146435792 -0.0116435350
## 2017-01-31 0.0936394059 0.022601817 -0.0350399061
## 2017-02-28 0.0258446800 -0.071874725 0.0608892318
## 2017-03-31 0.0479423007 0.070843562 0.0234087512
## 2017-04-28 0.0424566944 -0.043434021 0.0421087123
## 2017-05-31 0.0725778018 0.326022176 0.0511561816
## 2017-06-30 -0.0271286156 0.001453786 -0.0378575044
## 2017-07-31 0.0202278808 0.117044873 0.0553876853
## 2017-08-31 -0.0072953953 0.042639524 -0.0180254918
## 2017-09-29 -0.0198260355 0.053601089 0.0008961955
## 2017-10-31 0.1395154056 0.145700814 0.1109630081
## 2017-11-30 0.0626577318 -0.029245216 0.1076140277
## 2017-12-29 -0.0062057845 -0.036583511 0.0207685655
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## AMZN NVDA WMT
## AMZN 0.005466022 0.0015926684 0.0003511340
## NVDA 0.001592668 0.0077584837 0.0002178246
## WMT 0.000351134 0.0002178246 0.0022213952
# 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.3, 0.3, 0.4)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.04437183
# 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
## AMZN NVDA WMT
## [1,] 0.01526685 0.01955616 0.009548812
rowSums(component_contribution)
## [1] 0.04437183
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 3
## AMZN NVDA WMT
## <dbl> <dbl> <dbl>
## 1 0.344 0.441 0.215
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
## asset contribution
## <chr> <dbl>
## 1 AMZN 0.344
## 2 NVDA 0.441
## 3 WMT 0.215
NVDA had the highest volatility within my portfolio. It also had a relatively high wieght in the portfolio. I would lower the risk by spreading out the wight a bit more, or add more stocks with more diversity in my portfolio.