Kolmogorov - Smirnov test — a very effective way to determine if two samples differ significantly from each other. Usually used to check the uniformity of random numbers. Homogeneity is one of the most important properties of any random number generator, and the Kolmogorov - Smirnov test can be used to test it.
The Kolmogorov-Smirnov test can also be used to test whether the two underlying one-dimensional probability distributions are different. This is a very effective way to determine if two samples differ significantly from each other.
The Kolmogorov – Smirnov statistic quantifies a distance between the empirical distribution function of the sample and the cumulative distribution function of the reference distribution, or between the empirical distribution functions of two samples.
To use the test to test the uniformity of random numbers, we use the CDF (cumulative distribution function) U [0, 1].
F (x) = x for 0 "= x "= 1
Sn (x) = (number of R1, R2 .. .Rn "x) / N array of random numbers , random numbers must be in the range [0, 1].
The hypothesis used is —
H 0 (null hypothesis): null hypothesis assumes that the numbers are evenly distributed between 0-1.
If we can reject the null hypothesis, it mean s that the numbers are unevenly distributed between 0-1. Failure to reject the Null Hypothesis, although does not necessarily mean the numbers follow a uniform distribution.
kstest Function in Scipy Python —
Statistics: This is the calculated value of D, where
D = | F (x ) -Sn (x) |.
-" This D is compared with D alpha where alpha is the level of significance. Alpha is defined as the probability of rejecting the null hypothesis given the null hypothesis (H 0 ) is true. For most of the practical applications, alpha is chosen as 0.05.
p-value: This is calculated with the help of D.
-" If pvalue" alpha, we fail to reject the null hypothesis. Otherwise, we conclude that the numbers are not uniform. Ideally, the p-value should be as large as possible. For perfect uniform distribution pvalue = 1 and Statisitics = 0.
KS Test — this is a very powerful way to automatically distinguish samples from another distribution. The kstest function can also be used to check if the data follows a normal distribution or not. It compares the observed and expected cumulative relative frequencies of the normal distribution. The Kolmogorov-Smirnov test uses the maximum absolute difference between the observed and expected cumulative distribution.
- The null hypothesis used here assumes that the numbers follow a normal distribution.
- The function remains exactly the same ... Again, it returns statistics and a p-value. If the p value is "alpha, we reject the null hypothesis.