I need to test random numbers generators in 1, 2 and 3 dimensions. Criteria of test is that generated numbers are from uniform distribution. I am doing this using Matlab. I'm told to use Chi-2 test statistic $$\chi^2=\sum^B_{i=1}\frac{(expected(i)-observed(i))^2}{expected(i)}$$ where $B$ is number of bins, $expected(i)=\frac{N}{B}$ for all $i$ is expected number of elements in $i_{th}$ bin and $N$ is number of generated numbers.
What I have done is that I generated
- $N\times 1$ matrix of random numbers for $1D$
- $N\times 2$ matrix of random numbers for $2D$
- $N\times 3$ matrix of random numbers for $3D$
Then, I set a number of bins (B) for each component of matrix elements and I created zero matrices of sizes
- $1\times B$ for $1D$
- $B\times B$ for $2D$
- $B\times B\times B$ for $3D$
and, using loops, I counted how many generated numbers are in which bin and those were, actually, values $observed(i)$. At this point I am able to calculate $\chi^2$ test statistic for all 3 dimensions.
I know how to read $\chi^2$ table but I don't know how to check significance using Matlab. I'm also not sure about degrees of freedom, but I think it's $B-1$ for $1D$, $BB-1$ for $2D$ and $BBB-1$ for $3D$ since we don't have to estimate any parameter for uniform distribution. I would appreciate some help. Thanks!
That all sounds right; looks like you're almost done. All you need to do now is call
chi2cdf(x,v,'upper')where $x$ is your $\chi^2$ test statistic and $v$ is your degrees of freedom; this will output the P-value for the test.