Hardy-Weinberg principle
In population genetics, the HardyWeinberg principle is a relationship between the frequencies of alleles and the genotype of a population.  The occurrence of a genotype, perhaps one associated with a disease, stays constant unless matings are non-random or inappropriate, or mutations accumulate.  Therefore, the frequency of genotypes and the frequency of alleles are said to be at "genetic equilibrium".  Genetic equilibrium is a basic principle of population genetics.
The Hardy-Weinberg principle is like a Punnett square for populations, instead of individuals.  A Punnett square can predict the probability of offspring's genotype based on parents' genotype or the offsprings' genotype can be used to reveal the parents' genotype.  Likewise, the Hardy-Weinberg principle can be used to calculate the frequency of particular alleles based on frequency of, say, an autosomal recessive disease.
In the simplest case of a single locus with two alleles: the dominant allele is denoted A and the recessive a.  Their frequencies are p and q; freq(A)=p and freq(a)=q.  Based on the fact that the probabilities of all genotypes must sum to unity, we can determine useful, difficult-to-measure facts about a population.  For example, a patient's child is a carrier of a recessive mutation that causes cystic fibrosis in homozygous recessive children.   The parent wants to know the probability of her grandchildren inheriting the disease.  In order to answer this question, the genetic counselor must know the chance that the child will marry a carrier of the recessive mutation.  This fact may not be known, but disease frequency is known.  We know that the disease is caused by the homozygous recessive genotype; we can use the Hardy-Weinberg principle to work backward from disease occurrence to the frequency of heterozygous recessive individuals.
This concept is also known by a variety of names: HWP', HardyWeinberg equilibrium, HWE, or HardyWeinberg law.  It was named after G. H. Hardy and Wilhelm Weinberg.

# Assumptions
The original assumptions for HardyWeinberg equilibrium (HWE) were that the organism under consideration:
- Is diploid, and the trait under consideration is not on a chromosome that has different copy numbers for different sexes, such as the X chromosome in humans (i.e., the trait is autosomal)
- Is sexually reproducing, either monoecious or dioecious
- Has discrete generations
In addition, the population under consideration is idealised, that is:
- Random mating within a single population
- Infinite population size (or sufficiently large so as to minimize the effect of genetic drift)
and experiences:
- No selection
- No mutation
- No migration (gene flow)
The first group of assumptions are required for the mathematics involved.  It is relatively easy to expand the definition of HWE to include modifications of these, such as for sex-linked traits.  The other assumptions are inherent in the Hardy-Weinberg principle.
A Hardy-Weinberg population is used as a reference population when discussing various factors.  It is not surprising that these populations are static.

# Derivation
A better, but equivalent, probabilistic description for the HWP is that the alleles for the next generation for any given individual are chosen randomly and independent of each other. Consider two alleles, A and a, with frequencies p and q, respectively, in the population. The different ways to form new genotypes can be derived using a Punnett square, where the fraction in each cell is equal to the product of the row and column probabilities.
The final three possible genotypic frequencies in the offspring become:
- f(\mathbf{AA}) =  p^2\,
- f(\mathbf{Aa}) = 2pq\,
- f(\mathbf{aa}) = q^2\,
These frequencies are called Hardy-Weinberg frequencies (or Hardy-Weinberg proportions).  This is achieved in one generation, and only requires the assumption of random mating with an infinite population size.
Sometimes, a population is created by bringing together males and females with different allele frequencies.  In this case, the assumption of a single population is violated until after the first generation, so the first generation will not have Hardy-Weinberg equilibrium.  Successive generations will have Hardy-Weinberg equilibrium.

