DEV Community

Sıddık AÇIL
Sıddık AÇIL

Posted on

2 1

Runs Test for Randomness Testing in Python

Hello there,

This is my first post on this site. I've been running a Turkish/English blog on Medium platform for 28 months now and have just discovered this site. I absolutely fell in love with its retroesque design, so here we are. As you can see, I am looking forward for job opportunities abroad. Feel free to contact me for any vacancies.

Methodology

Runs test is a hypothesis testing based methodology that is widely used in statistical analysis to test if a set of values are generated randomly or not. It is a hypothesis test so we have a pair of a null hypothesis and an alternative one.

Null hypothesis: The values are randomly generated.

Alternative hypothesis: The values are NOT randomly generated.

A Z score for hypothesis can be acquired by simply following the general formula:

(Observed-Excepted) / Standard Deviation

The score is then tested against the confidence interval(two-tailed) we specify. If the value is higher, we conclude that our alternative hypothesis holds. Otherwise, if the value is lower, we cannot say anything about the randomness of data at this significance level. We will be using %95 confidence interval(alpha = 0.05) through the rest of this article.

Definitions and Formulas

  • A run: A series of positive or negative values:
Data: [1, -2, -3, 4, 5, 6, –7]

Runs: [[1], [-2, -3], [4, 5, 6], [-7]]
  • Score Formula
(Number of runs - Excepted Value for Number of Runs) / Standard Deviation
  • Expected Value Formula for Runs Test
n_p = Number of positive values
n_n = Number of negative values
Excepted value of runs = (2 * n_p * n_n) / (n_p + n_n) + 1
  • Standard Deviation Formula for Runs Test
n_p = Number of positive values
n_n = Number of negative values
Excepted value of runs = (2 * n_p * n_n) / (n_p + n_n) + 1

Test Data

I will using the data provided by NIST.

Octave/Matlab Implementation and Results

Using online Octave:

  1. Upload text file
  2. Load ‘statistics’ package

    pkg load statistics

  3. Import data to array

    x = importdata("test.txt")

  4. Run runstest

    [h, v, stats] = runstest(x, median(x))

Octave Online Result

Python Implementation

Implement the test using Python.

# H0: the sequence was produced in a random manner
# Ha: the sequence was not produced in a random manner
# Read test data
import numpy as np
import scipy.stats as st
# Assuming number of runs greater than 10
def runs_test(d, v, alpha = 0.05):
# Get positive and negative values
mask = d > v
# get runs mask
p = mask == True
n = mask == False
xor = np.logical_xor(p[:-1], p[1:])
# A run can be identified by positive
# to negative (or vice versa) changes
d = sum(xor) + 1 # Get number of runs
n_p = sum(p) # Number of positives
n_n = sum(n)
# Temporary intermediate values
tmp = 2 * n_p * n_n
tmps = n_p + n_n
# Expected value
r_hat = np.float64(tmp) / tmps + 1
# Variance
s_r_squared = (tmp*(tmp - tmps)) / (tmps*tmps*(tmps-1))
# Standard deviation
s_r = np.sqrt(s_r_squared)
# Test score
z = (d - r_hat) / s_r
# Get normal table
z_alpha = st.norm.ppf(1-alpha)
# Check hypothesis
return z, z_alpha
filename = "test.txt"
# Load array
d = np.array(np.loadtxt(filename))
print(runs_test(d, np.median(d)))
view raw runs_test.py hosted with ❤ by GitHub

Running the code produces the results below (Calculated Z Score, Z Score at %95 confidence):

(2.8355606218883844, 1.6448536269514722)

Since our test score is higher, alternative hypothesis holds. This means that our values are genuinely random.

Thanks for reading. Any corrections are welcome.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay