Nature, in Code

Genetic Drift

RELOAD SIMULATION

This graph shows the frequency of a neutral allele in a population of 500 individuals over 1,000 generations. It is based on a "one locus, two alleles" model, and p denotes the frequency of one of the alleles.

Code


var p = 0.5;
var N = 500;
var generations = 1000;
var data = [];

function next_generation() {
	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;
	data.push(p)
}

for (var i = 0; i < generations; i = i + 1) {
	next_generation();
}
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.