# Hardy-Weinberg equilibrium¶

`allel.stats.hw.``heterozygosity_observed`(g, fill=nan)[source]

Calculate the rate of observed heterozygosity for each variant.

Parameters: g : array_like, int, shape (n_variants, n_samples, ploidy) Genotype array. fill : float, optional Use this value for variants where all calls are missing. ho : ndarray, float, shape (n_variants,) Observed heterozygosity

Examples

```>>> import allel
>>> g = allel.GenotypeArray([[[0, 0], [0, 0], [0, 0]],
...                          [[0, 0], [0, 1], [1, 1]],
...                          [[0, 0], [1, 1], [2, 2]],
...                          [[1, 1], [1, 2], [-1, -1]]])
>>> allel.stats.heterozygosity_observed(g)
array([ 0.        ,  0.33333333,  0.        ,  0.5       ])
```
`allel.stats.hw.``heterozygosity_expected`(af, ploidy, fill=nan)[source]

Calculate the expected rate of heterozygosity for each variant under Hardy-Weinberg equilibrium.

Parameters: af : array_like, float, shape (n_variants, n_alleles) Allele frequencies array. ploidy : int Sample ploidy. fill : float, optional Use this value for variants where allele frequencies do not sum to 1. he : ndarray, float, shape (n_variants,) Expected heterozygosity

Examples

```>>> import allel
>>> g = allel.GenotypeArray([[[0, 0], [0, 0], [0, 0]],
...                          [[0, 0], [0, 1], [1, 1]],
...                          [[0, 0], [1, 1], [2, 2]],
...                          [[1, 1], [1, 2], [-1, -1]]])
>>> af = g.count_alleles().to_frequencies()
>>> allel.stats.heterozygosity_expected(af, ploidy=2)
array([ 0.        ,  0.5       ,  0.66666667,  0.375     ])
```
`allel.stats.hw.``inbreeding_coefficient`(g, fill=nan)[source]

Calculate the inbreeding coefficient for each variant.

Parameters: g : array_like, int, shape (n_variants, n_samples, ploidy) Genotype array. fill : float, optional Use this value for variants where the expected heterozygosity is zero. f : ndarray, float, shape (n_variants,) Inbreeding coefficient.

Notes

The inbreeding coefficient is calculated as 1 - (Ho/He) where Ho is the observed heterozygosity and He is the expected heterozygosity.

Examples

```>>> import allel
>>> g = allel.GenotypeArray([[[0, 0], [0, 0], [0, 0]],
...                          [[0, 0], [0, 1], [1, 1]],
...                          [[0, 0], [1, 1], [2, 2]],
...                          [[1, 1], [1, 2], [-1, -1]]])
>>> allel.stats.inbreeding_coefficient(g)
array([        nan,  0.33333333,  1.        , -0.33333333])
```