The Chi-Squared Goodness-of-Fit Test

A garden pea plant is crossed. 556 offspring seeds come back either round or wrinkled, yellow or green. Gregor Mendel predicted a clean 3:1 ratio. But nature never hands you exactly 417 and 139. How far can the counts drift from the prediction? At what point does "close enough" become "this theory is wrong"? Biology is built on categories — genotypes, phenotypes, species present or absent, alive or dead, disease or no disease. Almost every claim about categorical data eventually needs a formal test. That test checks whether observed counts match some theoretically expected pattern. The chi-squared goodness-of-fit test answers exactly this question. It converts the gap between what a hypothesis predicts and what was actually counted into a single number. It then tells you how often a gap that large (or larger) would arise from chance alone. This assumes the hypothesis were true. This is the workhorse statistic behind testing Mendelian Ratios. It also checks whether a population is in Hardy-Weinberg Equilibrium. And it asks whether an ecological survey's species counts fit a predicted distribution. The big picture is deceptively simple. Square the deviations, scale them by what you expected, and sum across categories. That sum follows a known mathematical distribution — the chi-squared distribution. This holds whenever sample sizes are reasonably large. This lets you attach an exact probability to your data. Karl Pearson derived this in 1900. It remains one of the most-used formulas in all of quantitative biology. It is taught right alongside the p-Value & Its Interpretation and the logic of Null & Alternative Hypotheses. This article builds the test from first principles. It covers what the statistic actually measures, and why it follows the chi-squared distribution. It explains how degrees of freedom are counted. It also shows how R. A. Fisher used it to re-examine — and famously suspect — Mendel's own data. You will work two full numerical examples. You will see the classic pitfalls that trip up students. And you will understand exactly when this one-sample test gives way to its two-variable cousin, the Chi-Squared Contingency Test.

Key takeaways
  • The chi-squared goodness-of-fit test compares observed category counts to counts expected under a null hypothesis (e.g., a 3:1 Mendelian ratio).
  • The test statistic is χ² = Σ (O − E)²/E, summed over all categories.
  • Under H₀, this statistic approximately follows a chi-squared distribution with degrees of freedom = (number of categories − 1) − (parameters estimated from data).
  • Large χ² values mean big, unlikely deviations from expectation; the p-value comes from the upper tail of the χ² distribution.
  • The test requires counts (not proportions or percentages) and needs expected cell counts generally ≥ 5 to be valid.
  • R. A. Fisher applied this test to Mendel's pooled data and got a suspiciously small χ² (too good a fit), reigniting a century-long debate about scientific data quality.
The core insight

The chi-squared goodness-of-fit test measures how far observed counts stray from a hypothesized pattern, in units that let you judge whether the discrepancy is due to chance or reflects the hypothesis being wrong.

The core idea

Goodness-of-fit testing asks a single question: does a set of observed frequencies across mutually exclusive categories match a set of frequencies predicted by a specific null hypothesis? The null hypothesis, H₀, specifies exact expected proportions for each category — for example, H₀: the phenotype ratio is 9:3:3:1 for a dihybrid cross, or H₀: a die is fair, so each face has probability 1/6. The alternative hypothesis, H_A, is simply that the true proportions differ from those specified, without committing to any particular alternative pattern. The raw ingredients are observed counts (O), the actual tallies from your sample, and expected counts (E), calculated by multiplying the total sample size by each hypothesized proportion. Critically, the test operates on counts, not percentages or proportions — converting to percentages destroys the information about sample size that the test needs to judge whether a deviation is large or small relative to sampling noise. A 10% deviation in a sample of 20 is unremarkable; the same 10% deviation in a sample of 20,000 would be extraordinary. The test statistic condenses all categories into one number:

χ² = Σ [(Oᵢ − Eᵢ)² / Eᵢ] summed over all k categories. Squaring the deviations (Oᵢ − Eᵢ) makes both positive and negative discrepancies count as evidence against H₀, and dividing by Eᵢ rescales each squared deviation relative to the amount of variation expected by chance in that category — a discrepancy of 10 is trivial if 1000 were expected but enormous if 5 were expected. Under the null hypothesis, and provided expected counts are not too small, this statistic follows — approximately — the chi-squared probability distribution, a right-skewed distribution defined for positive values only, indexed by a parameter called degrees of freedom (df).

Mechanism — how the test actually works

Step 1: State the null hypothesis as exact proportions. For a monohybrid cross with simple dominance, H₀ might specify a 3:1 ratio of dominant to recessive phenotype. For a dihybrid cross, H₀ specifies 9:3:3:1 across four phenotype classes. These proportions come from Mendelian theory (independent assortment and segregation), not from the data itself.

