Markowitz’s Efficient Frontier in Python [Part 1/2]

Bernard Brenyah
Oct 21, 2017 · 3 min read
Image Credit:

Harry Markowitz’s contribution to the world of finance and economics cannot be emphasized enough. He is widely regarded as the pioneer of Modern Portfolio Theory (MPT) with his groundbreaking paper “Portfolio Selection” in 1952. He eventually won a Nobel Memorial Prize in 1990 in Economic Sciences for his contribution to the field.

MPT is taught around the world in practically every business/finance class. This post (part of a 2 part series) looks at the underpinnings of the theory and the construction of an efficient frontier with Python using real-world stock data.

So what is MPT, why should you even bother understanding it and how does it even work with Python?

Like most theories, MPT was developed with some assumptions of the real world. The attempt to explain MTP commences with a presentation of its assumptions:


- Investors are rational and avoid risks whenever possible

- Investors aim for the maximum returns for their investment

- All investors share the aim maximizing their expected returns

- Commissions and taxes on the market are left out of consideration

- All investors have access to the same sources and level of all necessary information about investment decisions

- Investors have unlimited access to borrow and lend money at the risk free rate

odern Portfolio Theory is a theory about how investors (who are risk averse) construct portfolios that maximise their expected returns for given levels of risk. The breakthrough insight from MPT was the fact that risks and returns characteristics of various investments need not be isolated and analysed but looked at how these investments affected the performance of a portfolio. The assumptions of MPT, thus, emphasise that investors only assume additional risk when there is a possibility of higher expected returns — “High risk, High Reward”

The fundamental tenet of this theory is the possibility for investors to construct an “efficient set of portfolios — Efficient Frontier” that offers the maximum expected returns for a given level of risk. An investor’s tolerance for risk determines the type of “efficient portfolio” he/she opts for. An investor with the lowest tolerance opts for a portfolio that offers him/her the maximum expected return given the lowest possible risk and vice versa. The diagram below gives an overview of the concept of Efficient Frontier:

Efficient Frontier. Image Credit: InvestingAnswers

As covered in a previous post, different combinations of securities produce different expected returns. One of the most important realisation after Prof Markowitz proved an efficient set of portfolios was the power of diversification. By simply constructing portfolios with different combinations of securities, investors could achieve a maximum expected return given their risk preferences due to the fact that the returns of a portfolio are greatly affected by nature of the relationship between assets and their weights in the portfolio.

So how can we generate the Efficient Frontier with Python using real world stock data?

Let’s get the data first from Quandl:

Output of data.head():

Next up is some small data wrangling process. Let’s sort the adjusted closing prices by tickers:

Output of table.head() :

We are almost there now. To get the efficient frontier, we need to simulate imaginary combinations of portfolios (50,000 portfolios of different combinations in this case).

Output of df.head() :

Great! All the heavy lifting is now done. Let’s visualise our efficient frontier:


Efficeint Frontier

Quite a long post! I hope you have a solid foundation of the underpinnings of Modern Portfolio Theory. The next post completes this series on MPT by looking for the optimal portfolios from the set generated in this post. Until then, happy coding!

All the source codes at usual is hosted on the blog’s GitHub page but for those who don’t have time to go through GitHub, here’s the source code for this post:


A data analytic blog from a newbie for newbies. Check out the associated GitHub page for all the source codes:

Bernard Brenyah

Written by

I have a love/hate relationship with numbers



A data analytic blog from a newbie for newbies. Check out the associated GitHub page for all the source codes:

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade