The Chi-Square Distribution, Explained
After reading this you can turn a chi-square statistic into a p-value, find the critical value for a given significance level, and know which test each answer belongs to.
What the chi-square distribution is
The chi-square distribution, written \chi^2, describes the sum of squared standard normal variables. If you take k independent values drawn from a standard normal distribution, square each one, and add them up, the total follows a chi-square distribution with k degrees of freedom.
It only takes non-negative values, and it leans to the right. With small degrees of freedom the curve is steep near zero and has a long right tail. As the degrees of freedom grow, the shape drifts toward a symmetric bell.
Here is the hook. Suppose you roll a die 60 times and expect 10 of each face. You get counts that differ from 10. Are those differences just chance, or is the die loaded? The chi-square statistic measures the total gap between observed and expected counts, and the chi-square distribution tells you how big a gap is normal when the die is fair.
When to use it, and when not
Three common tests all produce a statistic that follows a chi-square distribution under the null hypothesis.
- Goodness of fit
- Do observed category counts match a claimed set of proportions? Degrees of freedom equal the number of categories minus 1.
- Test of independence
- Are two categorical variables in a contingency table related? For an r \times c table the degrees of freedom are (r-1)(c-1).
- Variance test
- Does a sample variance match a claimed population variance? Degrees of freedom equal n-1.
Do not use the chi-square approximation when expected counts are tiny. A common rule is that every expected cell count should be at least 5. With expected counts near 1 or 2 the approximation is poor and you should use an exact test instead. The chi-square distribution also assumes independent observations, so repeated measures on the same subjects break it.
The density and its parameter
The probability density function for x \gt 0 is
Here k is the degrees of freedom, \Gamma is the gamma function (a continuous version of the factorial), and x is the value of the statistic. The distribution has mean k and variance 2k. So with k = 4 the average value is 4 and the standard deviation is \sqrt{8} \approx 2.83.
For a hypothesis test you rarely need the density itself. You need the right-tail area, the probability of a statistic at least as large as the one you observed:
where F is the cumulative distribution function. That tail area is your p-value. The chi-square distribution is a special case of the gamma distribution with shape k/2 and scale 2.
Chi-square tests are almost always one-sided on the right. A large statistic means observed and expected differ a lot, which is evidence against the null. Small statistics mean a close fit, which is not evidence against anything.
Worked example: reproducing the demo
A p-value for four degrees of freedom
The demo uses df = 4 and x = 9.488 in p-value mode. This is the classic setup for a goodness-of-fit test with five categories, since 5 - 1 = 4.
- Set the degrees of freedom to 4. The mean of this distribution is 4, so a statistic of 9.488 sits well out in the right tail.
- Compute the cumulative probability up to 9.488. For k = 4 the CDF has a closed form: F(x) = 1 - e^{-x/2}\left(1 + \tfrac{x}{2}\right).
- Plug in x = 9.488: e^{-4.744} \approx 0.008703 and 1 + 4.744 = 5.744, so F \approx 1 - 0.008703 \times 5.744 = 1 - 0.05000 = 0.9500.
- The tail area is p = 1 - 0.9500 = 0.0500.
So x = 9.488 gives a p-value of almost exactly 0.05. That is not a coincidence: 9.488 is the standard critical value for \alpha = 0.05 with 4 degrees of freedom, printed in the back of most statistics texts. Run the demo in critical-value mode with alpha = 0.05 and you get 9.488 back.
How degrees of freedom reshape the curve
Degrees of freedom change both the location and the spread. The mean equals k and the spread grows as \sqrt{2k}, so critical values climb as the categories increase.
| df | α = 0.10 | α = 0.05 | α = 0.01 |
|---|---|---|---|
| 1 | 2.706 | 3.841 | 6.635 |
| 2 | 4.605 | 5.991 | 9.210 |
| 4 | 7.779 | 9.488 | 13.28 |
| 10 | 15.99 | 18.31 | 23.21 |
| 20 | 28.41 | 31.41 | 37.57 |
Read one row across and you see how much extra statistic each smaller significance level demands. Read the \alpha = 0.05 column down and you see the cost of more categories: with 20 degrees of freedom you need a statistic above 31.41 to reach significance.
Reading and interpreting the result
In p-value mode you supply the statistic and read off the tail probability. Compare it to your chosen \alpha. If p \le \alpha you reject the null hypothesis. With the demo, p = 0.05 sits right at the boundary for \alpha = 0.05, so the result is borderline.
In critical-value mode you supply \alpha and read off the cutoff. Any statistic above that cutoff is significant. This is handy when you want the threshold once and then check several samples against it.
The p-value is the probability of a statistic at least this extreme if the null is true. It is not the probability that the null is true. A p-value of 0.05 does not mean a 5 percent chance the die is fair.
Common mistakes
Do not run the chi-square test on percentages, rates or already-averaged numbers. The formula needs raw counts. If you observe 40 percent of 200 people, feed it 80, not 40, and not 0.40.
A second trap is the degrees of freedom. For a contingency table it is (r-1)(c-1), not the total number of cells. A 3 by 4 table has 12 cells but only 2 \times 3 = 6 degrees of freedom. Using 11 instead of 6 would give a critical value of 19.68 rather than 12.59, and you would wrongly fail to reject a real effect.
Third, watch small expected counts. If a cell expects 1.3 observations, the chi-square approximation misreports the tail. Combine sparse categories or switch to an exact test.
Fourth, do not confuse this with a two-tailed test. Except for the variance test, where you can build a two-sided interval from both tails, chi-square tests use the right tail alone.
Related calculators
The chi-square distribution connects to several others on this site. The square of a single z-score follows a chi-square with 1 degree of freedom, which links it to the normal distribution. The ratio of two independent chi-square variables, each divided by its degrees of freedom, gives the F distribution, and a normal divided by a scaled chi-square root gives the t distribution. When you count events rather than measure them, the Poisson and binomial calculators handle the raw counts that often feed a goodness-of-fit test.
Frequently asked questions
Why is my chi-square p-value only one-sided?
Because the statistic is a sum of squared differences, it is always non-negative and larger values always mean a worse fit. All the evidence against the null lives in the right tail, so the standard test is one-sided.
What does a very small chi-square statistic mean?
It means observed and expected values are unusually close. A statistic near 0 with 4 degrees of freedom gives a p-value near 1. That is not evidence for anything, though suspiciously perfect fits sometimes hint at fabricated or rounded data.
How do I get the degrees of freedom?
For goodness of fit use categories minus 1. For a contingency table use (r-1)(c-1). For a variance test use n-1. If you estimated extra parameters to compute expected values, subtract one more per estimated parameter.
Can degrees of freedom be a decimal?
The distribution is defined for any positive real degrees of freedom, so the calculator accepts values like 4.5. In standard tests, though, degrees of freedom come out as whole numbers.
Is chi-square the same as the gamma distribution?
It is a special case. A chi-square with k degrees of freedom is a gamma distribution with shape k/2 and scale 2. That is why the density in this article contains a gamma function.