# Hardy-Weinberg Equilibrium Calculator

> Estimates the allele frequencies p and q of a two-allele locus from observed genotype counts, computes the genotype counts expected under Hardy–Weinberg equilibrium (p², 2pq, q²) and tests the deviation with a chi-square goodness-of-fit test with 1 degree of freedom.

- Calculator id: `hardy-weinberg` · Category: Biology, Earth & Space (`science`) · Tool name: `calculate_hardy_weinberg`
- Canonical page: https://tttkmbb.com/science/hardy-weinberg · This document: https://tttkmbb.com/science/hardy-weinberg.md · JSON definition: https://tttkmbb.com/science/hardy-weinberg.json

## Purpose

Estimates the allele frequencies p and q of a two-allele locus from observed genotype counts, computes the genotype counts expected under Hardy–Weinberg equilibrium (p², 2pq, q²) and tests the deviation with a chi-square goodness-of-fit test with 1 degree of freedom.

**Use when:** You have counts of the three genotypes (AA, Aa, aa) of a population sample and need allele frequencies, expected genotype numbers or a test of whether the population is in Hardy–Weinberg equilibrium.

**Do not use when:** The locus has more than two alleles or is X-linked, you only know phenotype counts of a dominant trait (then q = √(recessive fraction) is the only estimate and no test is possible), or you need a general contingency-table test (use chi-square-test).

## Input

| Parameter | Type | Unit | Required | Description |
| --- | --- | --- | --- | --- |
| `count_homozygous_dominant` | integer |  | required | Number of individuals with genotype AA. (min 0, max 1000000000) |
| `count_heterozygous` | integer |  | required | Number of individuals with genotype Aa. (min 0, max 1000000000) |
| `count_homozygous_recessive` | integer |  | required | Number of individuals with genotype aa. (min 0, max 1000000000) |

## Output

| Field | Type | Unit | Description |
| --- | --- | --- | --- |
| `total_individuals` | integer |  | Sum of the three genotype counts. |
| `p` | number |  | Dominant allele frequency (2·AA + Aa) / 2N. |
| `q` | number |  | Recessive allele frequency 1 − p. |
| `observed_frequencies` | object |  | Observed fractions of AA, Aa and aa. |
| `expected_frequencies` | object |  | p_squared (AA), two_pq (Aa) and q_squared (aa) expected under equilibrium. |
| `expected_homozygous_dominant` | number |  | p² × N. |
| `expected_heterozygous` | number |  | 2pq × N. |
| `expected_homozygous_recessive` | number |  | q² × N. |
| `chi_square` | number |  | Σ (observed − expected)² / expected over the three genotypes. |
| `degrees_of_freedom` | integer |  | 3 genotype classes − 1 − 1 estimated parameter (p) = 1. |
| `p_value` | number |  | P(χ²₁ ≥ chi_square) = 2 × (1 − Φ(√χ²)). |
| `in_equilibrium` | boolean |  | true when p_value ≥ 0.05 (no significant deviation at α = 0.05). |
| `interpretation` | string |  | Plain-language conclusion of the test. |

## Formula

`p = (2·AA + Aa) / (2N), q = 1 − p; expected AA = p²N, Aa = 2pqN, aa = q²N; χ² = Σ (O − E)² / E, df = 1; p-value = 2 × (1 − Φ(√χ²))`

Hardy–Weinberg equilibrium assumes random mating and no selection, mutation, migration or drift. The test has 1 degree of freedom because p is estimated from the same data; the p-value uses the standard normal CDF exactly, since a χ² variable with 1 df is the square of a standard normal. Expected counts below 5 make the approximation unreliable (a note is added).

## Data Sources

- Wikipedia – Hardy–Weinberg principle — https://en.wikipedia.org/wiki/Hardy%E2%80%93Weinberg_principle (reference, retrieved 2026-09-24)
- OpenStax Biology 2e – 19.1 Population Evolution — https://openstax.org/books/biology-2e/pages/19-1-population-evolution (textbook, retrieved 2026-09-24)

