# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(readr)

# Time series
library(lubridate)
library(tibbletime)

# modeling
library(broom)

Goal

Examine how each asset contributes to portfolio standard deviation. This is to ensure that our risk is not concentrated in any one asset.

1 Import stock prices

Choose your stocks from 2012-12-31 to present.

symbols <- c("TSM", "MU", "NOW", "MRVL", "PLTR")

prices <- tq_get(x = symbols,
                 get = "stock.prices",
                 from = "2020-12-31",
                 to = "2026-11-22")
prices
## # A tibble: 7,400 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 TSM    2020-12-31  110.  110.  108.  109.  4909500     99.5
##  2 TSM    2021-01-04  111.  114.  110.  112. 11262100    102. 
##  3 TSM    2021-01-05  112.  115.  112.  113. 10583600    103. 
##  4 TSM    2021-01-06  114.  116.  113.  116. 10609300    105. 
##  5 TSM    2021-01-07  119.  123.  118.  121. 13556100    111. 
##  6 TSM    2021-01-08  126.  126.  117.  119. 18976800    108. 
##  7 TSM    2021-01-11  120.  124.  119.  123. 12006800    112. 
##  8 TSM    2021-01-12  125.  125.  122.  123  14163200    112. 
##  9 TSM    2021-01-13  124.  125.  118   119. 20650500    109. 
## 10 TSM    2021-01-14  123.  135.  122.  126. 37125400    115. 
## # ℹ 7,390 more rows

2 Convert prices to returns (monthly)

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"))

3 Calculate Component Contribution to Portfolio Volatility

