Nature, in Code

Genetic Drift: The Effect of Population Size

Genetic drift is the change in allele frequencies due to random chance events. The graphs below shows 10 simulation runs each, where the frequency of a neutral allele fluctuates over 200 generations. The simulations are based on a "one locus, two alleles" model, and p denotes the frequency of one of the alleles.

The first graph shows the evolutionary dynamics of a population of size N=20. By generation 200, all populations have usually lost one of the two alleles:

RELOAD SIMULATION

The effect of drift is strongly dependent on the population size N. In larger populations, the effect of drift is weaker, as can be seen in the graph below where N = 200:

RELOAD SIMULATION

And N = 2000:

RELOAD SIMULATION

Code


var p;
var N = 20; // change this to see effect
var generations = 200;
var data = [];
var simulations = 10;


function next_generation(simulation_data) {
	var draws = 2 * N;
	var a1 = 0;
	var a2 = 0;
	for (var i = 0; i < draws; i = i + 1) {
		if (Math.random() <= p) {
			a1 = a1 + 1;
		}
		else {
			a2 = a2 + 1;
		}
	}
	p = a1/draws;
	simulation_data.push(p);
}

function simulation(simulation_counter) {
	p = 0.5;
	for (var i = 0; i < generations; i = i + 1) {
		next_generation(data[simulation_counter]);
	}
}

for (var i = 0; i < simulations; i = i + 1) {
	data.push([]);
	simulation(i);
}

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