A goodness-of-fit test checks whether data are consistent with a stated model. The data are counts in categories, and the model gives the probability of each category. Typical questions are whether defects are equally likely on all shifts, whether a die lands on each face equally often, or whether defect counts per wafer follow a Poisson distribution.
The common recipe, from model to verdict:
The statistic. With $k$ categories, observed counts $O_i$, and expected counts $E_i=np_i$ under the model, the Pearson statistic is:
Here $m$ is the number of parameters that were estimated from the same data. If the model is fully specified in advance, as with equal shares, then $m=0$. If a parameter such as a Poisson mean was fitted, each estimate removes one degree of freedom because it makes the data look more like the model.
Example 1: equal shares. A line logs $120$ defects over four shifts: $32,\,18,\,25,\,45$. If shifts were equally likely, each would have $30$. Then $\chi^2=(2^2+12^2+5^2+15^2)/30\approx13.27$ with $\nu=3$. The $5\%$ critical value is $7.815$ and the p-value is $0.0041$, so equal shares are rejected. The Pearson residuals $(O-E)/\sqrt{E}$ are $0.37,\,-2.19,\,-0.91,\,+2.74$, so the fourth shift has too many and the second too few.
Example 2: a fitted Poisson model. Defect counts on $100$ wafers are: $52$ wafers with none, $24$ with one, $10$ with two, and $14$ with three or more. Counting the last group as three defects each gives a mean of $\hat{\lambda}=0.86$ per wafer (a simplification; exact fitting from grouped data would use maximum likelihood). The Poisson model then expects $42.3,\,36.4,\,15.7,\,5.6$ wafers in the four groups. The cell shares of the statistic are $2.22,\,4.22,\,2.04,\,12.37$, so $\chi^2\approx20.85$. One parameter was estimated, so $\nu=4-1-1=2$, and the p-value is below $0.0001$. The result matches scipy.
The largest share comes from the "3 or more" group, with $14$ wafers observed against $5.6$ expected, plus a surplus of defect-free wafers. A Poisson model assumes defects land independently and at random; this pattern means they cluster. A more flexible model, such as the negative binomial, is the natural next step. Using $\nu=3$ instead of $2$ would give a larger p-value, which is why the degrees-of-freedom rule matters.
Before trusting the result.
- Expected counts: use the approximation only when expected counts are about $5$ or more. Merge sparse tail categories, as the "3 or more" group does, or use an exact or simulated test.
- Fit is not proof: a large p-value shows no evidence against the model. A tiny sample can fit anything, and a huge one will reject every simple model for a trivial reason.
- Continuous data: to test a normal or exponential model, bin the data first or use a test built for continuous data, such as Kolmogorov-Smirnov or Anderson-Darling, which is usually more powerful.
- Inspect residuals: a rejection tells you the model is wrong somewhere, and the cell shares or residuals show where. Report them with the p-value.
- Independent units: each observation must fall in exactly one category and be independent of the others.
Read a goodness-of-fit test as asking whether the data look like something the model could have produced, with each category's mismatch added up in noise units. A small p-value says the model misses something; the biggest cell shares say what.