Chi-square test explained: 3 tests, one formula

By Jude Wallis · Published

All three chi-square tests use the same statistic: the sum of (observed - expected) squared over expected. Goodness-of-fit tests one variable against claimed proportions; independence tests whether two variables are associated in one sample; homogeneity tests whether groups share one distribution.

AP Statistics: Unit 3 (topics 3.14 Setting Up a Chi-Square Test for Homogeneity or Independence, 3.15 Carrying Out a Chi-Square Test for Homogeneity or Independence). Chi-square tests for homogeneity and independence are AP Statistics Unit 3 topics 3.14 to 3.15; the goodness-of-fit test was removed from the AP exam starting with the Fall 2026 course but remains standard college content.

Chi-square test explained: one formula, three questions

A chi-square test compares the counts you actually observed with the counts you would expect if some claim were true. The Greek symbol chi-square (χ2\chi^2) names both the test statistic and the distribution you compare it against. Every version of the test uses the same formula, which appears on the AP formula sheet:

χ2=(ObservedExpected)2Expected\chi^2 = \sum \frac{(\text{Observed} - \text{Expected})^2}{\text{Expected}}

Here Observed (call it O) is the count in a category from your data, and Expected (call it E) is the count the null hypothesis predicts for that category. You work out one term for every cell, then add the terms together. A large χ2\chi^2 means the observed counts sit far from what the claim predicts, which is evidence against the claim.

All three versions, goodness-of-fit, independence, and homogeneity, work on categorical data, meaning counts in categories rather than averages of a number. They differ only in the question they answer and in how you build the expected counts.

When to use a chi-square test

Use a chi-square test when your data are counts in categories and you want to compare them to a model. The choice among the three versions comes down to how many categorical variables you have and how many samples you drew.

  • Goodness-of-fit: one categorical variable from one sample. You test whether its distribution matches a set of claimed proportions, for example whether a six-sided die is fair.
  • Test for independence: two categorical variables measured on one sample. You test whether the two variables are associated, for example housing type and pet ownership among 100 surveyed people.
  • Test for homogeneity: one categorical variable measured across two or more separate groups. You test whether the groups share the same distribution, for example whether three schools have the same mix of grade levels.

If you are not sure a chi-square test fits your situation at all, compare it against the alternatives in which statistical test to use. Chi-square is for categorical counts, not for a single mean or the difference between means.

Chi-square goodness-of-fit vs independence vs homogeneity

The three tests answer different questions but share most of their machinery. This table lines them up side by side.

TestData setupNull hypothesisExpected countDegrees of freedom
Goodness-of-fitOne variable, one sampleDistribution matches claimed proportionssample size n×n \times claimed proportioncategories 1- 1
IndependenceTwo variables, one sampleThe two variables are independent(row total ×\times column total) / grand total(rows 1- 1)(columns 1- 1)
HomogeneityOne variable, 2 or more samplesAll groups share one distribution(row total ×\times column total) / grand total(rows 1- 1)(columns 1- 1)

Independence and homogeneity run identical arithmetic on a two-way table and give the same statistic and p-value from the same numbers. The difference is the design: independence starts from one sample split two ways, while homogeneity starts from separate samples you are comparing. You pick the label from how the data were collected, not from the calculation.

Expected counts in a chi-square test

An expected count is how many observations would land in a cell if the null hypothesis were exactly true. For a two-way table (independence or homogeneity), each expected count comes from its own row and column totals:

E=(row total)×(column total)grand totalE = \frac{(\text{row total}) \times (\text{column total})}{\text{grand total}}

This follows from assuming the variables are independent. Under independence, the probability of a cell equals the row probability times the column probability, and multiplying that probability by the grand total gives the expected count for the cell.

A goodness-of-fit test has no two-way table. You multiply the sample size by each claimed proportion: E=n×p0E = n \times p_0, where p0p_0 is the proportion the null hypothesis assigns to that category. For a fair die rolled 60 times, each face has E=60×16=10E = 60 \times \frac{1}{6} = 10.

Expected counts do not have to be whole numbers. A value like 24.6 is fine, and you keep the decimals when you plug into the chi-square formula. The chi-square calculator builds the expected-count table for you and adds the terms.

Degrees of freedom and conditions

Degrees of freedom (df) set which chi-square curve you compare your statistic to. The rules split by test type:

  • Goodness-of-fit: df=(number of categories)1df = (\text{number of categories}) - 1.
  • Independence and homogeneity: df=(rows1)(columns1)df = (\text{rows} - 1)(\text{columns} - 1), counting only the category rows and columns, not the total row or total column.

Before you trust the result, check three conditions:

  • Random: the data come from a random sample or a randomized experiment.
  • Independent observations: when sampling without replacement, the sample is less than 10% of the population.
  • Large counts: every expected count is at least 5. This common guideline keeps the chi-square curve a good approximation. If some expected counts fall below 5, combine categories or gather more data.

