# The Genetic Algorithm Behind Your Financial Plan

## What Is A Genetic Algorithm?

A Genetic Algorithm is a general-purpose method for solving optimisation problems, which uses a form of Artificial Intelligence (AI) inspired by the way reproduction and evolution occurs in nature. It can be applied to many different kinds of problem, not only in finance. There are other types of optimisation method, but in many cases a Genetic Algorithm has considerable advantages. For complex problems like financial and retirement planning it works extremely well.

## A Simple Example

Let’s look at a very simple optimisation problem where we could use a Genetic Algorithm.

We want to find the square root of 2. Like pi, the square root of 2 is an irrational number – meaning it never ends and never repeats – making it an infinitely long number that you could never write down, let alone read. So, our final goal is to represent it in up to eight decimal places, just as pi often is.

In this case there’s only one variable, which is the number we’re trying to find. Genetic Algorithms really come into their own with problems where there are many different variables, all of which need to be optimised. Although using a Genetic Algorithm in this example would be overkill, it nicely illustrates how it would work.

Let’s guess 6 possible solutions at random: say 0.5, 1, 1.1, 1.4, 1.75 and 2. We know that none of these are very good solutions. When we square them, we get 0.25, 1, 1.21, 1.96, 3.0625 and 4 respectively.

The differences between these numbers and 2 are 1.75, 1, 0.79, 0.04, 1.0625 and 2 respectively. We can use these differences to rank our guesses in order of how good they are.

## Individuals In a Genetic Algorithm, we treat each guess as an individual in a population. So we treat each of the 6 guesses in our example as an individual. Each individual has a number called its "fitness", which we must know how to calculate. In our example, the smaller the difference between the square of the guess and 2, the higher the fitness. One definition of fitness we could use is to subtract the difference from 1.5, and treating any difference of more than 1.5 as having zero fitness; this means the fitness can range from 0 to 1.5.

Using this rule, we get fitnesses of 0, 0.5, 0.71, 1.46, 0.4375 and 0 respectively. The highest fitness is the fourth one, which corresponds to the best of the six solutions, which was 1.4. This is not the exact solution, but it shows that our fitness calculation is working.

## Reproduction

Now comes the part where we simulate reproduction. We have a population of 6 individuals, for each of which we’ve calculated the fitness. This is our first generation. We now proceed to the second generation. We let the 2 fittest individuals survive into the next generation. We fill up the remaining 4 vacancies by randomly selecting individuals for breeding. The fitter an individual is, the greater the chance that we will select it for breeding, but unfit individuals might well be selected too.

When two individuals breed together, their values are encoded into binary chromosomes. Each chromosome contains genes, and each gene encodes an aspect of the solution to the problem. The two chromosomes are combined, exchanging ‘genetic’ information into two new offspring chromosomes. Occasionally a mutation might occur, and one or more binary bits in an offspring chromosome is changed at random. The offspring chromosomes are used to create new individuals, which go into the next generation.

## Evolution

This process of reproduction continues from generation to generation, resulting in better and better solutions being generated. In practice, a population size of 6 is far too low, even for a simple problem like this one. Depending on the problem, population sizes could be in the hundreds or thousands. The nice thing about solving a problem using a Genetic Algorithm is that, as long as we can predict the consequences of our choices, and express those consequences by means of a fitness calculation, we can optimise our choices. Over time, the breeding process selects the best features of a solution and discards the worst. The use of mutation means that occasionally brand new solutions pop into existence, which may result in improvements.

## Optimising Financial Planning

Unlike our simple example, financial planning involves finding optimal values, not just of one variable, but of many different variables simultaneously. In EvolveMyRetirement®, we use a Genetic Algorithm that treats each combination of values of financial variables as an individual, for which we can work out the fitness. Trying out every possible combination would be impossible, but using a Genetic Algorithm, we can rapidly weed out the bad combinations, and mix-and-match the better combinations to arrive at an optimised combination.

The use of a Genetic Algorithm is central to making EvolveMyRetirement® a truly powerful online financial planning tool.