<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/asset-contribution-to-portfolio-volatility/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/09/13/asset-contribution-to-portfolio-volatility/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <br>
<br>


<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>



</p>
<p>In our previous portfolio volatility work, we covered how to import stock prices, convert to returns and set weights, <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/12/introduction-to-volatility/" target="_blank">calculate portfolio volatility</a>, and <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/18/introduction-to-rolling-volatility/" target="_blank">calculate rolling portfolio volatility</a>.</p>
<p>Now we want to break that total portfolio volatility into its constituent parts and investigate how each asset contributes to the volatility. Why might we want to do that?</p>
<p>For our own risk management purposes, we might want to ensure that our risk isn’t too concentrated in one asset. Not only might this lead to a less-diversified portfolio than we thought we had, but it also might indicate that our initial assumptions about a particular asset were wrong – or, at least, they have become less right as the asset has changed over time.</p>
<p>Similarly, if this portfolio is governed by a mandate from, say, an institutional client, that client might have a preference or even a rule that no asset or sector can rise above a certain threshold of risk contribution. That institutional client might require a report like this from each of their outsourced managers, so they can sum the constituents.</p>
<p>With that motivation in mind, let’s get prices, returns, and set weights for five ETFs.</p>
<pre><code>library(timetk)
library(tidyverse)
library(tidyquant)
library(highcharter)


symbols &lt;- c(&quot;SPY&quot;,&quot;IJS&quot;,&quot;EFA&quot;,&quot;EEM&quot;,&quot;AGG&quot;)

prices &lt;- 
  getSymbols(symbols, src = 'google', from = &quot;2005-01-01&quot;, 
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Cl(get(.))) %>% 
  reduce(merge) %>%
  `colnames&lt;-`(symbols)

prices_monthly &lt;- to.monthly(prices, indexAt = &quot;first&quot;, OHLC = FALSE)

portfolioComponentReturns &lt;- na.omit(Return.calculate(prices_monthly, method = &quot;log&quot;))

w = c(0.25, 0.20, 0.20, 0.25, 0.10)</code></pre>
<p>We need to build the covariance matrix and calculate portfolio standard deviation.</p>
<pre><code>covariance_matrix &lt;- cov(portfolioComponentReturns)

# Square root of transpose of the weights cross prod covariance matrix returns 
# cross prod weights gives portfolio standard deviation.
sd_portfolio &lt;- sqrt(t(w) %*% covariance_matrix %*% w)</code></pre>
<p>Let’s start to look at the individual components.</p>
<p>The percentage contribution of asset <code>i</code> is defined as:</p>
<p>(marginal contribution of asset <code>i</code> * weight of asset <code>i</code>) / portfolio standard deviation</p>
<p>To find the marginal contribution of each asset, take the cross-product of the weights vector and the covariance matrix divided by the portfolio standard deviation.</p>
<pre><code># Marginal contribution of each asset. 
marginal_contribution &lt;- w %*% covariance_matrix / sd_portfolio[1, 1]</code></pre>
<p>Now multiply the marginal contribution of each asset by the weights vector to get total contribution. We can then sum the asset contributions and make sure it’s equal to the total portfolio standard deviation.</p>
<pre><code># Component contributions to risk are the weighted marginal contributions
component_contribution &lt;- marginal_contribution * w 

# This should equal total portfolio vol, or the object `sd_portfolio`
components_summed &lt;- rowSums(component_contribution)</code></pre>
<p>The summed components are 0.0448135 and the matrix calculation is 0.0448135.</p>
<p>To get to percentage contribution of each asset, we divide each asset’s contribution by the total portfolio standard deviation.</p>
<pre><code># To get the percentage contribution, divide component contribution by total sd.
component_percentages &lt;- component_contribution / sd_portfolio[1, 1]</code></pre>
<p>Let’s port this to a tibble for ease of presentation, and we’ll append <code>by_hand</code> to the object because we did the calculations step-by-step.</p>
<pre><code>percentage_tibble_by_hand &lt;- 
  tibble(symbols, w, as.vector(component_percentages)) %>% 
  rename(asset = symbols, 'portfolio weight' = w, 'risk contribution' = `as.vector(component_percentages)`)

percentage_tibble_by_hand</code></pre>
<pre><code>## # A tibble: 5 x 3
##   asset `portfolio weight` `risk contribution`
##   </code></pre>