# Deviations from Hardy-Weinberg equilibrium
Violations of the HardyWeinberg assumptions can cause deviations from expectation.  How this affects the population depends on the assumptions that are violated.
- Random mating.  The HWP states the population will have the given genotypic frequencies (called Hardy-Weinberg proportions) after a single generation of random mating within the population.  When violations of this provision occur, the population will not have Hardy-Weinberg proportions.  Three such violations are:
Inbreeding, which causes an increase in homozygosity for all genes.
Assortative mating, which causes an increase in homozygosity only for those genes involved in the trait that is assortatively mated (and genes in linkage disequilibrium with them).
Small population size, which causes a random change in genotypic frequencies, particularly if the population is very small.  This is due to a sampling effect, and is called genetic drift.
- Inbreeding, which causes an increase in homozygosity for all genes.
- Assortative mating, which causes an increase in homozygosity only for those genes involved in the trait that is assortatively mated (and genes in linkage disequilibrium with them).
- Small population size, which causes a random change in genotypic frequencies, particularly if the population is very small.  This is due to a sampling effect, and is called genetic drift.
The remaining assumptions affect the allele frequencies, but do not, in themselves, affect random mating.  If a population violates one of these, the population will continue to have Hardy-Weinberg proportions each generation, but the allele frequencies will change with that force.
- Selection, in general, causes allele frequencies to change, often quite rapidly.  While directional selection eventually leads to the loss of all alleles except the favored one, some forms of selection, such as balancing selection, lead to equilibrium without loss of alleles.
- Mutation will have a very subtle effect on allele frequencies.  Mutation rates are of the order 10-4 to 10-8, and the change in allele frequency will be, at most, the same order.  Recurrent mutation will maintain alleles in the population, even if there is strong selection against them.
- Migration genetically links two or more populations together.  In general, allele frequencies will become more homogeneous among the populations.  Some models for migration inherently include nonrandom mating (Wahlund effect, for example).  For those models, the Hardy-Weinberg proportions will normally not be valid.
How these violations affect formal statistical tests for HWE is discussed later.
Unfortunately, violations of assumptions in the Hardy-Weinberg principle does not mean the population will violate HWE.  For example, balancing selection leads to an equilibrium population with Hardy-Weinberg proportions.  This property with selection vs. mutation is the basis for many estimates of mutation rate (call mutation-selection balance).

# Sex linkage
Where the A gene is sex-linked, the heterogametic sex (e.g., mammalian males; avian females) have only one copy of the gene (and are termed hemizygous), while the homogametic sex (e.g., human females) have two copies.  The genotype frequencies at equilibrium are  p and q for the heterogametic sex but p^2, 2pq and q^2 for the homogametic sex.
For example, in humans red-green colorblindness is an X-linked recessive trait.  In western European males, the trait affects about 1 in 12, (q = 0.083) whereas it affects about 1 in 200 females (0.005, compared to  q^2 = 0.0070), very close to Hardy-Weinberg proportions.
If a population is brought together with males and females with different allele frequencies, the allele frequency of the male population follows that of the female population because each receives its X chromosome from its mother.  The population converges on equilibrium very quickly.

# Generalizations
The simple derivation above can be generalized for more than two alleles and polyploidy.

## Generalization for more than two alleles
Consider an extra allele frequency, r.  The two-allele case is the binomial expansion of (p+q)^2, and thus the three-allele case is the trinomial expansion of (p+q+r)^2.
More generally, consider the alleles A1, ... Ai given by the allele frequencies p_1 to p_i;
giving for all homozygotes:
and for all heterozygotes:

## Generalization for polyploidy
The HardyWeinberg principle may also be generalized to polyploid systems, that is, for organisms that have more than two copies of each chromosome.  Consider again only two alleles.  The diploid case is the binomial expansion of:
and therefore the polyploid case is the binomial expansion of:
where c is the ploidy, for example with tetraploid (c = 4):
Depending on whether the organism is a 'true' tetraploid or an amphidiploid will determine how long it will take for the population to reach Hardy-Weinberg equilibrium.

## Complete generalization
The completely generalized formula is the multinomial expansion of (p_1 + \cdots + p_n)^c:

# Applications
The HardyWeinberg principle may be applied in two ways, either a population is assumed to be in HardyWeinberg proportions, in which the genotype frequencies can be calculated, or if the genotype frequencies of all three genotypes are known, they can be tested for deviations that are statistically significant.

## Application to cases of complete dominance
Suppose that the phenotypes of AA and Aa are indistinguishable, i.e., there is complete dominance.  Assuming that the HardyWeinberg principle applies to the population, then q can still be calculated from f(aa):
and p can be calculated from q.  And thus an estimate of f(AA) and f(Aa) derived from p^2 and 2pq respectively.  Note however, such a population cannot be tested for equilibrium using the significance tests below because it is assumed a priori.

# Significance tests for deviation
Testing deviation from the HWP is generally performed using Pearson's chi-squared test, using the observed genotype frequencies obtained from the data and the expected genotype frequencies obtained using the HWP.  For systems where there are large numbers of alleles, this may result in data with many empty possible genotypes and low genotype counts, because there are often not enough individuals present in the sample to adequately represent all genotype classes.  If this is the case, then the asymptotic assumption of the chi-square distribution, will no longer hold, and it may be necessary to use a form of Fisher's exact test, which requires a computer to solve.  More recently a number of MCMC methods of testing for deviations from HWP have been proposed (Guo & Thompson, 1992; Wigginton et al 2005)

