Chi-Square Test Calculator
Enter observed counts — one list, or a table for two variables — and get the chi-square statistic, degrees of freedom, exact p-value, and the expected counts, with assumptions checked.
How this calculator works
Both modes run the same engine: compare every observed count against its expected count, square the gaps, scale by the expected values, and sum. Goodness-of-fit mode takes one list of observed counts and an expected pattern (leave it blank for "all equal" — proportions or counts both work, since they're normalized to your sample size). Independence mode takes a contingency table, computes each cell's expected count from the row and column totals, and tests whether the two variables are related.
The expected counts are displayed next to your observed ones — the single most informative output, because the biggest observed-versus- expected gaps are where the effect lives. The calculator also checks the classic small-count assumption and warns when any expected cell drops below 5.
The formula
χ² = Σ (O − E)² ÷ E
Goodness of fit: E_i from stated proportions; df = k − 1
Independence: E_ij = (row total × column total) ÷ n
df = (rows − 1)(columns − 1)
p = upper-tail probability of χ²_df beyond the statisticMethod per the NIST/SEMATECH e-Handbook of Statistical Methods (§1.3.5.15, itl.nist.gov) and standard categorical-data references (e.g., Penn State's online STAT 500 course notes). The p-value is the regularized incomplete gamma function's upper tail, computed by the series/continued-fraction algorithms of Numerical Recipes ch. 6 and verified against published critical values (χ² = 3.841 at df = 1 returns p = 0.0500).
Worked example
A die is rolled 120 times, landing 22, 17, 20, 13, 24, 24 on faces 1–6 (the calculator's default). Is it fair?
- Expected per face: 120 ÷ 6 = 20
- χ² = (2² + 3² + 0² + 7² + 4² + 4²) ÷ 20 = 94 ÷ 20 = 4.7
- df = 6 − 1 = 5
- p = 0.4536
A fair die produces deviations this size or larger about 45% of the time — the low count of 13 fours looks suspicious to the eye but is entirely ordinary statistically. Switch to independence mode for the default 2×2 treatment table and you'll see the opposite verdict: χ² = 8.16, p = 0.0043.
Assumptions & tips
- Counts, never percentages or measurements. The test is exquisitely sensitive to sample size — feeding it percentages destroys exactly that information.
- Read the expected column before the p-value. The cells with the biggest O−E gaps are the finding; the p-value only certifies that the pattern as a whole isn't noise.
- Sparse cells break the approximation. Below ~5 expected per cell, merge similar categories or use Fisher's exact test (2×2). The warning in the results takes care of noticing.
- Association is not causation — or direction. A significant independence test says the variables are related, not which causes which, and not even which cells drive it without looking at residuals.
- Big n makes everything "significant." With tens of thousands of observations, trivial differences clear p < 0.05. Pair the p-value with an effect size (for tables, Cramér's V) when samples are large.
Frequently asked questions
When do I use goodness of fit versus independence?
Goodness of fit compares one set of observed category counts against expected proportions — is this die fair, do these colors match the advertised mix. Independence (the contingency-table test) asks whether two categorical variables are related — does treatment affect outcome, does preference differ by region. Same statistic, different expected counts: given proportions in the first case, computed from the table's margins in the second.
Why must expected counts be at least 5?
The chi-square distribution is only an approximation to the statistic's true sampling distribution, and it grows unreliable when expected cell counts are small — below about 5 is the standard rule of thumb. The calculator flags offending cells. Fixes: collect more data, combine sparse categories, or for 2×2 tables use Fisher's exact test, which needs no approximation.
What are degrees of freedom here?
The number of cells free to vary once the totals are pinned down. Goodness of fit: categories minus one, because the counts must sum to n. Independence: (rows − 1) × (columns − 1), because each row and column total is fixed. A 2×2 table has just one free cell — set it and the margins dictate the rest — hence df = 1.
Does a significant chi-square tell me which cells differ?
No — it is an omnibus test: it says the observed pattern is unlikely under the null, not where the surprise lives. To localize it, compare each cell's observed and expected counts (the calculator shows both) and look at the largest contributions to the statistic. Formal follow-ups use standardized residuals; informally, the biggest observed-minus-expected gaps are where the story is.
Counts or percentages?
Raw counts, always. The test's math depends on sample size: 60% versus 40% is meaningless to it until you know whether that was 6-versus-4 or 600-versus-400. Convert percentages back to counts before entering anything, and never enter averages or measurements — chi-square is for category tallies only.
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods (NIST Handbook 151), §1.3.5.15 — Chi-Square Goodness-of-Fit Test — National Institute of Standards and Technology, 2020. itl.nist.govThe goodness-of-fit statistic Σ (O − E)² ÷ E and its degrees of freedom, plus the stated requirement that expected frequencies be at least 5 — the threshold behind this page's small-count warning.
- Dataplot Reference Manual — Chi-Square Independence Test — National Institute of Standards and Technology, Statistical Engineering Division. itl.nist.govExpected cell counts E_ij = row total × column total ÷ grand total, and df = (r − 1)(c − 1), for the R × C contingency table computed in independence mode.
- STAT 500: Applied Statistics — Lesson 8, Chi-Square Test for Independence — Penn State Department of Statistics. online.stat.psu.eduThe categorical-data course notes cited in the formula section above: building the expected table from the row and column margins, df = (r − 1)(c − 1), and the stated caution against small expected counts — cells with an expected frequency below five.
- NIST Digital Library of Mathematical Functions — §8.2 Definitions and Basic Properties — National Institute of Standards and Technology (DLMF release of 2026-06-15). dlmf.nist.govDefines the regularized incomplete gamma function P(a, x), whose upper tail 1 − P(df/2, χ²/2) is the exact p-value this calculator reports instead of a table lookup.
- Numerical Recipes: The Art of Scientific Computing, 3rd edition — W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Cambridge University Press, 2007. The series and continued-fraction algorithms for the incomplete gamma function used by this page's p-value routine, named in the formula section above.
- 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 — Karl Pearson, The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science, series 5, volume 50, pages 157–175, 1900. The paper that introduced the χ² statistic and its sampling distribution, on which both modes of this calculator rest. Cited bibliographically: no stable free full text was found to link.
Related calculators
Quadratic Equation Solver
Roots (real or complex, in exact and decimal form), discriminant, vertex, and factored form — with every step shown.
Complete the Square Calculator
Transform ax² + bx + c into vertex form step by step, with exact fractions at every stage.
t-Test Calculator
One-sample, Welch two-sample, or paired — t statistic, degrees of freedom, and exact p-values from your data.
Plot Digitizer (Graph → Data)
Turn a picture of a chart back into numbers: calibrate the axes, click the points, export CSV.