{
  "id": "hardy-weinberg",
  "version": "22d926430d25",
  "status": "published",
  "name": "Hardy Weinberg Calculator",
  "question": "Hardy-Weinberg: what are p and q?",
  "summary": "Computes the allele frequencies p and q and the genotype frequencies p², 2pq and q² from genotype counts or one known frequency, with the expected counts and a chi-square test of Hardy-Weinberg equilibrium.",
  "category": "biology",
  "subcategory": "genetics",
  "url": "https://www.acalculator.org/biology/hardy-weinberg-calculator",
  "markdown": "https://www.acalculator.org/biology/hardy-weinberg-calculator.md",
  "kind": "function",
  "method": "p + q = 1; p² + 2pq + q² = 1. From counts: p = (2 × AA + Aa) ÷ 2N; expected = N × (p², 2pq, q²); χ² = Σ (O − E)² ÷ E with 1 degree of freedom.",
  "assumptions": [
    "One gene with two alleles, A dominant over a. Hardy-Weinberg proportions hold with random mating and no selection, mutation, migration or drift.",
    "From q² (or p²), q (or p) is its square root.",
    "The chi-square test has 1 degree of freedom (3 genotypes − 2 alleles); it is shaky when an expected count is below about 5."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "mode": {
        "title": "I know",
        "description": "Genotype counts from a sample, or one frequency (such as the share with the recessive trait).",
        "type": "string",
        "enum": [
          "counts",
          "frequency"
        ]
      },
      "AA": {
        "title": "AA (homozygous dominant)",
        "description": "How many individuals in the sample are AA.",
        "type": "integer",
        "minimum": 0,
        "maximum": 1000000000
      },
      "Aa": {
        "title": "Aa (heterozygous)",
        "description": "How many individuals in the sample are Aa.",
        "type": "integer",
        "minimum": 0,
        "maximum": 1000000000
      },
      "aa": {
        "title": "aa (homozygous recessive)",
        "description": "How many individuals in the sample are aa.",
        "type": "integer",
        "minimum": 0,
        "maximum": 1000000000
      },
      "k": {
        "title": "The frequency I know",
        "description": "Which frequency you have: q² is the share of individuals that show the recessive trait.",
        "type": "string",
        "enum": [
          "q2",
          "p2",
          "p",
          "q"
        ]
      },
      "f": {
        "title": "Its value",
        "description": "The known frequency as a decimal from 0 to 1 (9% is 0.09).",
        "type": "number",
        "minimum": 0,
        "maximum": 1
      }
    }
  },
  "outputs": {
    "p": {
      "label": "p (allele A)",
      "description": "The frequency of the dominant allele A.",
      "format": "number"
    },
    "q": {
      "label": "q (allele a)",
      "description": "The frequency of the recessive allele a: 1 − p.",
      "format": "number"
    },
    "p2": {
      "label": "p² (AA)",
      "description": "The expected frequency of AA.",
      "format": "number"
    },
    "pq2": {
      "label": "2pq (Aa)",
      "description": "The expected frequency of Aa, the carriers.",
      "format": "number"
    },
    "q2": {
      "label": "q² (aa)",
      "description": "The expected frequency of aa.",
      "format": "number"
    },
    "expDom": {
      "label": "Expected AA",
      "description": "N × p², the AA count expected in equilibrium.",
      "format": "number"
    },
    "expHet": {
      "label": "Expected Aa",
      "description": "N × 2pq, the Aa count expected in equilibrium.",
      "format": "number"
    },
    "expRec": {
      "label": "Expected aa",
      "description": "N × q², the aa count expected in equilibrium.",
      "format": "number"
    },
    "chi": {
      "label": "Chi-square (1 degree of freedom)",
      "description": "Σ (observed − expected)² ÷ expected over the three genotypes.",
      "format": "number"
    },
    "pValue": {
      "label": "p-value",
      "description": "The chance of a chi-square this large or larger if the population is in equilibrium.",
      "format": "number"
    },
    "verdict": {
      "label": "At the 5% level",
      "description": "Whether the counts fit Hardy-Weinberg proportions (p-value of 0.05 or more).",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "mode": "counts",
      "AA": 1469,
      "Aa": 138,
      "aa": 5,
      "k": "q2",
      "f": 0.09
    },
    "outputs": {
      "p": 0.9540942928039702,
      "q": 0.04590570719602978,
      "p2": 0.9102959195611081,
      "pq2": 0.08759674648572431,
      "q2": 0.00210733395316762,
      "expDom": 1467.3970223325061,
      "expHet": 141.2059553349876,
      "expRec": 3.3970223325062037,
      "chi": 0.8309481891464123,
      "pValue": 0.36199849347509216,
      "verdict": "Fits Hardy-Weinberg proportions (p ≥ 0.05)"
    },
    "text": "The allele frequencies are p = 0.9541 and q = 0.0459."
  },
  "examples": [
    {
      "given": {
        "mode": "frequency",
        "k": "q2",
        "f": 0.09
      },
      "expect": {
        "q": 0.3,
        "p": 0.7,
        "p2": 0.49,
        "pq2": 0.42,
        "q2": 0.09
      },
      "source": "OpenStax, Biology 2e, section 19.1 Population Evolution (Hardy-Weinberg: p + q = 1 and p² + 2pq + q² = 1; with p = 0.7 and q = 0.3 the genotype frequencies are 0.49, 0.42 and 0.09), CC BY 4.0, https://openstax.org/books/biology-2e/pages/19-1-population-evolution (retrieved 2026-10-03)"
    },
    {
      "given": {
        "mode": "counts",
        "AA": 1469,
        "Aa": 138,
        "aa": 5
      },
      "expect": {
        "p": 0.9540942928039702,
        "expDom": 1467.3970223325061,
        "expHet": 141.2059553349876,
        "expRec": 3.3970223325062037,
        "chi": 0.8309481891464123,
        "pValue": 0.3619984934750926,
        "verdict": "Fits Hardy-Weinberg proportions (p ≥ 0.05)"
      },
      "source": "Wikipedia, Hardy–Weinberg principle, section Significance tests (Ford’s scarlet tiger moths: 1,469 AA, 138 Aa, 5 aa; p = 0.954; expected 1,467.4, 141.2 and 3.4; χ² = 0.83 with 1 degree of freedom, below the 5% critical value 3.84), https://en.wikipedia.org/wiki/Hardy%E2%80%93Weinberg_principle (retrieved 2026-10-03); OpenStax, Biology 2e, section 19.1 Population Evolution (Hardy-Weinberg: p + q = 1 and p² + 2pq + q² = 1; with p = 0.7 and q = 0.3 the genotype frequencies are 0.49, 0.42 and 0.09), CC BY 4.0, https://openstax.org/books/biology-2e/pages/19-1-population-evolution (retrieved 2026-10-03); tolerance for the p-value, computed as erfc(√(χ² ÷ 2)) in Python",
      "tolerance": 0.000001
    },
    {
      "given": {
        "mode": "counts",
        "AA": 30,
        "Aa": 20,
        "aa": 50
      },
      "expect": {
        "p": 0.4,
        "q": 0.6,
        "expDom": 16,
        "expHet": 48,
        "expRec": 36,
        "chi": 34.02777777777778
      },
      "source": "OpenStax, Biology 2e, section 19.1 Population Evolution (Hardy-Weinberg: p + q = 1 and p² + 2pq + q² = 1; with p = 0.7 and q = 0.3 the genotype frequencies are 0.49, 0.42 and 0.09), CC BY 4.0, https://openstax.org/books/biology-2e/pages/19-1-population-evolution (retrieved 2026-10-03); hand calculation in content.mdx: p = (60 + 20) ÷ 200 = 0.4; χ² = 196/16 + 784/48 + 196/36"
    },
    {
      "given": {
        "mode": "frequency",
        "k": "p",
        "f": 0.25
      },
      "expect": {
        "q": 0.75,
        "p2": 0.0625,
        "pq2": 0.375,
        "q2": 0.5625
      },
      "source": "OpenStax, Biology 2e, section 19.1 Population Evolution (Hardy-Weinberg: p + q = 1 and p² + 2pq + q² = 1; with p = 0.7 and q = 0.3 the genotype frequencies are 0.49, 0.42 and 0.09), CC BY 4.0, https://openstax.org/books/biology-2e/pages/19-1-population-evolution (retrieved 2026-10-03); hand calculation in content.mdx"
    }
  ],
  "sources": [
    "OpenStax, Biology 2e, section 19.1 Population Evolution (p + q = 1; p² + 2pq + q² = 1; the pea example with p = 0.7, q = 0.3 gives 0.49, 0.42 and 0.09; the conditions of Hardy-Weinberg equilibrium), CC BY 4.0, retrieved 2026-10-03. https://openstax.org/books/biology-2e/pages/19-1-population-evolution",
    "Wikipedia, Hardy–Weinberg principle, section Significance tests (Pearson’s chi-squared test with 1 degree of freedom; Ford’s scarlet tiger moths: 1,469, 138 and 5, χ² = 0.83 below the 5% critical value 3.84), retrieved 2026-10-03. https://en.wikipedia.org/wiki/Hardy%E2%80%93Weinberg_principle"
  ],
  "related": [
    "punnett-square",
    "chi-square",
    "blood-type"
  ],
  "changelog": []
}
