Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to build Markowitz Efficient Frontier for a portfolio of ETFs?

Dear quants,

I am a newbie to Quantopian / coding, hope this is the correct place to ask for help!

I was trying to replicate a Markowitz frontier but using ETFs as underlying assets, for some reason the frontier doesn't look like the typical Markowitz bullet at all. (Code works fine if I just randomly pick some stock tickers such as AAPL, MSFT, TSLA, etc; the one attaching is already a pretty one, I've seen some more random scatter plot looking ones with other ETF tickers)

Not sure what went wrong, can someone please help! Many thanks!


6 responses

I think you have done an excellent job and the greater the number of random portfolios you generate the more "bullet" shape the scatter plot will become. Of course as the portfolio grows in size it becomes impractical to use a brute force approach which leads on to the direction of "solvers".

I have had a merry time over the months learning all about optimisation and it has proved worthwhile. I have been digging deep into machine learning and have therefore been looking at the basics of solvers and hand building some models.

Prior to that I spent a while looking at various Markowitz interpretations here on the Quantopian website and soon realised that many solver approaches landed up at the wrong solution by getting caught in local minima and so forth.

For me it has been a long journey and unfortunately slow since I tend to be obsessively meticulous.

The other question I have been seeking to answer to my satisfaction for years is whether there is actually any point to devising systems, Markowitz inspired or otherwise.

The bogleheads and their "lazy" portfolios may actually represent a more robust solution.

Thanks Anthony, this is interesting. I am actually looking into optimization and machine learning very recently as well :)

Would you by any chance know any systems / methods that could somehow have a smoother experience reaching global minima for such diversified underlyings than solvers?

I am working my way through the examples in Jeff Heaton's Artificial Intelligence for Humans books. You can find all the books examples in python on Github. Here are a few from Volume 1 which contain a few optimization algos.


As I say I'm just taking it in a leisurely fashion out of intellectual curiosity.

I am more deeply aware than ever of the dangers of complexity and over-fitting. More sceptical than ever of the efficacy of back testing beyond the ability to give a very high level overview of what you MAY have been able to achieve with the benefit of hindsight.

One way to think about the optimization problem (actually more of a control problem) is that you'll want to make periodic adjustments to your portfolio (like a thermostat regulates temperature). For a portfolio, in the end, it is a vector of N weights (i.e. a vector in N-dimensional space), normalized to 1 (which is then multiplied times a leverage scale factor, which ranges from 0 to ~6). So, the problem comes down to updating the portfolio vector such that the "distance" between the old and new vectors is minimized, i.e. my objective function is an expression of the relationship between the old and new allocations. One approach is to use the square of the Euclidean distance between the old and new vectors.

So, with only one equality constraint that the new portfolio vector be normalized to 1, one ends up doing nothing. By adding additional constraints, the portfolio vector can be rotated in its N-dimensional space. I am still getting up the learning curve, but using the square of the Euclidean distance as an objective function puts one in the domain of quadratic programming. If the equality and inequality constraints can be expressed as linear functions of the portfolio weights, then there is computational machinery that can handle the problem. I'm not yet clear whether one is guaranteed of convergence to the constrained global minimum, and how long it might take, based on the number of weights (N), and the details of the constraints.

A relevant paper on the topic is http://icml.cc/2012/papers/168.pdf along with its references.

For other optimizers available on Quantopian, see: