NetLogo banner

 Contact Us

 Modeling Commons

 User Manuals:


NetLogo User Community Models

(back to the NetLogo User Community Models)

Hardy Weinberg Classroom Model

by Kenneth Letendre (Submitted: 01/02/2009)

[screen shot]

Download Hardy Weinberg Classroom Model
If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".

(You can also run this model in your browser, but we don't recommend it; details here.)


This is a model of the Hardy-Weinberg (HW) equilibrium. The HW principle predicts the genotypic frequencies that will be observed in a population over the course of generations given particular allele frequencies, and given that five assumptions (discussed below) hold true in the population.

Given two alleles, A and a, and the frequencies of each allele in the population, freq(A)=p and freq(a)=q, the HW principle predicts:

1) p + q = 1.
That is, since A and a are the only alleles at this locus in the model population, the allelic frequencies of A and a must add up to 1.

2) The genotypic frequency of AA homozygotes in the population is p^2.
The frequency of aa homozygotes is q^2. The probability that any given member of the population will inherit two A alleles is p x p.
The frequency of heterozygotes is 2pq. The probability that any given member of the population will inherit one A and one a allele is p x q x 2, since a heterozygote can inherit allele A from its mother and allele a from its father, OR allele a from its mother and A from its father.
p^2 + 2pq + q^2 = 1. That is, the frequencies of both types of homozygotes and the frequency of heterozygotes must add up to 1, since these are the only possible combinations.

These predictions hold true given that five assumptions about the population all hold true.

1) Large (infinite) population size. In small populations, chance differences in reproductive success and mating choices can produce deviations from the predictions of HW.

2) No selection. There is no systematic difference in the survival or reproductive success of organisms with different genotypes.

3) No mutation. The alleles are inherited from one generation to the next without being changed by mutation.

4) No migration. No organisms leave the population, and no new ones come in.

5) Random mating. Organisms choose mates at random with respect to the alleles of interest in the model.

If any of these assumptions do not hold true in a population, the observed genotypic frequencies will deviate from the predictions of HW in particular ways depending on the assumption(s) that is (are) violated.


The model is initialized with a randomly distributed population of blue (the dominant trait) and yellow (the recessive trait) organisms. Organisms are randomly assigned alleles according to the selected frequency of the A allele (the frequency of the a allele is determined as q = p - 1.)

As the model runs, organisms move around the world in a correlated random walk at a dispersal rate determined by the user. On each tick, organisms select a mate, either by mating with another organism chosen at random anywhere in the world, or by choosing a nearby organism to mate with. An offspring is produced adjacent to the reproducing organism, which randomly inherits either the a or A alleles from each of its parents. The organisms are diploid, but are essentially hermaphroditic, as every organism is capable of producing offspring and may mate with any other organism without the need to locate a mate of the opposite sex.

Following reproduction, the population is culled to bring the population size back down to the carrying capacity determined by the user. During each culling, each organism is subject to a probability of death determined by the degree to which the current population size exceeds the carrying capacity. As a result, an organism may live for several generations, or it may not survive to first reproduction. There is no maturation time, so that any organism that survives the first culling following its birth can reproduce during the next reproduction cycle.

The model ends when it reaches a specified number of generations ("ticks"), or when one allele becomes fixed in the population (that is, the other allele goes extinct), or when the entire population of organisms goes extinct (e.g. due to high selection against both phenotypes).


The "proportion-allele-A" slider bar determines the initial frequency of the allele A. The frequency of allele a is determined by calculating freq(a) = 1 - freq(A). The "population-size" slider determines the carrying capacity of the system. The "species" chooser allows the user to select from a list of possible icons to represent the organism as they move around the world.

Clicking the "setup" button initializes the world with a population of organisms of the selected species, with the specified allele frequencies. The "go" button starts the model.

The "Population size" monitor displays the current population size. Note that this population size will not always match exactly the value selected by the "population-size" slider. In fact, during each reproduction cycle, the population size will rise well above this value, and then fall back roughly to the specified population size at the end of the culling cycle. However because mortality for each organism is determined by a certain probability, the final population size will not be exactly the specified value, although it will be close.

