Solution (source code)

= Solution

The unrestricted likelihood is maximized at the empirical proportions, while the null likelihood is maximized at $\pi_i(\widehat\theta)$. Twice the log of their ratio is
$$
\boxed{\Lambda(\widehat\theta)=2\sum_i n_i\log\frac{n_i}{n\pi_i(\widehat\theta)}.}
$$
Large values mean the fitted null likelihood is much smaller than the unrestricted maximum, so rejection is in the upper tail.

For a regular identifiable scalar parameter lying in the interior, positive true cell probabilities, and sufficiently large expected counts, <Wilks theorem> gives an approximate <chi-squared distribution> with $(k-1)-1=k-2$ degrees of freedom under the null. The unrestricted <probability> <vector> has dimension $k-1$ and the fitted null has dimension one. At significance level $\alpha$, reject if $\Lambda(\widehat\theta)>F_{k-2}^{-1}(1-\alpha)$, or report the approximate p-value $1-F_{k-2}(\Lambda)$.

Cells with very small expected counts may require a prespecified pooling scheme or simulation calibration; the asymptotic reference is not an exact finite-sample law. For a regular null with $d$ fitted parameters, the corresponding degrees of freedom are $k-1-d$.