Details
This is the Wolfram Language counterpart of KS2sample and KS2sample_Rcpp in the R package
KSgeneral (Dimitrova, Jia, Kaishev 2026).
Unlike the R version, which uses floating-point arithmetic and requires a tolerance parameter, the Wolfram Language implementation is designed for exact arithmetic throughout and returns an exact rational p-value.
KS2Sample[data1,data2, …] tests the null hypothesis H0: F(x) = G(x) for all x, where F and G are the unknown underlying CDFs of data1 and data2 respectively. Both samples may come from continuous, discrete, or mixed distributions and may contain repeated observations (ties).
The two-sample KS statistics are

(two-sided),

(one-sided greater), and

(one-sided less), where
Fm(t),
Gn(t) and
Em+n(t) are the empirical CDF of the
data1,
data2 and the pooled sample
Zm+n={
data1,
data2} respectively and
W is the weight function.
The option "Alternative" controls the direction of the test. "TwoSided" (default) uses
Δm,n; "Greater" uses
Δm,n+ to test
H1: F(x) > G(x); "Less" uses
Δm,n- to test
H1: F(x) < G(x).
weight=Function[t,1/Sqrt[t(2-t)]]The option
"Weight" controls the weight function W.
"Weight"→ 0 (default) gives the standard unweighted KS test (
W(t)=1).
"Weight"→ν, for
ν ∈ (0, 1], gives the parametric family
W(t) = 1/[t(1−t)])ν due to Finner and Gontscharuk (2018);
ν = 0.5 recovers the Anderson-Darling weight of Canner (1975). A user-supplied function enables arbitrary weights, e.g. “Weight”
→ Function[t,1/Sqrt[t(2-t)]] for the Büning (2001) weight suited to left-skewed heavy-tailed data.
The data-interface form ResourceFunction["KS2Sample"][data1, data2, …] returns the list {d,p} where d is the observed KS statistic and p is the exact rational p-value. The low-level form ResourceFunction["KS2Sample"][m,n,multiplicity,q] returns only the p-value as an exact rational number.
In the low-level form,
multiplicity is an integer list of length k (the number of distinct values in the pooled sample) where
multiplicity[[i]] is the number of times the i-th distinct pooled-sample value appears.
Total[multiplicity] must equal
m+n. When there are no ties,
multiplicity=ConstantArray[1,m+n]. The weight function is specified via the
"Weight" option. It is evaluated at
i/(m+n) for
i = 1, …, m+n-1 internally.
The p-value is the exact conditional probability P(Dm,n≥q|Zm+n), where Dm,n is permutation version of the selected KS statistic (Δm,n, Δm,n+ or Δm,n-) obtained by randomly repartitioning the pooled sample Zm+n into two groups of sizes m and n. By Theorem 2.1 of Dimitrova, Jia, Kaishev (2026), this permutation p-value is asymptotically consistent with the unconditional p-value for arbitrary underlying distributions F and G.
The algorithm implements the recurrence Bs(i,j)=1s(i,j)[Bs(i-1,j)+Bs(i,j-1)] on the integer grid R = {(i,j): 0 ≤ i ≤ m, 0 ≤ j ≤ n}, where S is the region defined by d(i,j)< q. It runs in O(m n) time, processes one anti-diagonal per iteration, and uses vectorised slice assignment for the inner update. All arithmetic is exact; no floating-point tolerance is required.