Solution (source code)

= Solution

The count vector has a <multinomial distribution>, with <likelihood function>
$$
L(\pi)=\frac{n!}{\prod_i n_i!}\prod_{i=1}^k\pi_i^{n_i},
\qquad\sum_i\pi_i=1.
$$
Maximize its <log-likelihood> using a <Lagrange multiplier> for the sum constraint. The equations $n_i/\pi_i=\lambda$ and $\sum_i\pi_i=1$ give $\lambda=n$ and $\widehat\pi_i=n_i/n$. Since all counts are positive and the <log-likelihood> is strictly concave on the interior of the probability simplex, this is the global <maximum-likelihood estimate>.

Under the uniform <null hypothesis>, each category has expected count
$$
\boxed{e_i=n\pi_{i,0}=\frac nk.}
$$
The common multinomial coefficient cancels from the <likelihood ratio>:
$$
R=\frac{k^{-n}}{\prod_i(n_i/n)^{n_i}}
=\prod_{i=1}^k\left(\frac{e_i}{n_i}\right)^{n_i}.
$$
Taking minus twice its logarithm gives exactly
$$
\boxed{\Lambda=2\sum_{i=1}^k n_i\log\!\left(\frac{n_i}{e_i}\right).}
$$
There are $k-1$ free category probabilities, because their sum is one, and the null fixes all of them. Thus its regular asymptotic <chi-squared distribution> has $k-1$ <statistical degrees of freedom>.