Function Repository Resource:

RunLengthRandomnessTest

Source Notebook

Conduct a randomness test on a sequence of random reals between 0 and 1 using run lengths of increasing subsequences

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

ResourceFunction["RunLengthRandomnessTest"][sequence]

uses lengths of increasing runs to test the randomness of sequence and returns an associated p-value.

ResourceFunction["RunLengthRandomnessTest"][sequence,"property"]

uses lengths of increasing runs 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 creating a chi square–like statistic that measures the difference between the lengths of runs up in a sequence and the expected mean lengths of runs up in the sequence.
The test only works for sequences of random reals between 0 and 1.
ResourceFunction["RunLengthRandomnessTest"] results are valid only for sequence lengths greater than 600.
ResourceFunction["RunLengthRandomnessTest"] function performs a two-tailed test on the test statistic.

Examples

Basic Examples (3) 

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 run length-based test:

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

Scope (3) 

Generate a sequence of random integers:

In[5]:=
subsequence = RandomReal[{0, 1}, 100];
In[6]:=
sequence = Flatten[Table[subsequence, 20]];

Visualize the sequence:

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

Attempt to reject a non-random sequence:

In[8]:=
ResourceFunction["RunLengthRandomnessTest"][sequence, "TestStatistic"]
Out[8]=
In[9]:=
ResourceFunction["RunLengthRandomnessTest"][sequence, "PValue"]
Out[9]=

Neat Examples (1) 

Visualize the sampling distribution of the test statistic:

In[10]:=
samp = ResourceFunction["RunLengthRandomnessTest"][#, "TestStatistic"] & /@ RandomReal[{0, 1}, {1000, 1000}];
In[11]:=
Histogram[samp, Automatic, "PDF"]
Out[11]=
In[12]:=
dist = FindDistribution[samp, TargetFunctions -> {NormalDistribution}]
Out[12]=
In[13]:=
Show[Histogram[samp, Automatic, "PDF"], Plot[PDF[dist, x], {x, 0, 300}, PlotRange -> All]]
Out[13]=

Publisher

Emmy Blumenthal

Version History

  • 1.0.0 – 08 July 2019

Source Metadata

Related Resources

License Information