## Example  \chi^2 test for deviation
These data are from E.B. Ford (1971) on the Scarlet tiger moth, for which the phenotypes of a sample of the population were recorded.  Genotype-phenotype distinction is assumed to be negligibly small.  The null hypothesis is that the population is in HardyWeinberg proportions, and the alternative hypothesis is that the population is not in HardyWeinberg proportions.
From which allele frequencies can be calculated:
and
So the HardyWeinberg expectation is:
Pearson's chi-square test states:
There is 1 degree of freedom (degrees of freedom for test for Hardy-Weinberg proportions are # genotypes - # alleles).  The 5% significance level for 1 degree of freedom is 3.84, and since the 2 value is less than this, the null hypothesis that the population is in HardyWeinberg frequencies is not rejected.

## Fisher's exact test (probability test)
Fisher's exact test can be applied to testing for Hardy-Weinberg proportions.  Because the test is conditional on the allele frequencies, p and q, the problem can be viewed as testing for the proper number of heterozygotes.  In this way, the hypothesis of Hardy-Weinberg proportions is rejected if the number of heterozygotes are too large or too small.  The conditional probabilities for the heterozygote, given the allele frequencies are given in Emigh (1980) as
where n11, n12, n22 are the observed numbers of the three genotypes, AA, Aa, and aa, respectively, and n1 is the number of A alleles, where n_1 = 2 n_{11} + n_{12}.
An Example
Using one of the examples from Emigh (1980), we can consider the case where n = 100, and p = 0.34.  The possible observed heterozygotes and their exact significance level is given in Table 4.
Using this table, you look up the significance level of the test based on the observed number of heterozygotes.  For example, if you observed 20 heterozygotes, the significance level for the test is 0.007.  As is typical for Fisher's exact test for small samples, the gradation of significance levels is quite coarse.
Unfortunately, you have to create a table like this for every experiment, since the tables are dependent on both n and p.

# Analysis Software
- HelixTree

# Inbreeding coefficient
The inbreeding coefficient, F (see also F-statistics), is one minus the observed frequency of heterozygotes over that expected from HardyWeinberg equilibrium.
where the expected value from HardyWeinberg equilibrium is given by
For example, for Ford's data above;
For two alleles, the chi square goodness of fit test for Hardy-Weinberg proportions is equivalent to the test for inbreeding, F = 0.

# History
Mendelian genetics were rediscovered in 1900.  However, it remained somewhat controversial for several years as it was not then known how it could cause continuous characters.  Udny Yule (1902) argued against Mendelism because he thought that dominant alleles would increase in the population.  The American William E. Castle (1903) showed that without selection, the genotype frequencies would remain stable.  Karl Pearson (1903) found one equilibrium position with values of p = q = 0.5.  Reginald Punnett, unable to counter Yule's point, introduced the problem to G. H. Hardy, a British mathematician, with whom he played cricket.  Hardy was a pure mathematician and held applied mathematics in some contempt; his view of biologists' use of mathematics comes across in his 1908 paper where he describes this as "very simple".
The principle was thus known as Hardy's law in the English-speaking world until Curt Stern (1943) pointed out that it had first been formulated independently in 1908 by the German physician Wilhelm Weinberg (see Crow 1999).  Others have tried to associate Castle's name with the Law because of his work in 1903, but it is only rarely seen as the Hardy-Weinberg-Castle Law.

# Graphical representation
It is possible to represent the distribution of genotype frequencies for a bi-allelic locus within a population graphically using a de Finetti diagram. This uses a triangular plot (also known as trilinear, triaxial or ternary plot) to represent the distribution of the three genotype frequencies in relation to each other.  Although it differs from many other such plots in that the direction of one of the axes has been reversed.
File:De finetti diagram.png
The curved line in the above diagram is the Hardy-Weinberg parabola and represents the state where alleles are in Hardy-Weinberg equilibrium.
It is possible to represent the effects of Natural Selection and its effect on allele frequency on such graphs (e.g. Ineichen & Batschelet 1975)
The de Finetti diagram has been developed and used extensively by A.W.F. Edwards in his book Foundations of Mathematical Genetics.

# References and notes