Assessing the riskiness of a portfolio with Python

Bernard Brenyah
DS Biz
Published in
2 min readSep 28, 2017
Risks in Portfolios. Image Credit: safety4sea.com

In the last post, we looked at the concept of risk through the perspective of single assets. This post builds and extends the concept of risk within the domains of portfolios. A portfolio is simply a single pool which contains two or more securities in that single pool.

The formula for measuring risk in a portfolio of various securities is:

Measuring Risk in a Portfolio

OR

Measuring Risk (S.D.) in a Portfolio with correlation coefficient or covariance

Luckily, we have got Python’s number crunching libraries to do the heavy lifting for us. Translating this formula into a matrix sequence gives us a better view of the approach this tutorial adopts in assessing the riskiness of a portfolio.

Variance of a Portfolio expressed as matrix calculation

The matrix overview shows how the ‘raw formula’ is ascertained as a matrix multiplication sequence. The covariance matrix is multiplied with the weights and finally, the output of this operation is multiplied by a transpose of weights vector.

Weights transposed * (Covariance matrix * Weights)

Does it sound confusing? Don’t worry about! Once the calculations begin, you will have a clearer picture of this sequence. Let’s turn to Yahoo Finance for some analysis:

NB: Please read the .ipynb tutorial file on the desktop. Noticed the inability of attached notebooks to be read on smartphones. 

Based on the tutorial, I wrote a script that calculates the Variance and Volatility of an equally weighted portfolio made up of five (5) stocks; Apple, General Motors, General Electric, Facebook and Walmart.

Below is the output of the script:

Variance of Portfolio is 2.57%, Portfolio Risk is 16.02%

Great! In this post, we have covered the concept of assessing risk for investors who prefer holding on to a portfolio made up of various stocks. In the next post, we shall look at combining the concept of risk and returns with the inclusion of probabilites of scenarios.

--

--