Two-Sample Kolmogorov-Smirnov Test

Test whether two independent samples come from the same continuous distribution, without assuming any particular shape. The two-sample KS test compares the two empirical distributions and reports the D-statistic and p-value.

Run Two-Sample KS Test →

What is the Two-Sample KS Test?

The two-sample Kolmogorov-Smirnov (KS) test is a non-parametric test that compares the distributions of two independent samples. It asks whether the two samples could plausibly have come from the same underlying continuous distribution, without assuming that distribution has any particular form.

It computes the empirical cumulative distribution function for each sample and finds the maximum vertical distance between them. This D-statistic captures the largest difference anywhere between the two distributions. Because it compares whole distributions, the test is sensitive to differences in location, spread and shape, not just the mean.

This breadth is both a strength and a limitation. The two-sample KS test can detect any kind of distributional difference, but that also means a significant result does not tell you how the distributions differ. If you specifically want to compare central tendency, the Mann-Whitney U test is more focused and usually more powerful for that purpose.

In plain terms: You have two groups of numbers and want to know if they come from the same distribution, differing not just in average but in spread or shape too. The KS test finds the biggest gap between the two groups' cumulative curves. A big gap means the distributions genuinely differ somewhere.

Key Points

Whole-Distribution Comparison

Compares the entire distributions of two samples, sensitive to differences in location, spread and shape.

Distribution-Free

Assumes no particular distributional form, making it broadly applicable to continuous data.

General but Unfocused

Detects any difference, but a significant result does not say how the distributions differ.

Key Formulas

D = max | F1,n(x) − F2,m(x) |
F1,n, F2,m = empirical CDFs of the two samples
n, m = the two sample sizes
p-value from the two-sample KS distribution

Interpreting the Result

The null hypothesis is that both samples come from the same distribution. A small p-value means the largest gap between their cumulative distributions is bigger than chance would produce, so the distributions differ.

Because the test responds to any distributional difference, follow a significant result by inspecting the data (histograms, summary statistics) to see whether the difference is in center, spread or shape. If your question is specifically about central tendency, the Mann-Whitney U test targets that directly.

Assumptions & Validation

Independent Samples

The two samples are independent of each other.

If violated: Paired data requires a different approach.

Continuous Data

The data are continuous; the test is conservative for discrete data.

If violated: Use tailored methods for discrete distributions.

Random Sampling

Each sample is a random sample from its population.

If violated: Address sampling bias.

⚠️ Check assumptions first

The two-sample KS test is a general test that reacts to any difference in distribution, so a significant result tells you the samples differ but not how. If your actual question is whether one group tends to have higher values than the other, the Mann-Whitney U test is more focused and typically more powerful. Use the KS test when you genuinely want to detect differences in shape or spread as well as location.

When NOT to Use Two-Sample Kolmogorov-Smirnov Test

Central Tendency Only

If you only care whether one group tends higher, the Mann-Whitney U test is more powerful and interpretable.

Fit to a Known Distribution

To test one sample against a specified distribution, use the one-sample KS test.

Categorical Data

For category counts, use a chi-square test.

Industry Applications

Distribution Comparison

Test whether two processes, batches, or conditions produce the same distribution of outcomes.

Before-After Distributions

Compare the full output distribution of a process before and after a change (independent samples).

Model vs Reality

Compare simulated output against observed data to check distributional agreement.

Group Screening

Detect any distributional difference between two groups when the type of difference is unknown.

Frequently Asked Questions

What does the two-sample KS test compare?

It compares the full distributions of two independent samples to test whether they could have come from the same underlying continuous distribution. By finding the maximum gap between the two empirical cumulative distribution functions, it is sensitive to differences in location, spread and shape simultaneously, rather than focusing only on the mean or median as some other tests do.

How is the two-sample KS test different from the Mann-Whitney U test?

The KS test detects any difference between two distributions, including differences in spread and shape, while the Mann-Whitney U test focuses specifically on whether one group tends to have larger values than the other. If your question is about central tendency or stochastic dominance, Mann-Whitney is more powerful and interpretable. If you want to detect any distributional difference, the KS test is broader.

What is the D-statistic in the two-sample test?

The D-statistic is the maximum vertical distance between the empirical cumulative distribution functions of the two samples. It identifies the point at which the two distributions differ most. A larger D indicates a greater difference between the distributions, and it forms the basis for the p-value, which accounts for both sample sizes.

Do the two samples need to be the same size?

No. The two-sample KS test handles samples of different sizes, and the p-value calculation incorporates both sample sizes. Larger samples give the test more power to detect differences. As with most tests, very small samples may lack the power to detect real distributional differences, while very large samples can make trivial differences statistically significant.

Is the two-sample KS test affected by ties?

The KS test is designed for continuous data, where exact ties should not occur. When ties are present, as with rounded or discrete data, the test becomes conservative, reducing its power to detect true differences. For heavily tied or discrete data, alternative approaches or adjustments are advisable, and the interpretation of the p-value should be cautious.

What should I do after a significant KS result?

Because the KS test signals that the distributions differ without specifying how, the next step is to inspect the data directly. Compare histograms, box plots and summary statistics of the two samples to see whether the difference lies in the center, the spread, the shape, or a combination. This turns the general finding into an actionable understanding of how the two groups actually differ.

Compare Two Full Distributions

Run a two-sample KS test to detect any distributional difference. Free during Beta.

Run Two-Sample KS Test →