# 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
##                   MRVL           MU          NOW         PLTR          TSM
## 2021-01-29  0.07923333  0.040278987 -0.013295807  0.401351960  0.108363988
## 2021-02-26 -0.06378738  0.156502399 -0.018019867 -0.386599305  0.035726755
## 2021-03-31  0.01439462 -0.036946278 -0.064555998 -0.025854320 -0.058990102
## 2021-04-30 -0.07890148 -0.024559489  0.012439542 -0.010792248 -0.013105666
## 2021-05-28  0.06611327 -0.022678874 -0.066313512 -0.003913906  0.005297124
## 2021-06-30  0.18868492  0.009934116  0.148145659  0.138529763  0.027406640
## 2021-07-30  0.03775133 -0.091106578  0.067440086 -0.194074791 -0.029732006
## 2021-08-31  0.01117507 -0.051306959  0.090599658  0.193315760  0.020115315
## 2021-09-30 -0.01448572 -0.036207497 -0.033765438 -0.091369554 -0.059739523
## 2021-10-29  0.12827727 -0.026843547  0.114501117  0.073751289  0.018194483
## 2021-11-30  0.03823741  0.195262120 -0.074447644 -0.225755136  0.029891734
## 2021-12-31  0.20645337  0.104459855  0.002174667 -0.125744457  0.030744599
## 2022-01-31 -0.20251315 -0.124204514 -0.102657964 -0.283845347  0.019099837
## 2022-02-28 -0.04394889  0.077055598 -0.010054104 -0.145797597 -0.136249110
## 2022-03-31  0.04828132 -0.131764472 -0.040522454  0.147255287 -0.021223470
## 2022-04-29 -0.20990628 -0.131634095 -0.152547775 -0.277777417 -0.115041303
## 2022-05-31  0.01825526  0.079602325 -0.022484734 -0.180784206  0.025183373
## 2022-06-30 -0.30662604 -0.289489154  0.017073692  0.043950667 -0.148110862
## 2022-07-29  0.24754060  0.114418555 -0.062611216  0.132014326  0.079111096
## 2022-08-31 -0.17331070 -0.090102403 -0.027325559 -0.293172220 -0.059728012
## 2022-09-30 -0.08720545 -0.120750315 -0.140610169  0.051746601 -0.189699824
## 2022-10-31 -0.07701617  0.078920761  0.108153138  0.078053770 -0.107859282
## 2022-11-30  0.15903526  0.063556229 -0.010608917 -0.158711687  0.298749913
## 2022-12-30 -0.22788412 -0.140494353 -0.069705215 -0.155484891 -0.102282839
## 2023-01-31  0.15432439  0.187709181  0.158882138  0.192138236  0.219027260
## 2023-02-28  0.04530791 -0.041997467 -0.051764096  0.007682489 -0.062979904
## 2023-03-31 -0.04183791  0.042660668  0.072616069  0.074927565  0.071294365
## 2023-04-28 -0.09087505  0.066515888 -0.011470223 -0.086473575 -0.098432594
## 2023-05-31  0.39306157  0.057952037  0.170417202  0.640834694  0.156587950
## 2023-06-30  0.02181534 -0.077565187  0.031066388  0.041284151  0.027538092
## 2023-07-31  0.08671779  0.125158740  0.036738731  0.257888422 -0.017694322
## 2023-08-31 -0.11164102 -0.020520360  0.009950334 -0.280984162 -0.057923944
## 2023-09-29 -0.07335554 -0.027688980 -0.052059595  0.065872775 -0.068775099
## 2023-10-31 -0.13544483 -0.015531202  0.040134794 -0.077961529 -0.006812833
## 2023-11-30  0.16570086  0.129560618  0.164285804  0.303601922  0.119956381
## 2023-12-29  0.07897940  0.115670267  0.029810566 -0.155065436  0.071190864
## 2024-01-31  0.11664573  0.004792795  0.080089508 -0.064965709  0.082642875
## 2024-02-29  0.05684676  0.055128393  0.007730712  0.443872742  0.130217191
## 2024-03-28 -0.01094454  0.264152295 -0.011657973 -0.086141799  0.059824020
## 2024-04-30 -0.07185745 -0.042718122 -0.094965245 -0.046251059  0.009437165
## 2024-05-31  0.04305862  0.101279829 -0.053928599 -0.013287664  0.095085113
## 2024-06-28  0.01571673  0.050921171  0.180231364  0.155599270  0.143567860
## 2024-07-31 -0.04183994 -0.179518068  0.034630559  0.059764987 -0.047180356
## 2024-08-30  0.12949208 -0.131997442  0.048662065  0.157597961  0.034966398
## 2024-09-30 -0.05555476  0.074753356  0.045040486  0.166956373  0.015092208
## 2024-10-31  0.10589663 -0.038809253  0.042252566  0.110829417  0.092705329
## 2024-11-29  0.14585992 -0.017206729  0.117617449  0.478747765 -0.031345863
## 2024-12-31  0.17530226 -0.150438924  0.010125428  0.119967029  0.070342108
## 2025-01-31  0.02209285  0.080773441 -0.040169015  0.086824054  0.058176284
## 2025-02-28 -0.20631792  0.025857403 -0.091041917  0.029032567 -0.147967164
## 2025-03-31 -0.39965539 -0.073406673 -0.155151436 -0.006142196 -0.080020542
## 2025-04-30 -0.05223724 -0.121487079  0.181946776  0.338839104  0.004148008
## 2025-05-30  0.03070414  0.205020698  0.057062441  0.106727332  0.148211080
## 2025-06-30  0.25148052  0.266038231  0.016664004  0.033871261  0.162003686
## 2025-07-31  0.03847267 -0.120642399 -0.086254760  0.149802695  0.064665412
## 2025-08-29 -0.24557202  0.086575971 -0.027584484 -0.010410805 -0.045511572
## 2025-09-30  0.29058067  0.340700651  0.003068964  0.151914704  0.193545752
## 2025-10-31  0.10953715  0.291336683 -0.001087200  0.094352905  0.072964137
## 2025-11-28 -0.04740432  0.055244856 -0.123585664 -0.174025664 -0.030140425
## 2025-12-31 -0.05070475  0.188466979 -0.058888196  0.053739117  0.044287439
## 2026-01-30 -0.07326200  0.374062578 -0.269419577 -0.192738519  0.084122333
## 2026-02-27  0.03449691 -0.006068276 -0.080035584 -0.066272705  0.125017295
## 2026-03-31  0.19269319 -0.198928599 -0.032558380  0.064155741 -0.100093602
## 2026-04-30  0.51172978  0.425779854 -0.168812131 -0.050257593  0.158667765
## 2026-05-29  0.21615582  0.629974232  0.342407692  0.118046534  0.054991768
## 2026-06-30  0.37371432  0.172914238 -0.225316887 -0.293962104  0.134492431
## 2026-07-31 -0.46237881 -0.338094264  0.113656031  0.053322600 -0.166677265
## 2026-08-31  0.12088245  0.152616776  0.285544581  0.415115605  0.027015791
## 2026-09-30  0.22176299  0.105223891 -0.099230358  0.003588351  0.096539197
## 2026-10-30 -0.29637240 -0.185382171 -0.242811802 -0.015842569 -0.116236077
## 2026-11-20 -0.47383988 -0.397556252 -0.182207427 -0.249268024 -0.242391746
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##             MRVL          MU         NOW        PLTR         TSM
## MRVL 0.033765695 0.017931835 0.005384129 0.012415318 0.013466453
## MU   0.017931835 0.029120677 0.003124499 0.001923174 0.010464546
## NOW  0.005384129 0.003124499 0.012576405 0.012353913 0.002601579
## PLTR 0.012415318 0.001923174 0.012353913 0.038976924 0.005137778
## TSM  0.013466453 0.010464546 0.002601579 0.005137778 0.010483638
# 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.2, 0.25, 0.15, 0.2, 0.2)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##           [,1]
## [1,] 0.1113566
# Component contribution
# Similar to the formula for sd_portfolio
# Mathematical trick to summarize the same, sd_portfolio, by asset intead of a single number
component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]
component_contribution
##            MRVL         MU         NOW       PLTR        TSM
## [1,] 0.03092779 0.03101018 0.009072891 0.02449766 0.01584809
rowSums(component_contribution)
## [1] 0.1113566
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    MRVL    MU   NOW  PLTR   TSM
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.278 0.278 0.081  0.22 0.142
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 MRVL         0.278
## 2 MU           0.278
## 3 NOW          0.081
## 4 PLTR         0.22 
## 5 TSM          0.142

6 Plot: Colum Chart of Component Contribution and Weight

plot_data <- component_percentages %>%

pivot_longer(cols = everything(), names_to = "Asset", values_to = "Contribution") %>%
    
    # Add Weight
    add_column(weight = c(0.2, .25, .15, .2, .2)) %>%
    
    
    # Transform to long table
    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() +
    labs(title = "Percent contribution to Portfilo Volatility and weight",
         y = "Percent",
         x = NULL) +
    geom_text(aes(label = scales::percent(value, accuracy = 1)),
              position = position_dodge(width = 0.9),
              vjust = -0.5,
              size = 3)

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?

In the portfolio, the largest contributors of volatility are the MRVL and MU stock, with a contributing percentage of 28%. Accounting for the fact that MRVL contribute as much to the volatility of the portfolio as MU, while having a 5 percentage point lower weighting in the portfolio itself, it suggest that MRVL even though the stock matches MU in percentage contribution, it would increase its contribution to volatility if the weight of the portfolio were to be increased.

The contribution of volatility is relatively spread out across the portfolio, with 3 stocks contributing more than 20% to the volatility. As this is a technology concentrated portfolio, meaning high beta across most stocks; it were expected for there to be an even distribution of contribution of volatility to the portfolio.