🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

stats.kstest()

AI & DATA SCIENCE // stats-kstest

scipy.stats.kstest() performs the Kolmogorov-Smirnov test, checking whether a sample of data comes from a specified theoretical distribution (or comparing two samples to each other).

Syntax

scipy.stats.kstest(rvs, cdf, args=())

Deep Dive Course

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.

editor.html
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)
localhost:3000

2Practical Example

Here is a real-world application of stats.kstest() showing how it is used in production SciPy code.

editor.html
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)
localhost:3000

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.

editor.html
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)
localhost:3000

Examples

Example 01Basic Usage
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)
Example 02Advanced Example
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)

Best Practices

  • Use kstest() to check goodness-of-fit against any specified distribution, not just normality, since it's a general-purpose test
  • 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
  • Use kstest() to directly compare two independent samples' distributions to each other, not just a sample against a theoretical distribution

Interview Question

Why might scipy.stats.kstest() report a statistically significant deviation from normality for a very large sample, even when the data looks visually almost normal on a histogram?

Hint: Think about how a hypothesis test's sensitivity to detecting real, even tiny, differences changes as sample size grows.

As sample size grows, a hypothesis test like kstest() becomes increasingly able to detect even extremely small, practically negligible deviations from the reference distribution, since it has more data to reliably distinguish genuine, tiny discrepancies from random sampling noise. A dataset that's very close to, but not perfectly, normally distributed can easily produce a small p-value with enough samples, correctly signaling that a true, real difference from perfect normality exists, even though that difference might be far too small to matter for any practical purpose. This is exactly why it's important to look at the actual magnitude/shape of a detected deviation, not just whether a p-value happens to cross a conventional significance threshold.

Exercises

MediumPractice using stats.kstest() in a real scenario.
View Solution
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)

Frequently Asked Questions

Why might scipy.stats.kstest() report a statistically significant deviation from normality for a very large sample, even when the data looks visually almost normal on a histogram?

As sample size grows, a hypothesis test like kstest() becomes increasingly able to detect even extremely small, practically negligible deviations from the reference distribution, since it has more data to reliably distinguish genuine, tiny discrepancies from random sampling noise. A dataset that's very close to, but not perfectly, normally distributed can easily produce a small p-value with enough samples, correctly signaling that a true, real difference from perfect normality exists, even though that difference might be far too small to matter for any practical purpose. This is exactly why it's important to look at the actual magnitude/shape of a detected deviation, not just whether a p-value happens to cross a conventional significance threshold.

Related Functions

stats-normalteststats-normstats-ttest-ind