Skip to content

ADescribe · Guide test 117

Two sample Kolmogorov–Smirnov test

Tests equality of two distributions using their largest empirical cumulative difference.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Compare distributions (two or more groups), choose Test = Kolmogorov–Smirnov.

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Two sample Kolmogorov–Smirnov test

20
25
0.00
1.0
2.0
0.050

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 20 · mean 0.42Group 2 · n = 25 · mean −0.71

If H₀ were true

D = 0.53
  1. The reference: how D varies across samples when H₀ is true.
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .002).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

D=sup⁡x∣F1(x)−F2(x)∣D = \sup_x\left|F_1(x)-F_2(x)\right|

With your numbers

D=0.53D = 0.53
FjF_j
empirical CDF of sample j
sup⁡x\sup_x
the largest vertical gap over all x

The verdict

D = 0.530, p = .002

p ≤ α = .050: reject H₀

Effect size: largest gap between the ECDFs D = 0.530

Sensitive to any difference — location, spread, shape — but most sensitive near the middle and weak in the tails.

Statistic
0.530
p-value
0.0023
Two sample Kolmogorov–Smirnov test on Group 1: n = 20; Group 2: n = 25: D = 0.530, p = .002. The p-value is below α = .050, so the data are hard to reconcile with H0.

Two sample Kolmogorov–Smirnov test on Group 1: n = 20; Group 2: n = 25: D = 0.530, p = .002. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: two independent samples share the same distribution.

Assumptions

Independent samples; continuous distributions for standard exact formulas, or a tie-aware conditional procedure.

Formula

D=sup⁡x∣Fm(x)−Gn(x)∣D=\sup_x|F_m(x)-G_n(x)|

Parameters and notation

Fm,Gn are the two empirical CDFs; effective size is mn/(m+n).

Structure of the method

Pool two independent samples, compute both empirical CDFs at pooled support points and take their largest separation. Exact rank-path calculations, asymptotic approximations or group-label randomization provide different calibrations of the same basic discrepancy.

Estimated and nuisance parameters

The two empirical CDFs are estimated nonparametrically; there are no fitted normal means or variances. Sample sizes determine the effective scaling and the finite conditional permutation space.

Settings and design choices

Specify directional versus two-sided alternative, treatment of ties and exact/asymptotic/permutation method. Prespecify the Monte Carlo budget; larger resampling counts resolve smaller p-values but do not add information to the samples.

Fitting and adaptation

For exchangeable independent groups, randomly reassign pooled observations to labels while preserving group sizes and recompute D. This conditional calibration naturally retains observed ties. New data require recomputation rather than training. With paired, clustered or temporal observations, unrestricted label permutations are invalid; select a design-respecting alternative before examining results.

Sensitivity and robustness

At small samples the statistic takes few discrete values, limiting achievable p-values. An equality-of-distributions rejection can reflect location, spread or shape and is not automatically a median test. The preserved example uses an asymptotic option on rounded values; regard it as approximate, not exact tie-aware inference.

How to use and calibrate

Use exact two sample KS probabilities, asymptotics or label permutations; large D rejects.

Example application

Compare complete response distributions from two independent processes.

Interpretation and pitfalls

Watch out

This detects shape as well as location. Censoring and repeated measurements need other methods.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Independent continuous samples. Discrete/tied data need appropriate calibration.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
print(stats.ks_2samp(x,y,method="asymp"))
Output
KstestResult(statistic=np.float64(0.5), pvalue=np.float64(0.112), statistic_location=np.float64(5.2), statistic_sign=np.int8(-1))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
print(ks.test(x,y,exact=FALSE))
Output
	Asymptotic two-sample Kolmogorov-Smirnov test

data:  x and y
D = 0.5, p-value = 0.1641
alternative hypothesis: two-sided

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 117, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT117.