Math · Statistics
Chi-square calculator.
Test whether two categorical variables are independent in a table of counts, or whether observed counts fit expected proportions. Your data stays in this browser.
For the 2 × 2 table of counts 20, 30 / 30, 20, every expected count is 25, so χ² = 4.00 with 1 degree of freedom and p = 0.0455. With the Yates continuity correction χ² falls to 3.24 and p rises to 0.0719, which flips the result at the 5% level.
How it works
Both versions use χ² = Σ (O − E)² / E, where O is an observed count and E the expected count. The p-value is the upper tail of the chi-square distribution. For a test of independence, E = (row total)(column total) / grand total and the degrees of freedom are (number of rows − 1)(number of columns − 1). For a goodness-of-fit test the expected counts come from the distribution you are testing, and the degrees of freedom are the number of categories minus 1 minus any parameters estimated from the data. The Yates continuity correction, used almost exclusively for 2 × 2 tables, subtracts 0.5 from each |O − E| before squaring; it is more conservative, so it raises the p-value.
Worked example
Observed 20, 30 / 30, 20 has row and column totals of 50 and a grand total of 100, so every expected count is 50 × 50 / 100 = 25. Each cell contributes 25 / 25 = 1 (that is (5)² / 25), so χ² = 4 with df = 1. NIST’s critical-value table lists 3.841 as the value with 0.95 of the distribution below it at 1 degree of freedom; 4.00 exceeds it, giving p = 0.0455. With Yates, each |O − E| = 5 becomes 4.5, giving 20.25 / 25 × 4 = 3.24 and p = 0.0719. For goodness of fit with observed 18, 22, 20, 40 and equal expected counts of 25: χ² = (49 + 9 + 25 + 225) / 25 = 12.32 with df = 3 and p = 0.0064.
Limits
Both the NIST goodness-of-fit page and OpenStax say the expected frequency in each cell should be at least 5 for the chi-square approximation to be valid; the calculator warns when a cell falls short. Counts must be actual counts (not percentages), observations must be independent, and the test only tells you whether an association exists, not how strong it is.
Sources, opened 8 October 2026: NIST/SEMATECH e-Handbook, 1.3.5.15 Chi-Square Goodness-of-Fit Test (statistic, expected frequency at least 5, degrees of freedom k − c); OpenStax Introductory Statistics 2e, 11.3 Test of Independence (E = (row total)(column total) / total, df = (columns − 1)(rows − 1), expected value at least five); Wikipedia, Yates’s correction for continuity (Yates: subtracting 0.5 from the absolute difference, mainly for 2 × 2 tables, more conservative); NIST/SEMATECH e-Handbook, 1.3.6.7.4 Critical Values of the Chi-Square Distribution (critical value 3.841 at 1 degree of freedom).
Frequently asked questions
How do you calculate a chi-square test of independence?
Compute each expected count as (row total x column total) / grand total, then add (O - E)^2 / E over all cells. The degrees of freedom are (rows - 1) x (columns - 1). For counts 20, 30 / 30, 20 that gives chi-square 4.00, df 1 and p = 0.0455.
When should I use the Yates correction?
Yates is used almost exclusively for 2 x 2 tables, particularly with small samples. It subtracts 0.5 from |O - E| and is more conservative, so p is larger. In the example it moves p from 0.0455 to 0.0719.
Does the calculator work for 3 x 2 or 5 x 5 tables?
Yes. Enter any rectangular table of counts with the same number of values on each line. The Yates option is ignored unless the table is exactly 2 x 2.
What is a chi-square goodness-of-fit test?
It tests whether observed counts in categories match expected proportions. Switch the test type, enter the observed counts, and leave expected blank for equal expected counts. With 18, 22, 20, 40 the statistic is 12.32 on 3 degrees of freedom.
What does a warning about expected counts below 5 mean?
NIST and OpenStax both say each expected count should be at least 5 for the approximation to be valid. If a cell is lower, combine categories or collect more data before trusting the p-value.
Can I enter percentages instead of counts?
Not for the observed values: the test needs real counts. For the expected values in a goodness-of-fit test, proportions or percentages are fine because they are scaled to the observed total.
Learning aid, not for decisions
Educational calculator. Results assume the conditions listed on this page, which real data often only roughly meet. Do not use it for clinical, regulatory, legal or financial decisions.