# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(broom)
library(lubridate)
library(tibbletime)
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("SBUX", "GOOGL", "ABT", "GME", "WMT")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2024-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
## ABT GME GOOGL SBUX WMT
## 2013-01-31 0.0793053432 -0.078317043 0.066063335 0.045383119 0.0248965283
## 2013-02-28 -0.0026599630 0.077120544 0.058479372 -0.019138872 0.0117952905
## 2013-03-28 0.0442845792 0.120894320 -0.008787947 0.037571510 0.0620733389
## 2013-04-30 0.0480842011 0.221353977 0.037539425 0.066073753 0.0378936698
## 2013-05-31 -0.0067945027 -0.042899588 0.055032387 0.040473415 -0.0317801540
## 2013-06-28 -0.0500451725 0.237039171 0.010447798 0.036848664 -0.0046874760
## 2013-07-31 0.0529921253 0.154660476 0.008347829 0.084553220 0.0452744790
## 2013-08-30 -0.0944095916 0.028696648 -0.047107529 -0.008016812 -0.0596997341
## 2013-09-30 -0.0042093726 -0.011215904 0.033680780 0.087519403 0.0133388544
## 2013-10-31 0.1006622158 0.099056608 0.162613645 0.051650617 0.0370287906
## 2013-11-29 0.0438924221 -0.127659169 0.027760265 0.008261147 0.0540195499
## 2013-12-31 0.0036591818 0.026432185 0.056080427 -0.038416047 -0.0232520985
## 2014-01-31 -0.0389713909 -0.339766318 0.052373680 -0.097327793 -0.0523039027
## 2014-02-28 0.0816782444 0.061915362 0.028942854 0.001525095 0.0002674148
## 2014-03-31 -0.0324465657 0.105355101 -0.086864165 0.033535004 0.0293263555
## 2014-04-30 0.0118769033 -0.035160883 -0.041981023 -0.038337851 0.0420198637
## 2014-05-30 0.0322565376 -0.047216367 0.066484522 0.040101180 -0.0314092639
## 2014-06-30 0.0220007278 0.075687329 0.022520641 0.054986260 -0.0223926840
## 2014-07-31 0.0347413755 0.036393675 -0.008795566 0.003869864 -0.0200479808
## 2014-08-29 0.0028450323 0.013138819 0.004836839 0.005030828 0.0323260123
## 2014-09-30 -0.0155076743 -0.023982027 0.010335146 -0.030667131 0.0127655778
## 2014-10-31 0.0522663971 0.037165035 -0.035531479 0.001324476 -0.0026187429
## 2014-11-28 0.0208860030 -0.115478960 -0.033648376 0.076233311 0.1378161286
## 2014-12-31 0.0113930229 -0.112112902 -0.034122526 0.010290631 -0.0135737554
## 2015-01-30 -0.0005127505 0.042004796 0.012900186 0.064652707 -0.0105348515
## 2015-02-27 0.0566747119 0.047641367 0.045600426 0.069516429 -0.0124333137
## 2015-03-31 -0.0221990682 0.035540052 -0.014194811 0.012859123 -0.0142312626
## 2015-04-30 0.0070432663 0.015163523 -0.010747988 0.046021169 -0.0524135556
## 2015-05-29 0.0458932808 0.118993487 -0.006306612 0.050063080 -0.0433512577
## 2015-06-30 0.0098282723 -0.002231125 -0.009729557 0.031447602 -0.0460135639
## 2015-07-31 0.0370949589 0.065105815 0.196801618 0.077313546 0.0146949011
## 2015-08-31 -0.1126422136 -0.076341704 -0.014831965 -0.054435703 -0.0993584482
## 2015-09-30 -0.1187218081 -0.021684221 -0.014694816 0.038193945 0.0016980327
## 2015-10-30 0.1136336169 0.111481128 0.144198855 0.096045476 -0.1246702419
## 2015-11-30 0.0026749735 -0.273957358 0.033944592 -0.015806549 0.0275694303
## 2015-12-31 -0.0002226917 -0.212242361 0.019677922 -0.022402137 0.0492988000
## 2016-01-29 -0.1647383137 -0.067491205 -0.021646245 0.012251923 0.0793148227
## 2016-02-29 0.0232417400 0.162022934 -0.059710543 -0.039776765 -0.0003014758
## 2016-03-31 0.0767407537 0.040474604 0.061744333 0.025274892 0.0392705183
## 2016-04-29 -0.0665289364 0.033165905 -0.074852279 -0.059881614 -0.0239372467
## 2016-05-31 0.0185921877 -0.119690253 0.056264044 -0.020625970 0.0641211298
## 2016-06-30 -0.0081076368 -0.077571402 -0.062428276 0.039822796 0.0311566439
## 2016-07-29 0.1357534635 0.152214066 0.117617259 0.016150348 -0.0006849025
## 2016-08-31 -0.0629458206 -0.086336085 -0.001884707 -0.028377715 -0.0143680021
## 2016-09-30 0.0064048863 -0.015455863 0.017830815 -0.037877266 0.0094733988
## 2016-10-31 -0.0685169538 -0.137318649 0.007236987 -0.019961462 -0.0295504965
## 2016-11-30 -0.0302701868 0.041084567 -0.042912958 0.092935336 0.0058383964
## 2016-12-30 0.0088916222 0.022823795 0.021131558 -0.043182241 -0.0116436327
## 2017-01-31 0.0902991448 -0.030957251 0.034406548 -0.005418103 -0.0350396199
## 2017-02-28 0.0762601694 -0.002043695 0.029717749 0.033938604 0.0608888599
## 2017-03-31 -0.0149740836 -0.065169448 0.003390998 0.026376767 0.0234093047
## 2017-04-28 -0.0113927392 0.006189341 0.086629965 0.028199501 0.0421086178
## 2017-05-31 0.0452487294 -0.024538549 0.065486476 0.061537407 0.0511561772
## 2017-06-30 0.0626065674 -0.007325286 -0.059920727 -0.086997079 -0.0378577590
## 2017-07-31 0.0171765613 0.003695213 0.016873469 -0.077159972 0.0553880197
## 2017-08-31 0.0351614956 -0.159080714 0.010248746 0.020674968 -0.0180257371
## 2017-09-29 0.0464129103 0.130272636 0.019161110 -0.021185404 0.0008962783
## 2017-10-31 0.0210525859 -0.100210764 0.059137433 0.020820798 0.1109626381
## 2017-11-30 0.0387026070 0.022864475 0.003025297 0.058198502 0.1076145307
## 2017-12-29 0.0123416425 -0.043603633 0.016491788 -0.006768043 0.0207686279
## 2018-01-31 0.0902039003 -0.065616308 0.115371007 -0.010854461 0.0764920822
## 2018-02-28 -0.0298821832 -0.068950397 -0.068526562 0.010501073 -0.1691628229
## 2018-03-29 -0.0068193706 -0.193658391 -0.062400574 0.013740669 -0.0056771153
## 2018-04-30 -0.0255261873 0.078456645 -0.018057369 -0.005543268 -0.0057488218
## 2018-05-31 0.0568427505 -0.033522642 0.076900663 -0.010541040 -0.0629875024
## 2018-06-29 -0.0088147263 0.125248578 0.026190373 -0.148490363 0.0369862313
## 2018-07-31 0.0764358234 -0.011042227 0.083250885 0.069961108 0.0409480874
## 2018-08-31 0.0196407502 -0.082416485 0.003725084 0.026968782 0.0774627843
## 2018-09-28 0.0930774544 0.163276878 -0.020272252 0.061493765 -0.0205518883
## 2018-10-31 -0.0580391467 -0.044868592 -0.101494616 0.024847071 0.0656293407
## 2018-11-30 0.0715041801 -0.066549693 0.017334992 0.140772113 -0.0265766097
## 2018-12-31 -0.0235020173 -0.051018160 -0.060066014 -0.035391221 -0.0417362843
## 2019-01-31 0.0135728191 -0.106946524 0.074595188 0.056450963 0.0283646158
## 2019-02-28 0.0616396805 0.031252541 0.000586073 0.035843343 0.0324430877
## 2019-03-29 0.0294512207 -0.107764703 0.043715456 0.056446433 -0.0094926145
## 2019-04-30 -0.0006804100 -0.160899150 0.018579188 0.043948843 0.0530143432
## 2019-05-31 -0.0440695577 -0.132046087 -0.080252613 -0.016446724 -0.0084084853
## 2019-06-28 0.0995644268 -0.326234612 -0.021651647 0.097268230 0.0854573764
## 2019-07-31 0.0387883334 -0.307996680 0.117824128 0.121817749 -0.0009960789
## 2019-08-30 -0.0206481986 -0.012515796 -0.022975811 0.023338924 0.0394581229
## 2019-09-30 -0.0195282474 0.329611755 0.025386239 -0.088066438 0.0379543721
## 2019-10-31 0.0032836953 -0.014598785 0.030374045 -0.044636286 -0.0120373469
## 2019-11-29 0.0217679517 0.153099721 0.035346605 0.015222003 0.0154859431
## 2019-12-31 0.0163666675 -0.041874109 0.026708732 0.028729822 0.0023737783
## 2020-01-31 0.0074565723 -0.459532339 0.067402445 -0.035778165 -0.0372904697
## 2020-02-28 -0.1233213296 -0.064538525 -0.067506949 -0.073792745 -0.0613235897
## 2020-03-31 0.0241133600 -0.028170850 -0.142010043 -0.176499133 0.0581105694
## 2020-04-30 0.1586604542 0.492952566 0.147557474 0.154585261 0.0674662168
## 2020-05-29 0.0302681088 -0.344532575 0.062475789 0.021923353 0.0248287930
## 2020-06-30 -0.0374606852 0.066691424 -0.010850295 -0.058071682 -0.0351087296
## 2020-07-31 0.0998504606 -0.079083085 0.048116636 0.039173775 0.0772519194
## 2020-08-31 0.0841010442 0.510326663 0.090892411 0.104138782 0.0745884233
## 2020-09-30 -0.0058637692 0.423269740 -0.106027014 0.017020231 0.0076052152
## 2020-10-30 -0.0314519943 0.026126349 0.097757324 0.012031672 -0.0083258169
## 2020-11-30 0.0291589390 0.458476066 0.082104916 0.124427388 0.0963907286
## 2020-12-31 0.0116670506 0.128992161 -0.001003622 0.087470577 -0.0545614368
## 2021-01-29 0.1251567455 2.847842905 0.041748956 -0.099891553 -0.0257178951
## 2021-02-26 -0.0313128435 -1.161404664 0.101170346 0.113906802 -0.0782174555
## 2021-03-31 0.0005007382 0.623655775 0.019885978 0.011412803 0.0486517348
## 2021-04-30 0.0056655701 -0.089380118 0.131975027 0.046665884 0.0295951038
## 2021-05-28 -0.0289800652 0.245981206 0.001422378 -0.001371341 0.0189586205
## 2021-06-30 -0.0061914017 -0.036047378 0.035405408 -0.018344257 -0.0071365607
## 2021-07-30 0.0464384181 -0.284480605 0.098492439 0.082536900 0.0107908847
## 2021-08-31 0.0435888114 0.303446001 0.071398517 -0.029110883 0.0418681130
## 2021-09-30 -0.0674282515 -0.218127305 -0.079226408 -0.063058702 -0.0606837837
## 2021-10-29 0.0910272302 0.044801037 0.102104062 -0.039195220 0.0695573475
## 2021-11-30 -0.0245045678 0.066916416 -0.042417986 0.037405518 -0.0606287255
## 2021-12-31 0.1124592924 -0.279341610 0.020607475 0.064715289 0.0324792892
## 2022-01-31 -0.0955905825 -0.309138462 -0.068192040 -0.173690137 -0.0343091718
## 2022-02-28 -0.0551573156 0.124239263 -0.001823523 -0.063666591 -0.0338250365
## 2022-03-31 -0.0189143725 0.300530946 0.029262524 -0.008973592 0.1008100868
## 2022-04-29 -0.0379503020 -0.286602107 -0.197800995 -0.197853274 0.0269633909
## 2022-05-31 0.0342951088 -0.002642024 -0.003049899 0.057440616 -0.1698041867
## 2022-06-30 -0.0779661880 -0.019754486 -0.043100254 -0.027246584 -0.0563673953
## 2022-07-29 0.0061822258 0.106471843 0.065336720 0.104207614 0.0826080306
## 2022-08-31 -0.0585538555 -0.171850229 -0.072178836 -0.002728261 0.0081250972
## 2022-09-30 -0.0590913585 -0.130742003 -0.123470462 0.002257563 -0.0217359565
## 2022-10-31 0.0269872603 0.119152762 -0.011990047 0.027277035 0.0929241914
## 2022-11-30 0.0837210895 -0.077074098 0.066315804 0.171478071 0.0684916441
## 2022-12-30 0.0203347261 -0.350534803 -0.135074411 -0.029793709 -0.0685301181
## 2023-01-31 0.0114159758 0.169509694 0.113555346 0.095493411 0.0145631768
## 2023-02-28 -0.0832439424 -0.128644337 -0.093026297 -0.061778339 -0.0121680333
## 2023-03-31 -0.0045325925 0.179891887 0.141315290 0.019785355 0.0408372088
## 2023-04-28 0.0920376357 -0.176776549 0.034209913 0.093098997 0.0235920083
## 2023-05-31 -0.0797711326 0.220548090 0.135125634 -0.152484933 -0.0237417051
## 2023-06-30 0.0665584608 0.008281653 -0.026138339 0.014438496 0.0678437851
## 2023-07-31 0.0257520432 -0.088324294 0.103253082 0.025022478 0.0169067654
## 2023-08-31 -0.0787412442 -0.179622575 0.025662489 -0.036193013 0.0206049564
## 2023-09-29 -0.0605943195 -0.119536608 -0.039776100 -0.065414793 -0.0166184904
## 2023-10-31 -0.0185140402 -0.178440794 -0.053201522 0.010571835 0.0215261766
## 2023-11-30 0.0980466076 0.055098661 0.065882417 0.079231672 -0.0483956164
## 2023-12-29 0.0539410975 0.186322731 0.052616681 -0.033693213 0.0162177594
## 2024-01-31 0.0324171560 -0.208561358 0.002930838 -0.031530363 0.0470820679
## 2024-02-29 0.0473792338 0.002807084 -0.011774916 0.025900295 0.0620581657
## 2024-03-28 -0.0428820317 -0.130832061 0.086234536 -0.037687681 0.0296685050
## 2024-04-30 -0.0651083837 -0.121283587 0.075583506 -0.032246580 -0.0137217222
## 2024-05-31 -0.0363222309 0.735518880 0.057997597 -0.090558449 0.1060150534
## 2024-06-28 0.0166913014 0.064835630 0.055580332 -0.029989032 0.0292221364
## 2024-07-31 0.0246390239 -0.085355857 -0.060014092 0.001283661 0.0136415209
## 2024-08-30 0.0669014521 0.032547894 -0.048737601 0.199298532 0.1207589765
## 2024-09-30 0.0065118616 -0.021144251 0.016331049 0.030409470 0.0445699361
## 2024-10-31 -0.0009275200 -0.033255114 0.031222826 0.002151757 0.0147513058
## 2024-11-18 0.0345893088 0.175687040 0.024192181 0.033426992 0.0256594804
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)
covariance_matrix
## ABT GME GOOGL SBUX WMT
## ABT 0.0031387362 0.003996651 0.0015033931 0.0016021438 0.0006903371
## GME 0.0039966512 0.099170474 0.0023923857 -0.0016158472 0.0017843685
## GOOGL 0.0015033931 0.002392386 0.0042336816 0.0014259442 0.0004903251
## SBUX 0.0016021438 -0.001615847 0.0014259442 0.0042058464 0.0005837829
## WMT 0.0006903371 0.001784369 0.0004903251 0.0005837829 0.0026215789
# 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.15, 0.10, 0.25, 0.15, 0.35)
sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
## [,1]
## [1,] 0.05069558
# 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
## ABT GME GOOGL SBUX WMT
## [1,] 0.005113651 0.0226781 0.009412425 0.003758978 0.009732428
rowSums(component_contribution)
## [1] 0.05069558
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
round(3) %>%
as_tibble()
component_percentages
## # A tibble: 1 × 5
## ABT GME GOOGL SBUX WMT
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.101 0.447 0.186 0.074 0.192
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
## asset contribution
## <chr> <dbl>
## 1 ABT 0.101
## 2 GME 0.447
## 3 GOOGL 0.186
## 4 SBUX 0.074
## 5 WMT 0.192
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()
component_percentages %>%
as_tibble() %>%
gather(key = "asset", value = "contribution")
return(component_percentages)
}
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.15, 0.10, 0.25, 0.15, 0.35)) %>%
# transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contributions")
plot_data %>%
ggplot(aes(x = Asset, y = Contributions)) +
geom_col(fill = "steelblue") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = .5)) +
labs(title = "Percent Contribution to Portfolio Standerd Deveiation")
plot_data <- asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.15, 0.10, 0.25, 0.15, 0.35)) %>%
# transform to long form
pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contributions") %>%
# add wights
add_column(weight = c(.25,.25,.20,.20,.10)) %>%
# transform to long
pivot_longer(cols = c(Contributions, 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 = .5)) +
theme_tq() +
labs(title = "Percent Contribution to Portfolio volitlity 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?
The stock with the largest contribution to portfolio volatility is GME. I think my portfolio risk is heavily concentrated in GME compared to the other stocks. The only other stock that has a higher volatility to weight is WMT.