# Applications of Chi-Square Tests

This morning, in our mathematical statistical class, we’ve seen the use of the chi-square test. The first one was related to some goodness of fit of a multinomial distribution. Assume that $\boldsymbol{N}=(N_1,\cdots,N_k)\sim\mathcal{M}(n,\boldsymbol{p})$. In order to test $H_0:\boldsymbol{p}=\boldsymbol{p}_0$ against $H_1:\boldsymbol{p}\neq\boldsymbol{p}_0$, use the statistic

$Q=\sum_{j=1}^k \frac{[N_j-np_{0,j}]^2}{np_{0,j}}$Under $H_0$$Q\sim\chi^2(k-1)$. For instance, we have the number of weddings, in a large city, per season,

> n=c(301,356,413,262)

We want to test if weddings are celebrated uniformely over the year, i.e. $H_0:\boldsymbol{p}=\boldsymbol{1}/4$.

> np=rep(sum(n)/4,4)
> cbind(n,np)
n  np
[1,] 301 333
[2,] 356 333
[3,] 413 333
[4,] 262 333
> Q=sum( (n-np)^2/np  )
> Q
[1] 39.02102

This quantity should be compared with the quantile of the chi-square distribution

> qchisq(.95,df=4-1)
[1] 7.814728

but it is also possible to compute the p-value,

> 1-pchisq(Q,df=4-1)
[1] 1.717959e-08

Here, we reject the assumption that weddings are celebrated uniformly over the year.

# Inference for the Multinomial Distribution

This morning, in our mathematical statistical class, we’ve seen briefly the multinomial distribution, and statistical inference. $\boldsymbol{N}=(N_1,\cdots,N_k)$ has a $\mathcal{M}(n,\boldsymbol{p})$ distribution if its probability function is

$\mathbb{P}(\boldsymbol{N}=\boldsymbol{n})=\frac{n!}{n_1!\cdots n_k!}p_1^{n_1}\cdots p_k^{n_k}$

with $n_1+\cdots+n_k=n$ and $p_1+\cdots+p_k=1$.

The maximum likelihood estimator is then the optimum of

$\widehat{\boldsymbol{p}}=\text{argmax}\{\log\mathcal{L}(\boldsymbol{p}); \boldsymbol{p}^\text{\sffamily T}\boldsymbol{1}=1\}$

We use Lagrange multiplier to solve this constrained optimization problem,

$\widehat{\boldsymbol{p}}=\text{argmax}\{\log\mathcal{L}(\boldsymbol{p})-\lambda[ \boldsymbol{p}^\text{\sffamily T}\boldsymbol{1}-1]\}$

First order conditions are here

$\frac{\partial \log\mathcal{L}(\boldsymbol{p})-\lambda[ \boldsymbol{p}^\text{\sffamily T}\boldsymbol{1}-1]}{\partial p_j}=\frac{n_j}{p_j}-\lambda=0$

and

$\frac{\partial \log\mathcal{L}(\boldsymbol{p})-\lambda[ \boldsymbol{p}^\text{\sffamily T}\boldsymbol{1}-1]}{\partial\lambda}=\boldsymbol{p}^\text{\sffamily T}\boldsymbol{1}-1=0$

Thus,

$\frac{n_j}{\widehat{p}_j}=\widehat{\lambda}$

From

$\sum_{j=1}^k \widehat{p}_j=1$

we can easily get that Lagrande multiplier is $\widehat{\lambda}=n$. And then

$\widehat{p}_j=\frac{n_j}{n}$

One can easily get that this maximum likelihood estimator is unbiased, since $\mathbb{E}[\widehat{\boldsymbol{p}}]=\boldsymbol{p}$. Actually, we can easily prove that

$\mathbb{E}[N_j]=np_j$

and that $\text{Var}[N_j]=np_j(1-p_j)$, while $\text{Cov}[N_j,N_k]=-p_j p_k$. The trick to get the later is simple,

$\text{Var}[N_j+N_k]=\text{Var}[N_j]+\text{Var}[N_k]+2\text{Cov}[N_j,N_k]$

and $\text{Var}[N_j+N_k]=n(p_j+p_k)(1-p_j-p_k)$. Thus, we can easily get the covariance. From that term, we can write that

$\text{Var}[\widehat{\boldsymbol{p}}]=\Sigma$

with

$\Sigma_{j,j}=\frac{p_j(1-p_j)}{n}$

while

$\Sigma_{j,k}=-\frac{p_jp_k}{n}$