*! version 1.0.0 12jul2026

Title

kstest -- Weighted two-sample Kolmogorov-Smirnov equality-of-distributions test

Syntax

kstest
varname [if] [in] [weight] , "by(varlist:groupvar)"
[,
reps(#)
seed(#)
dots
graph]

groupvar must take on
two distinct values. The distribution of varname for the first value of
groupvar is compared with that of the second value.

* "by(varlist:groupvar)" specify a binary variable that identifies the two groups

reps(#) perform # Monte Carlo permutations; default is reps(1000)

seed(#) set random-number seed to #

dots display permutation progress dots; default is off

graph plot the empirical CDFs of the two groups, with the D statistic marked

* Required

aweights, pweights, and iweights are
allowed; see weight.

Description

kstest performs a (optionally weighted) two-sample Kolmogorov-Smirnov (KS)
permutation test to assess whether two samples come from the same distribution.
The KS statistic is the single largest absolute difference between the two
empirical cumulative distribution functions (ECDFs), making it most sensitive
to a discrepancy that is large and concentrated at one location, and less
sensitive to differences spread across the distribution or concentrated in
the tails (see adtest and cvmtest for alternative tests
better suited to those cases). The permutation test provides a valid p-value
by comparing the observed statistic to its distribution under random
reassignment of group labels.

Remarks

When weights are specified, each observation's contribution to its own group's
empirical CDF is weighted by w/sum(w), the observation's weight divided
by the sum of weights in its group, in place of the unweighted contribution
1/n. The KS statistic is then the maximum absolute difference between these two
weighted step functions, exactly as in the unweighted case, and reduces to
the ordinary (unweighted) KS statistic when no weight is specified.

Additionally, when weights are specified, each observation's weight stays fixed to that
observation throughout the permutation procedure --- only group labels are
reshuffled. This treats the weight as a fixed, given attribute of the
observation, regardless of how it was generated (e.g., inverse-probability
weights under any estimand, entropy balancing weights, or survey weights),
and the test reduces exactly to the unweighted case when no weight is
specified.

Options

"by(varlist:groupvar)" is required. It specifies a binary variable
that identifies the two groups.

reps(#) specifies the number of random permutations for the test;
the default is reps(1000).

seed(#) sets the random-number seed for reproducible results.

dots displays a progress dot for each permutation replicate, with a
running count every 50; the default is to display no dots.

graph plots the (weighted) empirical CDFs of the two groups as step
functions, with the location of the D statistic marked directly on the plot.

Examples

Set-up

. webuse cattaneo2, clear

. logit mbsmoke mmarried c.mage##c.mage fbaby medu

. predict pscore, pr

. gen iptw = cond(mbsmoke, 1/pscore, 1/(1-pscore))

kstest on unweighted mage, using defaults

. kstest mage, by(mbsmoke)

kstest on mage using weights

. kstest mage [pweight=iptw], by(mbsmoke) reps(2000) seed(12345)

add the diagnostic graph, with progress dots

. kstest mage [pweight=iptw], by(mbsmoke) reps(2000) seed(12345) dots graph

Stored results

kstest stores the following in r():

Scalars

r(stat) observed (weighted) D statistic

r(p) permutation p-value

r(reps) number of permutations performed

Macros

r(group1) name of group 1

r(group2) name of group 2

r(by) the group variable

References

Kolmogorov, A. 1933.
Sulla determinazione empirica di una legge di distribuzione.
Giornale dell'Istituto Italiano degli Attuari
4: 83-91.

Linden, A. 2026.
Weighted extensions of the Kolmogorov–Smirnov,
Cramer–von Mises, and Anderson–Darling tests for
assessing covariate balance. Preprint
arXiv

Smirnov, N. 1948.
Table for estimating the goodness of fit of empirical distributions.
Annals of Mathematical Statistics
19: 279-281.

Citation of kstest

kstest is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Linden A. (2026). KSTEST: Stata module to perform a weighted two-sample Kolmogorov-Smirnov equality-of-distributions test. Statistical Software Components,
Boston College Department of Economics.

Author

Ariel Linden

President, Linden Consulting Group, LLC

alinden@lindenconsulting.org

Also see

ksmirnov, adtest (if installed), cvmtest (if installed), kuipertest (if installed), wasstest (if installed), escftest (if installed),
distcomp (if installed)