a revisit to χ2-test

hantian

February 11, 2025

in high-school statistics, χ2-test is a very important element that enriches the students’ power to put statistics in real life scenarios. however, it is rarely explained from the scratch except for a few words of ‘the central limit theorem’. where and how the clt is used, and how come the degree of freedom is one less that the number of categories? this is so unsatisfactory.

1 goodness of fit

for categorical goodness-of-fit cases, the number of observation in each discrete case would be binomially distributed with parameter Oi B(n,pi) where n is the size of the sample. applying the normal approximation:

Zi = Oi npi npi N(0,1 pi).

however, it should be noted that Zi and Zj are not independent, as an observation can not be in two categories simultaneously. we can, however, find the covariance between Zi and Zj:

Cov(Zi,Zj) = E(ZiZj) = E(OiOj) npi pj npi pj.

where we can show that

E(OiOj) = E (OiE(Oj|Oi)) = npi(n 1)pj.

similar arguments can be made for test of independence.

notice that Cov(ZiZj) = pi pj, or

Z = (Zi)T N(0,Σ).

where

Σ = ( 1 p1 p1 p2 p1 pn p1 p2 1 p2 p2 pn p 1pn p2 pn 1 pn ) = IpTp

where p = (pi)T is a unit vector by definition. this tells us that Σ has n 1 eigenvalues of 1 and one eigenvalue of 0, or equivalently put: there is a orthonormal transformation A such that

AΣAT = ( In1 0 0 0 ).

this result is also known as the sylvester’s theroem.

this means the components of AZ, except for the last 0, have independent standardised normal distribution. measure the vector Z in this frame:

|Z|2 = |AZ|2 = i=1n1X i2.

finally we have

i=1n(Oi Ei)2 Ei χn12.

2 central limit theorem

however, in the above, the so-called ‘normal approximation’ is rather suspicious…the core of the argument lies in the well known central limit theorem. what is it and what are the conditions of it?

the central limit theorem states that if a random variable X is distributed with mean μ and standard deviation σ, then for a independant random sample of X, we would have the following:

Z = X¯ μ σ n N(0,1).

that is, the average error from the population distribution would be normally distributed and as the size of the sample increases, the deviation shrinks down. this is essentially the theoretical basis for multiple measurements to imporve the precision. however, the normality is rather intriguing, as the theorem makes no assumption on the population distribution as long as it has valid expectation and variance.

how come that the err automatically converges to a normal distribution? well, the proof is quite bland, we look for its density function.

talking about convolutions, generating function is the magic words one want to spell. remember that the generating function is defined by:

MX(t) = E (etX) .

notice that M(0) = E(e0) = 1, M(0) = E(X) = μ and M(0) = E(X2) = σ2 + nμ2.

readers with a bit exposure to the fourier analysis will easily identify that the generating function is actually a fourier transform of the density function:

M(t) =f(x)ext dx.

if Xi’s are i.i.d., then we would have E ( ieXi) = iE (eXi), that is

MX¯(t) = iMXi n (t) = (MX ( t n ))n.

notice that t n 0 when n , thus we can approximate the logarithm of M(t) by taylor expansion:

mX¯(t) = ln MX¯(t) = nln (MX ( t n )).

thus,

mX¯ = n (ln 1 + 1 M(0)M(0) t n + 1 2 M (0)M(0) M(0)2 M(0)2 t2 n2 + ).

that is

mX¯ = μt + 1 2n(σ2 + μ2 μ2)t2 + 1 n() = μt + 1 2 ( σ n )2t2 + O (1 n )

to recover the density function from a moment generating function would require an inverse transformation whose difficulty may vary depending on the actual format of M(t). nevertheless, since we are trying to prove the central limit theorem, we know which distribution to look up, it would be a good idea to compare the moment generating function, or rather, the logarithmic moment generating function to that of a normal distribution.

the density function of Y N(μ,σ2) is

f(y) = 1 2πσe(yμ)2 2σ2 .

thus

mY (t) = μt + 1 2σ2t2.

this proved the claim that X¯ N (μ, σ2 n ), and finally we can have the ‘normal approximation’.

3 one more thing

it should always be noted that aforementioned logics only applies if the distribution is discrete, for continuous distributions, χ2 test is not the go-to solution. then, what is the counterpart of it in the continuous population?

the central problem lies in that without categories, we no longer have any prior information of the distribution of observation. that is, we are no longer sure if the observation is normal, or asympototically normal, the question switched from a parametric problem to a non-parametric one.

a simple example can be: given the sample from i.i.d. Xi, i = 1,2,,n, we want to test the claim:

H0: X D.

where D is some known distribution.

3.1 q-q plot

q-q plot is a great way to visualise the case. by sorting the sample to X(1) X(2) X(n), we are not losing any information. notice that by null hypothesis, aX(i) + b should be somewhere around either

1.
the i-th quantile of D,
2.
the expected value of Y (i) from D.

plot (X(i),Y (i)) on a plane, we are expecting to see a linear correlation if the null hypothesis is true. thus, by looking at the correlation of the two sequence we can decide the statistic inference. this would be a topic in our future discussion though.

the test is known as shapiro-francia test when D is a normal distribution.