Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 · Projects and Practice

Project: Hardy-Weinberg Calculator

Learn the Hardy-Weinberg equations, work through a carrier-frequency problem by hand, and build a calculator that tests whether a population is in equilibrium.

The Hardy-Weinberg principle describes what happens to allele and genotype frequencies in a population when nothing is changing it. It gives a baseline: if the real frequencies differ from the prediction, some evolutionary force is acting. In this project you will learn the two equations, solve a typical exam problem by hand, and write a calculator.

The equations

For a gene with two alleles, A (dominant) and a (recessive), let:

  • p = frequency of allele A
  • q = frequency of allele a

Because there are only two alleles, p + q = 1.

If mating is random, the genotype frequencies in the next generation are:

p² + 2pq + q² = 1

where p² is the frequency of AA, 2pq of Aa and q² of aa.

Conditions for equilibrium

The frequencies stay the same from generation to generation only if:

  1. The population is very large (no genetic drift).
  2. Mating is random.
  3. There is no mutation.
  4. There is no migration (gene flow).
  5. There is no natural selection (all genotypes survive and reproduce equally).

No real population meets all five exactly, but the principle is a useful starting point.

A worked problem

In a population, 4% of people show a recessive trait. Find the frequency of carriers (Aa), assuming equilibrium.

  1. The recessive trait shows only in aa, so q² = 0.04.
  2. q = √0.04 = 0.2.
  3. p = 1 - q = 0.8.
  4. Carriers: 2pq = 2 x 0.8 x 0.2 = 0.32, so 32%.
  5. Check: p² = 0.64, 2pq = 0.32, q² = 0.04, and 0.64 + 0.32 + 0.04 = 1.
Python Version 1: from the recessive frequency

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Testing a real sample

Now suppose you count genotypes in a sample of 500 individuals: 320 AA, 140 Aa and 40 aa. Is it in equilibrium?

  1. Count alleles: there are 1,000 allele copies. A copies = 2 x 320 + 140 = 780, so p = 0.78. a copies = 2 x 40 + 140 = 220, so q = 0.22.
  2. Expected: AA = p² x 500, Aa = 2pq x 500, aa = q² x 500.
  3. Compare observed and expected with a chi-squared test. If the statistic is above 3.84 (the critical value for 1 degree of freedom at the 5% level), the difference is statistically significant, and the population is not in equilibrium.
Python Version 2: test a sample for equilibrium

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If the test says "significant", the next question is why. A shortage of heterozygotes, for example, often suggests non-random mating (inbreeding), while a pattern consistent with selection would suggest differences in survival.

Predicting change when selection acts

Python Version 3: allele frequency under selection against aa

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how slowly q falls when it is small: most copies of a are hidden in heterozygotes (Aa), where selection does not act. This is why harmful recessive alleles can persist in populations for a long time.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. In the Hardy-Weinberg equation p² + 2pq + q² = 1, what does 2pq stand for?

    2pq is the frequency of Aa genotypes.
  2. 2. If q² = 0.09, what is the frequency of carriers (2pq)?

    q = 0.3, p = 0.7, so 2pq = 2 x 0.7 x 0.3 = 0.42.
  3. 3. Which of these breaks the Hardy-Weinberg conditions?

    Migration changes allele frequencies, so the population is not in equilibrium.

Summary

  • p + q = 1 for alleles and p² + 2pq + q² = 1 for genotypes at equilibrium.
  • Start from the recessive phenotype: q = √(q²).
  • A chi-squared test compares observed counts with expected counts.
  • Departures from the prediction point to selection, drift, mutation, migration or non-random mating.

Next: Practice Set: Evolution and the Environment