Data freshness: `static`. Deterministic formula with fixed constants; results never go stale. Inputs supplied by the caller determine the output.

## API

- `GET https://tttkmbb.com/api/v1/calculate/hardy-weinberg?count_homozygous_dominant=…&count_heterozygous=…&count_homozygous_recessive=…`
- `POST https://tttkmbb.com/api/v1/calculate/hardy-weinberg` with JSON body `{"inputs": {…}}`
- Response: unified envelope (`success`, `request`, `result.values`, `result.units`, `sources`, `freshness`, `timestamp`, `next_actions`, `links`); see https://tttkmbb.com/docs/response-format.md
- Schema: https://tttkmbb.com/api/v1/calculators/hardy-weinberg · OpenAPI operationId `calculate_hardy_weinberg` in https://tttkmbb.com/openapi.json
- Authentication: none. Rate limit: fair use, see https://tttkmbb.com/docs/rate-limits.md.

## MCP

- Server: `https://tttkmbb.com/mcp` (Streamable HTTP, JSON-RPC 2.0, no auth)
- Tool:  `run_calculator` with `{"calculator_id": "hardy-weinberg", "inputs": {…}}`

## Example

- 50 AA, 40 Aa, 10 aa: inputs `{"count_homozygous_dominant":50,"count_heterozygous":40,"count_homozygous_recessive":10}` → `{"total_individuals":100,"p":0.7,"q":0.3,"expected_homozygous_dominant":49,"expected_heterozygous":42,"expected_homozygous_recessive":9,"expected_frequencies":{"p_squared":0.49,"two_pq":0.42,"q_squared":0.09},"chi_square":0.2268,"p_value":0.6339,"in_equilibrium":true}`
- 360 AA, 480 Aa, 160 aa (exact equilibrium): inputs `{"count_homozygous_dominant":360,"count_heterozygous":480,"count_homozygous_recessive":160}` → `{"p":0.6,"q":0.4,"expected_homozygous_dominant":360,"expected_heterozygous":480,"expected_homozygous_recessive":160,"chi_square":0,"p_value":1,"in_equilibrium":true}`

```
GET https://tttkmbb.com/api/v1/calculate/hardy-weinberg?count_homozygous_dominant=50&count_heterozygous=40&count_homozygous_recessive=10
```

## Limitations

The locus has more than two alleles or is X-linked, you only know phenotype counts of a dominant trait (then q = √(recessive fraction) is the only estimate and no test is possible), or you need a general contingency-table test (use chi-square-test). Hardy–Weinberg equilibrium assumes random mating and no selection, mutation, migration or drift. The test has 1 degree of freedom because p is estimated from the same data; the p-value uses the standard normal CDF exactly, since a χ² variable with 1 df is the square of a standard normal. Expected counts below 5 make the approximation unreliable (a note is added). All values are computed from the formula above; no measurement or live data is involved.

## FAQ

**Why only 1 degree of freedom with 3 classes?**

One degree is lost because the counts must sum to N and another because the allele frequency p is estimated from the observed counts, leaving 3 − 2 = 1.

**Can I use phenotype counts for a recessive trait?**

Only to estimate q as the square root of the recessive phenotype fraction (assuming equilibrium); the chi-square test needs the three genotype counts, which this calculator requires.

**What does a significant deviation mean?**

The genotype proportions differ from p², 2pq, q² more than sampling error explains, pointing to non-random mating, selection, population structure or genotyping error; it does not say which.

## Related

- [Punnett Square Calculator](https://tttkmbb.com/science/punnett-square.md) — Offspring ratios of a single cross instead of population frequencies.
- [Chi-Square Test Calculator](https://tttkmbb.com/statistics/chi-square-test.md) — General chi-square tests for contingency tables and other goodness-of-fit problems.
- [P-Value Calculator](https://tttkmbb.com/statistics/p-value.md) — Convert a chi-square statistic with other degrees of freedom to a p-value.
