*! version 1.0.0	10jul2026

Title

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

Syntax

wksmirnov varname [if] [in] [weight] , by(groupvar) [options]

by(groupvar) variable identifying the two groups to compare; required

reps(#) perform # Monte Carlo permutations; default is reps(0), i.e. skipped

seed(#) set random-number seed to #

nodots suppress permutation dots

graph plots the weighted empirical CDFs of the two groups

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

Description

wksmirnov computes a weighted two-sample Kolmogorov-Smirnov test of the equality of distributions.
This is the balance diagnostic used by the R package twang (Toolkit for Weighting and Analysis of
Nonequivalent Groups) to assess whether propensity-score weighting has equalized the distribution
of a covariate between a treatment group and a comparison group, and to drive its generalized boosted regression
stopping rules. When weights are not specified, wksmirnov produces the same results as ksmirnov.

Options

by(groupvar) identifies the two comparison groups. groupvar must
take exactly two distinct nonmissing values in the estimation sample.

reps(#) specifies the number of random permutations to perform.
# = 0 (the default) skips it.

seed(#) sets the random-number seed; see set seed.

nodots suppresses display of the permutation dots.

graph produces a step-function plot of the two weighted empirical
CDFs.

Remarks

Two p-values are available:

1. Analytic approximation. The
two-sample Kolmogorov asymptotic null distribution is evaluated using Kish's
(1965) effective sample size in each group g (g = 1 or 0),

ne_g = (sum of w_g)^2 / sum(w_g^2),

combined as en = (ne_1 * ne_0) / (ne_1 + ne_0), with the Stephens (1970)
small-sample correction

lambda = ( sqrt(en) + 0.12 + 0.11/sqrt(en) ) * D,

and p is twice the alternating sum, over k = 1, 2, 3, ..., of the terms

(-1)^(k-1) * exp(-2 * k^2 * lambda^2)

(the standard Kolmogorov tail series).

2. Permutation p-value. Weights are
rescaled within each group so the treatment group's weights sum to its own
Kish effective N (ne_1) and the comparison group's sum to its Kish
effective N (ne_0), then pooled and normalized; each of # replicates
draws trunc(ne_1 + ne_0) units with replacement from the
full pooled sample using those rescaled weights as draw probabilities,
labels the first floor(ne_1) draws "Group1" and the rest "Group0"
purely by position (not by each draw's original group), and computes an
unweighted KS statistic on that resampled, relabeled draw. The reported
p-value is the raw proportion of replicates whose resampled KS is at least
the observed D.

Stored results

wksmirnov stores the following in r():

Scalars

r(D) KS statistic

r(p) analytic p-value

r(p_perm) permutation p-value using twang's resampling method (if
reps(#) > 0)

r(n_reps) number of permutation replicates used

r(N1), r(N0) unweighted N in each group

r(effN1), r(effN0) Kish effective N in each group

Macros

r(varname) the variable tested

r(group1), r(group0) the two levels of groupvar

Examples

Load example data

. webuse cattaneo2, clear

Estimate propensity score for mbsmoke as the treatment, and generate inverse probability of treatment weights (IPTW)

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

. predict pscore, pr

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

Run wksmirnov on unweighted mage and compute analytic p-values only

. wksmirnov mage, by(mbsmoke)

Now run wksmirnov on mage using weights and compute permuted p-values

. wksmirnov mage [pweight=iptw], by(mbsmoke) reps(1000) seed(12345)

Same as above but specify that a graph also be produced

. wksmirnov mage [pweight=iptw], by(mbsmoke) reps(1000) seed(12345) graph

References

Kish, L. 1965. Survey Sampling. New York: John Wiley & Sons.

Ridgeway, G., D.F. McCaffrey, A. Morral, L. Burgette, and B.A. Griffin.
Toolkit for Weighting and Analysis of Nonequivalent Groups: A guide to the
twang package.
https://cran.r-project.org/web/packages/twang/vignettes/twang.pdf

Stephens, M.A. 1970. Use of the Kolmogorov-Smirnov, Cramer-von Mises and
Related Statistics Without Extensive Tables.
Journal of the Royal Statistical Society, Series B
32(1): 115-122.

Author

Ariel Linden

Linden Consulting Group, LLC

alinden@lindenconsulting.org

Citation of wksmirnov

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

Linden, Ariel. 2026. WKSMIRNOV: Stata module to perform a weighted two-sample Kolmogorov-Smirnov equality-of-distributions test.
Statistical Software Components Sxxxxxx, Boston College Department of Economics.

Also see

ksmirnov