Allele frequencies may also be studied at the population level
The frequency of different alleles in a population can be determined from the frequency of the various phenotypes in the population. In the simplest system, with two alleles of the same locus (e.g. A,a), we use the symbol p to represent the frequency of the dominant allele within the population, and q for the frequency of the recessive allele. Because there are only two possible alleles, we can say that the frequency of p and q together represent 100% of the alleles in the population (p+q=1).
We can calculate the values of p and q, in a representative sample of individuals from a population, by simply counting the alleles and dividing by the total number of alleles examined. For a given allele, homozygotes will count for twice as much as heterozygotes.
For example:
genotype 
number of individuals 
AA
Aa
aa

320
160
20


A (p)

a (q)

A (p)

p^{2}

pq

a (q)

pq

q^{2}

\[\begin{align} \mathbf{p} &= \mathbf{\dfrac{2(AA) + Aa}{total\: alleles\: counted}}\\ &= \dfrac{2(320) + 160}{2(320) + 2(160) + 2(20)} = 0.8 \end{align}\]
\[\begin{align} \mathbf{q} &= \mathbf{\dfrac{2(aa) + Aa}{total\: alleles\: counted}}\\ &= \dfrac{2(20) + 160}{2(320) + 2(160) + 2(20)} = 0.2 \end{align}\]
With the allele frequencies of a population we can use an extension of the Punnett Square, and the product rule, to calculate the expected frequency of each genotype following random matings within the entire population. This is the basis of the HardyWeinberg formula:
\[p^2 + 2pq + q^2=1 \label{HW}\]
Here \(p^2\) is the frequency of homozygotes AA, \(2pq\) is the frequency of the heterozygotes, and \(q^2\) is the frequency of homozygotes aa. Notice that if we substitute the allele frequencies we calculated above (p=0.8, q=0.2) into the Equation \ref{HW}, we obtain expected probabilities for each of the genotypes that exactly match our original observations:
\[\begin{align} &\mathrm{p^2=0.8^2=0.64} &&0.64 \times 500 = 320 \\ &\mathrm{2pq= 2(0.8)(0.2)=0.32} &&0.32 \times 500 = 160 \\ &\mathrm{q^2=0.2^2=0.04} &&0.04 \times 500 = 20 \end{align}\]
This is a demonstration of the HardyWeinberg Equilibrium, where both the genotype frequencies and allele frequencies in a population remain unchanged following successive matings within a population, if certain conditions are met. These conditions are listed in Table \(\PageIndex{1}\). Few natural populations actually satisfy all of these conditions. Nevertheless, large populations of many species, including humans, appear to approach HardyWeinberg equilibrium for many loci. In these situations, deviations of a particular gene from HardyWeinberg equilibirum can be an indication that one of the alleles affects the reproductive success of organism, for example through natural selection or assortative mating.
Conditions for the HardyWeinberg equilibrium
 Random mating: Individuals of all genotypes mate together with equal frequency. Assortative mating, in which certain genotypes preferentially mate together, is a type of nonrandom mating.
 No natural selection: All genotypes have equal fitness.
 No migration: Individuals do not leave or enter the population.
 No mutation: The allele frequencies do not change due to mutation.
 Large population: Random sampling effects in mating (i.e. genetic drift) are insignificant in large populations.
The HardyWeinberg formula can also be used to estimate allele frequencies, when only the frequency of one of the genotypic classes is known. For example, if 0.04% of the population is affected by a particular genetic condition, and all of the affected individuals have the genotype aa, then we assume that q^{2} = 0.0004 and we can calculate p, q, and 2pq as follows:
\[\mathrm{q^2 = 0.04\% = 0.0004}\]
\[\mathrm{q = \sqrt{0.0004} = 0.02}\]
\[\mathrm{p= 1q = 0.98}\]
\[\mathrm{2pq = 2(0.98)(0.02) = 0.04}\]
Thus, approximately 4% of the population is expected to be heterozygous (i.e. a carrier) of this genetic condition. Note that while we recognize that the population is probably not exactly in HardyWeinberg equilibrium for this locus, application of the HardyWeinberg formula nevertheless can give a reasonable estimate of allele frequencies, in the absence of any other information.