Chi-Square Test Calculator — Goodness of Fit & Independence
Paste category counts or a contingency table and get the χ² statistic, degrees of freedom, an exact p-value, the critical value, the full expected-count matrix and a plain-English verdict — with every step of the working shown. Runs entirely in your browser.
How it works
Pearson's chi-square test measures the gap between the counts you observed and the counts you would expect if a hypothesis were true. The statistic is always the same shape — χ² = Σ (O − E)² / E summed over every category or cell — and the two tests below differ only in how the expected counts E and the degrees of freedom are found. The formulas follow the NIST/SEMATECH e-Handbook of Statistical Methods.
Goodness of fit. You have counts in k categories and a target distribution. With an equal (uniform) expectation, each expected count is E = N / k, where N is the total. With custom weights, the weights are normalised to sum to N, so 20, 20, 20 and 1, 1, 1 describe the same uniform shape. The degrees of freedom are df = k − 1 − m, where m is the number of distribution parameters you estimated from the same data — leave m at 0 when the expected distribution was fixed in advance (NIST §1.3.5.15).
Test of independence. You have an r×c table of counts cross-classifying two variables. Under the null hypothesis of independence the expected count in each cell is E_ij = (row i total × column j total) / N. Degrees of freedom are df = (r − 1)(c − 1). For a 2×2 table that is a single degree of freedom (NIST §7.4.3).
Yates' continuity correction is offered for 2×2 tables only. It replaces each |O − E| with max(0, |O − E| − 0.5) before squaring, which compensates for fitting discrete counts to the continuous χ² curve. It always lowers χ², so it is conservative; the tool shows both the corrected and uncorrected values.
The p-value is the upper-tail area of the χ² distribution with the right degrees of freedom: p = P(X ≥ χ²) = Q(df/2, χ²/2), where Q is the regularised upper incomplete gamma function. This tool evaluates Q by series expansion and continued fraction (Numerical Recipes §6.2) rather than reading a rounded printed table, so every χ² and df resolve exactly to display precision. The critical valueis the inverse: the χ² whose upper-tail area equals your significance level α, found by bisection on the same distribution. You reject the null hypothesis when χ² ≥ the critical value, equivalently when p ≤ α. For 1 and 2 degrees of freedom the p-value is cross-checked against an independent closed form, and the critical values match the NIST table to three decimals. Finally, the tool flags any expected count below 5, where the χ² approximation weakens and Fisher's exact test or combining categories is the better move.
Worked examples
Frequently asked questions
Sources & references
- NIST/SEMATECH e-Handbook §1.3.5.15 — Chi-Square Goodness-of-Fit Test
- NIST/SEMATECH e-Handbook §7.4.3 — Chi-square test for independence (expected counts, df)
- NIST/SEMATECH e-Handbook §1.3.6.7.4 — Critical Values of the Chi-Square Distribution
- NIST/SEMATECH e-Handbook §1.3.6.6.6 — Chi-Square Distribution (CDF / PDF)
The formulas and the p-value engine on this page were last cross-checked against the NIST e-Handbook and the chi-square critical-value table on 2026-06-12.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.