# 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", "AAPL", "NFLX", "GOOGL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31")
prices
## # A tibble: 14,016 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 WMT 2012-12-31 22.5 22.8 22.5 22.7 21037500 17.4
## 2 WMT 2013-01-02 23.0 23.1 22.8 23.1 31172400 17.7
## 3 WMT 2013-01-03 23.1 23.1 22.8 22.9 26730300 17.6
## 4 WMT 2013-01-04 22.9 23.1 22.8 23.0 19314000 17.7
## 5 WMT 2013-01-07 22.9 23.0 22.7 22.8 18604200 17.5
## 6 WMT 2013-01-08 22.8 23.0 22.7 22.9 17600700 17.5
## 7 WMT 2013-01-09 22.9 22.9 22.7 22.9 15165600 17.5
## 8 WMT 2013-01-10 22.9 23.0 22.6 22.8 34361400 17.5
## 9 WMT 2013-01-11 22.9 22.9 22.7 22.9 18673500 17.6
## 10 WMT 2013-01-14 22.8 22.9 22.7 22.8 16471200 17.5
## # ℹ 14,006 more rows
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"))
asset_returns_tbl
## # A tibble: 672 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AAPL 2013-01-31 -0.156
## 2 AAPL 2013-02-28 -0.0256
## 3 AAPL 2013-03-28 0.00285
## 4 AAPL 2013-04-30 0.000271
## 5 AAPL 2013-05-31 0.0222
## 6 AAPL 2013-06-28 -0.126
## 7 AAPL 2013-07-31 0.132
## 8 AAPL 2013-08-30 0.0804
## 9 AAPL 2013-09-30 -0.0217
## 10 AAPL 2013-10-31 0.0920
## # ℹ 662 more rows
# 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 GOOGL NFLX WMT
## 2013-01-31 -1.555889e-01 0.0660632347 0.5792178352 0.0248959398
## 2013-02-28 -2.561100e-02 0.0584795188 0.1294686037 0.0117958475
## 2013-03-28 2.850088e-03 -0.0087881179 0.0063600533 0.0620731735
## 2013-04-30 2.711118e-04 0.0375395821 0.1323749474 0.0378940078
## 2013-05-31 2.217158e-02 0.0550324295 0.0460382575 -0.0317796762
## 2013-06-28 -1.258957e-01 0.0104476290 -0.0693561240 -0.0046879918
## 2013-07-31 1.321025e-01 0.0083478762 0.1468489367 0.0452743635
## 2013-08-30 8.044319e-02 -0.0471075467 0.1495238797 -0.0596999684
## 2013-09-30 -2.172375e-02 0.0336807468 0.0853634080 0.0133391628
## 2013-10-31 9.201510e-02 0.1626138793 0.0420203453 0.0370286294
## 2013-11-29 6.770790e-02 0.0277601595 0.1260456715 0.0540192924
## 2013-12-31 8.862272e-03 0.0560802959 0.0064580000 -0.0232520796
## 2014-01-31 -1.139490e-01 0.0523738562 0.1059768648 -0.0523034342
## 2014-02-28 5.591793e-02 0.0289428563 0.0849674837 0.0002672279
## 2014-03-31 1.975634e-02 -0.0868642646 -0.2357727088 0.0293265855
## 2014-04-30 9.476150e-02 -0.0419810769 -0.0890406585 0.0420197300
## 2014-05-30 7.576528e-02 0.0664846555 0.2603989261 -0.0314093661
## 2014-06-30 2.728590e-02 0.0225205845 0.0530628157 -0.0223928507
## 2014-07-31 2.832638e-02 -0.0087956065 -0.0414274490 -0.0200474545
## 2014-08-29 7.465181e-02 0.0048368650 0.1221473944 0.0323257641
## 2014-09-30 -1.722037e-02 0.0103352245 -0.0569909733 0.0127655093
## 2014-10-31 6.948865e-02 -0.0355314366 -0.1386422062 -0.0026188538
## 2014-11-28 1.007308e-01 -0.0336484640 -0.1250816593 0.1378163237
## 2014-12-31 -7.460591e-02 -0.0341225850 -0.0144727517 -0.0135737252
## 2015-01-30 5.961167e-02 0.0129002127 0.2571875640 -0.0105351193
## 2015-02-27 9.601577e-02 0.0456004447 0.0722680237 -0.0124328850
## 2015-03-31 -3.187432e-02 -0.0141947426 -0.1307829000 -0.0142312833
## 2015-04-30 5.769475e-03 -0.0107479392 0.2893246603 -0.0524137238
## 2015-05-29 4.434123e-02 -0.0063066882 0.1145793884 -0.0433516361
## 2015-06-30 -3.793798e-02 -0.0097295415 0.0513461588 -0.0460132631
## 2015-07-31 -3.348077e-02 0.1968015204 0.1972314899 0.0146950142
## 2015-08-31 -6.848898e-02 -0.0148317951 0.0062789518 -0.0993587304
## 2015-09-30 -2.205801e-02 -0.0146948251 -0.1079428863 0.0016979467
## 2015-10-30 8.011259e-02 0.1441988919 0.0483934975 -0.1246700504
## 2015-11-30 -5.821165e-03 0.0339444133 0.1292201068 0.0275692275
## 2015-12-31 -1.167903e-01 0.0196779416 -0.0753374900 0.0492993577
## 2016-01-29 -7.822347e-02 -0.0216461395 -0.2194782802 0.0793143128
## 2016-02-29 -1.288474e-03 -0.0597105893 0.0169504292 -0.0003012512
## 2016-03-31 1.197462e-01 0.0617442093 0.0902267716 0.0392707424
## 2016-04-29 -1.507312e-01 -0.0748522152 -0.1270821580 -0.0239373759
## 2016-05-31 6.931421e-02 0.0562641861 0.1304025124 0.0641207308
## 2016-06-30 -4.359655e-02 -0.0624283760 -0.1144251460 0.0311570202
## 2016-07-29 8.623532e-02 0.1176171530 -0.0025173508 -0.0006847182
## 2016-08-31 2.337663e-02 -0.0018845947 0.0657364217 -0.0143683336
## 2016-09-30 6.344849e-02 0.0178307736 0.0112245729 0.0094734379
## 2016-10-31 4.324778e-03 0.0072370309 0.2367092462 -0.0295506149
## 2016-11-30 -2.183788e-02 -0.0429130282 -0.0650993149 0.0058383639
## 2016-12-30 4.684088e-02 0.0211316313 0.0564934510 -0.0116434047
## 2017-01-31 4.664193e-02 0.0344064671 0.1280336724 -0.0350395123
## 2017-02-28 1.255551e-01 0.0297178081 0.0100411108 0.0608888841
## 2017-03-31 4.754147e-02 0.0033911499 0.0391855218 0.0234089353
## 2017-04-28 -6.941361e-05 0.0866298536 0.0292677885 0.0421084673
## 2017-05-31 6.560692e-02 0.0654866063 0.0689840448 0.0511565250
## 2017-06-30 -5.891534e-02 -0.0599209027 -0.0874853040 -0.0378574788
## 2017-07-31 3.217971e-02 0.0168734175 0.1954426244 0.0553876568
## 2017-08-31 1.016535e-01 0.0102489103 -0.0390093333 -0.0180254297
## 2017-09-29 -6.213512e-02 0.0191612561 0.0373013834 0.0008960423
## 2017-10-31 9.240359e-02 0.0591372996 0.0798772375 0.1109629881
## 2017-11-30 2.007522e-02 0.0030251751 -0.0461007257 0.1076142485
## 2017-12-29 -1.536292e-02 0.0164917822 0.0230815823 0.0207683254
## 2018-01-31 -1.069339e-02 0.1153710688 0.3422455062 0.0764921997
## 2018-02-28 6.596095e-02 -0.0685264793 0.0750957953 -0.1691627923
## 2018-03-29 -5.980393e-02 -0.0624007545 0.0135328146 -0.0056771891
## 2018-04-30 -1.513358e-02 -0.0180572318 0.0563153817 -0.0057486575
## 2018-05-31 1.267421e-01 0.0769006089 0.1180176912 -0.0629873504
## 2018-06-29 -9.463146e-03 0.0261903274 0.1073125782 0.0369858989
## 2018-07-31 2.759911e-02 0.0832509300 -0.1483894202 0.0409482030
## 2018-08-31 1.826730e-01 0.0037249952 0.0857957175 0.0774629596
## 2018-09-28 -8.337474e-03 -0.0202721765 0.0173902455 -0.0205519475
## 2018-10-31 -3.095162e-02 -0.1014945911 -0.2149050241 0.0656295433
## 2018-11-30 -1.999124e-01 0.0173349688 -0.0532519750 -0.0265767822
## 2018-12-31 -1.240887e-01 -0.0600660120 -0.0667287371 -0.0417364939
## 2019-01-31 5.368659e-02 0.0745950930 0.2377564232 0.0283648998
## 2019-02-28 4.380274e-02 0.0005861536 0.0533383459 0.0324429279
## 2019-03-29 9.260294e-02 0.0437155640 -0.0043098364 -0.0094922895
## 2019-04-30 5.490093e-02 0.0185791346 0.0384589452 0.0530141574
## 2019-05-31 -1.326323e-01 -0.0802525983 -0.0764150093 -0.0084086476
## 2019-06-28 1.226769e-01 -0.0216517031 0.0676869455 0.0854576147
## 2019-07-31 7.361728e-02 0.1178240533 -0.1286120040 -0.0009961598
## 2019-08-30 -1.659819e-02 -0.0229757325 -0.0948922909 0.0394578370
## 2019-09-30 7.042266e-02 0.0253861628 -0.0931610341 0.0379546394
## 2019-10-31 1.049762e-01 0.0303740817 0.0713417093 -0.0120374381
## 2019-11-29 7.469386e-02 0.0353467375 0.0905829375 0.0154857621
## 2019-12-31 9.420374e-02 0.0267085223 0.0279227475 0.0023739217
## 2020-01-31 5.260217e-02 0.0674024875 0.0643897941 -0.0372906043
## 2020-02-28 -1.218304e-01 -0.0675069198 0.0670727184 -0.0613235904
## 2020-03-31 -7.231423e-02 -0.1420101115 0.0173805126 0.0581106925
## 2020-04-30 1.444239e-01 0.1475577287 0.1116390683 0.0674663401
## 2020-05-29 8.166675e-02 0.0624754380 -0.0002858819 0.0248284871
## 2020-06-30 1.374866e-01 -0.0108501043 0.0807737255 -0.0351085753
## 2020-07-31 1.528340e-01 0.0481166872 0.0717317079 0.0772518555
## 2020-08-31 1.960350e-01 0.0908925155 0.0799294267 0.0745886230
## 2020-09-30 -1.081718e-01 -0.1060269972 -0.0573784336 0.0076049342
## 2020-10-30 -6.188790e-02 0.0977570438 -0.0497965496 -0.0083254964
## 2020-11-30 9.120441e-02 0.0821049390 0.0309614228 0.0963907313
## 2020-12-31 1.084720e-01 -0.0010036417 0.0970871760 -0.0545614490
## 2021-01-29 -5.516315e-03 0.0417492315 -0.0155438392 -0.0257181797
## 2021-02-26 -8.306890e-02 0.1011702431 0.0120609342 -0.0782173642
## 2021-03-31 7.312858e-03 0.0198859228 -0.0324211662 0.0486519734
## 2021-04-30 7.345261e-02 0.1319750762 -0.0158244342 0.0295950469
## 2021-05-28 -5.181638e-02 0.0014222941 -0.0209792775 0.0189585798
## 2021-06-30 9.450001e-02 0.0354054797 0.0492815858 -0.0071366152
## 2021-07-30 6.295826e-02 0.0984922938 -0.0203491346 0.0107911251
## 2021-08-31 4.161147e-02 0.0713984630 0.0950694939 0.0418681912
## 2021-09-30 -7.046196e-02 -0.0792262270 0.0698019104 -0.0606839689
## 2021-10-29 5.700166e-02 0.1021040370 0.1231245610 0.0695568501
## 2021-11-30 9.991922e-02 -0.0424180821 -0.0727081801 -0.0606285915
## 2021-12-31 7.160259e-02 0.0206076737 -0.0634444996 0.0324794207
## 2022-01-31 -1.583678e-02 -0.0681921261 -0.3438762220 -0.0343091467
## 2022-02-28 -5.558226e-02 -0.0018234516 -0.0794420172 -0.0338249208
## 2022-03-31 5.588257e-02 0.0292624379 -0.0518377428 0.1008101360
## 2022-04-29 -1.021776e-01 -0.1978009926 -0.6769151054 0.0269633043
## 2022-05-31 -5.603725e-02 -0.0030499543 0.0365177351 -0.1698040794
## 2022-06-30 -8.493679e-02 -0.0431001428 -0.1213919016 -0.0563675412
## 2022-07-29 1.728043e-01 0.0653366898 0.2516130360 0.0826080975
## 2022-08-31 -3.170531e-02 -0.0721788350 -0.0059759741 0.0081250678
## 2022-09-30 -1.289440e-01 -0.1234704936 0.0517762558 -0.0217359684
## 2022-10-31 1.039558e-01 -0.0119900769 0.2148866190 0.0929242475
## 2022-11-30 -3.358507e-02 0.0663159587 0.0457052075 0.0684915180
## 2022-12-30 -1.304195e-01 -0.1350745392 -0.0354794336 -0.0685300203
## 2023-01-31 1.048293e-01 0.1135553821 0.1823328761 0.0145630956
## 2023-02-28 2.291836e-02 -0.0930263793 -0.0939461699 -0.0121679101
## 2023-03-31 1.121215e-01 0.1413153973 0.0699795345 0.0408372613
## 2023-04-28 2.857494e-02 0.0342100070 -0.0460542525 0.0235920202
## 2023-05-31 4.502927e-02 0.1351255008 0.1805873321 -0.0237418670
## 2023-06-30 9.014236e-02 -0.0261383513 0.1084198950 0.0678437948
## 2023-07-31 1.270424e-02 0.1032530077 -0.0034566798 0.0169068486
## 2023-08-31 -4.330812e-02 0.0256624610 -0.0121241234 0.0206048931
## 2023-09-29 -9.285920e-02 -0.0397761802 -0.1384715458 -0.0166184755
## 2023-10-31 -2.573393e-03 -0.0532013164 0.0864352180 0.0215261153
## 2023-11-30 1.077597e-01 0.0658822467 0.1408734072 -0.0483955326
## 2023-12-29 1.349123e-02 0.0526167975 0.0268736655 0.0162178317
## 2024-01-31 -4.314489e-02 0.0029306084 0.1472315750 0.0470819921
## 2024-02-29 -1.871717e-02 -0.0117746532 0.0665352594 0.0620580669
## 2024-03-28 -5.264806e-02 0.0862344976 0.0072878139 0.0296685830
## 2024-04-30 -6.728906e-03 0.0755836032 -0.0979911057 -0.0137217155
## 2024-05-31 1.224142e-01 0.0579974151 0.1529150450 0.1060152147
## 2024-06-28 9.125909e-02 0.0555802870 0.0505386138 0.0292219507
## 2024-07-31 5.298204e-02 -0.0600139482 -0.0714375604 0.0136416096
## 2024-08-30 3.184121e-02 -0.0487376131 0.1099097581 0.1207588937
## 2024-09-30 1.731645e-02 0.0163310236 0.0112292200 0.0445699457
## 2024-10-31 -3.090185e-02 0.0312229878 0.0638447163 0.0147513387
## 2024-11-29 5.041448e-02 -0.0127039324 0.1595497355 0.1210993580
## 2024-12-31 5.368800e-02 0.1148756813 0.0050728010 -0.0213044909
## 2025-01-31 -5.930763e-02 0.0748849793 0.0915374451 0.0829078290
## 2025-02-28 2.554332e-02 -0.1807737773 0.0038828370 0.0045739165
## 2025-03-31 -8.501353e-02 -0.0951934155 -0.0502225296 -0.1134828620
## 2025-04-30 -4.432071e-02 0.0265457051 0.1935925475 0.1023373421
## 2025-05-30 -5.507326e-02 0.0783362960 0.0645899020 0.0174175432
## 2025-06-30 2.128048e-02 0.0270179734 0.1036916319 -0.0095675590
## 2025-07-31 1.162980e-02 0.0851843654 -0.1441174565 0.0020434014
## 2025-08-29 1.130063e-01 0.1038952526 0.0412703555 -0.0079256801
## 2025-09-30 9.247092e-02 0.1334974016 -0.0077518692 0.0608065605
## 2025-10-31 5.997971e-02 0.1455575901 -0.0691108133 -0.0184103109
## 2025-11-28 3.185163e-02 0.1298528165 -0.0392457361 0.0882050160
## 2025-12-31 -2.538676e-02 -0.0220262937 -0.1374964253 0.0101473610
## 2026-01-30 -4.660743e-02 0.0768427497 -0.1160115100 0.0670821632
## 2026-02-27 1.888611e-02 -0.0808122369 0.1421182109 0.0713402737
## 2026-03-31 -4.012354e-02 -0.0800984505 -0.0009355616 -0.0270522401
## 2026-04-30 6.690247e-02 0.2912921318 -0.0267722660 0.0597343530
## 2026-05-29 1.407069e-01 -0.0116581124 -0.0845574336 -0.1289321542
## 2026-06-30 -7.552395e-02 -0.0616967635 -0.1862818976 -0.0217466445
## 2026-07-31 6.537844e-02 -0.0034758081 0.0043323044 -0.0183557159
## 2026-08-31 2.624055e-02 -0.0482638227 0.1224360907 -0.0562176814
## 2026-09-30 4.977405e-02 0.0144846812 -0.1525890898 -0.0091001593
## 2026-10-30 3.426886e-02 -0.2066561513 -0.1140532907 0.1727007870
## 2026-11-30 2.758505e-02 0.0532995964 -0.0080867331 0.0514430419
## 2026-12-04 -2.323395e-02 -0.0336608774 -0.0602390566 -0.0254696320
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(.35, .30, .20, .15))
## # A tibble: 1 × 4
## AAPL GOOGL NFLX WMT
## <dbl> <dbl> <dbl> <dbl>
## 1 0.35 0.283 0.32 0.048
plot_data <- asset_returns_wide_tbl %>%
calculate_component_contribution(w = c(.35, .30, .20, .15)) %>%
# Transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
# Add weights
add_column(weight = c(.35, .30, .20, .15)) %>%
# 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 = "Pecent",
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 asset that contributes largest to the volatility of my portfolio is Apple. Apple contributes 35% towards the portfolio as a whole, but this is exactly equal to how much weight Apple holds here. However, the one asset that stands out the most to me is Netflix as its weight is not close to its contribution to portfolio volatility. Its risk share, so to speak, is 32% compared to only holding a portfolio weight 0f 15%. Despite the low weight, Netflix nearly rivals apple in terms of overall contribution to portfolio volatility.
This leads me to my next point. In terms of sheer size and technicality, my portfolio’s risk is concentrated in Apple with a 35% risk share. However, Apple, Google, and Netflix are all very close in portfolio risk meaning the standard deviation of this portfolio is not embedded in one singular asset. But, going back to my interesting tidbit above, if Netflix’s weight was any higher, it would be considered the riskiest asset of the portfolio. It holds the most volatility compared to what its weight is and if this weight was to increase, so would its volatility.