Kolmogorov-Smirnov Test

Performing a Kolmogorov-Smirnov test to compare two samples with SciPy

Python
import numpy as np
from scipy import stats

# Generate two sample datasets
np.random.seed(42)
sample1 = np.random.normal(loc=0, scale=1, size=100)
sample2 = np.random.normal(loc=0.5, scale=1.2, size=100)

# Perform Kolmogorov-Smirnov test
ks_statistic, p_value = stats.ks_2samp(sample1, sample2)

print(f"KS statistic: {ks_statistic:.4f}")
print(f"p-value: {p_value:.4f}")

if p_value < 0.05:
    print("Reject the null hypothesis: The samples are drawn from different distributions.")
else:
    print("Fail to reject the null hypothesis: The samples may be drawn from the same distribution.")
Click Run or press shift + ENTER to run code.