Professor Turing

Study guides

What is a chi-square test?

Last updated: 2026-11-23

A chi-square test compares the counts you observed with the counts you would expect if a stated assumption were true, and tells you how surprising the gap between them is. The counts are numbers of people or things in categories, such as how many students passed, not averages or measurements.

Start learning No account needed

If you rolled a die 60 times and each face came up about 10 times, you would not be surprised. If one face came up 20 times, you would start to doubt the die. The test puts a number on that doubt. If you want the statistics around it, learn statistics on your own covers the basics.

1. The pieces

The SciPy documentation describes the chi-square test of independence for a contingency table, a grid of observed counts with one category on the rows and another on the columns. It computes the expected counts from the row and column totals, under the assumption that the two categories are independent, which means that knowing one tells you nothing about the other. The number of degrees of freedom for a table with R rows and C columns works out to (R − 1) × (C − 1).

The NIST/SEMATECH handbook gives the same statistic for the goodness-of-fit version, which asks whether a sample came from a population with a specified distribution. For each category you take the observed count O and the expected count E, square the gap and divide by E, then add the results over all categories: the sum of (O − E)² / E. A larger sum means a larger gap overall. Both sources give the same rule of thumb for when the approximation holds, which is that the expected count in each cell should be at least 5. The handbook adds that the test is sensitive to how data are put into bins and that it needs a sufficient sample size.

2. A worked example: 120 students, three ways of studying

Suppose 120 students each used one of three methods to prepare for the same exam, 40 students per method, and we recorded whether each one passed. The numbers are made up for this page. Reading across, the passed row is 18, 24 and 33, and the did-not-pass row is 22, 16 and 7, for rereading, flashcards and practice tests. In all, 75 passed and 45 did not.

If method and result were unrelated, each method would have the same pass rate as the whole group, 75 out of 120. So the expected count of passes for a method with 40 students is 75 × 40 / 120 = 25, and the expected count of non-passes is 45 × 40 / 120 = 15. Those expected counts are the same for the three methods, and the smallest is 15, so the at-least-5 rule is met.

observed expected if unrelated Passed 18 25 rereading 24 25 flashcards 33 25 practice tests Did not pass 22 15 rereading 16 15 flashcards 7 15 practice tests

Observed counts (solid) beside the counts expected if method and result were unrelated (dashed).

Now take the gap in each cell, square it and divide by the expected count. For passes: rereading is (18 − 25)² / 25 = 1.96, flashcards is (24 − 25)² / 25 = 0.04, and practice tests is (33 − 25)² / 25 = 2.56. For non-passes: rereading is (22 − 15)² / 15 = 3.267, flashcards is (16 − 15)² / 15 = 0.067, and practice tests is (7 − 15)² / 15 = 4.267. The total is 12.16.

The degrees of freedom are (2 − 1) × (3 − 1) = 2. With 2 degrees of freedom, the chance of a total at least this large, if the assumption of no link were true, is exp(−12.16 / 2) = 0.0023. We checked this against the SciPy function chi2_contingency, which returns the same statistic, degrees of freedom and p-value. So a gap this large would show up in about 0.23 percent of samples if method and result were unrelated, and we have reason to doubt that they are.

The cell values also show where the gap sits. Practice tests and the did-not-pass row contribute the most, 4.267, followed by rereading and did-not-pass at 3.267. The flashcard cells contribute 0.04 and 0.067, so flashcards sit close to what no link would predict.

3. What the result does not tell you

It is natural to read a small p-value as proof that practice tests cause passing. The test says only that the counts are unlikely under no link. The students chose or were given their methods, and students who pick practice tests may differ in other ways, such as how early they started. The test cannot separate those explanations.

The statistic also depends on how many people you counted, not only on how big the gap is. Keep the same percentages in each column but double the students, to 240, and the statistic doubles to 24.32 with a p-value of about 0.0000052. And the test needs counts. If you put the column percentages from the table into the formula in place of the counts, you get 30.4, a number that describes nothing, because the formula needs to know how many students stand under each percentage. Treat a small p-value as a prompt to look at the table and ask why, not as a measure of how large or how important the difference is.

4. The goodness-of-fit version

The same formula answers a different question when you have one row of counts and a distribution to compare it with, such as a fair die. The expected count for each face is the number of rolls times that face’s probability. The NIST handbook says the degrees of freedom are k − c, where k is the number of categories and c is the number of estimated parameters plus one. With a fair die nothing is estimated, so c is 1 and the degrees of freedom are 6 − 1 = 5.

5. Test yourself

Close this page and take 60 rolls of a die that came out 5, 8, 9, 8, 10 and 20 for faces 1 to 6. Assume the die is fair, so each face is expected 10 times. Work out each face’s contribution, the total, the degrees of freedom, and say which face matters most. Then say what the result does and does not tell you about why face 6 came up so often. Reopen the page and compare. The contributions are 2.5, 0.4, 0.1, 0.4, 0 and 10, the total is 13.4, and the degrees of freedom are 5. The p-value is about 0.02, so such a gap would show up in about 2 percent of sets of 60 fair rolls. Face 6 contributes 10 of the 13.4. The test does not say whether the die is weighted or whether the rolling method was the cause. The first step where you slipped is the part to study again. For the general method, see how to test yourself.

What the sources found, and their limits

SciPy manual, scipy.stats.chi2_contingency (version 1.18.0). Documentation for the SciPy library. It describes the chi-square test of independence in a contingency table, says the expected frequencies come from the marginal sums under the assumption of independence, gives the degrees-of-freedom formula, and says an often quoted guideline is that observed and expected frequencies in each cell should be at least 5. It also says that when the degrees of freedom are 1, the default applies Yates’ correction, which moves each observed value 0.5 toward its expected value. Our table has 2 degrees of freedom, so no correction applies. Source: docs.scipy.org, chi2_contingency, read on 2026-11-23.

NIST/SEMATECH e-Handbook of Statistical Methods, section 1.3.5.15, Chi-Square Goodness-of-Fit Test. A reference from the US National Institute of Standards and Technology. It defines the test for the hypothesis that data follow a specified distribution, gives the statistic as the sum of (O − E)² / E, the degrees of freedom as k − c, the expected count of at least 5, and the dependence on binning and sample size. Source: itl.nist.gov, chi-square goodness-of-fit test, read on 2026-11-23.

Limits: we did not read the extended example on the SciPy page, its other options, the Fisher exact test it links to, or the Snedecor and Cochran book the handbook cites. The 120 students, the counts, the contributions, the doubled-sample and percentage figures, the die rolls and the plain-words reading of the p-value are our own arithmetic and explanation, not from the sources. The data are made up.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a topic you are studying, such as why the statistic grows when you double the sample. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and conversations are deleted after 7 days.

FAQ

What does the p-value of a chi-square test tell me? It tells you how often a gap at least as large as yours would turn up if the assumption being tested were true. In our example the assumption is that study method and result are unrelated, and the p-value of 0.0023 means a gap this large would show up in about 0.23 percent of samples. It does not say the assumption is false for sure, and it does not say how large or how important the difference is.

What if some expected counts are below 5? Both sources we read give a rule of thumb that expected counts should be at least 5. The NIST handbook says the test is not valid for small samples and that if some counts are under five you may need to combine some bins in the tails. SciPy's page lists Fisher's exact test as a related function, which we did not read, so we cannot say here when to use it.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides