Runs Test of Randomness in Python Programming

In this tutorial, we will learn about Randomness in Python and how to test it.

Have you ever used function “random” in your Python code? Do you know how random your values are? Let’s find out all this in this tutorial.

Background

Actually, computers are themselves unable to produce completely random outputs. Therefore they use algorithms called “pseudorandom number generators” to generate random results. But here comes a problem. The values generated by “pseudorandom number generators” are not random completely rather they depend on the initial values given to them. So, here comes various tests to check the randomness of the values generated by the “random” function. One of these tests is “Runs Test”, which we will explore in this tutorial.

Runs Test

In this test, we give generate a Z statistic value defining the randomness of the values. To calculate it we use the following values:-

Z= (R-r)/sR

r= {2n1n2/(n1 + n2)} + 1

s2R= {2n1n2 (2n1n2 – n1 – n2)} /{(n1-n2)2 (n1 + n2 – 1)}

Here, Run is defined as a series of values in sequence.

Z is the final output to be calculated (Statistics value)

n1 are values in the run of one kind,

n2 are values in the run of another kind

R and r are observed and the expected number of runs respectively

 

Code: The real Playground

Function Used:

Here, we will define a function to perform the Runs Test (runsTest) and give parameters “l and l_median”. Here, we will first define three variables namely, runs (to define the total number of runs), n1 (to define positive values in the run), n2 (to define negative values in the run), and initiate all of them to zero. Then using for loop we will divide all of the runs into positive and negative values and increase n1 and n2 accordingly.

Finally, after that, we will apply the formula to calculate the runs test statistic by first calculating runs Exp. and then standard deviation and finally return the resultant value.

def runsTest(l, l_median): 
  
    runs, n1, n2 = 0, 0, 0
      
    for i in range(len(l)): 
          
        # no. of runs 
        if (l[i] >= l_median and l[i-1] < l_median) or (l[i] < l_median and l[i-1] >= l_median): 
            runs += 1  
          
        if(l[i]) >= l_median: 
            n1 += 1   
          
        else: 
            n2 += 1   
  
    runs_exp = ((2*n1*n2)/(n1+n2))+1
    stan_dev = math.sqrt((2*n1*n2*(2*n1*n2-n1-n2))/  
                       (((n1+n2)**2)*(n1+n2-1))) 
  
    z = (runs-runs_exp)/stan_dev 
  
    return z

 

Use of function

Here we will first import the required libraries which are, random, math, and statistics.

Now we will use the above-defined function to perform the runs test on a sample of 50 random numbers and finally the code will give out the test statistic value, which is a single value defining the whole test outcome.

import random 
import math 
import statistics 
  
  
def runsTest(l, l_median): 
  
    runs, n1, n2 = 0, 0, 0
      
    for i in range(len(l)): 
          
        if (l[i] >= l_median and l[i-1] < l_median) or (l[i] < l_median and l[i-1] >= l_median): 
            runs += 1  
          
        if(l[i]) >= l_median: 
            n1 += 1   
          
        else: 
            n2 += 1   
  
    runs_exp = ((2*n1*n2)/(n1+n2))+1
    stan_dev = math.sqrt((2*n1*n2*(2*n1*n2-n1-n2))/  
                       (((n1+n2)**2)*(n1+n2-1))) 
  
    z = (runs-runs_exp)/stan_dev 
  
    return z 
    
l = [] 
for i in range(50): 
    l.append(random.random()) 
      
l_median= statistics.median(l) 
  
Z = abs(runsTest(l, l_median)) 
  
print('Z-statistic= ', Z)

Output:

Now we can test our code by running it. If we run the above code, then we will able to get the result like given below:

Z-statistic=  0.5715476066494082

Leave a Reply