On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

Math · Statistics & Probability · Grade 9-12

Chi-Square Test

A chi-square test compares observed category counts to expected counts using χ2=∑(O−E)2E to test goodness of fit or independence.

χ2=∑(Observed−Expected)2Expected

Quick Answer

A chi-square test compares observed category counts to expected counts using χ2=∑(O−E)2E to test goodness of fit or independence. Use it when your data are counts in categories, such as die faces, candy colors, or yes/no answers by grade, and you ask whether they match a claimed distribution or whether two categorical variables are related. Large gaps between observed and expected counts make χ2 large; comparing it with the chi-square distribution for the right degrees of freedom gives a p-value.

Why This Matters

Many questions have category answers, not numbers: is a die fair, do candy colors match the company's stated mix, does favorite lunch depend on grade? Means and t-tests can't handle these. The chi-square test gives one statistic for a whole table of counts, plus a p-value, so "these counts look a bit uneven" becomes a decision about whether the unevenness is more than chance.

Intuitive Explanation

Roll a die 60 times. If it is fair, each face should come up about 10 times, but random variation means some faces get 8 and others 13. Suppose you see 8, 12, 11, 7, 13 and 9. Is that much unevenness normal? For each face, take the gap from expected, square it so gaps above and below both count, and divide by the expected count: (8−10)210=0.4, and so on. The six pieces are 0.4, 0.4, 0.1, 0.9, 0.9 and 0.1, which add to χ2=2.8.

Dividing by the expected count scales each gap by how big it ought to be: missing by 5 when you expected 10 is a large miss, while missing by 5 when you expected 1,000 is tiny. Then compare χ2 with its distribution when H0 (a fair die) is true. With 6 categories there are df=6−1=5 degrees of freedom and the 5% cutoff is about 11.07. A value of 2.8 is far below it, so this unevenness is ordinary for a fair die.

The same idea tests two-way tables. To ask whether favorite sport depends on grade, compute each cell's expected count assuming independence, row total×column totalgrand total, add (O−E)2E over every cell, and use df=(r−1)(c−1).

Chi-square needs raw counts. If the data are measurements like test scores and the question is whether two means differ, use a two-sample t-test. And never feed it percentages: 30% of 10 people and 30% of 1,000 people carry very different amounts of evidence, which only the counts reveal.

Think of it like this. The chi-square statistic is a surprise score for a tally sheet. Each category adds its own surprise, the squared miss scaled by what was expected, and the total says how far the whole sheet strays from the prediction. Like any score, it means something only next to what chance alone usually produces, which the degrees of freedom pin down.

Core idea. The chi-square test sums squared gaps between observed and expected category counts to test independence or goodness of fit.


When to Use

Use a chi-square test when the data are counts of cases in categories and the question is whether they fit a stated distribution (goodness of fit, df=k−1) or whether two categorical variables are independent (two-way table, df=(r−1)(c−1)). Each expected count should be at least about 5. If the data are numeric measurements and you are comparing means, use a t-test. For exactly two proportions, a two-proportion z-test gives the same answer as a 2×2 chi-square test.

Tip. Ask: Are the data counts of cases falling into categories, being compared to expected counts (rather than means or continuous values)?

How to Recognize It

Before running a chi-square test, look at what kind of number sits in each cell, how many categorical variables are involved, and how large the expected counts are.

  1. Is there one categorical variable compared with a claimed distribution, such as whether a die's six faces come up equally often?

    If yes, it is a goodness-of-fit test with df=k−1 for k categories. If two variables are crossed in a table, such as grade and favourite sport, it is a test of independence with df=(r−1)(c−1).

  2. Is every number in the table a whole-number count of people or objects, not a percentage or an average?

    If yes, the test can go ahead. If you were handed percentages, turn them back into counts using the sample size first; if the values are measurements like heights, compare means with a t-test instead.

  3. Is every expected count at least about 5?

    If yes, the chi-square distribution gives a reliable p-value. If some expected counts are tiny, combine neighbouring categories or collect more data, because a small expected count in a denominator makes χ2 jump around.

  4. Are there exactly two groups and one yes-or-no outcome?

    If yes, a two-proportion z-test works too and gives the same two-sided p-value as the 2×2 chi-square, since χ2=z2. With more groups or more outcomes, only chi-square handles the whole table at once.

Chi-Square Test compared with Two-sample t-test, Two-proportion z-test, and Correlation / LSRL

Every row tests or measures whether groups or variables are related, so they overlap. Ask what the data look like: category counts in a table (chi-square), two yes-or-no proportions (z-test), averages of measurements in two groups (t-test), or two numeric variables (correlation).

