Nature, in Code

Directional Selection

Given the equation of the change in allele frequency per generation:

$$\Delta p = \frac{pqs ( ph + q (1-h))}{(1 - 2pqhs - q^{2}s)}$$
directional selection occurs when the heterozygous effect, h, is between 0 and 1. In other words, the fitness of the heterozygote A1A2 is intermediate. When plotting the change of the allele A1 frequency, Δp, as a function of the allele's current frequency, we'll get the following graph (with s = 0.1, h = 0.2):

Because Δp is always positive, p will go to 1 (i.e. the allele will go to fixation):

Code

First plot

var s = 0.1;
var h = 0.2;

var data=[];
var x_max = 1;

for (var i = 0; i <= x_max + 0.005; i = i + 0.01) {
    var p = i;
    var q = 1-p;
    var delta_p = (p*q*s * ( p*h + q*(1-h))) / (1 - 2*p*q*h*s -q*q*s);
    data.push(delta_p);
}

draw_line_chart(data,"p","\u0394 p",[],x_max,true);
			
Second plot

var s = 0.1;
var h = 0.2;
var p = 0.01;

var data=[];
var generations = 400;

for (var i = 0; i < generations; i = i + 1) {
    var q = 1-p;
    var delta_p = (p*q*s * ( p*h + q*(1-h))) / (1 - 2*p*q*h*s -q*q*s);
    p = p + delta_p;
    data.push(p);
}

draw_line_chart(data,"Generation","p",[],generations);
			
Note: the draw_line_chart function is built with D3.js and can be found here.