Photo by M. B. M. on Unsplash

Obtaining the Efficient Frontier with Python — Two-Fund Theorem.

A mathematical approach to creating and plot the Efficient Frontier from scratch, just with Pandas and Numpy library.

Nicolás Besser
5 min readMay 4, 2021

--

In this article, we will talk about how to obtain the entire Efficient Frontier from only two solutions to the Markowitz Problem and how to make a great plot of it. Let’s get into it.

I will address the following topics:

  • What is the Two-Fund Theorem?
  • How to obtain the data from Yahoo Finance and Expected Returns.
  • How to calculate and plot the covariance matrix, correlation matrix, and inverse matrix of the covariance.
  • Minimum variance optimization, two optimal solutions for the Markowitz Problem.
  • Plotting the Efficient Frontier.
  • Comparison with results obtained with PyportfolioOpt library solution.

I will only use other libraries for plotting and comparison purposes.

What is the Two-Fund Theorem?

The Two Fund theorem states that we can build the entire Efficient Frontier from two optimal solutions (i.e. two optimal portfolios) of the Markowitz problem given a rate of return. In other words, let’s call weights 1 to the first optimal solution, and weights 2 to the second. Then, we can create from those original weights another optimal solution, let’s call it weights 3, which is also on the Efficient Frontier. I will explain it in more detail later.

Yahoo Finance Data and Expected Returns

First, we need to import the libraries that we are going to use:

Now we are ready to import the “Adj Close” price data of our assets from Yahoo Finance. This article will be using Apple, Microsoft, Amazon, Facebook, Google, Tesla, and Visa stocks.

Before we do anything else, we need to convert our data to daily logarithmic returns. And then we can calculate the covariance matrix and the correlation matrix (it can only be calculated from the returns). The mathematical formulation of the daily logarithmic returns is the following:

Image by Author: Daily and annual logarithmic returns.

Then we multiply the daily returns by 252 because there 252 business days in the United States. In that way, we have calculated the expected returns in a very simple and fast way.

Data and Expected Return.

Covariance, Correlation Matrix, and Inverse of Covariance Matrix

Having done the above we can now compute the matrices and plot the correlation matrix.

Matrices

Plotting the Correlation Matrix

Image by Author: Correlation Matrix of the assets.

Minimum Variance Optimization

As I said before, the Two Fund theorem states that given the optimal solution of the Markowitz Problem, the entire Efficient Frontier can be obtained. So what we want to solve is the minimum variance we can obtain given a minimal return (second constraint).

Image by Author: Minimum variance problem subject to a minimal return.

The mathematical solution to this problem, which is obtained by solving the Lagrangian, is the following.

Image by Author: Optimal weights given a minimal return.

Now, by defining the following auxiliary variables the g and h vectors can be obtained. And therefore, the optimal weights given a minimal return can also be computed.

Image by Author: Auxiliary Variables.

And with g and h vectors we can calculate the optimal weights.

Image by author = g and h.

Python Formulation:

Before that let’s say that we want a minimal return of 25% and a minimum return of 28% in our portfolio. With that in mind, we can resolve the minimal variance for that return, i.e, the points in the efficient frontier for that return. Remembering, the Two Fund Theorem needs two optimal solutions to the Markowitz problem.

Optimal weights for given returns.

As a result, we obtain these optimal weights demanding a 25% return in our portfolio:

Optima weights for a 25% return in the portfolio.

As you can see, there are negative weights, which means that you need to do short selling in the optimal solution.

Now the solution for a 28% return is the following:

Optimal weights for a minimum variance for a 28% return.

Constructing the Efficient Frontier with the Two-Fund Theorem

So as I stated before we can construct the entire Efficient Frontier from just two optimal weights. So as the following image show, we can construct a third optimal weight, let’s call it weight 3, from the ponderation of the original weights with a constant value alpha that can have any value given value. In this case, we will set alpha from -5 to 5.

As we said before, we can calculate a third optimal weight from the original solutions, but also we can calculate a fourth optimal weight, and so on. We can calculate as many optimal weights as alpha values. Now we can construct the entire Efficient Frontier, but before we need to calculate the expected return and variance for each portfolio in the efficient frontier. That way we can plot the volatility-return space.

Plotting the efficient frontier.

The Efficient Frontier looks like this:

Image by Author: Efficient frontier of the portfolio of stocks

Comparison with PyportfolioOpt Solution

First, we need to install the PyportfolioOpt library.

pip install PyPortfolioOpt

Having done this we can now calculate the minimum variance portfolio using the library.

Optimal weights with PyportfolioOpt library.

The solution is the following:

OrderedDict([(0, 0.11988), (1, 0.00286), (2, 0.24669), (3, 0.2122), (4, -0.01113), (5, -0.00834), (6, 0.43784)])
Expected annual return: 25.0%
Annual volatility: 23.4%
Sharpe Ratio: 0.98

As you can see the result are the same as the ones we calculated before.

Photo by Sigmund on Unsplash

Conclusions

During the article, I have shown you the mathematical foundation of the minimum variance problem and how to get the Efficient Frontier from two optimal solutions of the Markowitz Problem.

Even though the PyportfolioOpt library does the same with less work, using the Two Funds Theorem is incredibly useful for plotting the Efficient Frontier, because it requires little to no computational effort as opposed to simulating random portfolios.

References

[1] John H. Cochrane, Asset Pricing: Revised Edition, Chapter 5 and 6.

[2] Karl Sigman, Portfolio mean and variance, IEOR 4700 Notes 2005.

--

--

Nicolás Besser

Industrial Engineering student at Universidad de Chile. Teacher Asistant in Operations Management and Finance II.