# Kolmogorov–Smirnov two-sample test

`stats_ks_two_sample` · Runs: On the server · Level: Checked against a reference: SciPy 1.18.1 ks\_2samp (exact), exact rational path counts and a second implementation in Python

Compares two samples with the Kolmogorov-Smirnov test, giving D and its exact two-sided p-value.

Research and education software; not medical advice, and not for diagnosis or treatment decisions. It computes from the inputs it receives. It does not check the person's data, design or sample.

[Catalogue](https://www.drybench.com/mcp/catalogue) / [Statistics](https://www.drybench.com/mcp/catalogue/statistics)

[Ask your assistant](https://www.drybench.com/mcp/connect)

```cite
Kolmogorov–Smirnov two-sample test (stats_ks_two_sample), Drybench MCP server 1.0.0; stats-ks-two-sample@1.0.1 (Checked against a reference); https://www.drybench.com/mcp/tools/stats-ks-two-sample
```

## What it is

Returns the two-sample Kolmogorov–Smirnov statistic D and its exact two-sided p-value. Use when asking whether two independent samples of a continuous measurement differ anywhere in their distributions, not only in location. Not for a question about medians or location alone: use \`stats\_column\_statistics\` for the rank-sum (Mann–Whitney) test. Two-sided only, up to 10,000 values per sample; p is given only when replicates is 'independent' (each value an independent biological replicate), any tied value withholds it, and a p below 1e-300 is given as a bound. A measurement's unit is required, never assumed: any unit of the kit's table, returned in its canonical spelling; a unit group takes one unit, never converted.

Field: [Statistics and design](https://www.drybench.com/mcp/catalogue/statistics).

## What it does

**You give**

- `x` (required): The first sample's values, as { values, unit }. Unit group sample (absolute).
- `y` (required): The second sample's values, as { values, unit }, in the same unit as the first. Unit group sample (absolute).
- `replicates` (required): What one value is: 'independent' (independent biological replicates; opens the p-value), 'technical' (technical replicates) or 'unspecified'. Required: never inferred from the data.
- `alternative` (required): 'two-sided', the only alternative in this version; one-sided tests ('less', 'greater') are not offered and are refused.

No request is recorded for this tool.

**Limits**

- Research statistics and experimental-design arithmetic on the data, margins, prevalence and settings you supply.
- Not for diagnosis or treatment decisions and not a regulatory analysis.
- It does not choose a design, a margin, a cut-off or a sample size for you.
- Two-sided and exact only, for independent replicates; any tied value withholds the p-value.
- A p above your level means no difference was shown at these sizes, not that the distributions are the same.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.

### Every input, with its units

4 inputs

- `x` (required): The first sample's values, as { values, unit }. Unit group sample (absolute).
- `y` (required): The second sample's values, as { values, unit }, in the same unit as the first. Unit group sample (absolute).
- `replicates` (required): What one value is: 'independent' (independent biological replicates; opens the p-value), 'technical' (technical replicates) or 'unspecified'. Required: never inferred from the data.
- `alternative` (required): 'two-sided', the only alternative in this version; one-sided tests ('less', 'greater') are not offered and are refused.

## The calculation

The method's equations, constants and worked example.

### [Two-sample Kolmogorov–Smirnov test, two-sided, with the exact null distribution](https://www.drybench.com/methods/m/stats-ks-two-sample/1.0.1)

`stats-ks-two-sample@1.0.1`

**Equation 1**

```text
D = max over the pooled values v of |F_m(v) − G_n(v)|, F_m(v) = #{xᵢ ≤ v}/m, G_n(v) = #{yⱼ ≤ v}/n
held exactly as N/(m·n), N = max |i·n − j·m| over the observed path, i and j the numbers of first- and second-sample values at or below each pooled value in ascending order
```

| Symbol | Meaning |
| --- | --- |
| `D` | statistic |
| `i` | first-sample values at or below the pooled value |
| `j` | second-sample values at or below the pooled value |
| `m` | first sample's size |
| `n` | second sample's size |

Source 1.

**Equation 2**

```text
under H0 with all values distinct and the values declared independent replicates (replicates 'independent'
p is withheld for 'technical' or 'unspecified'), each of the C(m+n, m) orderings of the pooled sample is equally likely
each ordering is a lattice path from (0, 0) to (m, n), one step in i for each first-sample value and one in j for each second-sample value in ascending order, and D is the largest |i/m − j/n| it reaches
```

| Symbol | Meaning |
| --- | --- |
| `C(m+n, m)` | number of orderings |
| `i, j` | path coordinates: first- and second-sample values taken so far |

Source 1.

**Equation 3**

```text
C_{i,j} = 1 if |i·n − j·m| ≥ N
0 if inside and i = 0 or j = 0
otherwise C_{i−1,j}·i/(i+j) + C_{i,j−1}·j/(i+j)
p = C_{m,n} = P(D ≥ D_obs). The boundary is the text's ≥, compared as integers (two source anomalies, neither followed: Listing 1 tests >, and p. 2 says the strict corridor gives "D ≤ d" where it gives D < d)
```

| Symbol | Meaning |
| --- | --- |
| `C` | proportion of paths to (i, j) leaving the corridor |
| `p` | two-sided p-value |

Source 1.

**Conventions**

- alternative = two-sided: P(D ≥ D\_obs) under the null that both samples come from one continuous distribution, with D the largest absolute gap between the two empirical distribution functions.

A definition, not a model: the equation states what the quantity means. A different convention gives a different number.

**Worked example**

Worked example: one case of stats-ks-two-sample\@1.0.1, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.

**Inputs**

- x.values\[0\] = 1
- x.values\[1\] = 2
- x.unit = 1
- y.values\[0\] = 3
- y.values\[1\] = 4
- y.unit = 1
- replicates = independent
- alternative = two-sided

**Steps**

1. d = 1, from x, y; by equation 1; gives d.
2. p = 0.33333333, from d; by equation 3; gives p.

The working, as the case writes it:

```text
D = 1
of the 6 orderings only xxyy and yyxx reach |i/m - j/n| = 1, so P(D >= 1) = 2/6 = 1/3
```

**Answer**

- d = 1
- p = 0.33333333

Where these values came from: an outside program, exact path counts and enumeration (Python fractions) 3.13.18; exact rational P by Hodges' inside recursion (Viehmann 2021, p. 2); equal to brute-force enumeration of all C(m+n, m) orderings.

At build the tool returned this answer within the tolerance the file states: p relative 1e-10 (absolute 1e-307 below 1e-297; Drybench's B4); D, m, n, the bound, statuses, codes and strings exact.

Who worked this case: not recorded.

## Sources

1. Viehmann T. Numerically more stable computation of the p-values for the two-sample Kolmogorov-Smirnov test. MathInf Technical Report 2021-1, arXiv:2102.08037v2 \[stat.CO\], 23 September 2021 Used for: Two-sample Kolmogorov–Smirnov test, two-sided, with the exact null distribution. At: section p. 1: Fm, Gn and D = sup\_x |Fm(x) − Gn(x)| (a maximum here); section section 1, p. 2: the combinatorial null and the path map; section section 1, p. 3: the stabilised recursion for the inside method, derived from Hodges' A recursion (p. 2), which Viehmann attributes to Hodges \[2\]; Hodges not read. **Read in full, 10 November 2026**

Read by AI-assisted sessions. No person has checked these readings.

## How it is checked

**Level:** Checked against a reference.

**Checked against:** An outside program, exact path counts and enumeration (Python fractions), 3.13.18; an outside program, SciPy, 1.18.1; a reference program this project wrote itself, second implementation (Python 3.13, standard library), 2026-11-14, double-precision recursion, the corridor test in integers (|i n - j m| >= N); small cases checked by the agent against exact rational enumeration; not an outside program.

**Cases:** No case set is linked to this version.

**Not checked:** One-sided alternatives; the asymptotic (Smirnov) p, not served in 1.0.0; a conditional (permutation) p for tied data; one-sample tests (not offered; Lilliefors is not in P7 1.0.0, Drybench's P7 plan, red line 1); Hodges 1957/58 and Smirnov's papers are not read (the recursion and the null are taken from Viehmann 2021, a technical report).

No independent scientist has yet reviewed this.

### Changes to its methods

0 changes

No change is logged for these method versions.
