ADescribe · Guide test 117
Two sample Kolmogorov–Smirnov test
Tests equality of two distributions using their largest empirical cumulative difference.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Compare distributions (two or more groups) — Do the groups come from the same distribution — anywhere, not only in their centers?
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
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how D varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .002).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- empirical CDF of sample j
- 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 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
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.
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))
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
- SciPy — SciPy · ks_2sampdocs.scipy.org
- scipy.stats.ks_2sampdocs.scipy.org
- 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.