← Guides Chi-Square HWE Test

Introductory 11 min read Open the module

Testing Hardy-Weinberg with chi-square

Three genotype counts, one statistic and one critical value decide whether a sample fits random mating.

Read first: Genotype frequencies and Hardy-Weinberg

Does a sample match Hardy-Weinberg?

The test asks one question: do the genotype counts in a sample look like the product of random mating at one locus? The answer is a verdict plus a number. Take 160 individuals scored at a locus with alleles A and a: 100 AA, 50 Aa and 10 aa. These are the module's starting counts.

Step one is to estimate the allele frequency from the counts. Each AA carries two A copies and each Aa carries one, so p = (2 × 100 + 50) / (2 × 160) = 0.78125, and q = 0.21875. Step two is to ask what Hardy-Weinberg proportions p², 2pq and q² would give for 160 individuals at that p. The answer is 97.66 AA, 54.69 Aa and 7.66 aa. The sample has 100, 50 and 10.

The gap between observed and expected counts is the evidence. A gap of a few individuals per class is ordinary sampling noise, because a sample is a random draw from the population. The test measures the gap against that noise. Nothing is simulated: the same three counts always give the same result.

The starting sample

Figure 1. Bars are the observed counts 100, 50 and 10. The dashed line is the expected counts 97.66, 54.69 and 7.66. The line passes close to the top of every bar.

The statistic and one degree of freedom

Pearson's statistic adds one term per genotype: χ² = sum of (O − E)² / E, with O the observed and E the expected count. Squaring removes the sign, so a surplus and a deficit both count. Dividing by E makes a miss of 5 individuals matter more when 8 were expected than when 100 were.

For the starting sample the three terms are 0.056 for AA, 0.402 for Aa and 0.717 for aa. They sum to χ² = 1.176.

Is 1.176 large? The reference is the chi-square distribution with 1 degree of freedom. There are three genotype classes. The counts must sum to N, which removes one. The allele frequency p was estimated from the same counts, which removes another. Three minus two leaves one.

At 1 degree of freedom and a 5% error rate the critical value is 3.841. The module rejects Hardy-Weinberg when χ² is above it. The P-value is the chance of a χ² at least this large if the population really were in equilibrium. Here P = 0.278, so the sample is consistent with Hardy-Weinberg. Failing to reject is weaker than confirming: this small departure (F = 0.086, see the box below) would reject only with about 520 individuals or more.

A heterozygote deficit

Figure 2. 50 AA, 20 Aa and 30 aa. The allele frequency is p = 0.6, so Hardy-Weinberg expects 36, 48 and 16. The Aa bar is under half the expected 48, while both homozygote bars are too tall.

Where the statistic comes from

Figure 3. The three terms for the same sample: 5.444 for AA, 16.333 for Aa and 12.250 for aa. They sum to χ² = 34.03, P < 0.0001. The heterozygote class gives the largest term.

Exercise

Same proportions, different sample size

Enter 30, 60 and 10 and run. Then run 15, 30 and 5, and 60, 120 and 20. All three have the same genotype proportions.

What to expect. The allele frequency is p = 0.6 each time. The sample of 100 gives χ² = 6.25 and P = 0.0124, which rejects. Halving it gives χ² = 3.125 and P = 0.077, which does not reject. Doubling gives χ² = 12.5 and P = 0.0004. Multiplying every count by k multiplies χ² by k, so the same departure is stronger evidence in a larger sample.

Open the walkthrough step →

Exercise

A sample that is too small

Enter 12, 6 and 2 and run. Read the warning under the results.

What to expect. N is 20 and p = 0.75. The expected aa count is only 1.25, so the module warns that an expected count is below 5. χ² = 0.80 and P = 0.371. The statistic is computed, but the chi-square approximation is unreliable below that size. An exact test is the better tool for such a sample. Entering 100, 0 and 0 gives a monomorphic sample (p = 1) and the module reports the test as undefined.

Open the walkthrough step →

Summary

  • The test estimates p from the counts, computes the expected counts p²N, 2pqN and q²N, and sums (O − E)² / E.
  • It uses 1 degree of freedom: three classes, minus the fixed total, minus the estimated p.
  • The critical value is 3.841. The starting sample gives χ² = 1.176 (P = 0.278) and is not rejected; 50, 20 and 30 gives χ² = 34.03 and is rejected.
  • With p estimated, χ² = N × F², so the same proportions give a larger χ² in a larger sample.
  • Expected counts below 5 make the approximation unreliable, and a monomorphic sample (p = 0 or 1) has no test at all.
  • A rejection reports a departure from equilibrium, not its cause.