Check expected counts against the 5 threshold, not observed counts. Once you have the statistic and df, turn them into a probability with the p-value calculator or a chi-square table.

What the AP exam includes (Fall 2026 onward)

On the redesigned AP Statistics course (effective Fall 2026, first exam May 2027), chi-square tests for homogeneity and independence are Unit 3 topics 3.14 and 3.15, inside Inference for Categorical Data: Proportions. The chi-square formula is printed on the formula sheet you get during the exam.

The goodness-of-fit test was removed from the AP exam starting with the Fall 2026 course. It is still standard content in a one-semester college introductory statistics course, so it is worth learning even though it will not be tested on the AP exam. You can confirm the current course scope on the College Board AP Statistics page.

Chi-square test for independence on a 2x2 table

A researcher surveys 100 randomly chosen residents and records each person's housing type (house or apartment) and whether they own a pet. The counts are shown below. Test at the 0.05 level whether housing type and pet ownership are independent.

Owns petNo petTotal
House402060
Apartment202040
Total6040100
  1. State the hypotheses. The null hypothesis H0H_0: housing type and pet ownership are independent. The alternative hypothesis HaH_a: housing type and pet ownership are associated (not independent).

  2. Check the conditions. The 100 residents are a random sample, and 100 is less than 10% of all residents, so observations are independent. The large-counts condition is checked with the expected counts computed next.

  3. Compute the expected counts using E=(row total×column total)/grand totalE = (\text{row total} \times \text{column total}) / \text{grand total}. House and owns pet: (60×60)/100=36(60 \times 60)/100 = 36. House and no pet: (60×40)/100=24(60 \times 40)/100 = 24. Apartment and owns pet: (40×60)/100=24(40 \times 60)/100 = 24. Apartment and no pet: (40×40)/100=16(40 \times 40)/100 = 16.

  4. Confirm the large-counts condition. The expected counts are 36, 24, 24, and 16, and all four are at least 5, so the condition holds.

  5. Compute each (ObservedExpected)2/Expected(\text{Observed} - \text{Expected})^2 / \text{Expected} term. House, owns: (4036)2/36=16/36=0.4444(40 - 36)^2 / 36 = 16/36 = 0.4444. House, no pet: (2024)2/24=16/24=0.6667(20 - 24)^2 / 24 = 16/24 = 0.6667. Apartment, owns: (2024)2/24=16/24=0.6667(20 - 24)^2 / 24 = 16/24 = 0.6667. Apartment, no pet: (2016)2/16=16/16=1.0000(20 - 16)^2 / 16 = 16/16 = 1.0000.

  6. Add the four terms to get the statistic: χ2=0.4444+0.6667+0.6667+1.0000=2.7778\chi^2 = 0.4444 + 0.6667 + 0.6667 + 1.0000 = 2.7778, which rounds to 2.778.

  7. Find the degrees of freedom: df=(rows1)(columns1)=(21)(21)=1df = (\text{rows} - 1)(\text{columns} - 1) = (2 - 1)(2 - 1) = 1.

  8. Find the p-value. For χ2=2.778\chi^2 = 2.778 with df=1df = 1, a calculator or chi-square table gives p0.096p \approx 0.096. The statistic falls between the df=1df = 1 critical values 2.706 (for 0.10) and 3.841 (for 0.05), so the p-value is between 0.05 and 0.10.

  9. Make the decision. Since p0.096p \approx 0.096 is greater than 0.05, you fail to reject H0H_0.

χ2=2.778\chi^2 = 2.778 with df=1df = 1 and p0.096p \approx 0.096. Because 0.096 is greater than 0.05, you fail to reject the null hypothesis. There is not enough evidence to conclude that housing type and pet ownership are associated.

Frequently asked questions

Do the tests for independence and homogeneity use the same formula?

Yes. Both use the same chi-square statistic, the same expected-count formula (row total times column total divided by grand total), and the same degrees of freedom, (rows1)(columns1)(\text{rows} - 1)(\text{columns} - 1). They differ only in how the data were collected: independence uses one sample split by two variables, while homogeneity compares two or more separate samples.

What should you do if an expected count is below 5?

Combine adjacent categories so the merged cell has an expected count of at least 5, or collect more data. The large-counts condition applies to expected counts, not observed counts, so check the expected values before running the test. If you cannot meet it, the chi-square approximation may be unreliable.

Is the chi-square goodness-of-fit test on the AP Statistics exam?

No. The goodness-of-fit test was removed from the AP exam starting with the Fall 2026 course (first exam May 2027). The tests for homogeneity and independence stayed, as Unit 3 topics 3.14 and 3.15. Goodness-of-fit is still standard in college introductory statistics, so it is worth knowing.