Function Repository Resource:

# SpectralRandomnessTest

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

Visualize the sequence:

 In[2]:=
 Out[2]=
 In[3]:=
 Out[3]=
 In[4]:=
 Out[4]=

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

 In[5]:=

Visualize the sequence:

 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=
 In[8]:=
 Out[8]=

### Scope

Attempt to reject a non-random sequence:

 In[9]:=
 In[10]:=
 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=

### Applications

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

 In[14]:=
 In[15]:=
 Out[15]=
 In[16]:=
 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]:=
 In[18]:=
 In[19]:=
 Out[19]=
 In[20]:=
 Out[20]=
 In[21]:=
 Out[21]=

### Neat Examples

Visualize the sampling distribution of the test statistic:

 In[22]:=
 In[23]:=
 Out[23]=