Why the Number of Categories Is Not the Degrees of Freedom
A goodness-of-fit question compares observed category counts with counts predicted by a specified probability model. With \(k\) categories, it may seem that there are \(k\) separate ways for the counts to differ from expectation. But the total number of observations is fixed: if some categories have more observations than expected, others must have fewer. That restriction means only \(k-1\) count deviations can vary independently.
This tutorial derives that result for a model whose null probabilities are fixed in advance. The degrees of freedom determine which chi-square reference distribution is associated with the Pearson statistic. They do not count the observations, nor do they count the number of categories without accounting for constraints.
The Fixed-Probability Model
Suppose each of \(n\) independent observations belongs to exactly one of \(k\) mutually exclusive, collectively exhaustive categories. Under the null model, the probability of category \(i\) is a known, fixed value \(p_i\), where each \(p_i>0\) and the probabilities sum to one. Write \(O_i\) for the observed count in category \(i\). The count vector follows a multinomial model with sample size \(n\) and probability vector \((p_1,\ldots,p_k)\).
The expected count in category \(i\) under this model is \(E_i=np_i\). The deviation of the observed count from its expected count is \(O_i-E_i=O_i-np_i\). Pearson's goodness-of-fit statistic sums the squared deviations divided by their expected counts:
The question here is not how to calculate a p-value, but why this statistic is associated with \(k-1\) degrees of freedom when the null probabilities are fixed. To answer it, first examine the count constraint.
The Constraint on the Count Deviations
The observed counts must add to the total sample size, so \(\sum_{i=1}^{k}O_i=n\). The expected counts also add to \(n\), because \(\sum_{i=1}^{k}np_i=n\sum_{i=1}^{k}p_i=n\). Subtracting these totals gives:
Once the deviations for the first \(k-1\) categories are known, the last deviation is determined: \(O_k-np_k=-\sum_{i=1}^{k-1}(O_i-np_i)\). Therefore, the deviations cannot provide \(k\) independent pieces of information. Their possible values lie in a \((k-1)\)-dimensional space.
Worked Example: Four Categories and One Constraint
Imagine 100 hypothetical observations classified into four categories. A fixed null model assigns probabilities \(0.10,\ 0.20,\ 0.30,\) and \(0.40\). Suppose the observed counts are \(8,\ 22,\ 28,\) and \(42\). Find the expected counts, the deviations, and the degrees of freedom.
The expected counts are \(np_i\): \(100(0.10)=10\), \(100(0.20)=20\), \(100(0.30)=30\), and \(100(0.40)=40\). The expected counts sum to 100, as they must.
Subtract expected counts from observed counts to obtain the deviations:
Their sum is \(-2+2-2+2=0\). If the first three deviations are specified, the fourth must be their negative sum. There are four categories but only three independent deviations, so the degrees of freedom are \(4-1=3\).
For illustration, the Pearson statistic is \(4/10+4/20+4/30+4/40=0.4000+0.2000+0.1333+0.1000=0.8333\). This calculation uses all four categories, but the count constraint means its reference degrees of freedom are three, not four.
Why the Pearson Statistic Has a Chi-Square Distribution with \(k-1\) Degrees of Freedom
The constraint gives an intuitive count of the independent deviations. A short asymptotic derivation explains why that count becomes the degrees of freedom of the chi-square reference distribution. Define a standardized deviation for each category by \(Z_i=(O_i-np_i)/\sqrt{np_i}\). Then Pearson's statistic is the sum of the squared standardized deviations, \(X^2=\sum_i Z_i^2\).
The standardized deviations also obey a constraint. Multiplying each \(Z_i\) by \(\sqrt{p_i}\) and summing gives:
In vector terms, the vector of standardized deviations is restricted to the subspace perpendicular to \(v=(\sqrt{p_1},\ldots,\sqrt{p_k})\). Since \(\sum_i p_i=1\), the vector \(v\) has length one. Its perpendicular subspace has dimension \(k-1\).
For completeness, under the fixed multinomial model, the covariance between two counts is \(n(p_i-p_i^2)\) when \(i=j\), and \(-np_ip_j\) when \(i\ne j\). After standardizing by \(\sqrt{np_i}\) and \(\sqrt{np_j}\), these entries form the matrix \(I-vv^\mathsf{T}\): its diagonal entries are \(1-p_i\), and its off-diagonal entries are \(-\sqrt{p_ip_j}\). This matrix projects onto the subspace perpendicular to \(v\) and has rank \(k-1\).
As \(n\) grows, the multinomial central limit theorem gives an approximately multivariate normal distribution for the standardized deviations on that subspace. Choose orthonormal coordinates within it. There are \(k-1\) such coordinates, each approximately standard normal, and the sum of their squares is the Pearson statistic. A chi-square distribution with \(k-1\) degrees of freedom is, by definition, the distribution of a sum of squares of \(k-1\) independent standard normal variables. Thus the fixed-probability Pearson statistic has an approximate chi-square reference distribution with \(k-1\) degrees of freedom when the approximation is appropriate.
This argument explains both parts of the result: the total-count constraint removes one dimension, and the squared standardized deviations give the chi-square form. The chi-square calibration is an approximation, not an exact claim that every finite-sample statistic has that distribution.
Worked Example: Five Categories with Unequal Fixed Probabilities
Suppose 120 hypothetical observations are assigned to five categories. Under a specified fixed model, the category probabilities are \(0.25,\ 0.25,\ 0.20,\ 0.20,\) and \(0.10\). Observed counts are \(32,\ 28,\ 22,\ 26,\) and \(12\). Find the degrees of freedom and verify the constraint.
The probabilities sum to one. The expected counts are \(120(0.25)=30,\ 30,\ 120(0.20)=24,\ 24,\) and \(120(0.10)=12\). The observed counts sum to 120, and the expected counts also sum to 120.
The deviations are \(2,\ -2,\ -2,\ 2,\) and \(0\). Their sum is \(2-2-2+2+0=0\). Four of the five deviations can vary independently; the fifth is fixed once those four are known. Therefore \(df=5-1=4\).
As a check, Pearson's statistic is:
The statistic has contributions from five categories, but the degrees of freedom are four because the five deviations are linked by their zero-sum constraint.
Degrees of Freedom in Practice
For a fixed-probability goodness-of-fit model, the calculation is direct: count the categories represented in the model and subtract one. For example, a model with six categories has \(df=6-1=5\), whether its fixed probabilities are equal or unequal. The probability values affect the expected counts and the statistic, but they do not change the number of independent count deviations.
Worked Example: Six Categories in a Fixed Model
A hypothetical classification has six possible outcomes with fixed probabilities \(0.05,\ 0.10,\ 0.15,\ 0.20,\ 0.20,\) and \(0.30\). There are 400 independent observations. Observed counts are \(22,\ 38,\ 62,\ 78,\ 79,\) and \(121\). Determine the expected counts and degrees of freedom.
The fixed probabilities sum to one. Multiplying each by 400 gives expected counts \(20,\ 40,\ 60,\ 80,\ 80,\) and \(120\), which sum to 400. The observed counts also sum to \(22+38+62+78+79+121=400\).
The deviations are \(2,\ -2,\ 2,\ -2,\ -1,\) and \(1\), whose sum is zero. Therefore only five of the six deviations can vary independently, and \(df=6-1=5\). The unequal probabilities do not alter this count of independent deviations.
The word fixed matters. In this setting, the probabilities \(p_i\) are specified by the null model rather than estimated from the observed counts. The result derived here applies to that setting. If probabilities are estimated from the same data, the model has additional fitted quantities and the degrees-of-freedom calculation requires separate treatment.
Common Mistakes and Why the Distinction Matters
- Using \(k\) degrees of freedom just because there are \(k\) categories. This ignores the fact that the counts must total \(n\). The deviations sum to zero, leaving only \(k-1\) independent deviations.
- Using \(n-1\) degrees of freedom. That formula belongs to other settings, not this categorical count constraint. Here the degrees of freedom are determined by the number of categories and the one total-count constraint.
- Thinking unequal probabilities change the answer. Unequal fixed probabilities change expected counts, but the deviations still sum to zero. The result remains \(k-1\).
- Assuming a chi-square approximation is exact for every sample size. The derivation uses an asymptotic normal approximation. The statistic's degrees of freedom and the adequacy of its calibration are distinct issues.
Getting the degrees of freedom right matters because the chi-square reference distribution changes with its degrees of freedom. Choosing \(k\) rather than \(k-1\) therefore uses the wrong reference curve, even if the observed counts and the Pearson statistic have been calculated correctly.
Check Your Understanding
Answer each question by considering the count constraint, not just the number of categories.
- A fixed null model has seven categories. How many degrees of freedom does its Pearson goodness-of-fit statistic have, and why?
- Why must the deviations \(O_i-np_i\) sum to zero?
- A model has four categories with unequal but fixed probabilities. Does the unequal probability vector change the degrees of freedom? Explain.
- In a six-category model, what determines the sixth count deviation once the first five deviations are known?
- Why is \(k-1\) the degrees of freedom even though the Pearson statistic includes a term for every one of the \(k\) categories?