Kolmogorov-Smirnov Test Python Two-Sample
- The null hypothesis assumes that the two samples come from the same distribution.
- The decision is based on comparing the p-value with a chosen significance level (e.g., 0.05). If the p-value is less than the significance level, reject the null hypothesis, indicating that the two samples come from different distributions.
Python3
import numpy as np from scipy.stats import ks_2samp np.random.seed( 42 ) sample1 = np.random.normal( 0 , 1 , 100 ) sample2 = np.random.normal( 0.5 , 1.5 , 120 ) ks_statistic, p_value = ks_2samp(sample1, sample2) print (f "Kolmogorov–Smirnov Statistic: {ks_statistic}" ) print (f "P-value: {p_value}" ) alpha = 0.05 if p_value < alpha: print ( "Reject the null hypothesis. The two samples come from different distributions." ) else : print ( "Fail to reject the null hypothesis. There is not enough evidence to suggest different distributions." ) |
Output:
Kolmogorov–Smirnov Statistic: 0.35833333333333334
P-value: 9.93895980740741e-07
Reject the null hypothesis. The two samples come from different distributions.
- The statistic is, indicating a relatively large discrepancy between the two sample distributions.
- The small p-value suggests strong evidence against the null hypothesis that the two samples come from the same distribution.
Therefore, two samples come from different distributions.
Kolmogorov-Smirnov Test (KS Test)
The Kolmogorov-Smirnov (KS) test is a non-parametric method for comparing distributions, essential for various applications in diverse fields.
In this article, we will look at the non-parametric test which can be used to determine whether the shape of the two distributions is the same or not.