Chi-square test calculator: 2x2 table and goodness-of-fit

By Jude Wallis · Published

A chi-square test calculator finds each cell's expected count as (row total times column total) / table total, then computes chi^2 = sum of (observed - expected)^2 / expected. For observed 25, 15, 15, 45 the expected counts are 16, 24, 24, 36, so chi^2 = 14.0625 with df = 1.

test for independence / homogeneity (df = 1)

chi-square = 10.5263, p = 0.0012

Smallest expected count: 23.75 (all at least 5, condition met).

Steps

  1. 1.row totals: 70, 50; column totals: 63, 57; grand total: 120
  2. 2.expected count = (row total x column total) / grand total for every cell
  3. 3.expected counts: 36.75, 33.25 | 26.25, 23.75
  4. 4.chi-square = sum of (observed - expected)² / expected = 10.5263
  5. 5.df = (rows - 1)(columns - 1) = 1; right-tail p-value = 0.0012

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). In the Fall 2026 AP Statistics course, chi-square tests for homogeneity and independence are Unit 3 topics 3.14 and 3.15, while the chi-square goodness-of-fit test was removed from the AP exam and now appears only in college introductory statistics.

What a chi-square test measures

A chi-square test compares the counts you actually observed in a table of categories against the counts you would expect if there were no relationship in the data. The chi-square statistic, written χ2\chi^2 (chi-squared), measures how far the observed counts fall from the expected counts, relative to those expected counts. A larger χ2\chi^2 means the observed table sits farther from what the null hypothesis predicts.

Two versions appear on the AP exam, and both run the same arithmetic on a two-way table. A test for independence asks whether two categorical variables are associated within a single population. A test for homogeneity asks whether the distribution of one categorical variable is the same across two or more populations or treatments.

Expected counts for a two-way table

The expected count for a cell is what you would predict if the row variable and the column variable were unrelated. For each cell, multiply its row total by its column total, then divide by the grand total:

expected count=(row total)×(column total)table total\text{expected count} = \frac{(\text{row total}) \times (\text{column total})}{\text{table total}}

Run this for every cell before you compute anything else. Expected counts do not have to be whole numbers, and the expected counts in any row still add up to that row's observed total.

The chi-square statistic and degrees of freedom

Once you have every expected count, apply the formula from the AP formula sheet:

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

Compute one term per cell, where Observed is the count you recorded and Expected is the count you predicted, then add the terms. Squaring each difference makes every term nonnegative, and dividing by the expected count scales each gap by how large it was supposed to be.

For a two-way table, the degrees of freedom are

df=(number of rows1)(number of columns1).df = (\text{number of rows} - 1)(\text{number of columns} - 1).

A 2 by 2 table therefore has df=(21)(21)=1df = (2-1)(2-1) = 1. The chi-square distribution you compare against takes only positive values and is skewed right, with the skew easing as dfdf grows.

Conditions to check first

The AP course requires three conditions before you trust a chi-square test for independence or homogeneity:

  • Randomization: the data come from a random sample for a test of independence, or from independent random samples or a randomized experiment for a test of homogeneity.
  • The 10% condition: when you sample without replacement, the sample size is no more than 10% of the population.
  • Expected counts: every expected count is at least 5. Check the expected counts, not the observed counts.

If an expected count is less than 5, the chi-square approximation gets shaky, so combine categories or collect more data before running the test.

How to use the chi-square calculator

This chi-square calculator has two modes. For a two-way table, enter the observed counts row by row, and the tool fills in the row totals, column totals, expected counts, the χ2\chi^2 statistic, the degrees of freedom, and the p-value.

  • Type your observed counts into the grid, matching your rows and columns.
  • Read the expected-count grid the tool returns, and confirm every expected count is at least 5.
  • Use the reported χ2\chi^2, dfdf, and p-value to make a decision against your significance level.

Because it shows the expected grid and each cell's term, this chi-square calculator lets you retrace the arithmetic by hand. The second mode runs a goodness-of-fit test: enter observed counts and either equal or custom expected proportions for a single categorical variable.

Which version is on the AP exam

The chi-square tests for homogeneity and independence are on the Fall 2026 AP Statistics exam, in Unit 3 topics 3.14 and 3.15. The chi-square goodness-of-fit test was removed from the redesigned AP exam, though it stays standard in college introductory statistics, which is why this calculator still offers it.

For a goodness-of-fit test, the degrees of freedom equal the number of categories minus 1, and the expected count for each category is the total sample size times that category's hypothesized proportion. The statistic uses the same χ2\chi^2 formula.

Chi-square test for independence on a 2 by 2 table

A random sample of 100 high school students is cross-classified by whether they play a school sport and whether they hold a part-time job. Test whether playing a sport and holding a job are independent. These are sample counts.

Sport / JobJob: yesJob: noRow total
Sport: yes251540
Sport: no154560
Column total4060100
  1. State the hypotheses: the null hypothesis H0H_0 is that playing a sport and holding a job are independent for this population; the alternative hypothesis HaH_a is that they are associated.

  2. Find the totals: row totals 40 and 60, column totals 40 and 60, grand total 100.

  3. Expected count, sport yes and job yes: (40×40)/100=1600/100=16(40 \times 40)/100 = 1600/100 = 16.

  4. Expected count, sport yes and job no: (40×60)/100=2400/100=24(40 \times 60)/100 = 2400/100 = 24.

  5. Expected count, sport no and job yes: (60×40)/100=2400/100=24(60 \times 40)/100 = 2400/100 = 24.

  6. Expected count, sport no and job no: (60×60)/100=3600/100=36(60 \times 60)/100 = 3600/100 = 36.

  7. Check the condition: every expected count (16, 24, 24, 36) is at least 5.

  8. Cell term, sport yes and job yes: (2516)2/16=81/16=5.0625(25 - 16)^2/16 = 81/16 = 5.0625.

  9. Cell term, sport yes and job no: (1524)2/24=81/24=3.375(15 - 24)^2/24 = 81/24 = 3.375.

  10. Cell term, sport no and job yes: (1524)2/24=81/24=3.375(15 - 24)^2/24 = 81/24 = 3.375.

  11. Cell term, sport no and job no: (4536)2/36=81/36=2.25(45 - 36)^2/36 = 81/36 = 2.25.

  12. Add the terms: 5.0625+3.375+3.375+2.25=14.06255.0625 + 3.375 + 3.375 + 2.25 = 14.0625.

  13. Degrees of freedom: (21)(21)=1(2-1)(2-1) = 1.

  14. From technology, a χ2\chi^2 of 14.0625 with df=1df = 1 gives a p-value of about 0.0002.

χ2=14.0625\chi^2 = 14.0625 with df=1df = 1 and a p-value near 0.0002. At any usual significance level you reject H0H_0 and conclude that playing a sport and holding a job are associated for this population.

Goodness-of-fit test for a fair die (college stats, not on the AP exam)

A six-sided die is rolled 60 times to test whether it is fair. This is a goodness-of-fit test, which is college statistics content and is not on the AP exam. These are sample counts.

Face123456
Observed812911146
  1. State the hypotheses: H0H_0 is that the die is fair, so each face has probability 1/61/6; HaH_a is that at least one face has a different probability.

  2. Expected count per face: 60×(1/6)=1060 \times (1/6) = 10 for all six faces, and each is at least 5.

  3. Cell terms: (810)2/10=0.4(8-10)^2/10 = 0.4, (1210)2/10=0.4(12-10)^2/10 = 0.4, (910)2/10=0.1(9-10)^2/10 = 0.1, (1110)2/10=0.1(11-10)^2/10 = 0.1, (1410)2/10=1.6(14-10)^2/10 = 1.6, (610)2/10=1.6(6-10)^2/10 = 1.6.

  4. Add the terms: 0.4+0.4+0.1+0.1+1.6+1.6=4.20.4 + 0.4 + 0.1 + 0.1 + 1.6 + 1.6 = 4.2.

  5. Degrees of freedom: number of categories minus 1, so 61=56 - 1 = 5.

  6. From technology, a χ2\chi^2 of 4.2 with df=5df = 5 gives a p-value of about 0.52.

χ2=4.2\chi^2 = 4.2 with df=5df = 5 and a p-value near 0.52. The data do not provide strong enough evidence to conclude that the die is unfair.

Frequently asked questions

What is the difference between a chi-square test for independence and homogeneity?

A test for independence uses one random sample and asks whether two categorical variables are associated in that population. A test for homogeneity uses two or more samples or treatment groups and asks whether one categorical variable has the same distribution across them. The expected counts, statistic, and degrees of freedom are computed the same way; only the setup and conclusion differ.

What are the degrees of freedom for a chi-square test?

For a two-way table used in a test of independence or homogeneity, df=(rows1)(columns1)df = (\text{rows} - 1)(\text{columns} - 1), so a 2 by 2 table has df=1df = 1. For a goodness-of-fit test the degrees of freedom equal the number of categories minus 1.

What if an expected count is less than 5?

The expected counts condition is not met, so the chi-square approximation may be unreliable. Combine adjacent categories to raise the expected counts, or collect more data. Check the expected counts for this rule, not the observed counts.

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

No. The goodness-of-fit test was removed from the Fall 2026 redesigned AP Statistics course, which keeps only the chi-square tests for homogeneity and independence in Unit 3, topics 3.14 and 3.15. Goodness-of-fit is still standard in college introductory statistics, so this calculator includes it.