The chi-square test is used to test how well a certain number of outcomes are following a given distribution.
The binomial and normal distributions are two specific distributions. Once you decide that your problem is modelled by one of them you can use them to calculate theorical statistics such as average or percentile values.
If you need to model the outcomes of a die, then you would use a discrete uniform distribution, where all the outcomes are equally probable and with probability of 1/6.
Just to expend on it and give the all the code.
Setting parameters
N=100; % sample size
a=0; % lower boundary
b=1; % higher boundary
Sample N uniformly distributed values between a and b. And in the second line add some bais to make it not uniform if you want to test the code.
x=unifrnd(a,b,N,1);
%x(x<.9) = rand(sum(x<.9),1);
As described here, with chi2gof
, you can't use the 'cdf of the hypothesized distribution' and need to specified the bins, the edges and the expected values.
nbins = 10; % number of bin
edges = linspace(a,b,nbins+1); % edges of the bins
E = N/nbins*ones(nbins,1); % expected value (equal for uniform dist)
[h,p,stats] = chi2gof(x,'Expected',E,'Edges',edges)
With this function you need to supply the chi-squared test statistic, $\displaystyle \chi ^{2}$ which can be computed with the function histogramm
:
h = histogram(x,edges);
chi = sum((h.Values - N/nbins).^2 / (N/nbins));
k = nbins-1; % degree of freedom
chi2cdf(chi, k)
Note, that if you don't use the edges to compute the number of value per bins, histogramm
will choose them from the lower value to the highest and therefore the final score will be different than with chi2gof
Quick Theory recall
Just to recall how to interpret the final value, few definition:
- The null hypothesis ($H_0$) is that the data
x
are coming from a uniform distribution.
- Pearson's chi-squared test is testing if you can safely reject the null hypothesis, i.e. "Can I say that
x
is not a coming from a uniform distribution ? "
- Pearson's cumulative test statistic $\displaystyle \chi ^{2}$ is a measure of the error between observations and expected value
$$\displaystyle \chi ^{2} = \sum_{i=1}^N \frac{(O_i-E_i)^2}{E_i}$$
- Chi-squared distribution is the distribution that the Pearson's cumulative test statistic $\displaystyle \chi ^{2}$ would follow according $H_0$ (i.e. if the observation are coming from a uniform distribution)
- The p-value is the probability of obtaining a worst result than what was observation, when the null hypothesis is true. That is, the probability that, if we randomly draw a dataset
y
in a uniform distribution, the error (or Pearson's cumulative test) will be equal of higher than the actual observation x
(=worst case).
- So, we can reject $H_0$ if
p
is lower than a significant level $\alpha$. That is, for small value of p
, we can safely say that x
is not coming from a uniform distribution.
Best Answer
I think I finally found one in this document, page 2.
Theorem (Pearson): The random variable
$$\sum_{j=1}^n \frac{(\nu_j-n p_j)^2}{n p_j} \rightarrow \chi_{r-1}^2$$ converges in distribution to $\chi_{r-1}^2$-distribution with $r − 1$ degrees of freedom.