Runs Test Implementation in Python

Test for Randomness in NumPy

Sıddık Açıl
2 min readOct 28, 2019

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 is generated on the data 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 number of runs) / Standard Deviation
  • Expected value formula
n_p = Number of positive values
n_n = Number of negative values…

--

--