Function Repository Resource:

SpectralRandomnessTest

Source Notebook

Use a discrete cosine transform–based method to test the randomness of a sequence of random reals

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

ResourceFunction["SpectralRandomnessTest"][sequence]

conducts a discrete cosine transform-based test to asses the randomness of either a sequence of 0s and 1s or reals between 0 and 1, returning the associated p-value.

ResourceFunction["SpectralRandomnessTest"][sequence,"property"]

conducts a discrete cosine transform-based test and returns the associated property.

Details and Options

Properties include:
"TestStatistic"returns the test statistic
"PValue"returns the p-value associated with the test
The test statistic is generated by comparing the discrete cosine transformed data with the normal distribution using the Kolmogorov-Smirnov test.
The test works for sequences of of random reals between 0 and 1 and sequences of only 0s and 1s

Examples

Basic Examples (2) 

Generate a sequence of random integers:

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

Visualize the sequence:

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

Apply a discrete cosine transform-based test:

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

Generate a sequence of random integers:

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

Visualize the sequence:

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

Apply a discrete cosine transform-based test:

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

Scope (3) 

Generate a sequence of random integers:

In[9]:=
subsequence = RandomInteger[{0, 1}, 100];
In[10]:=
sequence = Flatten[Table[subsequence, 20]];

Visualize the sequence:

In[11]:=
ArrayPlot[Partition[Take[sequence, 200], 25], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Full]]
Out[11]=

Attempt to reject a non-random sequence:

In[12]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "TestStatistic"]
Out[12]=
In[13]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "PValue"]
Out[13]=

Applications (1) 

Test to see if rule 30 is random according to the spectral test:

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

Possible Issues (1) 

For non-random data, the Kolmogorov–Smirnov test, a part of the entire test, may return ties. Observing such ties is a strong indicator that the data is non-random:

In[17]:=
subsequence = RandomInteger[{0, 1}, 100];
In[18]:=
sequence = Flatten[Table[subsequence, 20]];
In[19]:=
ArrayPlot[Partition[Take[sequence, 200], 25], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Full]]
Out[19]=
In[20]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "TestStatistic"]
Out[20]=
In[21]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "PValue"]
Out[21]=

Neat Examples (1) 

Visualize the sampling distribution of the test statistic:

In[22]:=
samp = ResourceFunction["SpectralRandomnessTest"][#, "TestStatistic"] & /@ RandomReal[{0, 1}, {1000, 1000}];
In[23]:=
Histogram[samp, Automatic, "PDF"]
Out[23]=

Publisher

Emmy Blumenthal

Version History

  • 1.0.0 – 08 July 2019

Source Metadata

Related Resources

Author Notes

This test, as far as I am aware, is an original test.

License Information