Function Repository Resource:

ChiSquareRandomnessTest

Source Notebook

Test a sequence of zeroes and ones (or a set of random reals between 0 and 1) for equidistribution and return a p-value

Contributed by: Emmy/Noah Blumenthal
noahb320@gmail.com
emmyb320@bu.edu

ResourceFunction["ChiSquareRandomnessTest"][sequence]

tests a sequence of 0s and 1s, or random reals between 0 and 1, and returns a p-value.

ResourceFunction["ChiSquareRandomnessTest"][sequence,"property"]

tests a sequence and returns a a property of the test.

Details and Options

Properties include:
"TestStatistic"returns the chi square test statistic
"PValue"returns the p-value associated with the test
The bin size for testing random reals is 1/n where n is the sequence length.
The chi-square randomness test works best for larger sequences of numbers.
The chi-square randomness test only tests for equidistribution.

Examples

Basic Examples (3) 

Generate a sequence of random integers and test it for equidistribution:

In[1]:=
sequence = RandomInteger[{0, 1}, 1000];

Visualize the sequence:

In[2]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[2]=

Apply a chi square randomness test:

In[3]:=
ResourceFunction["ChiSquareRandomnessTest"][sequence, "TestStatistic"]
Out[3]=
In[4]:=
ResourceFunction["ChiSquareRandomnessTest"][sequence, "PValue"]
Out[4]=

Generate a sequence of random reals and test it for equidistribution:

In[5]:=
sequence = RandomReal[{0, 1}, 1000];

Visualize the sequence:

In[6]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[6]=

Apply a chi square randomness test:

In[7]:=
ResourceFunction["ChiSquareRandomnessTest"][sequence, "TestStatistic"]
Out[7]=
In[8]:=
ResourceFunction["ChiSquareRandomnessTest"][sequence, "PValue"]
Out[8]=

Generate variates of the normal distribution and test if they are equidistributed:

In[9]:=
variates = RandomVariate[NormalDistribution[], 1000];

Visualize the variates:

In[10]:=
ArrayPlot[Partition[variates, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[10]=
In[11]:=
Histogram[variates, 40, "PDF"]
Out[11]=

Transform the variates so that the range is between 0 and 1:

In[12]:=
transformedvariates = CDF[NormalDistribution[], variates];

Visualize the transformed variates:

In[13]:=
ArrayPlot[Partition[transformedvariates, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[13]=
In[14]:=
Histogram[transformedvariates, 40, "PDF"]
Out[14]=

Test if the transformed variates are equidistributed:

In[15]:=
ResourceFunction[
 "ChiSquareRandomnessTest"][transformedvariates, "TestStatistic"]
Out[15]=
In[16]:=
ResourceFunction[
 "ChiSquareRandomnessTest"][transformedvariates, "PValue"]
Out[16]=

Applications (1) 

Test to see if rule 30 is equidistributed:

In[17]:=
rule30seq = CellularAutomaton[30, {{1}, 0}, {10000, 0}]\[Transpose][[1]];
In[18]:=
ArrayPlot[Partition[Take[rule30seq, 100], 25], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Full]]
Out[18]=
In[19]:=
ResourceFunction["ChiSquareRandomnessTest"][rule30seq]
Out[19]=

Publisher

Emmy Blumenthal

Version History

  • 1.0.0 – 08 July 2019

Author Notes

The chi square test is meant to work with sequences of a certain length (typically longer than 30).

License Information