Step 2: Collect data and compute expected counts. If n is the total sample size and pᵢ is the hypothesized proportion for category i, then Eᵢ = n × pᵢ. These must sum to n across all categories, exactly matching the total number of observations.

Step 3: Compute the statistic. For each category, calculate (Oᵢ − Eᵢ)², divide by Eᵢ, and sum across all categories: χ² = Σ (Oᵢ − Eᵢ)²/Eᵢ

Step 4: Determine degrees of freedom. df = k − 1 − m, where k is the number of categories and m is the number of parameters estimated from the sample data itself (rather than fixed by theory in advance). For a simple Mendelian ratio test where all expected proportions are fully specified by theory (no parameters estimated from the data), df = k − 1. If, however, you were testing Hardy-Weinberg proportions and had to estimate an allele frequency from the very same sample, you would subtract one additional degree of freedom for that estimated parameter.

Step 5: Compare χ² to the chi-squared distribution with the appropriate df, and obtain a p-value. The p-value is the probability of observing a χ² statistic at least as large as the one calculated, if H₀ were true. This is always a one-tailed test in the sense that only large χ² values (large deviations) count as evidence against H₀ — a χ² near zero is entirely consistent with the null.

Step 6: Decide. Compare the p-value to a pre-chosen significance level, conventionally α = 0.05. If p < α, reject H₀: the observed counts deviate from the hypothesized ratio by more than chance alone would plausibly produce. If p ≥ α, fail to reject H₀: the data are consistent with the hypothesized ratio (this is not proof the ratio is exactly correct — merely that the data do not contradict it). Why does Σ(O−E)²/E follow the chi-squared distribution at all? Each category's count, when the sample is large, is approximately normally distributed around its expected value (a consequence of the normal approximation to the binomial/multinomial distribution). A sum of squared, standardized normal variables follows a chi-squared distribution by definition — this is precisely how the chi-squared distribution is constructed mathematically. Because the k category counts must sum to the fixed total n, only k−1 of them can vary freely; the last one is fully determined once the others are known. This constraint is exactly why one degree of freedom is lost for every fixed total, and an additional one for every parameter estimated from the data.

Figure
Building the chi-squared statistic from observed vs expected counts
Dihybrid Cross: Observed vs Expected (n = 556) Count Round Yellow O=315 E=313 Round Green O=108 E=104 Wrinkled Yellow O=101 E=104 Wrinkled Green O=32 E=35 Observed Expected (9:3:3:1) χ² = Σ(O−E)²/E ≈ 0.47 df = 3 p ≈ 0.93 (no evidence against 9:3:3:1)
Figure
The chi-squared distribution shifts and stretches with degrees of freedom
Chi-Squared Distribution at Different df χ² value Density χ²₀.₀₅,3 = 7.81 df = 1 df = 3 df = 6 As df rises, the distribution's peak shifts right and it becomes less skewed.

