kstest() compares the empirical distribution of your sample data against a reference cumulative distribution function, like 'norm' for a normal distribution, and returns a test statistic, the largest observed gap between the two distributions, along with a p-value — a small p-value suggests the sample likely doesn't come from the specified distribution. Unlike a test specifically designed for normality, like normaltest(), kstest() is a general-purpose goodness-of-fit test that can check against any specified distribution, not just the normal one, and it can also compare two independent samples directly against each other.
1Understanding stats.kstest()
kstest() compares the empirical distribution of your sample data against a reference cumulative distribution function, like 'norm' for a normal distribution, and returns a test statistic, the largest observed gap between the two distributions, along with a p-value — a small p-value suggests the sample likely doesn't come from the specified distribution. Unlike a test specifically designed for normality, like normaltest(), kstest() is a general-purpose goodness-of-fit test that can check against any specified distribution, not just the normal one, and it can also compare two independent samples directly against each other.
kstest() is quite sensitive to even small deviations from the reference distribution for large sample sizes, which can lead to statistically significant results even for differences that are practically negligible — always look at the actual size/shape of the deviation, not just whether the p-value crosses a threshold.
from scipy import stats
import numpy as np
np.random.seed(0)
data = np.random.normal(0, 1, 100)
statistic, p_value = stats.kstest(data, "norm")
print(p_value > 0.05)2Practical Example
Here is a real-world application of stats.kstest() showing how it is used in production SciPy code.
from scipy import stats
import numpy as np
np.random.seed(0)
data = np.random.uniform(0, 1, 100)
statistic, p_value = stats.kstest(data, "norm")
print(p_value > 0.05)3Best Practices
Follow these guidelines when working with stats.kstest():
1. Use kstest() to check goodness-of-fit against any specified distribution, not just normality, since it's a general-purpose test
2. Look at the actual test statistic and the shape of the deviation, not just the p-value, since kstest() can flag statistically significant but practically negligible deviations for large samples
3. Use kstest() to directly compare two independent samples' distributions to each other, not just a sample against a theoretical distribution
Tip: kstest() is quite sensitive to even small deviations from the reference distribution for large sample sizes, which can lead to statistically significant results even for differences that are practically negligible — always look at the actual size/shape of the deviation, not just whether the p-value crosses a threshold.
from scipy import stats
import numpy as np
np.random.seed(0)
data = np.random.normal(0, 1, 100)
statistic, p_value = stats.kstest(data, "norm")
print(p_value > 0.05)