Using Genetic Algorithms to Analyze Stock Portfolios

by Greg Thatcher, MCSD, MCDBA, MCSE

Try my new Genetic Algorithms Stock Portfolio Generator

The term "Genetic Algorithms" refers to an artificial intelligence technique that can be used to find "best" solutions to very difficult problems. This technique consists of a simple step-by-step algorithm that simulates the theory of evolution. Genetic Algorithms have been used to solve a wide variety of business and scientific problems. This paper serves as an introduction to using genetic algorithms to analyze stock portfolios.

According to evolutionary theory, Mother Nature is often confronted with the problem of evolving species to fit their environment. Using selection (good specimens reproduce more often, weak specimens less often or not at all), recombination (both parents pass on characteristics), and mutation (occasionally, a child is born with a new characteristic), evolution can produce optimum specimens relatively quickly. Granted, these evolutions can take millions of years, but when you consider the number of possible genetic combinations, it is amazing that Nature can find so many optimum solutions in so short a time period, an observation that has been born out in our research.

Genetic Algorithms are good at finding "maximum" solutions. To illustrate, we will consider the problem of finding a "best" stock portfolio. The steps of the genetic algorithm are listed below:

  • Step 1 From the list of NASDAQ stocks, generate several portfolios of randomly chosen stocks.
  • Step 2 Evaluate each of the portfolios, assigning it a score.
  • Step 3 If a portal has a high score, make several "clones" (copies) of it. If it has a low score, get rid of it.
  • Step 4 Recombine the portfolios. Choose several pairs of portfolios and mix and match their stocks.
  • Step 5 Mutate a few of the portfolios by randomly adding or removing a small number of stocks.
  • Repeat steps 2-5 above many, many times to weed out weak portfolios and find the "best" portfolios.

To illustrate, consider the following simple example. First, we randomly generate several portfolios:

Portfolio 1 Portfolio 2 Portfolio 3 Portfolio 4 ...
Stock A Stock E Stock J Stock N
Stock B Stock F Stock K Stock P
Stock C Stock G Stock L Stock Q
Stock D Stock H Stock M Stock T

Next, we assign a score to each portfolio. There are many criteria you can use for this such as:

  • best average monthly percentage price gain (for low risk, high gain portfolios)
  • Best average annual percentage gain
  • Best annual market capitalization gain
  • etc.

Because of negative and positive correlations between stocks in a portfolio, it would be exceedingly difficult to perform these types of calculations using another method. Fortunately, Genetic Algorithms can quickly solve these kinds of problems.

After scoring, we perform selection on the portfolios, deciding how many "child" portfolios that portfolio will "create" based on its score.

Portfolio Score Children
Portfolio 1 40 0
Portfolio 2 60 1
Portfolio 3 35 0
Portfolio 4 90 2

Since Portfolio 4 had a high score (90), it will "reproduce" twice (have two children or clones) in the next round. Since portfolio 1 had the lowest score, it will not appear in the next round.

After selection, we are left with the following:

Portfolio 2 Portfolio 4 Portfolio 4 (again)   ...
Stock E Stock N Stock N
Stock F Stock P Stock P
Stock G Stock Q Stock Q
Stock H Stock T Stock T

For the Recombine phase, we swap two stocks between Portfolio 2 and Portfolio 4 (stock G for stock Q and stock H for stock T).

Portfolio 2 Portfolio 4 Portfolio 4 (again)   ...
Stock E Stock N Stock N
Stock F Stock P Stock P
Stock Q Stock G Stock Q
Stock T Stock H Stock T

Then, we mutate one of the stocks in one of the portfolios (stock T is randomly replaced with stock Z)

Portfolio 2 Portfolio 4 Portfolio 4 (again)   ...
Stock E Stock N Stock N
Stock F Stock P Stock P
Stock Q Stock G Stock Q
Stock T Stock H Stock Z

Then, we repeat the above process of scoring, selection, recombination, and mutation many thousands (or millions) of times until we are left with a set of optimum portfolios.

Try my new Genetic Algorithms Stock Portfolio Calculator