# Load packages

# Core
library(tidyverse)
library(tidyquant)

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("NFLX", "AAPL", "VRTX")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",    
                 from = "2012-12-31",
                 to   = "2017-12-31")

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
##                     AAPL         NFLX         VRTX
## 2013-01-31 -1.555889e-01  0.579217807  0.066475721
## 2013-02-28 -2.561110e-02  0.129468379  0.044548872
## 2013-03-28  2.850395e-03  0.006360071  0.160477149
## 2013-04-30  2.710898e-04  0.132375020  0.334677415
## 2013-05-31  2.217169e-02  0.046038179  0.044678154
## 2013-06-28 -1.258957e-01 -0.069356059 -0.003366850
## 2013-07-31  1.321023e-01  0.146848883 -0.003252780
## 2013-08-30  8.044275e-02  0.149523815 -0.060037406
## 2013-09-30 -2.172326e-02  0.085363345  0.008875970
## 2013-10-31  9.201523e-02  0.042020452 -0.060764784
## 2013-11-29  6.770798e-02  0.126045633 -0.027422338
## 2013-12-31  8.862685e-03  0.006458091  0.067936009
## 2014-01-31 -1.139496e-01  0.105976906  0.061843072
## 2014-02-28  5.591830e-02  0.084967340  0.022765207
## 2014-03-31  1.975633e-02 -0.235772622 -0.133990836
## 2014-04-30  9.476106e-02 -0.089040653 -0.043642301
## 2014-05-30  7.576523e-02  0.260398864  0.065184620
## 2014-06-30  2.728605e-02  0.053062785  0.270232033
## 2014-07-31  2.832658e-02 -0.041427371 -0.062878125
## 2014-08-29  7.465173e-02  0.122147235  0.051085153
## 2014-09-30 -1.722082e-02 -0.056990921  0.182553067
## 2014-10-31  6.948895e-02 -0.138642147  0.002934004
## 2014-11-28  1.007308e-01 -0.125081717  0.045470248
## 2014-12-31 -7.460601e-02 -0.014472733  0.007774298
## 2015-01-30  5.961158e-02  0.257187512 -0.075689154
## 2015-02-27  9.601584e-02  0.072268016  0.080978150
## 2015-03-31 -3.187422e-02 -0.130782770 -0.012300063
## 2015-04-30  5.769660e-03  0.289324652  0.044027816
## 2015-05-29  4.434124e-02  0.114579343  0.039835093
## 2015-06-30 -3.793791e-02  0.051346180 -0.038214047
## 2015-07-31 -3.348119e-02  0.197231490  0.089195552
## 2015-08-31 -6.848907e-02  0.006278919 -0.057001590
## 2015-09-30 -2.205786e-02 -0.107942853 -0.202537047
## 2015-10-30  8.011263e-02  0.048393445  0.180495412
## 2015-11-30 -5.821027e-03  0.129220160  0.036367666
## 2015-12-31 -1.167903e-01 -0.075337490 -0.027667416
## 2016-01-29 -7.822371e-02 -0.219478322 -0.326823331
## 2016-02-29 -1.288554e-03  0.016950552 -0.059709088
## 2016-03-31  1.197463e-01  0.090226765 -0.072768184
## 2016-04-29 -1.507310e-01 -0.127082275  0.059225004
## 2016-05-31  6.931419e-02  0.130402555  0.099354909
## 2016-06-30 -4.359662e-02 -0.114425083 -0.079631325
## 2016-07-29  8.623541e-02 -0.002517413  0.120131190
## 2016-08-31  2.337655e-02  0.065736402 -0.026005307
## 2016-09-30  6.344843e-02  0.011224670 -0.080386679
## 2016-10-31  4.324808e-03  0.236709154 -0.139429448
## 2016-11-30 -2.183764e-02 -0.065099283  0.073062267
## 2016-12-30  4.684070e-02  0.056493450 -0.102356175
## 2017-01-31  4.664174e-02  0.128033698  0.153238921
## 2017-02-28  1.255552e-01  0.010041084  0.053840413
## 2017-03-31  4.754128e-02  0.039185483  0.187878764
## 2017-04-28 -6.961753e-05  0.029267777  0.078670064
## 2017-05-31  6.560732e-02  0.068984176  0.043826736
## 2017-06-30 -5.891536e-02 -0.087485372  0.041753574
## 2017-07-31  3.218010e-02  0.195442599  0.163891550
## 2017-08-31  1.016532e-01 -0.039009333  0.055847434
## 2017-09-29 -6.213448e-02  0.037301404 -0.054399491
## 2017-10-31  9.240339e-02  0.079877197 -0.038962905
## 2017-11-30  2.007519e-02 -0.046100666 -0.013355579
## 2017-12-29 -1.536322e-02  0.023081621  0.037876412
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##              AAPL         NFLX         VRTX
## AAPL 0.0048301411 0.0006512414 0.0006955348
## NFLX 0.0006512414 0.0177864943 0.0039643502
## VRTX 0.0006955348 0.0039643502 0.0115168857
# 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.3, 0.3, 0.4)

sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)
sd_portfolio
##           [,1]
## [1,] 0.0715108
# 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
##             AAPL       NFLX       VRTX
## [1,] 0.008065755 0.02985728 0.03358776
rowSums(component_contribution)
## [1] 0.0715108
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 3
##    AAPL  NFLX  VRTX
##   <dbl> <dbl> <dbl>
## 1 0.113 0.418  0.47
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 3 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.113
## 2 NFLX         0.418
## 3 VRTX         0.47

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?

Vertex pharmaceuticals has the highest volatility in my portfolio. And it has a considerably high weight to it in my portfolio being 50% of my holdings. This is not a good percentage and to lower this risk down i should spread the weight out more. Another solutions is to add more stocks in less tech based companies that have similar sensitivities in there value.