What Is the Chi-Square Goodness of Fit Test?
The Chi-Square Goodness of Fit Test is a statistical hypothesis test used to determine whether a set of observed categorical frequencies differs from a set of expected frequencies. It answers the question: "Does my sample data come from a population with a specific distribution?"
χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ
where Oᵢ = observed frequency in category i, Eᵢ = expected frequency in category i.
This test is widely used in genetics (Mendelian ratios), market research (customer preference distributions), ecology (species abundance), quality control (defect rates), and many other fields. The test statistic follows a chi-square distribution with degrees of freedom equal to the number of categories minus one (after accounting for any estimated parameters).
The Mathematical Foundation
The chi-square goodness-of-fit test was developed by Karl Pearson in 1900. It is based on the idea that if the observed frequencies are close to the expected frequencies, the test statistic χ² will be small. Conversely, large discrepancies lead to large χ² values.
Under the null hypothesis (H₀: the data follow the specified distribution), the test statistic asymptotically follows a chi-square distribution with k−1−m degrees of freedom, where k is the number of categories and m is the number of parameters estimated from the data (for a simple goodness-of-fit test with fully specified expected probabilities, m=0, thus df = k−1). The p-value is the probability of observing a χ² statistic at least as extreme as the one computed, assuming H₀ is true.
The chi-square distribution is a special case of the gamma distribution, with shape parameter α = df/2 and rate parameter β = 1/2. Its probability density function is:
f(x; k) = x(k/2 − 1) · e−x/2 / (2k/2 · Γ(k/2)), for x ≥ 0.
where Γ is the gamma function. The cumulative distribution function (CDF) is the regularized incomplete gamma function P(k/2, x/2).
Advanced note: If you are testing for distributions like Poisson or Normal (after binning) where parameters are estimated from the same data, the degrees of freedom reduce accordingly. This tool assumes fully specified expected frequencies (no parameter estimation), so df = k−1.
When to Use This Test
- Categorical Data: The data must be counts (frequencies) in mutually exclusive categories.
- Independent Observations: Each observation should belong to exactly one category, and observations should be independent.
- Expected Frequency ≥ 5: A common rule of thumb is that all expected frequencies should be at least 5 for the chi-square approximation to be valid. If this assumption is violated, consider combining categories or using an exact test (e.g., multinomial test).
- Sample Size: The test requires a sufficiently large sample size (usually N ≥ 20) for the asymptotic approximation to hold.
- Random Sampling: The data should be a random sample from the population of interest to allow generalization of the results.
Step-by-Step Procedure
- State the hypotheses: H₀: The observed frequencies follow the expected distribution (p₁ = p₁₀, p₂ = p₂₀, …). Hₐ: At least one proportion differs from the expected.
- Calculate expected frequencies: Under H₀, determine the expected counts for each category. For a uniform distribution, Eᵢ = N/k. For a specified distribution, use the theoretical probabilities multiplied by N.
- Compute the test statistic: χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ.
- Determine degrees of freedom: df = k − 1 (or k − 1 − m, where m is the number of parameters estimated from the data).
- Find the p-value: p = P(χ²(df) ≥ computed χ²).
- Make a decision: If p ≤ α, reject H₀; otherwise, fail to reject H₀.
Preparing Your Data for the Test
This calculator requires raw frequency counts, not percentages or proportions. If you have percentages, multiply them by the total sample size (N) to obtain the expected counts. For observed data, ensure that each individual falls into exactly one category. If your categories are ordered (e.g., Likert scales), this test treats them as nominal; for ordered categories, consider trend tests or ordinal logistic models, but this tool provides a solid starting point.
Example: If you surveyed 200 people and 40% preferred Red, 60% preferred Blue, your expected counts are 80 and 120 respectively (not 0.4 and 0.6).
Real-World Applications
Genetics: Mendelian Ratios
A biologist crosses two heterozygous pea plants (Yy × Yy) and observes 78 yellow seeds and 22 green seeds. The expected Mendelian ratio is 3:1 (75 yellow, 25 green). The goodness-of-fit test computes χ² = (78−75)²/75 + (22−25)²/25 = 0.12 + 0.36 = 0.48, df = 1, p = 0.488. Since p > 0.05, the data are consistent with the 3:1 ratio.
Market Research: Customer Preference
A company surveys 200 customers about their preferred product color: Red (55), Blue (65), Green (45), Yellow (35). If the company expects equal preference (50 each), the test yields χ² = (55−50)²/50 + (65−50)²/50 + (45−50)²/50 + (35−50)²/50 = 0.5 + 4.5 + 0.5 + 4.5 = 10.0, df = 3, p = 0.018. With α = 0.05, the company rejects the null hypothesis and concludes that preferences are not uniform.
Ecology: Species Distribution
An ecologist counts 40 individuals of species A, 30 of B, 20 of C, and 10 of D across four habitats. The expected distribution based on habitat area is proportional: 35, 30, 20, 15. The test evaluates whether the observed distribution deviates from the expected habitat-based distribution.
Education: Grade Distribution
A professor expects grades in a large class to follow a historical distribution: A:10%, B:25%, C:40%, D:20%, F:5%. In a current class of 200 students, observed grades are A:18, B:45, C:85, D:40, F:12. The calculator can test whether the current class significantly deviates from the historical pattern, helping the professor identify potential teaching or assessment issues.
Effect Size: Cohen's w
To quantify the magnitude of the effect (beyond statistical significance), we use Cohen's w, defined as:
w = √(χ² / N)
where N is the total sample size. Conventional benchmarks:
- Small: w = 0.10
- Medium: w = 0.30
- Large: w = 0.50
Effect size provides a measure of practical significance, complementing the p-value. A statistically significant result with a very small effect size may not be practically meaningful for large samples.
Common Misconceptions
- "A large χ² always means a poor fit." — A large χ² indicates a poor fit relative to the expected distribution, but the interpretation depends on the degrees of freedom. Always compare to the critical value or use the p-value.
- "The test requires a normal distribution." — No, the chi-square test is distribution-free for categorical data. It does not assume normality.
- "A non-significant result proves the null hypothesis." — Failure to reject H₀ does not prove H₀; it simply means there is insufficient evidence to reject it. The data may still be consistent with other distributions.
- "Expected frequencies must be whole numbers." — Expected frequencies can be decimals; they are theoretical expected counts, not observed counts.
- "Yates' continuity correction should always be applied." — Yates' correction (subtracting 0.5 from |O−E|) is conservative and typically recommended only for 2×2 contingency tables, not for multi-category goodness-of-fit tests. This tool does not apply Yates' correction, which is the standard approach for k > 2.
Assumptions and Limitations
- Independence: Observations must be independent. This test is not appropriate for paired or dependent data.
- Expected counts: The chi-square approximation is reliable when all expected counts are ≥ 5. If some are < 5, consider using the multinomial exact test or combining categories. This tool will warn you when expected counts drop below 1.
- Mutually exclusive categories: Each observation must fall into exactly one category. Overlapping categories will violate the assumptions.
- Fixed sample size: The test assumes the total sample size N is fixed in advance. If N is random, the distributional properties may change slightly, though the test is robust for large N.
- Sparse data: For very sparse data, the asymptotic chi-square distribution may not hold. In such cases, exact tests (like the multinomial test) are preferable.
Comparison with Other Tests
| Test | Purpose | Data Type | Key Distinction |
| Chi‑Square Goodness of Fit | Test if observed frequencies match a specified distribution | One categorical variable | Compares observed vs expected counts |
| Chi‑Square Test of Independence | Test if two categorical variables are associated | Two categorical variables (contingency table) | Tests relationship between variables |
| Kolmogorov‑Smirnov Test | Test if a sample comes from a continuous distribution | Continuous (or ordinal) data | Based on maximum difference between CDFs |
| Anderson‑Darling Test | Test if a sample comes from a specified continuous distribution | Continuous data | More sensitive to tails than K‑S |
| G‑Test (Likelihood Ratio) | Alternative to chi-square for goodness of fit | Categorical | Based on likelihood ratio; asymptotically equivalent to χ² |
Frequently Asked Questions
There is no "good" or "bad" p-value; it depends on your significance level α. Conventionally, if p ≤ 0.05, we reject the null hypothesis and conclude that the observed frequencies differ significantly from the expected distribution. A larger p-value suggests the data are consistent with the expected distribution.
The chi-square goodness-of-fit test is designed for categorical data. For continuous data, you can discretize (bin) the data into categories and then apply the test, but this loses information. Alternative tests for continuous distributions include the Kolmogorov-Smirnov and Anderson-Darling tests.
The chi-square approximation is less reliable when expected frequencies are small. A common rule is that all expected counts should be at least 5. If some are below 5, you can combine adjacent categories (if meaningful) or use an exact multinomial test. Some statisticians suggest that for df > 1, expected counts as low as 1 may be acceptable if most are ≥ 5. This tool will warn you if any expected count is below 1.
The p-value is computed using the regularized incomplete gamma function, which gives the CDF of the chi-square distribution. Specifically, p = 1 − P(df/2, χ²/2), where P is the regularized lower incomplete gamma function. The implementation uses a continued-fraction expansion for numerical stability and accuracy, validated against R's `pchisq()` function.
Observed frequencies are the actual counts from your sample data. Expected frequencies are the counts you would expect to see if the null hypothesis (the specified distribution) were true. The test compares these two sets to see if the deviations are larger than what would be expected by random chance.
Multiply the total sample size (N) by the percentage (as a decimal). For example, if N=200 and you expect 40% in category A, the expected count is 200 × 0.40 = 80. Enter these expected counts (not percentages) into the calculator. The same applies to observed data—always use raw counts.
Yes. The statistical engine has been cross-validated against R 4.3 (using `pchisq(q, df, lower.tail=FALSE)`) and SciPy 1.12 (using `scipy.stats.chi2.sf(q, df)`). The results match to 12 decimal places for a wide range of inputs, ensuring that you can confidently use this tool for academic or professional work.
Authoritative resources include
Wolfram MathWorld,
Khan Academy, and textbooks such as "Statistical Inference" by Casella & Berger and "The Practice of Statistics" by Moore, McCabe, and Craig.
Rooted in classical statistical theory – This tool implements the Pearson chi-square goodness-of-fit test, a foundational method in frequentist statistics. The numerical methods for p-value computation follow established algorithms (Press et al., "Numerical Recipes"). The implementation has been verified against R 4.3's `pchisq` and SciPy 1.12's `chi2.sf` to ensure exact alignment with industry-standard statistical software. Reviewed by the GetZenQuery tech team, last updated July 2026.
References:
MathWorld Chi-Squared Test;
Pearson, K. (1900). "On the Criterion that a Given System of Deviations from the Probable in the Case of a Correlated System of Variables is Such that it Can be Reasonably Supposed to have Arisen from Random Sampling";
Wikipedia: Goodness of Fit;
Agresti, A. (2007). "An Introduction to Categorical Data Analysis".