# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(readr)
# Time series
library(lubridate)
library(tibbletime)
# modeling
library(broom)
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", "NVDA", "GOOGL", "ORCL", "JNJ")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2025-11-19")
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
## GOOGL JNJ NVDA ORCL TSLA
## 2013-01-31 0.0660632363 0.053060754 0.000000000 0.0636567735 0.102078114
## 2013-02-28 0.0584792943 0.037163514 0.038221603 -0.0364198204 -0.074128640
## 2013-03-28 -0.0087879305 0.068791067 0.013338502 -0.0573990450 0.084208138
## 2013-04-30 0.0375394049 0.044382624 0.070706383 0.0138227608 0.354111531
## 2013-05-31 0.0550323642 -0.004907008 0.054651767 0.0300503665 0.593716684
## 2013-06-28 0.0104478684 0.019760457 -0.030167155 -0.0952805819 0.093672163
## 2013-07-31 0.0083479232 0.085243391 0.028092031 0.0558402711 0.223739522
## 2013-08-30 -0.0471076533 -0.071352229 0.026270533 -0.0152626930 0.229971642
## 2013-09-30 0.0336807849 0.003235254 0.053460133 0.0402940879 0.134706620
## 2013-10-31 0.1626137250 0.066058520 -0.024066129 0.0135164253 -0.189806595
## 2013-11-29 0.0277602670 0.028851485 0.032034371 0.0520545807 -0.228409405
## 2013-12-31 0.0560803026 -0.032969828 0.026567262 0.0808049311 0.167108541
## 2014-01-31 0.0523738012 -0.034658306 -0.020177016 -0.0330165069 0.187261714
## 2014-02-28 0.0289427809 0.047598738 0.162107219 0.0581665569 0.299722785
## 2014-03-31 -0.0868640265 0.064219604 -0.025903867 0.0449962682 -0.160783242
## 2014-04-30 -0.0419811620 0.030676194 0.030788834 0.0022432132 -0.002690122
## 2014-05-30 0.0664845037 0.008621860 0.032886399 0.0275049625 -0.000577422
## 2014-06-30 0.0225206996 0.030670184 -0.024508407 -0.0361035630 0.144457224
## 2014-07-31 -0.0087955553 -0.044265300 -0.057729614 -0.0005533268 -0.072372676
## 2014-08-29 0.0048367480 0.042463222 0.110059762 0.0278334856 0.188794007
## 2014-09-30 0.0103351872 0.027198043 -0.052782631 -0.0814881499 -0.105566477
## 2014-10-31 -0.0355314306 0.011102369 0.057399020 0.0230059365 -0.004046477
## 2014-11-28 -0.0336483299 0.010843305 0.074852823 0.0825411832 0.011599801
## 2014-12-31 -0.0341227305 -0.034586770 -0.044863951 0.0586114258 -0.094774519
## 2015-01-30 0.0129003351 -0.043287774 -0.043318895 -0.0682382496 -0.088365289
## 2015-02-27 0.0456004612 0.030367037 0.142698864 0.0450436355 -0.001277808
## 2015-03-31 -0.0141948432 -0.018808335 -0.052582613 -0.0154081973 -0.074350051
## 2015-04-30 -0.0107480468 -0.014014369 0.058909118 0.0143327902 0.180226808
## 2015-05-29 -0.0063066193 0.016697166 0.001459399 -0.0029845273 0.103899574
## 2015-06-30 -0.0097294304 -0.027127363 -0.095716683 -0.0761796040 0.067300935
## 2015-07-31 0.1968015479 0.027826063 -0.007988191 -0.0052496964 -0.007896616
## 2015-08-31 -0.0148319296 -0.056565038 0.123595536 -0.0740313449 -0.066366250
## 2015-09-30 -0.0146948149 -0.006726056 0.092151214 -0.0265004681 -0.002653519
## 2015-10-30 0.1441987378 0.079061207 0.140555005 0.0765708707 -0.182659777
## 2015-11-30 0.0339446528 0.009421720 0.115405292 0.0033414250 0.106828586
## 2015-12-31 0.0196779511 0.014512470 0.038347287 -0.0646584303 0.041471519
## 2016-01-29 -0.0216463862 0.016606145 -0.118048318 -0.0019259208 -0.227360626
## 2016-02-29 -0.0597104828 0.014566389 0.071923635 0.0128610788 0.003810669
## 2016-03-31 0.0617443718 0.028023060 0.127654633 0.1064203642 0.179948109
## 2016-04-29 -0.0748524466 0.035231614 -0.002810478 -0.0222805855 0.046721797
## 2016-05-31 0.0562642557 0.012542024 0.276388498 0.0084934416 -0.075597968
## 2016-06-30 -0.0624283498 0.073626245 0.006187808 0.0179964210 -0.050296440
## 2016-07-29 0.1176172557 0.031885126 0.194443934 0.0063557566 0.100785334
## 2016-08-31 -0.0018845645 -0.041526589 0.073468739 0.0043760787 -0.102058091
## 2016-09-30 0.0178306943 -0.010190829 0.110693866 -0.0482082216 -0.038366372
## 2016-10-31 0.0072369172 -0.018281748 0.037805172 -0.0182706915 -0.031364583
## 2016-11-30 -0.0429129612 -0.034388029 0.260525379 0.0450399561 -0.043041267
## 2016-12-30 0.0211316800 0.034527155 0.146435567 -0.0442596965 0.120665178
## 2017-01-31 0.0344064980 -0.017158623 0.022602110 0.0461762227 0.164624916
## 2017-02-28 0.0297177730 0.082738115 -0.071874834 0.0599936846 -0.007730364
## 2017-03-31 0.0033910671 0.018966251 0.070843642 0.0463387121 0.107278727
## 2017-04-28 0.0866298992 -0.008708719 -0.043433766 0.0121141508 0.120916212
## 2017-05-31 0.0654865504 0.044592777 0.326021818 0.0095185258 0.082295892
## 2017-06-30 -0.0599207005 0.031014840 0.001453869 0.0995275007 0.058654468
## 2017-07-31 0.0168734011 0.003245140 0.117044795 -0.0004322815 -0.111459860
## 2017-08-31 0.0102486771 0.003700555 0.042639418 0.0079797263 0.095543446
## 2017-09-29 0.0191612587 -0.017989696 0.053601051 -0.0401356319 -0.042474144
## 2017-10-31 0.0591372329 0.069808000 0.145700510 0.0553336876 -0.028457409
## 2017-11-30 0.0030254217 0.005531008 -0.029245281 -0.0368188706 -0.070862541
## 2017-12-29 0.0164917239 0.002795358 -0.036583449 -0.0369566609 0.008061928
## 2018-01-31 0.1153710866 -0.011011086 0.239240793 0.0911269175 0.129254571
## 2018-02-28 -0.0685266960 -0.055635286 -0.014959122 -0.0179936602 -0.032266877
## 2018-03-29 -0.0624004789 -0.013409764 -0.043968799 -0.1021425774 -0.253920408
## 2018-04-30 -0.0180574801 -0.013038019 -0.029313000 0.0023816220 0.099254576
## 2018-05-31 0.0769007461 -0.048454051 0.115145172 0.0227309004 -0.031698139
## 2018-06-29 0.0261902858 0.014276474 -0.062544483 -0.0586199384 0.186043257
## 2018-07-31 0.0832509941 0.088136789 0.033048238 0.0828741231 -0.140021491
## 2018-08-31 0.0037250797 0.022884757 0.137075391 0.0187000061 0.011737389
## 2018-09-28 -0.0202722585 0.025508382 0.001210556 0.0595344030 -0.130439038
## 2018-10-31 -0.1014946112 0.013086722 -0.287373476 -0.0501784545 0.242170576
## 2018-11-30 0.0173349752 0.054528800 -0.253667428 -0.0016392076 0.038271580
## 2018-12-31 -0.0600659903 -0.129552122 -0.202283237 -0.0769200521 -0.051761952
## 2019-01-31 0.0745951838 0.030750443 0.073974237 0.1105732693 -0.080628789
## 2019-02-28 0.0005859974 0.033009694 0.071593837 0.0371283925 0.041032981
## 2019-03-29 0.0437155813 0.022791490 0.151870079 0.0298585768 -0.133656413
## 2019-04-30 0.0185791859 0.010036265 0.007987546 0.0341846075 -0.159123803
## 2019-05-31 -0.0802526680 -0.067016582 -0.288679893 -0.0893638706 -0.253945372
## 2019-06-28 -0.0216516864 0.060144556 0.192591650 0.1185732763 0.188012109
## 2019-07-31 0.1178240652 -0.067261009 0.026972636 -0.0077903821 0.078092373
## 2019-08-30 -0.0229757140 -0.006921059 -0.006208219 -0.0782979557 -0.068516948
## 2019-09-30 0.0253862532 0.007914775 0.038414351 0.0554817267 0.065449565
## 2019-10-31 0.0303740414 0.020351367 0.143947110 -0.0054109754 0.268061253
## 2019-11-29 0.0353464853 0.047350652 0.076031308 0.0298314205 0.046592176
## 2019-12-31 0.0267089914 0.059164410 0.082162985 -0.0579342710 0.237359743
## 2020-01-31 0.0674022889 0.020357539 0.004790775 -0.0056128915 0.441578342
## 2020-02-28 -0.0675069091 -0.095301299 0.133626951 -0.0586960894 0.026424253
## 2020-03-31 -0.1420101889 -0.025226300 -0.024248267 -0.0231116876 -0.242781458
## 2020-04-30 0.1475576024 0.134712480 0.103279387 0.0964125399 0.400209535
## 2020-05-29 0.0624759003 -0.001726558 0.194461732 0.0149899060 0.065730500
## 2020-06-30 -0.0108504069 -0.056134746 0.068216908 0.0275146837 0.257108657
## 2020-07-31 0.0481167775 0.035829163 0.111189180 0.0074700670 0.281420674
## 2020-08-31 0.0908922901 0.057786244 0.231105710 0.0314217159 0.554719320
## 2020-09-30 -0.1060270501 -0.029973259 0.011895355 0.0424286463 -0.149762306
## 2020-10-30 0.0977573409 -0.082357109 -0.076501460 -0.0579769157 -0.100371771
## 2020-11-30 0.0821049517 0.060665504 0.066921820 0.0282896815 0.380308519
## 2020-12-31 -0.0010037544 0.084138999 -0.025899910 0.1140029343 0.217730753
## 2021-01-29 0.0417491045 0.035884051 -0.005010726 -0.0643016863 0.117343701
## 2021-02-26 0.1011702093 -0.022828866 0.054293189 0.0653345591 -0.161038203
## 2021-03-31 0.0198859540 0.036496072 -0.026723310 0.0841007191 -0.011269836
## 2021-04-30 0.1319750499 -0.009905989 0.117298025 0.0813625696 0.060292565
## 2021-05-28 0.0014224538 0.045504067 0.079071146 0.0381851194 -0.126372343
## 2021-06-30 0.0354055391 -0.027008303 0.208331984 -0.0114961501 0.083547955
## 2021-07-30 0.0984923432 0.044288164 -0.025494023 0.1165427007 0.010973846
## 2021-08-31 0.0713984116 0.011310664 0.138204150 0.0225799114 0.068224268
## 2021-09-30 -0.0792263636 -0.069537338 -0.077484777 -0.0226947591 0.052632612
## 2021-10-29 0.1021043452 0.008508712 0.210396074 0.0997945229 0.362230202
## 2021-11-30 -0.0424181740 -0.037078241 0.245338233 -0.0557246831 0.027237848
## 2021-12-31 0.0206074785 0.092665699 -0.105149407 -0.0396793126 -0.079968440
## 2022-01-31 -0.0681922340 0.007106292 -0.183267317 -0.0681882315 -0.120597475
## 2022-02-28 -0.0018233819 -0.039444185 -0.004133207 -0.0660842371 -0.073397011
## 2022-03-31 0.0292625405 0.074112141 0.112575757 0.0852437319 0.213504290
## 2022-04-29 -0.1978010326 0.018060787 -0.386065586 -0.1157541244 -0.213125289
## 2022-05-31 -0.0030498641 0.001238676 0.006716914 -0.0203695353 -0.138340062
## 2022-06-30 -0.0431002502 -0.011315486 -0.208219093 -0.0289176782 -0.118657126
## 2022-07-29 0.0653366808 -0.016987538 0.180792289 0.1124810629 0.280480149
## 2022-08-31 -0.0721787637 -0.071828917 -0.185089489 -0.0485649760 -0.075250271
## 2022-09-30 -0.1234704585 0.012442282 -0.217576962 -0.1940695811 -0.038313992
## 2022-10-31 -0.0119900816 0.062926820 0.106044152 0.2507125773 -0.153346760
## 2022-11-30 0.0663158756 0.029334465 0.226461906 0.0615960980 -0.155866121
## 2022-12-30 -0.1350744950 -0.007613319 -0.146693474 -0.0156586985 -0.457813194
## 2023-01-31 0.1135552574 -0.077846894 0.290330101 0.0827499149 0.340915767
## 2023-02-28 -0.0930261625 -0.057021157 0.172531600 -0.0120550742 0.171904973
## 2023-03-31 0.1413153217 0.011289516 0.179536380 0.0612434143 0.008471140
## 2023-04-28 0.0342099375 0.054610312 -0.001008558 0.0233651819 -0.233183710
## 2023-05-31 0.1351254510 -0.046706546 0.310008438 0.1119477828 0.216021889
## 2023-06-30 -0.0261383286 0.065279834 0.111729531 0.1170064512 0.249689452
## 2023-07-31 0.1032530520 0.012070398 0.099530656 -0.0122382037 0.021391609
## 2023-08-31 0.0256625218 -0.028309127 0.054674204 0.0265984787 -0.035588260
## 2023-09-29 -0.0397762381 -0.037366991 -0.126218673 -0.1280524264 -0.030929027
## 2023-10-31 -0.0532014207 -0.048745389 -0.064546301 -0.0204264361 -0.219831983
## 2023-11-30 0.0658825893 0.049698574 0.137050160 0.1167938296 0.178463656
## 2023-12-29 0.0526166950 0.013359304 0.057263038 -0.0973516310 0.034390133
## 2024-01-31 0.0029309133 0.013686634 0.217059118 0.0616368212 -0.282704160
## 2024-02-29 -0.0117752170 0.023050953 0.251388415 -0.0001789835 0.075015304
## 2024-03-28 0.0862345793 -0.019965061 0.132939745 0.1175442329 -0.138383423
## 2024-04-30 0.0755835466 -0.089894746 -0.044746579 -0.0959569793 0.041724969
## 2024-05-31 0.0579976865 0.022333937 0.238127649 0.0297935993 -0.028782134
## 2024-06-28 0.0555803201 -0.003483159 0.119508657 0.1863807684 0.105427913
## 2024-07-31 -0.0600140847 0.076943228 -0.054220237 -0.0096517999 0.159378271
## 2024-08-30 -0.0487375884 0.057060246 0.019883211 0.0131083161 -0.080549177
## 2024-09-30 0.0163310513 -0.023177269 0.017277928 0.1873343227 0.200441406
## 2024-10-31 0.0312227830 -0.013668357 0.089122631 -0.0128915674 -0.046070548
## 2024-11-29 -0.0127038811 -0.022816984 0.040520710 0.0964794610 0.323147326
## 2024-12-31 0.1148757734 -0.069379958 -0.028993106 -0.1036547450 0.157010663
## 2025-01-31 0.0748849616 0.050757164 -0.111926736 0.0227704555 0.001880189
## 2025-02-28 -0.1807740431 0.089172670 0.039598579 -0.0238022114 -0.322794555
## 2025-03-31 -0.0951931520 0.004956838 -0.141937962 -0.1720647672 -0.122658739
## 2025-04-30 0.0265457341 -0.059182340 0.004970099 0.0100735041 0.085028653
## 2025-05-30 0.0783361038 0.001474289 0.215623673 0.1623803668 0.205293048
## 2025-06-30 0.0270181626 -0.015976457 0.156363856 0.2782283503 -0.086785707
## 2025-07-31 0.0851844061 0.075565872 0.118521186 0.1511701709 -0.030005653
## 2025-08-29 0.1038951429 0.080054478 -0.020963769 -0.1153182866 0.079775694
## 2025-09-30 0.1334973732 0.045513799 0.068827324 0.2180982722 0.286693192
## 2025-10-31 0.1455576997 0.018435410 0.081830418 -0.0668044992 0.026275247
## 2025-11-18 0.0109290576 0.057258445 -0.110206520 -0.1748176188 -0.129135446
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## GOOGL JNJ NVDA ORCL TSLA
## GOOGL 0.0046293639 0.0004939527 0.0037751744 0.0017560433 0.0040125321
## JNJ 0.0004939527 0.0020288420 0.0004849893 0.0008354829 0.0006556015
## NVDA 0.0037751744 0.0004849893 0.0146658200 0.0044502602 0.0063942705
## ORCL 0.0017560433 0.0008354829 0.0044502602 0.0057588035 0.0029027902
## TSLA 0.0040125321 0.0006556015 0.0063942705 0.0029027902 0.0289838743
# 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.25, 0.25, 0.2, 0.2, 0.1)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.05684351
# 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
## GOOGL JNJ NVDA ORCL TSLA
## [1,] 0.01226317 0.004223672 0.01944878 0.01048483 0.01042306
rowSums(component_contribution)
## [1] 0.05684351
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## GOOGL JNJ NVDA ORCL TSLA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.216 0.074 0.342 0.184 0.183
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 GOOGL 0.216
## 2 JNJ 0.074
## 3 NVDA 0.342
## 4 ORCL 0.184
## 5 TSLA 0.183
# Transform data into wide form
calculate_component_contribution <- asset_returns_tbl %>%
pivot_wider(names_from = asset, values_from = returns) %>%
column_to_rownames(var = "date")
asset_returns_wide_tbl
## GOOGL JNJ NVDA ORCL TSLA
## 2013-01-31 0.0660632363 0.053060754 0.000000000 0.0636567735 0.102078114
## 2013-02-28 0.0584792943 0.037163514 0.038221603 -0.0364198204 -0.074128640
## 2013-03-28 -0.0087879305 0.068791067 0.013338502 -0.0573990450 0.084208138
## 2013-04-30 0.0375394049 0.044382624 0.070706383 0.0138227608 0.354111531
## 2013-05-31 0.0550323642 -0.004907008 0.054651767 0.0300503665 0.593716684
## 2013-06-28 0.0104478684 0.019760457 -0.030167155 -0.0952805819 0.093672163
## 2013-07-31 0.0083479232 0.085243391 0.028092031 0.0558402711 0.223739522
## 2013-08-30 -0.0471076533 -0.071352229 0.026270533 -0.0152626930 0.229971642
## 2013-09-30 0.0336807849 0.003235254 0.053460133 0.0402940879 0.134706620
## 2013-10-31 0.1626137250 0.066058520 -0.024066129 0.0135164253 -0.189806595
## 2013-11-29 0.0277602670 0.028851485 0.032034371 0.0520545807 -0.228409405
## 2013-12-31 0.0560803026 -0.032969828 0.026567262 0.0808049311 0.167108541
## 2014-01-31 0.0523738012 -0.034658306 -0.020177016 -0.0330165069 0.187261714
## 2014-02-28 0.0289427809 0.047598738 0.162107219 0.0581665569 0.299722785
## 2014-03-31 -0.0868640265 0.064219604 -0.025903867 0.0449962682 -0.160783242
## 2014-04-30 -0.0419811620 0.030676194 0.030788834 0.0022432132 -0.002690122
## 2014-05-30 0.0664845037 0.008621860 0.032886399 0.0275049625 -0.000577422
## 2014-06-30 0.0225206996 0.030670184 -0.024508407 -0.0361035630 0.144457224
## 2014-07-31 -0.0087955553 -0.044265300 -0.057729614 -0.0005533268 -0.072372676
## 2014-08-29 0.0048367480 0.042463222 0.110059762 0.0278334856 0.188794007
## 2014-09-30 0.0103351872 0.027198043 -0.052782631 -0.0814881499 -0.105566477
## 2014-10-31 -0.0355314306 0.011102369 0.057399020 0.0230059365 -0.004046477
## 2014-11-28 -0.0336483299 0.010843305 0.074852823 0.0825411832 0.011599801
## 2014-12-31 -0.0341227305 -0.034586770 -0.044863951 0.0586114258 -0.094774519
## 2015-01-30 0.0129003351 -0.043287774 -0.043318895 -0.0682382496 -0.088365289
## 2015-02-27 0.0456004612 0.030367037 0.142698864 0.0450436355 -0.001277808
## 2015-03-31 -0.0141948432 -0.018808335 -0.052582613 -0.0154081973 -0.074350051
## 2015-04-30 -0.0107480468 -0.014014369 0.058909118 0.0143327902 0.180226808
## 2015-05-29 -0.0063066193 0.016697166 0.001459399 -0.0029845273 0.103899574
## 2015-06-30 -0.0097294304 -0.027127363 -0.095716683 -0.0761796040 0.067300935
## 2015-07-31 0.1968015479 0.027826063 -0.007988191 -0.0052496964 -0.007896616
## 2015-08-31 -0.0148319296 -0.056565038 0.123595536 -0.0740313449 -0.066366250
## 2015-09-30 -0.0146948149 -0.006726056 0.092151214 -0.0265004681 -0.002653519
## 2015-10-30 0.1441987378 0.079061207 0.140555005 0.0765708707 -0.182659777
## 2015-11-30 0.0339446528 0.009421720 0.115405292 0.0033414250 0.106828586
## 2015-12-31 0.0196779511 0.014512470 0.038347287 -0.0646584303 0.041471519
## 2016-01-29 -0.0216463862 0.016606145 -0.118048318 -0.0019259208 -0.227360626
## 2016-02-29 -0.0597104828 0.014566389 0.071923635 0.0128610788 0.003810669
## 2016-03-31 0.0617443718 0.028023060 0.127654633 0.1064203642 0.179948109
## 2016-04-29 -0.0748524466 0.035231614 -0.002810478 -0.0222805855 0.046721797
## 2016-05-31 0.0562642557 0.012542024 0.276388498 0.0084934416 -0.075597968
## 2016-06-30 -0.0624283498 0.073626245 0.006187808 0.0179964210 -0.050296440
## 2016-07-29 0.1176172557 0.031885126 0.194443934 0.0063557566 0.100785334
## 2016-08-31 -0.0018845645 -0.041526589 0.073468739 0.0043760787 -0.102058091
## 2016-09-30 0.0178306943 -0.010190829 0.110693866 -0.0482082216 -0.038366372
## 2016-10-31 0.0072369172 -0.018281748 0.037805172 -0.0182706915 -0.031364583
## 2016-11-30 -0.0429129612 -0.034388029 0.260525379 0.0450399561 -0.043041267
## 2016-12-30 0.0211316800 0.034527155 0.146435567 -0.0442596965 0.120665178
## 2017-01-31 0.0344064980 -0.017158623 0.022602110 0.0461762227 0.164624916
## 2017-02-28 0.0297177730 0.082738115 -0.071874834 0.0599936846 -0.007730364
## 2017-03-31 0.0033910671 0.018966251 0.070843642 0.0463387121 0.107278727
## 2017-04-28 0.0866298992 -0.008708719 -0.043433766 0.0121141508 0.120916212
## 2017-05-31 0.0654865504 0.044592777 0.326021818 0.0095185258 0.082295892
## 2017-06-30 -0.0599207005 0.031014840 0.001453869 0.0995275007 0.058654468
## 2017-07-31 0.0168734011 0.003245140 0.117044795 -0.0004322815 -0.111459860
## 2017-08-31 0.0102486771 0.003700555 0.042639418 0.0079797263 0.095543446
## 2017-09-29 0.0191612587 -0.017989696 0.053601051 -0.0401356319 -0.042474144
## 2017-10-31 0.0591372329 0.069808000 0.145700510 0.0553336876 -0.028457409
## 2017-11-30 0.0030254217 0.005531008 -0.029245281 -0.0368188706 -0.070862541
## 2017-12-29 0.0164917239 0.002795358 -0.036583449 -0.0369566609 0.008061928
## 2018-01-31 0.1153710866 -0.011011086 0.239240793 0.0911269175 0.129254571
## 2018-02-28 -0.0685266960 -0.055635286 -0.014959122 -0.0179936602 -0.032266877
## 2018-03-29 -0.0624004789 -0.013409764 -0.043968799 -0.1021425774 -0.253920408
## 2018-04-30 -0.0180574801 -0.013038019 -0.029313000 0.0023816220 0.099254576
## 2018-05-31 0.0769007461 -0.048454051 0.115145172 0.0227309004 -0.031698139
## 2018-06-29 0.0261902858 0.014276474 -0.062544483 -0.0586199384 0.186043257
## 2018-07-31 0.0832509941 0.088136789 0.033048238 0.0828741231 -0.140021491
## 2018-08-31 0.0037250797 0.022884757 0.137075391 0.0187000061 0.011737389
## 2018-09-28 -0.0202722585 0.025508382 0.001210556 0.0595344030 -0.130439038
## 2018-10-31 -0.1014946112 0.013086722 -0.287373476 -0.0501784545 0.242170576
## 2018-11-30 0.0173349752 0.054528800 -0.253667428 -0.0016392076 0.038271580
## 2018-12-31 -0.0600659903 -0.129552122 -0.202283237 -0.0769200521 -0.051761952
## 2019-01-31 0.0745951838 0.030750443 0.073974237 0.1105732693 -0.080628789
## 2019-02-28 0.0005859974 0.033009694 0.071593837 0.0371283925 0.041032981
## 2019-03-29 0.0437155813 0.022791490 0.151870079 0.0298585768 -0.133656413
## 2019-04-30 0.0185791859 0.010036265 0.007987546 0.0341846075 -0.159123803
## 2019-05-31 -0.0802526680 -0.067016582 -0.288679893 -0.0893638706 -0.253945372
## 2019-06-28 -0.0216516864 0.060144556 0.192591650 0.1185732763 0.188012109
## 2019-07-31 0.1178240652 -0.067261009 0.026972636 -0.0077903821 0.078092373
## 2019-08-30 -0.0229757140 -0.006921059 -0.006208219 -0.0782979557 -0.068516948
## 2019-09-30 0.0253862532 0.007914775 0.038414351 0.0554817267 0.065449565
## 2019-10-31 0.0303740414 0.020351367 0.143947110 -0.0054109754 0.268061253
## 2019-11-29 0.0353464853 0.047350652 0.076031308 0.0298314205 0.046592176
## 2019-12-31 0.0267089914 0.059164410 0.082162985 -0.0579342710 0.237359743
## 2020-01-31 0.0674022889 0.020357539 0.004790775 -0.0056128915 0.441578342
## 2020-02-28 -0.0675069091 -0.095301299 0.133626951 -0.0586960894 0.026424253
## 2020-03-31 -0.1420101889 -0.025226300 -0.024248267 -0.0231116876 -0.242781458
## 2020-04-30 0.1475576024 0.134712480 0.103279387 0.0964125399 0.400209535
## 2020-05-29 0.0624759003 -0.001726558 0.194461732 0.0149899060 0.065730500
## 2020-06-30 -0.0108504069 -0.056134746 0.068216908 0.0275146837 0.257108657
## 2020-07-31 0.0481167775 0.035829163 0.111189180 0.0074700670 0.281420674
## 2020-08-31 0.0908922901 0.057786244 0.231105710 0.0314217159 0.554719320
## 2020-09-30 -0.1060270501 -0.029973259 0.011895355 0.0424286463 -0.149762306
## 2020-10-30 0.0977573409 -0.082357109 -0.076501460 -0.0579769157 -0.100371771
## 2020-11-30 0.0821049517 0.060665504 0.066921820 0.0282896815 0.380308519
## 2020-12-31 -0.0010037544 0.084138999 -0.025899910 0.1140029343 0.217730753
## 2021-01-29 0.0417491045 0.035884051 -0.005010726 -0.0643016863 0.117343701
## 2021-02-26 0.1011702093 -0.022828866 0.054293189 0.0653345591 -0.161038203
## 2021-03-31 0.0198859540 0.036496072 -0.026723310 0.0841007191 -0.011269836
## 2021-04-30 0.1319750499 -0.009905989 0.117298025 0.0813625696 0.060292565
## 2021-05-28 0.0014224538 0.045504067 0.079071146 0.0381851194 -0.126372343
## 2021-06-30 0.0354055391 -0.027008303 0.208331984 -0.0114961501 0.083547955
## 2021-07-30 0.0984923432 0.044288164 -0.025494023 0.1165427007 0.010973846
## 2021-08-31 0.0713984116 0.011310664 0.138204150 0.0225799114 0.068224268
## 2021-09-30 -0.0792263636 -0.069537338 -0.077484777 -0.0226947591 0.052632612
## 2021-10-29 0.1021043452 0.008508712 0.210396074 0.0997945229 0.362230202
## 2021-11-30 -0.0424181740 -0.037078241 0.245338233 -0.0557246831 0.027237848
## 2021-12-31 0.0206074785 0.092665699 -0.105149407 -0.0396793126 -0.079968440
## 2022-01-31 -0.0681922340 0.007106292 -0.183267317 -0.0681882315 -0.120597475
## 2022-02-28 -0.0018233819 -0.039444185 -0.004133207 -0.0660842371 -0.073397011
## 2022-03-31 0.0292625405 0.074112141 0.112575757 0.0852437319 0.213504290
## 2022-04-29 -0.1978010326 0.018060787 -0.386065586 -0.1157541244 -0.213125289
## 2022-05-31 -0.0030498641 0.001238676 0.006716914 -0.0203695353 -0.138340062
## 2022-06-30 -0.0431002502 -0.011315486 -0.208219093 -0.0289176782 -0.118657126
## 2022-07-29 0.0653366808 -0.016987538 0.180792289 0.1124810629 0.280480149
## 2022-08-31 -0.0721787637 -0.071828917 -0.185089489 -0.0485649760 -0.075250271
## 2022-09-30 -0.1234704585 0.012442282 -0.217576962 -0.1940695811 -0.038313992
## 2022-10-31 -0.0119900816 0.062926820 0.106044152 0.2507125773 -0.153346760
## 2022-11-30 0.0663158756 0.029334465 0.226461906 0.0615960980 -0.155866121
## 2022-12-30 -0.1350744950 -0.007613319 -0.146693474 -0.0156586985 -0.457813194
## 2023-01-31 0.1135552574 -0.077846894 0.290330101 0.0827499149 0.340915767
## 2023-02-28 -0.0930261625 -0.057021157 0.172531600 -0.0120550742 0.171904973
## 2023-03-31 0.1413153217 0.011289516 0.179536380 0.0612434143 0.008471140
## 2023-04-28 0.0342099375 0.054610312 -0.001008558 0.0233651819 -0.233183710
## 2023-05-31 0.1351254510 -0.046706546 0.310008438 0.1119477828 0.216021889
## 2023-06-30 -0.0261383286 0.065279834 0.111729531 0.1170064512 0.249689452
## 2023-07-31 0.1032530520 0.012070398 0.099530656 -0.0122382037 0.021391609
## 2023-08-31 0.0256625218 -0.028309127 0.054674204 0.0265984787 -0.035588260
## 2023-09-29 -0.0397762381 -0.037366991 -0.126218673 -0.1280524264 -0.030929027
## 2023-10-31 -0.0532014207 -0.048745389 -0.064546301 -0.0204264361 -0.219831983
## 2023-11-30 0.0658825893 0.049698574 0.137050160 0.1167938296 0.178463656
## 2023-12-29 0.0526166950 0.013359304 0.057263038 -0.0973516310 0.034390133
## 2024-01-31 0.0029309133 0.013686634 0.217059118 0.0616368212 -0.282704160
## 2024-02-29 -0.0117752170 0.023050953 0.251388415 -0.0001789835 0.075015304
## 2024-03-28 0.0862345793 -0.019965061 0.132939745 0.1175442329 -0.138383423
## 2024-04-30 0.0755835466 -0.089894746 -0.044746579 -0.0959569793 0.041724969
## 2024-05-31 0.0579976865 0.022333937 0.238127649 0.0297935993 -0.028782134
## 2024-06-28 0.0555803201 -0.003483159 0.119508657 0.1863807684 0.105427913
## 2024-07-31 -0.0600140847 0.076943228 -0.054220237 -0.0096517999 0.159378271
## 2024-08-30 -0.0487375884 0.057060246 0.019883211 0.0131083161 -0.080549177
## 2024-09-30 0.0163310513 -0.023177269 0.017277928 0.1873343227 0.200441406
## 2024-10-31 0.0312227830 -0.013668357 0.089122631 -0.0128915674 -0.046070548
## 2024-11-29 -0.0127038811 -0.022816984 0.040520710 0.0964794610 0.323147326
## 2024-12-31 0.1148757734 -0.069379958 -0.028993106 -0.1036547450 0.157010663
## 2025-01-31 0.0748849616 0.050757164 -0.111926736 0.0227704555 0.001880189
## 2025-02-28 -0.1807740431 0.089172670 0.039598579 -0.0238022114 -0.322794555
## 2025-03-31 -0.0951931520 0.004956838 -0.141937962 -0.1720647672 -0.122658739
## 2025-04-30 0.0265457341 -0.059182340 0.004970099 0.0100735041 0.085028653
## 2025-05-30 0.0783361038 0.001474289 0.215623673 0.1623803668 0.205293048
## 2025-06-30 0.0270181626 -0.015976457 0.156363856 0.2782283503 -0.086785707
## 2025-07-31 0.0851844061 0.075565872 0.118521186 0.1511701709 -0.030005653
## 2025-08-29 0.1038951429 0.080054478 -0.020963769 -0.1153182866 0.079775694
## 2025-09-30 0.1334973732 0.045513799 0.068827324 0.2180982722 0.286693192
## 2025-10-31 0.1455576997 0.018435410 0.081830418 -0.0668044992 0.026275247
## 2025-11-18 0.0109290576 0.057258445 -0.110206520 -0.1748176188 -0.129135446
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
# 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(.25, .25, .2, .2, .1))
## # A tibble: 1 × 5
## GOOGL JNJ NVDA ORCL TSLA
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.216 0.074 0.342 0.184 0.183
Column Chart of Component Contribution and Weight
plot_data <- calculate_component_contribution(asset_returns_wide_tbl,
w = c(.25, .25, .2, .2, .1)) %>%
as_tibble() %>%
pivot_longer(cols = everything(),
names_to = "asset",
values_to = "contribution") %>%
mutate(weight = c(.25, .25, .2, .2, .1)) %>%
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?
NVDA is the highest contributor to my portfolio volatility, but I don’t think all of the volatility is on NVDA. As the graph suggests, GOOGL, ORCL and TSLA all have high contributions, but overshadowed by NVDA because it’s high, and technically the most concentrated.