# 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

symbols <- c("AMZN", "AAPL", "TSLA", "NFLX", "GOOGL")

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          AMZN        GOOGL         NFLX         TSLA
## 2013-01-31 -1.555894e-01  0.0566799395  0.066063345  0.579217835  0.102078114
## 2013-02-28 -2.561110e-02 -0.0046435024  0.058479294  0.129468604 -0.074128640
## 2013-03-28  2.850685e-03  0.0083654162 -0.008787930  0.006360053  0.084208138
## 2013-04-30  2.708784e-04 -0.0487507497  0.037539312  0.132374947  0.354111531
## 2013-05-31  2.217180e-02  0.0588686246  0.055032633  0.046038257  0.593716684
## 2013-06-28 -1.258957e-01  0.0310507506  0.010447605 -0.069356124  0.093672163
## 2013-07-31  1.321022e-01  0.0813355350  0.008347924  0.146848937  0.223739522
## 2013-08-30  8.044311e-02 -0.0695574090 -0.047107567  0.149523880  0.229971642
## 2013-09-30 -2.172367e-02  0.1067688897  0.033680785  0.085363408  0.134706620
## 2013-10-31  9.201504e-02  0.1521839116  0.162613725  0.042020345 -0.189806595
## 2013-11-29  6.770833e-02  0.0781496860  0.027760195  0.126045671 -0.228409405
## 2013-12-31  8.862110e-03  0.0130490386  0.056080375  0.006458000  0.167108541
## 2014-01-31 -1.139490e-01 -0.1059765119  0.052373801  0.105976865  0.187261714
## 2014-02-28  5.591794e-02  0.0094619003  0.028942844  0.084967484  0.299722785
## 2014-03-31  1.975630e-02 -0.0737086161 -0.086864158 -0.235772709 -0.160783242
## 2014-04-30  9.476160e-02 -0.1007565303 -0.041981093 -0.089040658 -0.002690122
## 2014-05-30  7.576557e-02  0.0273091844  0.066484504  0.260398926 -0.000577422
## 2014-06-30  2.728580e-02  0.0383836202  0.022520700  0.053062816  0.144457224
## 2014-07-31  2.832588e-02 -0.0369768154 -0.008795555 -0.041427449 -0.072372676
## 2014-08-29  7.465222e-02  0.0799468404  0.004836748  0.122147394  0.188794007
## 2014-09-30 -1.722078e-02 -0.0502010184  0.010335252 -0.056990973 -0.105566477
## 2014-10-31  6.948900e-02 -0.0540982347 -0.035531496 -0.138642206 -0.004046477
## 2014-11-28  1.007308e-01  0.1031187277 -0.033648400 -0.125081659  0.011599801
## 2014-12-31 -7.460613e-02 -0.0872368614 -0.034122588 -0.014472752 -0.094774519
## 2015-01-30  5.961161e-02  0.1330922557  0.012900263  0.257187564 -0.088365289
## 2015-02-27  9.601590e-02  0.0697992426  0.045600393  0.072268024 -0.001277808
## 2015-03-31 -3.187415e-02 -0.0214295755 -0.014194775 -0.130782900 -0.074350051
## 2015-04-30  5.769497e-03  0.1253212736 -0.010748047  0.289324660  0.180226808
## 2015-05-29  4.434124e-02  0.0175090293 -0.006306619  0.114579388  0.103899574
## 2015-06-30 -3.793770e-02  0.0112589814 -0.009729502  0.051346159  0.067300935
## 2015-07-31 -3.348132e-02  0.2111621090  0.196801619  0.197231490 -0.007896616
## 2015-08-31 -6.848888e-02 -0.0443525782 -0.014832048  0.006278952 -0.066366250
## 2015-09-30 -2.205783e-02 -0.0019516837 -0.014694757 -0.107942886 -0.002653519
## 2015-10-30  8.011247e-02  0.2010808743  0.144199006  0.048393497 -0.182659777
## 2015-11-30 -5.821062e-03  0.0602956777  0.033944344  0.129220107  0.106828586
## 2015-12-31 -1.167904e-01  0.0165440008  0.019677953 -0.075337490  0.041471519
## 2016-01-29 -7.822370e-02 -0.1410054620 -0.021646086 -0.219478280 -0.227360626
## 2016-02-29 -1.288088e-03 -0.0605352209 -0.059710792  0.016950429  0.003810669
## 2016-03-31  1.197458e-01  0.0717834363  0.061744680  0.090226772  0.179948109
## 2016-04-29 -1.507304e-01  0.1053453760 -0.074852648 -0.127082158  0.046721797
## 2016-05-31  6.931367e-02  0.0915002899  0.056264256  0.130402512 -0.075597968
## 2016-06-30 -4.359616e-02 -0.0099694639 -0.062428350 -0.114425146 -0.050296440
## 2016-07-29  8.623518e-02  0.0586021229  0.117617256 -0.002517351  0.100785334
## 2016-08-31  2.337645e-02  0.0135476418 -0.001884467  0.065736422 -0.102058091
## 2016-09-30  6.344814e-02  0.0848953908  0.017830693  0.011224573 -0.038366372
## 2016-10-31  4.325035e-03 -0.0583893058  0.007236727  0.236709246 -0.031364583
## 2016-11-30 -2.183746e-02 -0.0509721927 -0.042912767 -0.065099315 -0.043041267
## 2016-12-30  4.684047e-02 -0.0009330556  0.021131678  0.056493451  0.120665178
## 2017-01-31  4.664179e-02  0.0936394059  0.034406495  0.128033672  0.164624916
## 2017-02-28  1.255555e-01  0.0258446800  0.029717679  0.010041111 -0.007730364
## 2017-03-31  4.754138e-02  0.0479423007  0.003391067  0.039185522  0.107278727
## 2017-04-28 -6.963148e-05  0.0424566944  0.086629982  0.029267788  0.120916212
## 2017-05-31  6.560714e-02  0.0725778018  0.065486545  0.068984045  0.082295892
## 2017-06-30 -5.891570e-02 -0.0271286156 -0.059920944 -0.087485304  0.058654468
## 2017-07-31  3.218023e-02  0.0202278808  0.016873566  0.195442624 -0.111459860
## 2017-08-31  1.016531e-01 -0.0072953953  0.010248677 -0.039009333  0.095543446
## 2017-09-29 -6.213500e-02 -0.0198260355  0.019161022  0.037301383 -0.042474144
## 2017-10-31  9.240401e-02  0.1395154056  0.059137470  0.079877238 -0.028457409
## 2017-11-30  2.007521e-02  0.0626577318  0.003025348 -0.046100726 -0.070862541
## 2017-12-29 -1.536355e-02 -0.0062057845  0.016491871  0.023081582  0.008061928
# Covariance of asset returns
covariance_matrix <- cov(asset_returns_wide_tbl)

