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

Bernard Brenyah
Oct 21, 2017 · 3 min read
Image for post
Image for post
Image Credit: http://hmarkowitz.com

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:

Modern 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:

Image for post
Image for post
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:

BOOM!

Image for post
Image for post
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:

DS Biz

A blog for data analytics using data science technologies

Bernard Brenyah

Written by

I have a love/hate relationship with numbers

DS Biz

DS Biz

A data analytic blog from a newbie for newbies. Check out the associated GitHub page for all the source codes: https://github.com/PyDataBlog/Python-for-Data-Science

Bernard Brenyah

Written by

I have a love/hate relationship with numbers

DS Biz

DS Biz

A data analytic blog from a newbie for newbies. Check out the associated GitHub page for all the source codes: https://github.com/PyDataBlog/Python-for-Data-Science

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store