Portfolio optimization in R using a Genetic Algorithm

Gianluca Malato
The Trading Scientist
10 min readNov 17, 2018

--

Portfolio optimization is one of the most interesting fields of study of financial mathematics. Since the birth of Modern Portfolio Theory (MPT) by Harry Markowitz, many scientists have studied a lot of analytical and numerical methods to build the best investment portfolio according to a defined set of assets.

In this article, I’ll show a method to optimize a portfolio by using a numerical technique based on genetic algorithms. The power of genetic algorithms makes it possible to find the optimal portfolio even when the objective function isn’t smooth or differentiable.

Portfolio composition

Let’s say we have selected N financial assets we want to invest in. They can be stock, funds, bonds, ETF etc. Each one of them has many historical returns, that are the price relative difference from one period to another. Periods can be days, weeks, months and so on.

The return of the i-th asset between period t and period t-1 is defined as:

Now, when we want to build an investment portfolio, we want to mix many assets together allocating a fraction x of our capital to each one of them. Each fraction is called weight.

The portfolio return at time t is then:

--

--

Gianluca Malato
The Trading Scientist

Theoretical Physicists, Data Scientist and fiction author. I teach Data Science, statistics and SQL on YourDataTeacher.com. E-mail: gianluca@gianlucamalato.it