Plotting Financial Data With Python: Efficient Frontier (2 assets)
Updated on
The main idea behind the Efficient Frontier is that the overall risk (volatility) of a portfolio may not be equal to the sum of the risk of its components so some combinations are better than others. In this post we’re going to visualize the optimal weights of 2 given assets in a hypothetical portfolio.
Contents
This is the fourth 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) (you are here)
 Part 5  Efficient Frontier (N Assets)
Source Code
You can get the full source code here: https://github.com/bubelov/marketplots
Components
Let’s compare IBM and Disney by using the script from the previous post:
$ python compare.py IBM DIS
It looks like both of those stocks have made a lot of money for their shareholders but it’s unclear who will be better off in the long run. Let’s analyze both stocks in order to make a few assumptions based on their historical performance.
Stats
Here are some stats for IBM:
 Monthly mean return: 0.76%
 Standard deviation of monthly return: 7.51%
Here are the same stats tor DIS:
 Monthly mean return: 0.83%
 Standard deviation of monthly return: 7.25%
Those stocks look pretty close in terms of risk and return so which one should we choose? Would it be a better idea to split the portfolio ^{50}⁄_{50}? Let’s run a few calculations to find out.
Diversified Portfolio
So what are the mean return of the ^{50}⁄_{50} portfolio? It’s just a weighted average of its components’ return:
\( r_p = r_1 w_1 + r_2 w_2 \)
Where:
\( r_p \) = portfolio mean return
\( r_1, r_2 \) = return of the portfolio components
\( w_1, w_2 \) = weights of the portfolio components
0.76% * 50% + 0.83% * 50% = 0.80%
Not bad, but what about the risk? Here is the formula:
\( σ_p^2 = w_1^2 σ_1^2 + w_2^2 σ_2^2 + 2 w_1 w_2 cov(1, 2) \)
In our case covariance is 0.00207, so:
 Portfolio variance = 0.5^2 * 0.0751^2 + 0.5^2 * 0.0725^2 + 2 * 0.5 * 0.5 * 0.00207 = 0.003759065
 Portfolio standard deviation = sqrt(Portfolio Variance) = 6.13%
It looks like the ^{50}⁄_{50} split is not a bad idea after all, we would get the same return with significantly less risk but what about the other possible combinations? How much would we get if we were ready to accept more risk? Is it possible to decrease the risk? Plotting many different combinations on a graph might give us a good picture of how diversification works and helps us to make the right choice.
Plotting Portfolios
Let’s plot a lot of different asset combinations in order to find out how they affect portfolio volatility and expected return. A scatter plot is a good tool for showing that kind of data. Here is the script, you can call it frontier2.py:


Now let’s run this script in order to see the scatter plot:
python frontier2.py IBM DIS
As you can see, the ^{50}⁄_{50} portfolio is on the bottom half of this chart. That’s not very good because it means that there is another portfolio that has more returns with the same risks. Usually, it’s better to ignore the bottom half of the efficient frontier and pick a portfolio from the top half that best suits your risk tolerance. Many young people prefer high returns and it might be a good idea if your investment horizon is long enough but the people who are close to retirement age are usually not comfortable with the idea of waiting for another 2030 years in order to recover from a possible loss caused by holding too many highrisk assets.
Conclusion
Now we have a script that allows us to find an optimal combination of 2 different assets. That’s a good start but it’s more practical to be able to include more assets and that’s what we’re going to do in the next post.