Change language

# ML | Kolmogorov-Smirnov test

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 `

Empirical CDF, ` 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.

### The `kstest ` Function in Scipy Python —

Parameters :

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.

 ` from ` ` scipy.stats ` ` import ` ` kstest ` ` import ` ` random `   ` # N = int ( input (& quot; Enter a random number: & quot;)) ` ` N ` ` = ` ` 5 `   ` actual ` ` = ` ` [] ` ` print ` ` (` ` "Enter outcomes:" ` `) ` ` for ` ` i in range (N): `` # x = float (input (& quot; Outcomes of class & quot; + str (i + 1) + & quot;: & quot;)) actual.append (random.random ())   print (actual) x = kstest (actual, "uniform" )  print (x) `

Exit:

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.

 ` from ` ` scipy.stats ` ` import ` ` kstest ` ` import ` ` random `   ` # N = int ( input (& quot; Enter a random number: & quot;)) ` ` N ` ` = ` ` 10 `   ` actual ` ` = ` ` [] ` ` print ` ` (` ` "Enter outcomes:" ` `) `   ` for ` ` i ` ` in ` ` range ` ` (N): ` ` # x = float (input (& quot; Outcomes of class & quot; + str (i + 1) + & quot;: & quot;)) ` ` actual.append (random.random ()) `   ` print ` ` (actual) ` ` x ` ` = ` ` kstest (actual, ` ` "norm" ` `) ` ` print ` ` (x) `

Exit:

## Shop

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best laptop for development

\$499+

Best laptop for Cricut Maker

\$299+

Best laptop for hacking

\$890

Best laptop for Machine Learning

\$699+

Raspberry Pi robot kit

\$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries