Kolmogorov-Smirnov (K-S) Test Calculator — Free Interactive Nonparametric Tool
Compare two empirical distributions by finding their largest vertical separation at a common measurement value.
Quick Answer
The Kolmogorov-Smirnov test (K-S test) is a powerful, nonparametric statistical procedure used to determine whether two samples are drawn from the same underlying probability distribution.
Kolmogorov-Smirnov (K-S) Test Calculator
Interactive two-sample K-S test with live ECDF visualization — compare Normal, Uniform, Exponential, and Bimodal distributions with instant results.
Free CalculatorNonparametricLive ECDF Plotp-value & Critical Value4 Distribution Types
Interactive K-S Test Calculator
Distribution Comparison
Critical Values
Real-World Examples
Distribution 1
Distribution 2
Test Settings
ECDF — Distribution 1
ECDF — Distribution 2
Max Distance D
K-S Test Results
K-S Statistic (D): —
Critical Value: —
p-value: —
Conclusion: —
How to Interpret These Results
The K-S statistic D (green dashed line) is the maximum absolute vertical gap between the two blue/red ECDF curves. If D exceeds the critical value — or equivalently, if p < α — we reject H₀ and conclude the distributions differ. Increase sample size to see how the test becomes more sensitive to smaller differences.
Critical Value Calculator
The critical value is the threshold that D must exceed to reject H₀. For two equal samples of size n at significance level α:
As n increases, Dcrit decreases (∝ 1/√n) — meaning larger samples detect smaller distributional differences. This is why the K-S test applied to very large datasets may reject H₀ for trivially small practical differences. Always consider effect size alongside statistical significance.
Real-World Application Examples
Compares stock return distributions before and after a major economic event. The K-S test detects whether return volatility or location shifted significantly.
Example K-S Test Results
K-S Statistic (D): —
Critical Value: —
p-value: —
Conclusion: —
What is the Kolmogorov-Smirnov (K-S) Test?
The Kolmogorov-Smirnov test (K-S test) is a powerful, nonparametric statistical procedure used to determine whether two samples are drawn from the same underlying probability distribution. Unlike parametric tests such as the t-test or ANOVA — which make specific assumptions about the shape of the population distribution — the K-S test is distribution-free: it makes no assumption that your data is normally distributed, Poisson-distributed, or of any other specific family.
The defining feature of the K-S test is the test statistic D, which measures the maximum absolute vertical distance between the two empirical cumulative distribution functions (ECDFs) of the two samples. A large D indicates that the two samples behave very differently across their entire range; a small D suggests they may come from the same distribution.
Key Insight: The K-S test is sensitive to differences in location (where the distribution is centred), scale (how spread out it is), and shape (symmetry, tail weight, modality). This makes it more comprehensive than tests that only assess mean differences.
Core Principles at a Glance
Distribution-free: No parametric assumption about the data's family of distributions.
Supremum statistic: D = sup|F₁(x) − F₂(x)| captures the worst-case discrepancy.
Two flavours: One-sample (data vs. theory) and two-sample (data vs. data).
Exact for small samples: Exact p-values can be computed via the K-S distribution; asymptotic approximations improve with n.
Glivenko-Cantelli guarantee: The ECDF converges uniformly to the true CDF as n → ∞, providing the theoretical foundation for the test.
Historical Background
The K-S test is named after two giants of 20th-century Russian mathematics:
Andrey Nikolaevich Kolmogorov (1903–1987)
Kolmogorov was one of the most prolific mathematicians in history, making foundational contributions to probability theory (his 1933 axiomatisation of probability is still the standard today), turbulence, algorithmic information theory, and topology. In 1933, he published the limiting distribution of the supremum of the difference between an ECDF and a theoretical CDF for a continuous distribution, establishing the theoretical basis for the one-sample test.
Nikolai Vasilyevich Smirnov (1900–1966)
Smirnov extended Kolmogorov's work to the two-sample case in 1948 and derived the tables of critical values that statisticians used for decades before computers made exact computation feasible. His 1948 paper in The Annals of Mathematical Statistics is one of the most-cited works in nonparametric statistics.
Historical Note: The K-S test predates modern computers. Statisticians originally computed ECDFs by hand and compared them to tables of critical values printed in statistical handbooks — a tedious but important practice that underscores the test's mathematical elegance and simplicity.
The CDF is a non-decreasing, right-continuous function taking values in [0, 1]. It starts at 0 as x → −∞ and approaches 1 as x → +∞. For a normal distribution with mean μ and standard deviation σ:
Normal CDF
\[F(x) = \Phi\!\left(\frac{x-\mu}{\sigma}\right) = \frac{1}{2}\left[1 + \operatorname{erf}\!\left(\frac{x-\mu}{\sigma\sqrt{2}}\right)\right]\]
The Empirical CDF (ECDF)
Given n observations x₁, x₂, …, xₙ from an unknown distribution, the empirical CDF is the step function:
The ECDF assigns probability mass of 1/n to each observation and places a "step" of height 1/n at every observation. The Glivenko-Cantelli Theorem (proof by Kolmogorov, 1933) guarantees that:
This almost-sure uniform convergence is the cornerstone of K-S test theory: as sample size grows, the ECDF becomes an increasingly accurate estimate of the true CDF in an exact, worst-case sense.
Properties of the ECDF
Non-decreasing step function with jumps of size 1/n at each data point
F_n(x) = 0 for x < min(x₁,…,xₙ) and F_n(x) = 1 for x ≥ max(x₁,…,xₙ)
Unbiased estimator of the true CDF: E[F_n(x)] = F(x) for all x
Pointwise variance: Var[F_n(x)] = F(x)(1−F(x)) / n
The K-S Statistic: Mathematical Derivation
Two-Sample K-S Statistic
Given two independent samples of sizes n₁ and n₂ with ECDFs F_{n₁}(x) and G_{n₂}(x), the two-sample K-S statistic is:
Because ECDFs are step functions that only change at observed data points, the supremum is attained at one of the combined ordered observations. The algorithm evaluates |F_n(x) − G_m(x)| at every unique point in the merged sorted sample and takes the maximum.
One-Sample K-S Statistic
When comparing a sample to a fully specified theoretical CDF F₀(x):
Under H₀ (both samples from the same continuous distribution), for the one-sample test:
Kolmogorov Distribution (asymptotic)
\[P\!\left(\sqrt{n}\,D_n \leq z\right) \to K(z) = 1 - 2\sum_{k=1}^{\infty}(-1)^{k+1} e^{-2k^2z^2} \quad \text{as } n \to \infty\]
The function K(z) is the Kolmogorov distribution, a distribution specific to this test. For the two-sample test with unequal sample sizes n₁ and n₂, replace √n with the effective sample size:
Parameters must be pre-specified (not estimated from data)
Requires both samples to be continuous
Common alternatives
Shapiro-Wilk, Anderson-Darling
Mann-Whitney U, Wilcoxon rank-sum
Important: If you estimate parameters (μ, σ) from the same sample you are testing against a normal distribution, you must use the Lilliefors test (a modification of the one-sample K-S test) rather than the standard K-S test. Using estimated parameters with the standard K-S test will produce an overly conservative test (inflated p-values).
Step-by-Step: Performing the Two-Sample K-S Test
Step 1 — State the Hypotheses
Clearly define what you are testing:
H₀: The two samples are drawn from the same continuous distribution.
H₁: The two samples are drawn from different distributions.
Choose the significance level α (typically 0.05).
Step 2 — Compute the ECDFs
Sort each sample independently. For sample 1 with n₁ observations sorted as x₍₁₎ ≤ x₍₂₎ ≤ … ≤ x₍n₁₎:
\[\text{Reject } H_0 \iff D > D_{\text{crit}} \iff p < \alpha\]
Both conditions are mathematically equivalent for the asymptotic test. If you reject H₀, conclude the two samples come from different distributions. Failing to reject does not prove they are the same — only that there is insufficient evidence to distinguish them at the chosen α.
Worked Example: Full K-S Test Calculation
Scenario: A pharmaceutical researcher wants to determine whether the body temperature (°C) distribution of patients in Group A (treated) differs from Group B (control). She collects n₁ = n₂ = 8 measurements from each group.
Data
Group A (Treated)
Group B (Control)
36.4, 36.8, 37.0, 37.1, 37.3, 37.5, 37.7, 38.1
36.6, 36.9, 37.0, 37.2, 37.4, 37.6, 37.8, 37.9
Step 1: Sort Each Sample (already sorted)
Group A sorted: 36.4, 36.8, 37.0, 37.1, 37.3, 37.5, 37.7, 38.1
Group B sorted: 36.6, 36.9, 37.0, 37.2, 37.4, 37.6, 37.8, 37.9
There is insufficient evidence at α = 0.05 to conclude that the temperature distributions of the two groups are different. The two ECDFs are very close to each other throughout the range, which is confirmed by the small D = 0.125. Note: with only n = 8 per group, the test has limited power to detect moderate differences.
Critical Values and P-Value Interpretation
Interpreting the p-value
The p-value in the K-S test is the probability of observing a test statistic as extreme as D under H₀. It is approximately:
The single-term approximation \(p \approx 2e^{-2\lambda^2}\) is accurate for λ > 0.5 and provides an upper bound on the true p-value. For small samples, exact tables (Massey 1951, Smirnov 1948) should be used.
Standard Critical Value Reference Table
n (per group)
α = 0.10
α = 0.05
α = 0.01
10
0.369
0.409
0.486
20
0.265
0.294
0.352
30
0.218
0.242
0.290
50
0.170
0.188
0.225
100
0.121
0.134
0.161
200
0.086
0.095
0.114
500
0.054
0.060
0.072
Large n
1.22/√n
1.36/√n
1.63/√n
Assumptions of the K-S Test
While the K-S test is nonparametric and distribution-free, it is not assumption-free. Violating these conditions can invalidate the test:
Independence within samples: Each observation must be independently and identically distributed (i.i.d.). Correlated observations (time series, clustered data) violate this assumption.
Independence between samples: The two samples must be independent of each other. Paired data (before/after on the same subjects) requires paired tests.
Continuous distribution: The underlying distribution must be continuous. The K-S test can be conservative (inflated p-values) when applied to discrete data due to ties at the same value.
Pre-specified parameters (one-sample only): For the one-sample test, the theoretical CDF parameters must be specified in advance, not estimated from the data. If parameters are estimated, use the Lilliefors test.
Ties: When ties occur in the data, the K-S statistic can still be computed, but its null distribution changes. For heavily discretised data, consider Barnard's exact test or a permutation-based K-S test.
⚖ K-S Test vs. Other Statistical Tests
Test
Parametric?
What it Detects
Best Use Case
Power vs. K-S
K-S Test
No
All distributional differences
General comparison of two distributions
Baseline
t-test (two-sample)
Yes
Difference in means only
Normal data, comparing means
Higher if normality holds
Mann-Whitney U
No
Location shift (stochastic dominance)
Ordinal or skewed data
Higher for location shifts
Anderson-Darling
No (one-sample)
Tail differences (weighted)
Normality testing, tail-sensitive applications
Higher for tail differences
Shapiro-Wilk
No
Departure from normality
Normality testing (n < 2000)
Much higher for normality test
Chi-square GoF
No
Binned frequency differences
Categorical or binned continuous data
Lower (depends on binning)
Cramér–von Mises
No
Integrated squared difference
Sensitive to global distributional differences
Generally comparable
When to Choose the K-S Test
You want to compare full distributions, not just means or medians.
You cannot or do not want to assume a parametric form for the data.
You need a test that detects location, scale, AND shape differences simultaneously.
You want a test with an intuitive, visual representation (the ECDF plot).
Your data is continuous or nearly continuous.
Real-World Applications of the K-S Test
Finance and Econometrics
The K-S test is frequently used to test whether financial returns follow a specific distribution (e.g., normality, which underpins Black-Scholes option pricing). Value-at-Risk models often assume normal or log-normal returns, and the K-S test can formally challenge this assumption. Quantitative analysts also use K-S tests to detect regime shifts — for example, comparing the distribution of daily S&P 500 returns before and after the 2008 financial crisis.
Clinical Trials and Pharmacology
In clinical research, the K-S test compares the outcome distribution between treatment and control groups. Unlike the t-test, it detects distributional differences even when means are similar — for example, a treatment might shift the tail of the distribution while leaving the median unchanged, which would be missed by a t-test but caught by the K-S test. FDA guidance documents recognise nonparametric tests including K-S for situations where normality cannot be assumed.
Machine Learning and Data Science
A critical challenge in machine learning is covariate shift — when the distribution of input features changes between the training environment and the production environment. The K-S test is widely used to detect such shifts automatically in production monitoring systems. Libraries such as Evidently AI, Deepchecks, and AWS SageMaker Model Monitor implement K-S tests as a core drift detection metric.
Environmental scientists compare pollutant concentration distributions across different sites, seasons, or years. Public health researchers test whether the distribution of disease biomarkers differs between exposed and unexposed populations. The K-S test is particularly valuable here because environmental data is often skewed and non-normal.
Quality Control and Manufacturing
Statistical process control (SPC) uses K-S tests to detect shifts in the distribution of product dimensions, weights, or chemical properties over time. If a production batch shows D > D_crit compared to historical data, it signals a process change requiring investigation.
Physics and Engineering
Particle physicists and signal processing engineers use K-S tests to compare observed event distributions with theoretical predictions. The Large Hadron Collider (LHC) data analysis pipelines employ automated K-S testing to compare energy deposition distributions between simulated and measured data.
⚡ Advantages and Limitations
Advantages
Nonparametric: No assumption about the underlying distribution family — works on any continuous data.
Comprehensive: Detects differences in location, scale, and shape simultaneously.
Interpretable: The statistic D has a direct geometric interpretation (maximum vertical gap between ECDFs).
Exact for finite samples: Exact p-values are available from computed tables (no large-sample approximation needed for small n).
Universal: The K-S distribution is universal — D_crit depends only on n, not on the family of the underlying distributions.
Visual: The ECDF plot provides an immediate visual summary of where the two distributions differ.
Limitations
Pairwise only: Compares exactly two distributions; extending to k > 2 groups requires multiple testing corrections.
Less powerful at tails: The K-S test is less sensitive to differences in the extreme tails. The Anderson-Darling test (which down-weights the centre) is preferred for tail-focused inference.
Conservative with ties: Discrete or heavily tied data inflates the p-value, leading to under-rejection.
Large n over-sensitivity: With very large samples, even trivially small and practically irrelevant distributional differences become statistically significant at any α level.
Does not localise: D tells you that distributions differ but not where or how — supplementary analysis (quantile-quantile plots, density estimates) is needed to characterise the difference.
One-sample limitation: Parameters must be pre-specified; estimating them from the same data invalidates the standard critical values.
Frequently Asked Questions
What is the Kolmogorov-Smirnov test used for?
The K-S test determines whether two samples come from the same probability distribution by comparing their empirical CDFs. It is used in machine learning (drift detection), finance (return distribution testing), medicine (clinical trial comparison), environmental science (site comparison), and quality control (process monitoring).
How do I interpret the K-S statistic D?
D is the maximum absolute vertical difference between the two ECDFs over all x values. D = 0 means the ECDFs are identical (distributions are the same). D = 1 means the distributions are completely non-overlapping. In practice, D values around 0.05–0.30 are typical for moderately different distributions with n = 100–500.
What is the null hypothesis of the K-S test?
H₀: Both samples are drawn from the same continuous distribution (i.e., F₁(x) = F₂(x) for all x). H₁: The distributions differ for at least one value of x. Rejecting H₀ (p < α) means there is statistically significant evidence that the distributions differ.
What sample size does the K-S test require?
The K-S test works for any n ≥ 1, but the asymptotic critical values (based on the Kolmogorov distribution) are most accurate for n ≥ 25–30 per group. For very small samples (n < 20), use exact tables (Massey 1951) or simulation-based p-values for reliable inference.
Can the K-S test be used for discrete data?
Technically yes, but the K-S test is most valid for continuous data. With discrete data, ties at the same value cause the test to be conservative — it under-rejects H₀. For discrete data, consider the chi-squared goodness-of-fit test or exact permutation tests.
What is the difference between the K-S test and the t-test?
The t-test only compares the means of two normally distributed populations. The K-S test is nonparametric and compares entire distributions, detecting any kind of difference (mean, variance, skewness, kurtosis, shape). The t-test is more powerful when normality holds and only mean differences are of interest; the K-S test is more general and robust.
What does "fail to reject H₀" mean?
It means there is insufficient statistical evidence to conclude the two distributions differ at the chosen significance level α. Crucially, it does NOT prove the two distributions are identical — it only means the data cannot distinguish them given the available sample size. The test may simply lack power (especially with small n).
How is the p-value computed in the K-S test?
For large samples, p ≈ 2·exp(−2λ²) where λ = D·√(n₁·n₂/(n₁+n₂)). This is derived from the asymptotic Kolmogorov distribution. For small samples, the exact p-value is computed using the Smirnov-Massey tables or simulation. Most statistical software (R, Python's scipy, SPSS) computes exact p-values numerically.
What is covariate shift and how does the K-S test detect it?
Covariate shift occurs in machine learning when the distribution of input features P(X) changes between training and deployment, even if the conditional output distribution P(Y|X) remains stable. The K-S test detects this by comparing distributions of each feature between training data and new incoming data. A significant D (p < α) for any feature triggers a data drift alert.
Can the K-S test be applied to more than two groups?
The standard K-S test compares exactly two distributions. For k > 2 groups, you have two options: (1) pairwise K-S tests with Bonferroni or Holm correction for multiple comparisons, or (2) the k-sample Anderson-Darling test, which generalises the goodness-of-fit approach to multiple groups simultaneously.
What is the Lilliefors correction and when do I need it?
The Lilliefors test is a correction to the one-sample K-S test when population parameters (μ, σ) are estimated from the same sample being tested. Standard K-S critical values are too large when parameters are estimated, leading to under-rejection. Lilliefors (1967) derived new critical values for this common situation. Always use Lilliefors when testing normality with estimated parameters.
How do I perform the K-S test in Python or R?
In Python: scipy.stats.ks_2samp(sample1, sample2) returns (D_statistic, p_value). In R: ks.test(sample1, sample2) performs the two-sample test. Both automatically compute appropriate p-values. The HeLovesMath calculator above lets you explore K-S test results interactively — without any coding.
Advertisement
Construct two empirical distribution functions
Take sample A as one, two and four, and sample B as two, three and five. At the value one, one third of A and none of B are at or below the cutoff. At two, the cumulative proportions are two thirds and one third. At three they are equal, and at four the difference returns to one third. The largest absolute difference is therefore one third. This statistic describes the separation; interpreting significance also requires an appropriate sampling model.
Sampling and Gathering Data
The Digest's failure highlights the need for what is now considered the most important criterion for sampling: randomness. This randomness can be achieved in several ways. Here we cover some of the most common.
A simple random sample is chosen in a way that every unit in the population has an equal chance of being selected, and the chances of a unit being selected do not depend on the units already chosen. An example of this is choosing a group of people by drawing names out of a hat (assuming the names are well-mixed in the hat).
A systematic random sample is selected from an ordered list of the population (for example, names sorted alphabetically or students listed by student ID). First, we decide what proportion of the population will be in our sample. We want to express that proportion as a fraction with 1 in the numerator. Let’s call that number D. Next, we’ll choose a random number between one and D. The unit at that position will go into our sample. We’ll find the rest of our sample by choosing every Dth unit in the list, starting with our random number.
To walk through an example, let’s say we want to sample 2% of the population: . (Note: If the number in the denominator isn’t a whole number, we can just round it off. This part of the process doesn’t have to be precise.) We can then use a random number generator to find a random number between 1 and 50; let's use 31. In our example, our sample would then be the units in the list at positions 31, 81 (31 + 50), 131 (81 + 50), and so forth.
A stratified sample is one chosen so that particular groups in the population are certain to be represented. Let’s say you are studying the population of students in a large high school (where the grades run from 9th to 12th), and you want to choose a sample of 12 students. If you use a simple or systematic random sample, there’s a pretty good chance that you’ll miss one grade completely. In a stratified sample, you would first divide the population into groups (the strata), then take a random sample within each stratum (that’s the singular form of “strata”). In the high school example, we could divide the population into grades, then take a random sample of three students within each grade. That would get us to the 12 students we need while ensuring coverage of each grade.
A cluster sample is a sample where clusters of units are chosen at random, instead of choosing individual units. For example, if we need a sample of college students, we may take a list of all the course sections being offered at the college, choose three of them at random (the sections are the clusters), and then survey all the students in those sections. A sample like this one has the advantage of convenience: If the survey needs to be administered in person, many of your sample units will be located in one place at the same time.
Random Sampling
For each of the following situations, identify whether the sample is a simple random sample, a systematic random sample, a stratified random sample, a cluster random sample, or none of these.
A postal inspector wants to check on the performance of a new mail carrier, so she chooses four streets at random among those that the carrier serves. Each household on the selected streets receives a survey.
A hospital wants to survey past patients to see if they were satisfied with the care they received. The administrator sorts the patients into groups based on the department of the hospital where they were treated (ICU, pediatrics, or general), and selects patients at random from each of those groups.
A quality control engineer at a factory that makes smartphones wants to figure out the proportion of devices that are faulty before they are shipped out. The phones are currently packed in boxes for shipping, each of which holds 20 devices. The engineer wants to sample 100 phones, so he selects five crates at random and tests every phone in those five crates.
A newspaper reporter wants to write a story on public perceptions on a project that will widen a congested street. She stands on the side of the street in question and interviews the first five people she sees there.
An executive at a streaming video service wants to know if her subscribers would support a second season of a new show. She gets a list of all the subscribers who have watched at least one episode of the show, and uses a random number generator to select a sample of 50 people from the list.
An agent for a state’s Department of Revenue is in charge of selecting 100 tax returns for audit. He has a list of all of the returns eligible for audit (about 12,000 in all), sorted by the taxpayer’s ID number. He asks a computer to give him a random number between 1 and 120; it gives him 15. The agent chooses the 15th, 135th, 255th, 375th, and every 120th return after that to be audited.
Solution
To decide which type of random sample is being used in each of these, we need to focus on how the randomization is being incorporated.
The surveys are being given to households, so households are the units in this case. But households aren’t being chosen randomly; instead, streets are being chosen at random. These form clusters of units, so this is a cluster random sample.
In this case, the administrator isn’t selecting patients at random from the entire list of patients. Instead, she is choosing at random from the patients who were in each of the departments (ICU, pediatrics, general) separately. The departments form strata, so this is a stratified random sample.
The engineer is testing whether the phones are faulty, so those are the units. But the random process is being used to select the crates of phones. Those crates form clusters, so this is a cluster random sample.
The reporter isn’t using a random process at all, so this sample doesn’t belong to any of the types we have been talking about. A sample like this one is sometimes described as a convenience sample, and shouldn’t be used in a statistical setting.
The executive is choosing her sample completely at random from the full population, so this is a simple random sample.
The agent is choosing from the full population, but is only choosing the first unit for the sample at random; the rest are chosen by skipping down the list systematically. Thus, this is a systematic random sample.