A crucial practical constraint: the chi-squared approximation only holds well when expected counts are not too small. The standard rule of thumb, following Cochran (1954), is that all expected counts should be at least 5 (some texts allow up to 20% of cells below 5, provided none are below 1). When expected counts are too small, the discrete, lumpy nature of real count data makes the smooth chi-squared curve a poor approximation, and the resulting p-values become unreliable — typically too small, over-stating significance. In that regime, an exact test (e.g., an exact multinomial test or Fisher's exact test for 2×2 tables) is preferred.

The evidence — Mendel, Fisher, and the "too good to be true" data

The chi-squared goodness-of-fit test was not derived to solve a genetics problem — Karl Pearson introduced it in 1900 in the context of testing whether observed frequencies (in games of chance, anthropometric measurements, and other data) matched theoretical distributions. But its most famous biological application came a generation later, when R. A. Fisher turned it on Gregor Mendel's own pea-breeding data.

Mendel's original experiments (1856–1863). Mendel bred garden peas (Pisum sativum) through thousands of controlled crosses, tracking seven pairs of contrasting traits (round vs. wrinkled seeds, yellow vs. green cotyledons, and so on). For a monohybrid cross between true-breeding parents, Mendel predicted that the F2 generation would show a 3:1 ratio of dominant to recessive phenotype — a direct consequence of segregation of two alleles at a single locus. Across his combined monohybrid crosses, Mendel recorded totals such as 5,474 round seeds to 1,850 wrinkled seeds (a ratio of 2.96:1, strikingly close to 3:1). For his dihybrid cross tracking both seed shape and color simultaneously, one classic dataset gives:

PhenotypeObservedExpected (9:3:3:1 of 556)
Round, Yellow315312.75
Round, Green108104.25
Wrinkled, Yellow101104.25
Wrinkled, Green3234.75

Computing χ² = Σ(O−E)²/E here gives approximately 0.47, with df = 4 − 1 = 3 (no parameters estimated — the 9:3:3:1 ratio is fixed by theory). The critical value for df = 3 at α = 0.05 is 7.81. Since 0.47 is far below 7.81, the corresponding p-value is roughly 0.93 — the data are entirely consistent with the theoretical 9:3:3:1 ratio.

Fisher's re-analysis (1936). R. A. Fisher, one of the founders of modern statistics, pooled χ² statistics across all of Mendel's published experiments and found something odd: the aggregate fit was too good. Summing χ² values across many independent experiments should itself follow a chi-squared distribution (with degrees of freedom equal to the sum of the individual df's), and Fisher found the total χ² was implausibly small — the data agreed with the predicted ratios far more closely, on average, than random sampling variation would be expected to produce, even if Mendel's theory were exactly correct. Fisher calculated that data fitting this well would arise by chance only about once in 10,000 replications of Mendel's experimental program.

What this proved (and what it didn't). Fisher's finding did not challenge Mendelian genetics itself — the ratios were correct. Instead, it raised the possibility that Mendel (or an assistant) had, consciously or not, discarded or adjusted some inconvenient counts, stopped counting once a batch looked "right," or otherwise let expectation bias data collection or reporting. This episode became a landmark case study in the ethics and psychology of data collection: even entirely genuine scientific breakthroughs can carry statistically detectable fingerprints of biased recording. Later statisticians (including Sewall Wright and, more recently, a formal reassessment using modern methods by A. W. F. Edwards) have debated whether the effect is fully explained by mundane factors — plant biology causing subtle non-independence between traits, rounding of intermediate ratios, or selective reporting of the "cleanest" datasets among many trials — but the chi-squared statistic itself is precisely what made the anomaly visible in the first place. The tool designed to test whether data fit a theory ended up testing whether the data fit too well.

Worked examples

Example 1 — A trihybrid-adjacent asymmetric cross (dominance with unequal category counts). A plant geneticist crosses two heterozygous pea plants for a single gene with incomplete penetrance-adjusted expectations, predicting a 3:1 ratio of purple to white flowers. She grows out an unusually small F2 population — perhaps due to poor germination — and counts:

  • Purple: 74
  • White: 38
  • Total n = 112

Expected counts under H₀ (3:1 ratio): E(purple) = 112 × 3/4 = 84 E(white) = 112 × 1/4 = 28 χ² = (74−84)²/84 + (38−28)²/28 χ² = (100)/84 + (100)/28 χ² = 1.190 + 3.571 χ² = 4.76 df = k − 1 = 2 − 1 = 1 (no parameters estimated from data) Critical value at α = 0.05, df = 1, is 3.84. Since 4.76 > 3.84, we reject H₀. The p-value is approximately 0.029 — data this discrepant from a 3:1 ratio would arise by chance only about 2.9% of the time if the true ratio were exactly 3:1. The geneticist should suspect either reduced viability of white-flowered genotypes, a genotyping/scoring error, or that a second locus modifies the phenotype — the test flags a real problem worth investigating, though it cannot by itself say which explanation is correct.

Example 2 — Testing a species-abundance model in ecology. An ecologist samples 200 individual trees across a plot and hypothesizes that four tree species occur in a ratio of 4:3:2:1 based on a dispersal-limitation model. She counts:

SpeciesObserved
A92
B58
C30
D20

Total n = 200. Expected proportions: 4/10, 3/10, 2/10, 1/10. E(A) = 200 × 0.4 = 80 E(B) = 200 × 0.3 = 60 E(C) = 200 × 0.2 = 40 E(D) = 200 × 0.1 = 20 χ² = (92−80)²/80 + (58−60)²/60 + (30−40)²/40 + (20−20)²/20 χ² = (144)/80 + (4)/60 + (100)/40 + (0)/20 χ² = 1.80 + 0.067 + 2.50 + 0 χ² = 4.37 df = 4 − 1 = 3. Critical value at α = 0.05 is 7.81. Since 4.37 < 7.81, we fail to reject H₀ (p ≈ 0.22). The observed abundances are statistically consistent with the 4:3:2:1 dispersal-limitation model — species A is somewhat over-represented and C somewhat under-represented, but not enough to conclude the model is wrong given this sample size. A larger sample might sharpen — or overturn — this conclusion, since larger n increases the statistical power to detect the same proportional deviation.

Common misconceptions & traps

Misconception / TrapWhy it's wrong
"A large χ² proves the alternative hypothesis is correct."It only shows the null is unlikely to be exactly true; it says nothing about why, or what the correct model is.
"You can run the test on percentages or proportions."The test needs raw counts; percentages discard the sample-size information the statistic depends on entirely.
"Failing to reject H₀ proves the ratio is exactly 3:1 (or whatever is hypothesized)."Absence of evidence is not evidence of absence — small samples routinely fail to detect real deviations (low power).
"Any expected count works, no matter how small."With expected counts below ~5, the chi-squared approximation breaks down and p-values become unreliable; exact tests are safer.
"Degrees of freedom is always (number of categories) − 1."Subtract one more degree of freedom for every parameter estimated from the sample itself (e.g., an allele frequency in Hardy-Weinberg testing).
"A tiny χ² (near-perfect fit) is always reassuring."An implausibly small χ² across repeated trials, as Fisher showed with Mendel's data, can itself be a red flag for data manipulation or bias.

The scientist behind it

Karl Pearson (1857–1936) was a British mathematician and one of the founders of modern mathematical statistics, working principally at University College London, where he established the world's first university statistics department. Pearson's motivating problems were biological and social: he wanted rigorous tools to test whether biometric data — skull measurements, moth coloration frequencies, gambling outcomes — matched theoretical distributions predicted by evolutionary or probabilistic models. His 1900 paper introducing the chi-squared goodness-of-fit test was part of a broader program, alongside Francis Galton and W. F. R. Weldon, called biometrics: applying quantitative statistical rigor to the study of heredity and evolution, in direct competition (and eventually productive synthesis) with the newly rediscovered Mendelian genetics that his own test would later help validate. Pearson's original derivation contained a subtle error regarding degrees of freedom when parameters must be estimated from the data — an error corrected only in 1922 by R. A. Fisher, who showed that each estimated parameter costs one additional degree of freedom. This correction mattered enormously in practice and set off a long-running, occasionally bitter dispute between Pearson and Fisher, two towering but famously combative figures in the history of statistics.

Origins · 1 minKarl Pearson and how this was discovered
KP
Karl Pearson
1857–1936 · Biometric Statistics, University College London

Pearson founded the field of mathematical statistics applied to biological variation, co-founding the journal Biometrika and deriving the chi-squared goodness-of-fit test as a general tool for testing any theoretical distribution against observed data.

History
- 1857 — Karl Pearson born in London.
- 1900 — Pearson publishes the chi-squared goodness-of-fit test in Philosophical Magazine.
- 1901 — Pearson co-founds the journal Biometrika with Weldon and Galton.
- 1922 — R. A. Fisher corrects Pearson's degrees-of-freedom formula for cases with estimated parameters.
- 1936 — Fisher publishes his statistical re-analysis of Mendel's pea data, questioning its "too good" fit.

Where this matters today

The chi-squared goodness-of-fit test remains a routine first step whenever biologists need to check categorical predictions against data. In clinical genetics, it screens for departures from expected Mendelian inheritance patterns in pedigree studies, flagging possible genotyping errors, non-random segregation, or reduced viability of certain genotypes (segregation distortion). In population genetics and conservation biology, it underlies tests of Hardy-Weinberg Equilibrium, checking whether observed genotype frequencies at a locus match those predicted from allele frequencies — deviations can signal inbreeding, non-random mating, selection, or genotyping artifacts in a conservation-managed population. In agricultural breeding programs, plant and animal breeders use goodness-of-fit tests to confirm expected segregation ratios in crosses before committing to large-scale breeding decisions, catching linked genes or epistatic interactions that produce non-Mendelian ratios. In epidemiology and public health, the test checks whether disease case counts across categories (blood type, age bracket, exposure category) match a null model of no association with a risk factor, serving as an entry point before more complex modeling. In molecular biology and next-generation sequencing, goodness-of-fit tests check whether the observed distribution of reads across categories (e.g., across the four nucleotide bases, or across expected vs. unexpected splice variants) matches a null expectation, helping detect technical bias in sequencing platforms. The test's conceptual descendant, the Chi-Squared Contingency Test, extends this same logic from one categorical variable to the association between two categorical variables — asking not "does this match a fixed ratio?" but "are these two classifications independent of each other?" Mastering the one-sample goodness-of-fit logic here — observed vs. expected, squared and scaled deviations, degrees of freedom, and the chi-squared distribution's tail — is the direct foundation for that two-variable extension.

Ask the tutor: How do I decide whether to subtract extra degrees of freedom when I've estimated a parameter (like an allele frequency) from my own sample before running the test?

Why is the chi-squared test only valid when expected counts are reasonably large?

The chi-squared distribution is a continuous, smooth mathematical curve, but real count data are discrete and lumpy — you can observe 3 or 4 offspring but never 3.5. When expected counts are large (a common threshold is E ≥ 5 in every category), the discreteness of the data gets smoothed out and the sampling distribution of Σ(O−E)²/E closely approximates the continuous chi-squared curve. When expected counts are small, this approximation breaks down badly, typically making p-values artificially small and leading to false rejections of true null hypotheses. In those small-count situations, exact tests (such as an exact multinomial test) that do not rely on this large-sample approximation are the statistically correct choice.

How is the chi-squared goodness-of-fit test different from the chi-squared contingency test?

The goodness-of-fit test examines a single categorical variable and compares its observed distribution to one specific, pre-stated hypothesis (like a 3:1 Mendelian ratio). The Chi-Squared Contingency Test examines two categorical variables simultaneously, arranged in a table, and asks whether they are statistically independent of each other — for example, whether disease status is independent of exposure status. Mechanically the two tests share the identical Σ(O−E)²/E formula and both use the chi-squared distribution, but the contingency test computes expected counts from the row and column totals of the data itself (assuming independence), rather than from an externally specified theoretical ratio, and it uses a different degrees-of-freedom formula: (rows − 1) × (columns − 1).

Can the chi-squared goodness-of-fit test tell you which category is causing a poor fit?

The overall χ² statistic only tells you that at least one category deviates more than chance would predict from expectation; it does not by itself identify which category is the culprit. In practice, researchers examine each category's individual contribution — the term (Oᵢ−Eᵢ)²/Eᵢ — to see which categories contribute disproportionately to the total. A category contributing, say, 70% of the total χ² value is the primary driver of a significant result. Some analyses follow up with post-hoc partitioning of the chi-squared statistic or standardized (adjusted) residuals to formally test each category's individual deviation, analogous to post-hoc tests following a significant ANOVA.

Does a non-significant chi-squared result mean the biological hypothesis is definitely correct?

No — this is one of the most common misinterpretations of any hypothesis test, not just chi-squared. Failing to reject H₀ means the data are consistent with the hypothesized ratio, but it does not prove the ratio is exactly correct. With a small sample, even a substantially wrong ratio can easily fail to produce a statistically significant deviation, simply because the test lacks the statistical power to detect it. Confidence in a null result should scale with sample size and with the statistical power of the study — a large, well-powered study returning a non-significant result is far more reassuring than a small underpowered one.

What role did biological reasoning play in choosing the expected ratios Mendel and later geneticists tested?

The expected ratios themselves come from a theoretical model of inheritance — Mendel's laws of segregation and independent assortment — not from the chi-squared machinery itself. The chi-squared test is a purely statistical tool that evaluates any stated hypothesis; it has no biological content of its own. Getting the biology right (correctly predicting 3:1, 9:3:3:1, 1:1 test-cross ratios, or Hardy-Weinberg genotype proportions from allele frequencies) is a separate, prior step grounded in genetics theory. The test's job begins only after the biological model has generated a specific, falsifiable numerical prediction.

Why did Fisher find Mendel's data suspicious rather than simply confirming it as excellent scientific work?

Fisher's reasoning relied on a deeper statistical fact: even if a null hypothesis is exactly true, sampling variation guarantees that observed counts will deviate somewhat from expected counts in most individual experiments, following a predictable statistical spread. If you sum the chi-squared statistics from many independent, honestly conducted experiments testing a true hypothesis, the total should itself follow a chi-squared distribution with a much larger degrees of freedom — and that total should show a "typical" amount of scatter, not near-perfect agreement in almost every single trial. Fisher found Mendel's aggregate fit was closer to the theoretical ratios than random chance would produce roughly 99.99% of the time, suggesting some undocumented practice — possibly informal rounding, selective reporting, or unconscious bias in classifying ambiguous phenotypes — inflated the apparent precision.

Is the chi-squared goodness-of-fit test the same thing as a "goodness of fit" check for a regression model?

No, and this terminology overlap causes confusion. In categorical data analysis, "goodness-of-fit" specifically refers to comparing observed counts across discrete categories to a hypothesized distribution, using the Σ(O−E)²/E statistic described here. In regression contexts, "goodness of fit" instead usually refers to measures like R² or residual analysis, which assess how well a continuous model's predicted values match continuous observed values — a related concept in spirit (both ask "does the model fit the data?") but a mathematically distinct framework using different statistics, assumptions, and underlying probability distributions.

Before this

Where this leads