# 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 branch of Artificial Intelligence (AI) inspired by the way reproduction and evolution occur 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 simple problem where we could use a Genetic Algorithm. To follow this example you'll need to understand basic algebra. We want to find numbers x and y for which the following are all true:

x + 2y = 4

8xy = 7

x > 1

In this example there are 2 variables. With a bit of algebraic manipulation, it's fairly straightforward to work out that the only solution is when x = 3.5 and y = 0.25. Genetic Algorithms really come into their own with problems where there are many different variables, all of which need to be optimised. Sometimes there's no analytical way of reaching an exact solution. Although using a Genetic Algorithm in this example would be overkill, it nicely illustrates how it would work. So let's forget that we know the solution, and see how we'd use a Genetic algorithm to try to find it.

## Individuals Let’s guess a solution at random, allowing x to be anywhere between 2 and 100 (we know x must be greater than 1), and y between -100 and 100 (we don't have any such constraint on y). Let's guess x = 3 and y = 2. We'll call this combination of x and y an "individual". We could make a number of different guesses, and each guess would be a different individual. If we group these individuals together we have a "population" of individuals. Some guesses would obviously be better than others, but how do we measure 'better'?

Let's see how well our first guess fits the equations. Substituting the values of x and y, we calculate that x + 2y = 7 and 8xy = 48. These are a long way off the mark, as we were looking for 4 and 7 respectively. We need some way of measuring how 'fit' our solution is. A simple way of measuring 'fitness' is subtracting 4 from the value of x + 2y (ignoring the sign), subtracting 7 from the value of 8xy (also ignoring the sign), and adding the two results together. In this case the measure of fitness comes to 3 + 41 = 44. A perfect solution would have a fitness of zero, so 44 is not very good. But in a population of random individuals, some may be better, some worse, but probably none perfect.

## Genes

Each of our individuals has two variables, x and y. This is analogous to how a biological individual has traits like height and weight. Obviously biological organisms have many more traits than just two, but our example is intentionally simple. In biology, traits are encoded by genes, which form part of an organism's DNA. We can do something similar for our individual, with a gene for x and a gene for y. Instead of using DNA, we'll use binary, converting the values of x and y into strings of binary digits. We'll then join the two binary strings together, to give a longer binary string. This will be the chromosome for the individual.

## Reproduction

Now comes the part where we simulate reproduction. Say we start with a population of random individuals, for each of which we’ve calculated the fitness. This is our first generation. We now proceed to the second generation. We let a few of the fittest individuals survive into the next generation. We fill up the remaining vacancies by randomly selecting individuals for breeding. The fitter an individual is, the greater the chance that we'll select it for breeding, but some less fit individuals may get lucky and be selected.

When two individuals breed together, their values are encoded into binary chromosomes, as we described earlier. 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 are changed at random. The offspring chromosomes are used to create new individuals, which go into the next generation.

As an example, we already have a 'random' individual for which x = 3 and y = 2, which we'll call individual A. Suppose A is matched for breeding with individual B for which x = 6 and y = 0.2, giving a fitness of 5. Now suppose that A and B have 2 offspring, C and D. Suppose C inherits the x gene from A and the y gene from B, and that D inherits the y gene from A and the x gene from B. Then for C, x = 3 and y = 0.2, and for D, x = 6 and y = 2. We calculate that C has a fitness of 2.8, and D has 95. C has inherited the best traits from its parents, D the worst, and the fitness values reflect this. So with C we now have a better solution to the problem than either of its parents, even without any mutation. C is likely to propagate its genes to the next generation. D however is a very poor solution, and is unlikely to compete successfully in finding a breeding mate.

## Evolution

This process of reproduction continues from generation to generation, resulting in better and better solutions being generated. 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 two variables, but of many different variables simultaneously. In EvolveMyRetirement®, we use a Genetic Algorithm that treats each combination of values of financial choices as an individual, for which we can work out the fitness. Calculating the fitness of a financial model is much harder than in our simple example above; among other things it involves using Monte Carlo Simulation, since projecting the future involves uncertainty. Trying out every possible combination would be impossible, but using our advanced 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.