Wolfram Research

Function Repository Resource:

RunCountRandomnessTest

Source Notebook

Conduct a runs up–based randomness test on a sequence of random reals between 0 and 1

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

ResourceFunction["RunCountRandomnessTest"][sequence]

counts the number of increasing runs to assess randomness of sequence and returns an associated p value.

RunLengthRandomnessTest[sequence,"property"]

counts the number of increasing runs to assess randomness and returns the specified 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 counts of runs up in a sequence and the expected mean counts of runs up in the sequence.
The test only works for sequences of random reals between 0 and 1.
RunCountRandomnessTest results are valid only for sequence lengths greater than 600.
The RunCountRandomnessTest function performs a two-tailed test on the test statistic.

Examples

Basic Examples

Generate a sequence of random integers and apply a run count–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["RunCountRandomnessTest"][sequence, "TestStatistic"]
Out[3]=
In[4]:=
ResourceFunction["RunCountRandomnessTest"][sequence, "PValue"]
Out[4]=

Scope

Attempt to reject a nonrandom sequence:

In[5]:=
subsequence = RandomReal[{0, 1}, 10];
In[6]:=
sequence = Flatten[Table[subsequence, 200]];
In[7]:=
ArrayPlot[Partition[Take[sequence, 200], 25], Sequence[
 ColorFunction -> "CandyColors", ImageSize -> Full]]
Out[7]=
In[8]:=
ResourceFunction["RunCountRandomnessTest"][sequence, "TestStatistic"]
Out[8]=
In[9]:=
ResourceFunction["RunCountRandomnessTest"][sequence, "PValue"]
Out[9]=

Possible Issues

This test is not very powerful and fails to reject nonrandom sequences that repeat over longer intervals on occasion; ideally, it should be used in conjunction with another test:

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

Neat Examples

Visualize the sampling distribution of the test statistic:

In[15]:=
samp = ResourceFunction["RunCountRandomnessTest"][#, "TestStatistic"] & /@ RandomReal[{0, 1}, {1000, 1000}];
In[16]:=
Histogram[samp, {1}, "PDF"]
Out[16]=
In[17]:=
dist = BinomialDistribution[n, p] /. FindDistributionParameters[samp, BinomialDistribution[n, p]]
Out[17]=
In[18]:=
Show[Histogram[samp, Automatic, "PDF"], DiscretePlot[PDF[dist, x], {x, 0, 600, 1}, PlotRange -> All]]
Out[18]=

Resource History

Source Metadata

See Also

License Information