Chi-Square Test

Meaning
Use this when your data are counts in categories and you test whether they fit an expected distribution or whether two categorical variables are independent.
Key test
Are the data counts of cases falling into categories, being compared to expected counts (rather than means or continuous values)?
Formula
χ2=∑(Observed−Expected)2Expected
Example
A die is rolled 600 times; face counts are 90, 110, 105, 95, 100, 100. Test goodness of fit to a fair die at α=0.05.

Two-sample t-test

Meaning
Compares MEANS of a numeric variable across two groups, not category counts.
Key test
Use when the response is numeric and you compare averages.
Formula
t=xˉ1−xˉ2SE
Example
Comparing mean test scores of two classes

Two-proportion z-test

Meaning
Compares two proportions directly; chi-square of a 2×2 table is equivalent but generalizes to bigger tables.
Key test
Use for exactly two groups and one yes/no outcome.
Formula
z=p^1−p^2SE
Example
Comparing vaccination rates in two towns

Correlation / LSRL

Meaning
Measures linear association between two NUMERIC variables, not categorical association.
Key test
Use when both variables are quantitative.
Formula
r=…
Example
Height vs weight relationship

Formula & Notation

χ2=∑(Observed−Expected)2Expected
χ2=∑i=1k(Oi−Ei)2Ei where Oi is observed count and Ei is expected count; df=k−1 (GOF) or (r−1)(c−1) (independence)

How to read it: χ2 is the test statistic. Degrees of freedom: goodness-of-fit df=k−1; independence/homogeneity df=(r−1)(c−1).

Worked Examples

Example 1 — Is the die fair?

Easy

A die is rolled 600 times; face counts are 90, 110, 105, 95, 100, 100. Test goodness of fit to a fair die at α=0.05.
Show the steps and the answer
  1. State H0: each face has probability 16. Expected count per face: 6006=100.

    Expected counts come from the claimed distribution and the total number of rolls.

  2. Compute each piece (O−E)2E: (90−100)2100=1, then 1, 0.25, 0.25, 0, 0.

    Squaring makes a gap of 10 below count the same as 10 above; dividing by 100 scales it by the expected size.

  3. Add: χ2=1+1+0.25+0.25+0+0=2.5, with df=6−1=5.

    Six counts that must total 600 leave 5 free to vary.

  4. Compare: the 5% cutoff for df=5 is about 11.07, and 2.5<11.07 (p-value about 0.78).

    Under a fair die, a χ2 of 2.5 or more happens most of the time, so there is no evidence against fairness.

Answer: χ2=2.5, p-value large, fail to reject — die looks fair

Takeaway: Expected counts from H0, then ∑(O−E)2E, then compare with the chi-square distribution for df=k−1.

Example 2 — Comparing two class averages

Standard

Instead you have the numeric test scores of two classes and want to know if their mean scores differ. Chi-square?
Show the steps and the answer
  1. Identify the data type: each student has a numeric score, such as 74 or 88.

    Chi-square works with counts of cases in categories, not with measurements.

  2. Identify the question: do two means differ?

    Comparing two means calls for a two-sample t-test, t=xˉ1−xˉ2s12/n1+s22/n2.

  3. Note when chi-square would fit: if the scores were turned into pass/fail counts for each class, a chi-square test of independence could compare those counts.

    The same data call for a different test depending on whether you keep the numbers or count categories.

Answer: No — use a two-sample t-test

Takeaway: Numeric measurements and means call for a t-test; counts in categories call for chi-square.

Example 3 — Find the mistake: dividing by the observed count

Application

A candy company says its bags are 50% red, 30% green and 20% yellow. A bag of 100 candies has 40 red, 35 green and 25 yellow. Eli computes χ2=(40−50)240+(35−30)235+(25−20)225≈4.21. Find the mistake and correct it.
Show the steps and the answer
  1. Spot the error: Eli divided each squared gap by the observed count (40, 35, 25).

    The formula divides by the expected count, because the expected count sets how big random gaps should be under H0.

  2. Find the expected counts from the claim: 0.5×100=50, 0.3×100=30, 0.2×100=20.

    Expected counts come from H0, not from the data.

  3. Recompute: (40−50)250+(35−30)230+(25−20)220=2+0.833+1.25≈4.08.

    Each squared gap is now scaled by what the company's mix predicts.

  4. Compare with df=3−1=2: the 5% cutoff is 5.99, and 4.08<5.99 (p-value about 0.13).

    The bag's mix is within ordinary random variation, so there is no evidence against the company's claim.

Answer: χ2≈4.08 with df=2 and a p-value of about 0.13: fail to reject the company's stated mix.

