Wolfram Research

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.

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

Generate a sequence of random reals and apply a discrete cosine transform-based test:

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

Visualize the sequence:

In[2]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[2]=
In[3]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "TestStatistic"]
Out[3]=
In[4]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "PValue"]
Out[4]=

Generate a sequence of random integers and apply a discrete cosine transform-based test:

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

Visualize the sequence:

In[6]:=
ArrayPlot[Partition[sequence, 16]\[Transpose], Sequence[
 ImageSize -> Large, ColorFunction -> "CandyColors"]]
Out[6]=
In[7]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "TestStatistic"]
Out[7]=
In[8]:=
ResourceFunction["SpectralRandomnessTest"][sequence, "PValue"]
Out[8]=

Scope

Attempt to reject a non-random sequence:

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

Applications

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

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

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]=

Resource History

Source Metadata

See Also

License Information