covariance_matrix
##               AAPL         AMZN        GOOGL        NFLX         TSLA
## AAPL  0.0048301406 0.0015511087 0.0007893271 0.000651237 0.0004839032
## AMZN  0.0015511087 0.0054660218 0.0023866023 0.003732119 0.0005128766
## GOOGL 0.0007893271 0.0023866023 0.0029010266 0.003241516 0.0005226525
## NFLX  0.0006512370 0.0037321192 0.0032415161 0.017786500 0.0049890817
## TSLA  0.0004839032 0.0005128766 0.0005226525 0.004989082 0.0209573536
# 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.05621416
# 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      AMZN       GOOGL       NFLX      TSLA
## [1,] 0.008591321 0.0134722 0.007381614 0.02063662 0.0061324
rowSums(component_contribution)
## [1] 0.05621416
# Component contribution in percentage
component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
    round(3) %>%
    as_tibble()

component_percentages
## # A tibble: 1 × 5
##    AAPL  AMZN GOOGL  NFLX  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.153  0.24 0.131 0.367 0.109
component_percentages %>%

    as_tibble() %>%
    gather(key = "asset", value = "contribution")
## # A tibble: 5 × 2
##   asset contribution
##   <chr>        <dbl>
## 1 AAPL         0.153
## 2 AMZN         0.24 
## 3 GOOGL        0.131
## 4 NFLX         0.367
## 5 TSLA         0.109

6 Plot: Colum Chart of Component Contribution and Weight

asset_returns_wide_tbl <- asset_returns_tbl %>%

    pivot_wider(names_from = asset, values_from = returns) %>%

    column_to_rownames(var = "date")

calculate_component_contribution <- function(asset_returns_wide_tbl, w) {

    covariance_matrix <- cov(asset_returns_wide_tbl)
    
    sd_portfolio <- sqrt(t(w) %*% covariance_matrix %*% w)

    component_contribution <- (t(w) %*% covariance_matrix * w) / sd_portfolio[1,1]

    component_percentages <- (component_contribution / sd_portfolio[1,1]) %>%
        round(3) %>%
        as_tibble()
    
    return(component_percentages)

}

asset_returns_wide_tbl %>% calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1))
## # A tibble: 1 × 5
##    AAPL  AMZN GOOGL  NFLX  TSLA
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.153  0.24 0.131 0.367 0.109
# Figure 10.1 Contribution to Standard Deviation ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%

    ggplot(aes(asset, contribution)) +
    geom_col(fill = "cornflowerblue") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    
    labs(title = "Percent Contribution to Portfolio Standard Deviation",
         y = "Percent Contribution to Risk",
         x = NULL)

# Figure 10.2 Weight versus Contribution ----
asset_returns_wide_tbl %>%

    calculate_component_contribution(w = c(0.25,0.25,0.2,0.2,0.1)) %>%
    gather(key = "asset", value = "contribution") %>%
    add_column(weights = c(0.25,0.25,0.2,0.2,0.1)) %>%
    pivot_longer(cols = c(contribution, weights), names_to = "type", values_to = "value") %>%

    ggplot(aes(asset, value, fill = type)) +
    geom_col(position = "dodge") +
    
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
    theme_tq() +
    scale_fill_tq() +

    labs(title = "Percent Contribution to Volatility",
         y = "percent",
         x = "asset") 

Which of the assets in your portfolio the largest contributor to your portfolio volatility. Do you think your portfolio risk is concentrated in to any one asset.

Based on the graphs I would say that the stock with the most contribution is Netflix. I do not think that the portfolio risk is all in one asset, however, Netflix holds more than the rest.