Takeaway: Divide by Expected, (O−E)2E; the expected counts come from the null, never from the data.

Where You'll See It

  • Mendel's pea plants

    Genetics predicts a 3:1 ratio of purple to white flowers in certain crosses, so among 400 plants the expected counts are 300 and 100. A student who grows 290 purple and 110 white gets χ2=102300+102100≈1.33 with df=1, well below the 3.84 cutoff, so the data fit the 3:1 model.

  • Homework preference by grade

    A school asks 200 students in two grades whether they prefer online or paper homework. A chi-square test of independence on the 2×2 table of counts tells whether preference depends on grade, or whether the differences between grades are the size chance alone would produce.

  • Auditors checking first digits

    Benford's law predicts that about 30% of amounts in many real financial records begin with the digit 1. Auditors compare the first digits of a company's invoices with Benford's percentages using a chi-square goodness-of-fit test across the nine digits (df=8), and books that stray more than chance allows get a closer look.

Common Mistakes

  1. The slip: Dividing by Observed instead of Expected

    Instead: the formula is (O−E)2E, scaling each gap by the expected count.

  2. The slip: Using percentages or means as the data

    Instead: chi-square requires raw category counts, not proportions or averages.

  3. The slip: Using the wrong degrees of freedom

    Instead: goodness-of-fit uses df=k−1; a two-way independence test uses df=(r−1)(c−1).


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A spinner has 4 equal sections and is spun 200 times. If it is fair, what is the expected count for each section?

    Hint: Each section should get an equal share of the spins.

  2. That 4-section spinner, spun 200 times, lands 60, 45, 55 and 40 times. Compute χ2 for a fair spinner and compare it to the critical value 7.81 (df=3, α=0.05).

    Hint: Each expected count is 50. Square each gap, divide by 50, then add.

  3. In a two-way table with 2 rows and 3 columns (grand total 120), one cell has row total 40 and column total 30. Find that cell's expected count and the test's degrees of freedom.

    Hint: Expected count is row total times column total over grand total.

  4. A goodness-of-fit test checks a die using 5 categories of outcomes. What are the degrees of freedom?

    Hint: Goodness-of-fit uses the number of categories minus one.

  5. 150 students (7th and 8th grade) each pick cats, dogs or neither. You want to know whether pet preference depends on grade. Chi-square test or two-proportion z-test?

    Hint: How many categories does the response have, and are the data counts?

  6. A category has observed count 30 and expected count 20. Max computes its piece of χ2 as (30−20)230≈3.33. Correct his work.

    Hint: Check which count belongs in the denominator.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why square the differences instead of just adding them?

Because the raw differences O−EO - E always add up to zero: observed and expected counts share the same total, so an excess in one category is balanced by a shortfall elsewhere. Squaring makes every gap positive and also punishes big gaps more than small ones. Dividing by EE then scales each gap, since being off by 10 matters far more when you expected 12 than when you expected 1,000.

Can χ2 ever be negative?

No. Each term (O−E)2E\frac{(O-E)^2}{E} is a square divided by a positive count, so the smallest possible total is 0, reached only when every observed count equals its expected count exactly. That is why the chi-square distribution starts at 0 and is skewed right, and why the p-value always comes from the right tail: only large values count as evidence against the null hypothesis.

Where do the expected counts in a two-way table come from?

If the two variables were independent, each cell would get its fair share of both margins: expected count =row total×column totalgrand total= \frac{\text{row total} \times \text{column total}}{\text{grand total}}. If 40 of 100 students are 9th graders and 30 of the 100 prefer soccer, independence predicts 40×30100=12\frac{40 \times 30}{100} = 12 ninth graders who prefer soccer. The test then asks whether the observed counts stray from these predictions more than chance explains.

Can a chi-square result be too good to be true?

Yes. A χ2\chi^2 close to 0 means the counts match the prediction almost perfectly, and real random data rarely do that. In 1936 the statistician Ronald Fisher reanalysed Gregor Mendel's famous pea experiments and found that the results fit Mendel's predicted ratios more closely than chance usually allows. Historians still argue about why, but the case shows that a suspiciously small χ2\chi^2 can be a warning sign too.

Learning Path

Chi-Square Test

You are here

Next →

Nothing after this yet
You need Hypothesis Testing, the P-Value and Probability (to compute expected counts) first. The chi-square test extends testing from means and single proportions to whole tables of category counts, using one statistic, ∑(O−E)2E. With it you can test whether dice, genetic crosses or survey answers match a model, and whether two categorical variables are related, which is the starting point for analyzing survey tables.

See Also