Plotting Financial Data With Python: Efficient Frontier (N assets)
We already have the efficient frontier script that we created in the previous post but it has one major limitation: it does not allow us to plot more than 2 assets. Plotting 2 assets is enough to see diversification in action but it’s not practical to have a portfolio that consists of 2 assets. In this post we’re going to extend the previous script in order to support an arbitrary number of assets.
This is the fifth part of the “Plotting Financial Data With Python” series and it’s better if you read it in chronological order:
 Part 1  History
 Part 2  Variance
 Part 3  Comparing Returns
 Part 4  Efficient Frontier (2 Assets)
 Part 5  Efficient Frontier: (N assets) (you are here)
Why Diversify?
Diversification helps to reduce portfolio volatility but to what extent? Well, it depends on the correlations between different assets but we can safely assume that the number of assets should be greater than 2. If you decide to add another asset, the smaller the number of assets you already have in your portfolio, the better the effect of diversification. Here is the picture that helps to visualize how the number of assets affects the portfolio risk:
As you can see, one thing is clear: having 2 assets does not allow us to get all of the benefits of diversification. There are many opinions on what number of assets is “right” but almost everyone agrees that 2 is far too low.
Goal
We already calculated the efficient frontier for a portfolio that consists of the IBM and DIS stocks. Let’s add one more stock to it. You can pick any stock or an index but I will go with CocaCola (KO).
So, how do we calculate our risks and rewards?
Expected Return
Here is how we can calculate the expected return on a portfolio:
\( E(R_p) = \sum_{i=1}^N w_i E(R_i) \)
Where:
\( R_p \) = expected return on a portfolio
\( N \) = number of assets in a portfolio
\( w_i \) = weight of an asset i in a portfolio
\( R_i \) = expected return on asset i
All of this is pretty simple, we just need to find the weighted average of the returns of every asset in a portfolio.
Variance
Variance is a bit more tricky to calculate because we have to include the correlations between each pair of assets:
\( σ_p^2 = \sum_{i=1}^N w_i^2 σ_i^2 + \sum_{i=1}^N \sum_{j \not = i}^N w_i w_j σ_i σ_j p_{ij} \)
Where:
\( σ_p^2 \) = portfolio volatility
\( w_i \) = weight of an asset i in a portfolio
\( σ_i \) = standard deviation of an asset i
\( p_{ij} \) = correlation of returns between the assets i and j
Standard Deviation
Standard deviation of a portfolio is just a square root of it’s variance:
\( σ_p = (σ_p^2)^{1 \over 2} \)
That gives us a hint about the portfolio riskiness.
Implementation
Let’s create a new file and call it frontier.py:


Testing
Now, let’s run our new script in order to see the efficient frontier:
python frontier.py IBM DIS KO
You should see the following image:
Conclusion
Now we are able to plot the efficient frontier based on an arbitrary number of assets. Please note that nothing is “for sure” in the world of investing and this model has a lot of limitations, although it’s probably the best model that is currently available. Our expected return is based purely on the past performance which might not be an accurate assumption about the future.
Another thing to consider is the limit of diversification. The benefits of having more assets tend to wear off with each new asset added to your portfolio. There is a huge difference between the 2asset and 10asset portfolios but there might be no gain in having 200 assets, especially if you take into account all of the transaction costs of rebalancing your portfolio.