# 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.
symbols <- c("AAPL", "MSFT", "META", "TSLA", "NFLX")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2022-10-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 META MSFT NFLX TSLA
## 2013-01-31 -1.555892e-01 0.1516789845 0.027328660 0.5792178375 0.102078031
## 2013-02-28 -2.561108e-02 -0.1282883139 0.020914822 0.1294683583 -0.074128613
## 2013-03-28 2.850414e-03 -0.0632427250 0.028720360 0.0063600861 0.084208141
## 2013-04-30 2.710413e-04 0.0821455052 0.145777154 0.1323750291 0.354111527
## 2013-05-31 2.217163e-02 -0.1314244518 0.059941394 0.0460381605 0.593716693
## 2013-06-28 -1.258958e-01 0.0215323781 -0.010369083 -0.0693560534 0.093672182
## 2013-07-31 1.321025e-01 0.3914335903 -0.081394622 0.1468488958 0.223739545
## 2013-08-30 8.044279e-02 0.1151225461 0.054854297 0.1495238068 0.229971572
## 2013-09-30 -2.172353e-02 0.1959920936 -0.003599162 0.0853633332 0.134706682
## 2013-10-31 9.201529e-02 -0.0003982676 0.062037543 0.0420204569 -0.189806650
## 2013-11-29 6.770823e-02 -0.0658538876 0.081562677 0.1260456343 -0.228409431
## 2013-12-31 8.862378e-03 0.1505889487 -0.019063747 0.0064581012 0.167108548
## 2014-01-31 -1.139497e-01 0.1353366788 0.011428796 0.1059769044 0.187261770
## 2014-02-28 5.591814e-02 0.0899636885 0.019815136 0.0849673277 0.299722757
## 2014-03-31 1.975673e-02 -0.1279130018 0.067617261 -0.2357726146 -0.160783192
## 2014-04-30 9.476115e-02 -0.0076654766 -0.014498492 -0.0890406639 -0.002690159
## 2014-05-30 7.576519e-02 0.0572141732 0.020307616 0.2603988793 -0.000577395
## 2014-06-30 2.728603e-02 0.0611263387 0.018393640 0.0530627817 0.144457218
## 2014-07-31 2.832685e-02 0.0766417655 0.034413101 -0.0414273689 -0.072372711
## 2014-08-29 7.465131e-02 0.0294318114 0.057484880 0.1221472235 0.188794049
## 2014-09-30 -1.722064e-02 0.0548688372 0.020264146 -0.0569909090 -0.105566506
## 2014-10-31 6.948910e-02 -0.0525993214 0.012646540 -0.1386421452 -0.004046457
## 2014-11-28 1.007306e-01 0.0355004741 0.024438836 -0.1250817146 0.011599756
## 2014-12-31 -7.460596e-02 0.0041099469 -0.028858117 -0.0144727373 -0.094774520
## 2015-01-30 5.961147e-02 -0.0274166849 -0.139546846 0.2571875149 -0.088365243
## 2015-02-27 9.601610e-02 0.0395195654 0.089036260 0.0722680075 -0.001277805
## 2015-03-31 -3.187433e-02 0.0403305475 -0.075529871 -0.1307827615 -0.074350086
## 2015-04-30 5.769867e-03 -0.0428664180 0.179201375 0.2893246451 0.180226844
## 2015-05-29 4.434104e-02 0.0053178778 -0.030803827 0.1145793405 0.103899535
## 2015-06-30 -3.793805e-02 0.0798192065 -0.059571652 0.0513461850 0.067300966
## 2015-07-31 -3.348106e-02 0.0917319208 0.056151346 0.1972314914 -0.007896618
## 2015-08-31 -6.848892e-02 -0.0499449845 -0.063950854 0.0062789167 -0.066366266
## 2015-09-30 -2.205792e-02 0.0052417674 0.016860584 -0.1079428564 -0.002653542
## 2015-10-30 8.011248e-02 0.1259806985 0.173395030 0.0483934436 -0.182659742
## 2015-11-30 -5.821282e-03 0.0220172514 0.038686058 0.1292201633 0.106828579
## 2015-12-31 -1.167902e-01 0.0040211255 0.020577973 -0.0753374935 0.041471546
## 2016-01-29 -7.822358e-02 0.0696550676 -0.007054349 -0.2194783232 -0.227360646
## 2016-02-29 -1.288319e-03 -0.0482912342 -0.072343986 0.0169505597 0.003810669
## 2016-03-31 1.197463e-01 0.0649943668 0.082036289 0.0902267574 0.179948112
## 2016-04-29 -1.507316e-01 0.0300437306 -0.102086799 -0.1270822691 0.046721799
## 2016-05-31 6.931435e-02 0.0104065917 0.067842227 0.1304025553 -0.075597977
## 2016-06-30 -4.359626e-02 -0.0388739930 -0.035138496 -0.1144250904 -0.050296472
## 2016-07-29 8.623519e-02 0.0811460251 0.102268240 -0.0025174095 0.100785361
## 2016-08-31 2.337665e-02 0.0174362648 0.019880925 0.0657364027 -0.102058076
## 2016-09-30 6.344795e-02 0.0169035900 0.002433472 0.0112246689 -0.038366402
## 2016-10-31 4.325224e-03 0.0209835741 0.039487744 0.2367091561 -0.031364578
## 2016-11-30 -2.183789e-02 -0.1008834124 0.012391060 -0.0650992854 -0.043041257
## 2016-12-30 4.684104e-02 -0.0288707677 0.030721422 0.0564934497 0.120665160
## 2017-01-31 4.664156e-02 0.1246261755 0.039598157 0.1280337001 0.164624944
## 2017-02-28 1.255553e-01 0.0392737301 -0.004373321 0.0100410824 -0.007730394
## 2017-03-31 4.754131e-02 0.0469123814 0.028960826 0.0391854848 0.107278734
## 2017-04-28 -6.961806e-05 0.0561214440 0.038718330 0.0292677740 0.120916240
## 2017-05-31 6.560737e-02 0.0080210365 0.025672527 0.0689841737 0.082295867
## 2017-06-30 -5.891591e-02 -0.0031742587 -0.013115178 -0.0874853678 0.058654469
## 2017-07-31 3.218039e-02 0.1142295606 0.053249753 0.1954425985 -0.111459838
## 2017-08-31 1.016533e-01 0.0159431370 0.033389270 -0.0390093335 0.095543417
## 2017-09-29 -6.213490e-02 -0.0064170446 -0.003751810 0.0373014023 -0.042474117
## 2017-10-31 9.240372e-02 0.0523871134 0.110342037 0.0798771993 -0.028457431
## 2017-11-30 2.007516e-02 -0.0161239921 0.016840777 -0.0461006664 -0.070862536
## 2017-12-29 -1.536331e-02 -0.0040718640 0.016145975 0.0230816217 0.008061927
## 2018-01-31 -1.069341e-02 0.0574259417 0.104997759 0.3422453554 0.129254601
## 2018-02-28 6.596091e-02 -0.0469404748 -0.008450487 0.0750958655 -0.032266877
## 2018-03-29 -5.980390e-02 -0.1097193190 -0.027022951 0.0135328397 -0.253920406
## 2018-04-30 -1.513350e-02 0.0736340675 0.024353210 0.0563153181 0.099254538
## 2018-05-31 1.267419e-01 0.1088543997 0.059652215 0.1180177507 -0.031698143
## 2018-06-29 -9.462794e-03 0.0131574442 -0.002329917 0.1073124992 0.186043287
## 2018-07-31 2.759872e-02 -0.1186454120 0.073021020 -0.1483892953 -0.140021525
## 2018-08-31 1.826733e-01 0.0180877948 0.061088383 0.0857955889 0.011737398
## 2018-09-28 -8.337539e-03 -0.0662812813 0.017997752 0.0173903683 -0.130439016
## 2018-10-31 -3.095166e-02 -0.0801694816 -0.068387186 -0.2149050722 0.242170573
## 2018-11-30 -1.999124e-01 -0.0765078328 0.041797915 -0.0532520013 0.038271593
## 2018-12-31 -1.240884e-01 -0.0701060280 -0.087790571 -0.0667287372 -0.051761993
## 2019-01-31 5.368661e-02 0.2402517323 0.027768624 0.2377564137 -0.080628787
## 2019-02-28 4.380298e-02 -0.0319403333 0.074511447 0.0533383468 0.041033018
## 2019-03-29 9.260263e-02 0.0319403333 0.051409368 -0.0043097707 -0.133656450
## 2019-04-30 5.490093e-02 0.1486247400 0.101962977 0.0384589024 -0.159123800
## 2019-05-31 -1.326323e-01 -0.0859589658 -0.050746671 -0.0764149876 -0.253945344
## 2019-06-28 1.226768e-01 0.0838886027 0.079843598 0.0676869840 0.188012089
## 2019-07-31 7.361723e-02 0.0063528143 0.017097311 -0.1286120898 0.078092368
## 2019-08-30 -1.659815e-02 -0.0450721096 0.014924818 -0.0948922666 -0.068516906
## 2019-09-30 7.042249e-02 -0.0417379949 0.008450909 -0.0931610203 0.065449564
## 2019-10-31 1.049765e-01 0.0734378618 0.030739018 0.0713417365 0.268061223
## 2019-11-29 7.469366e-02 0.0508131637 0.057761314 0.0905829219 0.046592166
## 2019-12-31 9.420417e-02 0.0177448241 0.040901280 0.0279227920 0.237359751
## 2020-01-31 5.260173e-02 -0.0164066750 0.076456050 0.0643897455 0.441578346
## 2020-02-28 -1.218303e-01 -0.0478817708 -0.046764769 0.0670726979 0.026424249
## 2020-03-31 -7.231427e-02 -0.1431447947 -0.026900064 0.0173805324 -0.242781463
## 2020-04-30 1.444241e-01 0.2047988697 0.127800487 0.1116390477 0.400209547
## 2020-05-29 8.166667e-02 0.0949059268 0.025074149 -0.0002858453 0.065730498
## 2020-06-30 1.374865e-01 0.0087580664 0.104863806 0.0807736733 0.257108653
## 2020-07-31 1.528341e-01 0.1107758310 0.007343588 0.0717317239 0.281420680
## 2020-08-31 1.960350e-01 0.1448208091 0.097808926 0.0799293985 0.554719315
## 2020-09-30 -1.081717e-01 -0.1128922823 -0.069775640 -0.0573783747 -0.149762308
## 2020-10-30 -6.188817e-02 0.0046094101 -0.038085895 -0.0497966291 -0.100371771
## 2020-11-30 9.120459e-02 0.0513370620 0.058325962 0.0309615021 0.380308523
## 2020-12-31 1.084722e-01 -0.0138514819 0.038264423 0.0970870739 0.217730754
## 2021-01-29 -5.516873e-03 -0.0558199317 0.041997419 -0.0155437098 0.117343701
## 2021-02-26 -8.306866e-02 -0.0027521755 0.004109539 0.0120608629 -0.161038203
## 2021-03-31 7.312888e-03 0.1338952505 0.014482809 -0.0324212116 -0.011269836
## 2021-04-30 7.345263e-02 0.0986904038 0.067286307 -0.0158244343 0.060292565
## 2021-05-28 -5.181656e-02 0.0111655127 -0.007656518 -0.0209791874 -0.126372344
## 2021-06-30 9.450017e-02 0.0561319945 0.081569577 0.0492816124 0.083547956
## 2021-07-30 6.295830e-02 0.0244042658 0.050423601 -0.0203491783 0.010973846
## 2021-08-31 4.161128e-02 0.0627653155 0.059768872 0.0950695058 0.068224264
## 2021-09-30 -7.046191e-02 -0.1113884217 -0.068406101 0.0698019221 0.052632613
## 2021-10-29 5.700148e-02 -0.0477344373 0.162366289 0.1231245460 0.362230204
## 2021-11-30 9.991943e-02 0.0027467388 -0.001283070 -0.0727082035 0.027237847
## 2021-12-31 7.160283e-02 0.0359900681 0.017184187 -0.0634444870 -0.079968439
## 2022-01-31 -1.583682e-02 -0.0711187537 -0.078334396 -0.3438762152 -0.120597477
## 2022-02-28 -5.558228e-02 -0.3950332498 -0.037922108 -0.0794420553 -0.073397011
## 2022-03-31 5.588246e-02 0.0522973959 0.031364736 -0.0518377244 0.213504290
## 2022-04-29 -1.021776e-01 -0.1036330800 -0.105212831 -0.6769150623 -0.213125290
## 2022-05-31 -5.603723e-02 -0.0346638547 -0.018242399 0.0365177293 -0.138340063
## 2022-06-30 -8.493687e-02 -0.1830448042 -0.056909822 -0.1213919387 -0.118657123
## 2022-07-29 1.728044e-01 -0.0134229826 0.089014731 0.2516130202 0.280480148
## 2022-08-31 -3.170530e-02 0.0237876447 -0.068989117 -0.0059760068 -0.075250273
## 2022-09-30 -1.289443e-01 -0.1830214973 -0.115710400 0.0517762870 -0.038313991
## 2022-10-28 1.194862e-01 -0.3131611364 0.012671636 0.2279569517 -0.149049026
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.2, 0.2, 0.1))
## # A tibble: 1 × 5
## AAPL META MSFT NFLX TSLA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.198 0.25 0.113 0.286 0.153
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(0.25, 0.25, 0.2, 0.2, 0.1)) %>%
# 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.2, 0.2, 0.1)) %>%
# 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)
when I observe the graph, the largest contributors to my portfolios volatility proves to be Netflix and Tesla compared to its weight. By the looks of Microsoft, it plays the role as a damper of the portfolio since the volatility only plays 10 %.
I would say my portfolio is not at risk due to volatility concentrated in one asset. It looks well structured and the assets compliment each other good overall.
In my personal investing, I am more concentrated into higher volatility stocks since think long term and I rarely let my feelings take over my decisions. Therefore, I am a fan of higher volatility stocks. As we know by the stocks in my chosen portfolio, they have generated huge amount of returns the last 10 years.