Skip to content

Genetic algorithm

Selection, crossover and mutation search a population of candidates. A four-asset example shows why better parents need not produce better or feasible children.

A genetic algorithm is a population-based search method that repeatedly selects candidate solutions, recombines parts of them through crossover, and introduces random changes through mutation. In quantitative work it can search discrete choices—such as which assets to include—when the objective is awkward to differentiate or the feasible combinations are expensive to inspect. It returns a candidate found under a particular search budget; a good fitness score alone establishes neither global optimality nor future trading profitability.
The biological vocabulary names concrete programming choices. A chromosome encodes one candidate, its genes encode individual decisions, and a fitness function assigns the score used by selection. Binary strings are one representation, not a requirement of the method. The encoding and operators must fit the problem. Cornell's algorithm discussion introduces these pieces.

Watch one crossover, then check the answer exactly

Consider a fictional four-asset selection problem. A chromosome has bits in asset order A, B, C, D; 1 means include that asset. We require exactly two assets, a cardinality constraint. The stipulated one-period scores are 4, 3, 2 and 1 points. These are teaching scores, not estimated returns or portfolio weights; there is no risk, correlation or transaction-cost model here.

For xi∈{0,1}x_i\in\{0,1\}, let the fitness be

F(x)=4x1+3x2+2x3+x4−100(∑i=14xi−2)2.F(\mathbf{x})=4x_1+3x_2+2x_3+x_4 -100\left(\sum_{i=1}^{4}x_i-2\right)^2.

The last term penalises the wrong number of selected assets. The coefficient 100 is stipulated for this tiny example: it makes every infeasible chromosome worse than every feasible one. It is not a generally suitable penalty for other problems.

Suppose selection has supplied parents 1010 and 0101. Their fitness scores are 4+2=64+2=6 and 3+1=43+1=4. Cut each string after the second bit and exchange the tails:
CandidateBefore recombinationAfter recombinationFitness after
First10 followed by 1010 followed by 01: 10015
Second01 followed by 0101 followed by 10: 01105
Both children satisfy the constraint. Neither beats the better parent. Crossover creates proposals; it does not certify improvements. This event is stipulated to expose the mechanics, rather than presented as a simulated search run.
Fictional four-asset selection: parents 1010 and 0101 have fitness 6 and 4; crossover after bit two gives 1001 and 0110, both fitness 5. Across all six feasible pairs, scores are 3, 4, 5, 5, 6 and 7; 1100 is uniquely best.
Fictional four-asset selection: parents 1010 and 0101 have fitness 6 and 4; crossover after bit two gives 1001 and 0110, both fitness 5. Across all six feasible pairs, scores are 3, 4, 5, 5, 6 and 7; 1100 is uniquely best.
Figure: Recombination can lose the better parent's score. Exhaustive enumeration supplies the exact benchmark in this small fictional problem: 1100 has fitness 7.
There are 24=162^4=16 binary strings and six feasible pairs. Checking them all gives the unique optimum 1100, with score 7. In fact, this additive toy needs no genetic algorithm: simply choose the two largest scores. Its purpose is to make the operators auditable. More complicated interactions and constraints can remove that simple solution, but complexity should be established from the actual objective and feasible set.

The constraint must survive the operators

Starting from child 1001, flip its second bit. The mutation produces 1101: three assets, raw score 8, but fitness 8−100=−928-100=-92. More selected assets raised the raw score while violating the problem.
There are several defensible designs. Reject infeasible children; evaluate them with an explicit penalty; repair them before evaluation; or choose operators that preserve feasibility. A swap mutation, for example, removes one included asset and adds one excluded asset. Swapping D out and B in takes 1001 to 1100 while keeping exactly two assets. This illustrates a possible move, not a claim about how frequently a search will find it. A repair rule also changes which solutions the search tends to visit, so it belongs in the algorithm specification.

Ordinary one-point crossover can itself violate cardinality for other parent pairs. Feasible parents alone do not make an operator feasible.

What makes it a search algorithm

One generation combines several decisions:

  1. Evaluate the population under the declared fitness and constraint treatment.
  2. Select parents with a bias toward better fitness. Tournament selection, for example, compares sampled candidates; fitness-proportional selection instead samples using nonnegative selection weights.
  3. Apply the chosen crossover and mutation operators, then evaluate the resulting candidates.
  4. Choose survivors and repeat until an evaluation budget or another declared stopping rule is reached.
The distinction in step 2 matters here: negative penalised scores cannot be used directly as probabilities. José M. Vidal's notes on fitness and selection compare selection rules. A rule that only ranks or compares fitness avoids interpreting the raw scores as probability weights.
Elitism explicitly retains one or more of the best candidates. With a fixed, deterministic fitness function and unchanged feasibility rules, retaining the best candidate keeps the best population score from falling. It does not force further improvement. The Bologna-hosted tutorial's comments on elitism explain why an otherwise good parent can disappear during replacement.

Selection that repeatedly reproduces similar candidates can leave too little diversity to explore alternatives. Mutation offers additional variation, but its presence does not guarantee that a finite run escapes a poor region. Report the encoding, operators, population size, stopping budget and random seeds; compare runs against an exact benchmark where available or against simpler searches under a comparable evaluation budget.

For a trading-rule fitness based on historical performance, search is also repeated model selection. Keep backtest overfitting separate from numerical optimisation: improving the search score can mean fitting historical noise more effectively. More generations do not replace evaluation on data kept outside the search.

To understand the distinction between a heuristic result and a proven approximation guarantee, see NP-Complete Problems and Approximation as a Fallback. Its public page is a preview; the full lesson requires membership.

Sources

Last updated
  • genetic algorithm
  • optimisation
  • heuristics
  • asset selection
  • constraints
← Back to the glossaryBack to top ↑