Kolmogorov-Smirnov Test Python One-Sample
Python3
import numpy as np from scipy.stats import norm, kstest # Step 1: Generate a sample from a normal distribution np.random.seed( 42 ) sample_size = 100 mean = 0 std_dev = 1 sample = np.random.normal(mean, std_dev, sample_size) # Step 2: Compute the Empirical Distribution Function (EDF) def empirical_distribution_function(x, data): return np. sum (data < = x) / len (data) edf_values = [empirical_distribution_function(x, sample) for x in sample] # Step 3: Define the Reference Distribution reference_cdf = norm.cdf(sample) # Step 4: Calculate the Kolmogorov–Smirnov Statistic ks_statistic, ks_p_value = kstest(sample, 'norm' ) # Step 5: Comparing alpha = 0.05 critical_value = 1.36 # This value can be obtained from the Kolmogorov-Smirnov table for a specific significance level print (f "Kolmogorov-Smirnov Statistic: {ks_statistic}" ) print (f "P-value: {ks_p_value}" ) if ks_statistic > critical_value or ks_p_value < alpha: print ( "Reject the null hypothesis. The sample does not come from the specified distribution." ) else : print ( "Fail to reject the null hypothesis. The sample comes from the specified distribution." ) |
Output:
Kolmogorov-Smirnov Statistic: 0.10357070563896065
P-value: 0.21805553378516235
Fail to reject the null hypothesis. The sample comes from the specified distribution.
- The statistic is relatively small (0.103), suggesting that the EDF and CDF are close.
- Since the p-value (0.218) is greater than the chosen significance level (commonly 0.05), we fail to reject the null hypothesis.
Therefore, we cannot conclude that the sample does not come from the specified distribution (normal distribution with mean and standard deviation).
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.