Kolmogorov–Smirnov test a very efficient way to determine if two samples are significantly different from each other. It is usually used to check the uniformity of random numbers. Uniformity is one of the most important properties of any random number generator and Kolmogorov–Smirnov test can be used to test it.
The Kolmogorov–Smirnov test may also be used to test whether two underlying one-dimensional probability distributions differ. It is a very efficient way to determine if two samples are significantly different 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 for checking the uniformity of random numbers, we use the CDF (cumulative distribution function) of U[0, 1].
F(x)= x for 0<=x<=1
Empirical CDF, Sn(x)= (number of R1, R2…Rn < x)/N array of random numbers, the random numbers must be in the range of [0, 1].
Hypothesis Used –
H0(Null Hypothesis): Null hypothesis assumes that the numbers are uniformly distributed between 0-1. If we are able to reject the Null Hypothesis, this means that the numbers are not uniformly distributed between 0-1. Failure to reject the Null Hypothesis although does not necessarily mean that the numbers follow the uniform distribution.
Algorithm:
-> Rank the N random numbers in ascending order.
-> Calculate D+ as max(i/N-Ri) for all i in(1, N)
-> Calculate D- as max(Ri-((i-1)/N)) for all i in(1, N)
-> Calculate D as max(sqrt(N) * D+, sqrt(N) * D-)
-> If D>D(alpha)
Rejects Uniformity
else
It fails to reject the Null Hypothesis.
Below is the Python implementation of above algorithm :
import random N = int(input("Enter the size of random numbers to be produced : ")) D_plus =[] D_minus =[] _random =[] # Rank the N random numbers for i in range(0, N): _random.append(random.random()) _random.sort() # Calculate max(i/N-Ri) for i in range(1, N + 1): x = i / N - _random[i-1] D_plus.append(x) # Calculate max(Ri-((i-1)/N)) for i in range(1, N + 1): y =(i-1)/N y =_random[i-1]-y D_minus.append(y) # Calculate max(D+, D-) ans = max(sqrt(N) * D_plus, sqrt(N) * D_minus) print("Value of D is :") print(ans) |
Reference – https://dl.acm.org/doi/pdf/10.1145/355719.355724
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Turing Test in Artificial Intelligence
- Python - Test if List contains elements in Range
- Python - Test if common values are greater than K
- Analysis of test data using K-Means Clustering in Python
- Python | Test list element similarity
- ML | Chi-square Test for feature selection
- Python | Test for False list
- Python | Create Test DataSets using Sklearn
- Python | Test if string contains element from list
- Python | Test if dictionary contains unique keys and values
- ML | Kolmogorov-Smirnov Test
- Python | Unit Test Objects Patching | Set-1
- Python | Unit Test Objects Patching | Set-2
- Python | Skipping Test Failures
- Python | Logging Test Output to a File
- Python | Generate test datasets for Machine learning
- Chi-Square Test for Feature Selection - Mathematical Explanation
- Python | Test for nested list
- Python | Test if all elements are present in list
- Python | Test if any list element returns true for condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ardadrl