Graphs track the genotypic frequencies, phenotypic frequencies, and allele frequencies over time as the model runs. Monitors display the current values for each of these.

As the model runs, the user may change the settings of any slider, chooser or input -- with the exception of the "proportion-allele-A" slider -- and the model will reflect these newly selected values. The value of the "proportion-allele-A" slider is used only at model setup; allele frequencies are determined only by the behavior of the organisms after the model begins running.


Note that the genotypic and phenotypic frequencies approximate the values predicted by the HW formulas. Try calculating the predicted values based on the allele frequencies you have specified (or the current allele frequencies obtained by the current A and a alleles preent in the population) and compare these to the actual values produced by the model as it runs. The model itself does not make use of the HW formulas, but produces values similar to those predicted by HW by the interactions of the model organisms.

Note the random changes in the genotypic, phenotypic, and allelic frequencies over time. These changes are more apparent with smaller population sizes, but can still be observed even with populations in the thousands. These random changes result from random differences in the survival and reproductive success of individuals each generation, and are called genetic drift. Genetic drift has a bigger effect on the makeup of small populations than larger ones. Theoretically, the HW assumption of "large population" actually requires an infinitely large population in order to completely eliminate the effect of drift.


Experiment with the settings of the model to create violations of the five assumptions described above. The Hardy-Weinberg equilibrium describes a theoretical population that cannot exist in the real world; perhaps its greatest value is in describing a population where no evolution is occurring, in order to better understand real populations where one or more of the five assumptions are violated, and evolution is occurring.

1) Large (infinite) population size. Try running the model with populations of different sizes in order to observe differences in the strength of genetic drift.
2) No selection. Try experimenting with different degrees of selection against (increased mortality of) the blue and yellow phenotypes. You will observe that selection against the recessive phenotype (yellow color) takes much longer to completely remove the a allele from the population, even with heavy selection against the yellow phenotype. Why is this? What does this tell us about the persistence of recessive genetic disorders in the population?
3) No mutation. Experiment with different mutation rates from dominant to recessive, or recessive to dominant. What happens if there is a large rate of mutation in both directions?
4) No migration. Experiment with different rates of immigration of blue and yellow individuals. How does immigration of individuals of a particular color effect the overall genetic makeup of the population?
5) Random mating. The "mate-with" chooser can cause the organisms in the model to choose mates completely at random, selecting any other organism in the world as a mate. You can also cause organisms to mate with a neighbor, so that organisms must be adjacent in order to mate with each other. Note what happens to the frequency of heterozygotes when organisms are mating with their neighbors. Also note that increasing the dispersal rate (the distance the organisms move each time step) decreases the effect of mating with neighbors on the phenotypic makeup of the population. Why does this happen?


Other methods for violating the assumptions of the HW equilibrium could be added. For example, selection in this model is caused by increasing the mortality rate of one or both of the phenotypes. Selection could also result from differential reproductive output, or from differential success in finding mates (sexual selection).

The model could also be extended to multiple genes to, for example, examine the effect of linkage on inheritance.


NetLogo Library Models:
-GenDrift (T reproduce)
-Simple Birth Rates
NetLogo Community Models:
-PopGen Fishbowl 1
-Genetics and Cellular Automata


Hardy-Weinberg Classroom Model (2009)
Kenneth Letendre
Departments of Biology and Computer Science
University of New Mexico

Council-Garcia, C.L., S. Ligon, B. Milne, R. Thornhill and D. Swenton. 2004. Biology 203L: Evolution and Ecology Lab Manual.
Hardy, G. H. (1908). "Mendelian proportions in a mixed population". Science 28: 49 – 50.
Stern, C. (1943). "The Hardy–Weinberg law". Science 97: 137–138.
Weinberg, W. (1908). "Über den Nachweis der Vererbung beim Menschen". Jahreshefte des Vereins für vaterländische Naturkunde in Württemberg 64: 368–382.

(back to the NetLogo User Community Models)