---
title: 'MD2sample: R Package for Two-Sample Testing'
url: https://www.emergentmind.com/topics/md2sample
type: topic
---

# MD2sample: R Package for Two-Sample Testing

Searching arXiv for the supplied MD2sample-related papers and closely related records.
MD2sample is an R package for nonparametric multivariate two-sample testing, designed to run and compare a large collection of tests for continuous and discrete data and to support systematic power studies across diverse alternatives [2605.12089]. In the two-sample framework, one observes independent samples \(X_1,\dots,X_n \sim F\) and \(Y_1,\dots,Y_m \sim G\), with the null hypothesis \(H_0: F = G\) against the general alternative \(H_1: F \neq G\) [2507.16630]. The package is positioned around a central empirical conclusion: for multivariate data, no single method can be relied upon to provide good power uniformly across all alternatives, so MD2sample emphasizes breadth of methodology, comparative evaluation, and short lists of complementary tests [2605.12089].

## 1. Statistical setting and motivation

MD2sample addresses the multivariate nonparametric two-sample problem in settings where reliable parametric models for \(F\) and \(G\) are unavailable or undesirable [2507.16630]. The package is intended for continuous data in arbitrary dimension and for discrete data in two dimensions, including binned or histogram representations [2605.12089]. The associated power studies explicitly examine \(d=2\) and \(d=5\) continuous settings and \(2\)-dimensional discrete settings [2605.12089].

The methodological motivation is the heterogeneity of alternatives. Differences between \(F\) and \(G\) may appear in marginals, dependence, covariance structure, local clustering, or mixture composition [2507.16630]. The supplied studies distinguish cases with equal marginals, where only dependence changes, from cases with unequal marginals, where mean, variance, or marginal family changes occur [2605.12089]. This distinction matters because univariate marginal tests can completely miss dependence changes, especially in the “D2” and “D5” classes of scenarios described in the studies [2507.16630].

A further motivation is that multivariate methods differ materially in what they detect well. EDF-based procedures may respond strongly to marginal differences; distance- and graph-based methods may be more effective for dependence changes or local structure; chi-square procedures can be strong when binning captures relevant features [2605.12089]. This suggests that MD2sample is less a single test than a comparative framework for selecting and combining tests with complementary sensitivity profiles.

## 2. Scope of the package and computational design

MD2sample is available on CRAN and is designed to provide a large collection of multivariate two-sample tests in a unified framework [2507.16630]. Its stated purposes include two-sample testing for multivariate data, support for power studies and simulation experiments, and the ability to combine multiple tests with multiplicity-corrected \(p\)-values [2507.16630]. The package also allows the addition of user-defined tests for comparison in simulation workflows [2605.12089].

For continuous data, MD2sample implements EDF-based, nearest-neighbor, energy- or distance-based, graph-based, Ball divergence, and binning-based chi-square procedures [2507.16630]. For discrete data, it includes discrete analogues of KS, Kuiper, CvM, AD, nearest-neighbor methods, Aslan–Zech, Baringhaus–Franz, and a chi-square test on discrete cell counts [2507.16630]. In the broader workflow described in the 2026 paper, MD2sample is also used as the two-sample engine for hybrid goodness-of-fit tests in combination with MDgof, where a Monte Carlo sample from a fitted null model is compared to observed data [2605.12089].

Implementation emphasizes computational practicality. The package uses Rcpp and parallel programming to handle computationally demanding distance calculations, graph constructions, permutations, and repeated simulations [2605.12089]. For some procedures, large-sample formulas or asymptotic approximations are used instead of full permutation calibration, which is particularly relevant for very large datasets [2507.16630]. Permutation tests are used for \(p\)-value computation for essentially all two-sample methods in the 2026 description unless otherwise stated, while the 2025 description identifies asymptotic or large-sample calibration for chi-square, FR, CF1–CF4, Ball divergence, and NN0 [2605.12089; 2507.16630]. This difference in emphasis reflects the package’s dual role as both a simulation platform and an applied testing toolkit.

## 3. Families of tests implemented in MD2sample

MD2sample collects several distinct classes of nonparametric two-sample procedures, each defined by a different representation of discrepancy between \(F\) and \(G\) [2507.16630].

### EDF-based tests

For empirical distribution functions \(\hat F\), \(\hat G\), and pooled \(\hat H\), MD2sample implements multivariate versions of Kolmogorov–Smirnov, Kuiper, Cramér–von Mises, and Anderson–Darling [2507.16630]. In the pooled-support formulation reported in the studies, the statistics are

\[
T_{\text{KS}} = \max\{\vert \hat{F}(z_i) - \hat{G}(z_i)\vert;\; z_1,\dots,z_{n+m}\},
\]

\[
T_{\text{K}} = \max\{\hat{F}(z_i)-\hat{G}(z_i)\} - \min\{\hat{F}(z_i)-\hat{G}(z_i)\},
\]

\[
T_{\text{CvM}} = \sum_{i=1}^{n+m} \left(\hat{F}(z_i)-\hat{G}(z_i)\right)^2,
\]

\[
T_{\text{AD}} = \sum_{i=1}^{n+m} \frac{\left(\hat{F}(z_i)-\hat{G}(z_i)\right)^2}{\hat{H}(z_i)\bigl(1-\hat{H}(z_i)\bigr)}.
\]

The studies report that these EDF-type tests tend to work well for simple differences in distribution, especially when marginals differ, but can be weaker for pure dependence changes when marginals are equal [2605.12089].

### Nearest-neighbor procedures

Nearest-neighbor procedures in MD2sample include NN1, NN5, and a simple nearest-neighbor test denoted NN0 or NN [2507.16630]. For NN1 and NN5, each observation’s \(k\) nearest neighbors in the pooled sample are examined, and the statistic is based on the average number of same-sample neighbors for both samples [2507.16630; 2605.12089]. Under \(H_0\), labels are exchangeable; under \(H_1\), within-sample clustering increases the statistic [2605.12089]. The simple nearest-neighbor test counts points in the first sample whose nearest neighbor is also in the first sample and uses an approximate binomial distribution under \(H_0\) for large samples [2507.16630].

These tests are described as particularly sensitive to local clustering differences and dependence changes, and NN5 is repeatedly identified as a stable compromise between locality and variance [2605.12089].

### Energy- and distance-based tests

The package implements the Aslan–Zech, Baringhaus–Franz, and Biswas–Ghosh tests [2507.16630]. Using Euclidean distance \(\|\cdot\|\), the Aslan–Zech statistic is

\[
\begin{aligned}
T_{\text{AZ}} &=
\frac{1}{nm}\sum_{i=1}^n \sum_{j=1}^m \log\bigl(\|x_i-y_j\|\bigr) \\
&\quad - \frac{1}{n^2}\sum_{i=1}^n \sum_{i<j} \log\bigl(\|x_i-x_j\|\bigr) \\
&\quad - \frac{1}{m^2}\sum_{i=1}^m \sum_{i<j} \log\bigl(\|y_i-y_j\|\bigr),
\end{aligned}
\]

the Baringhaus–Franz statistic is

\[
\begin{aligned}
T_{\text{BF}} &=
\frac{nm}{n+m}\Bigg[
\frac{1}{nm}\sum_{i=1}^n \sum_{j=1}^m \sqrt{\|x_i-y_j\|} \\
&\qquad + \frac{1}{n^2}\sum_{i=1}^n \sum_{i<j} \sqrt{\|x_i-x_j\|} \\
&\qquad - \frac{1}{m^2}\sum_{i=1}^m \sum_{i<j} \sqrt{\|y_i-y_j\|}
\Bigg],
\end{aligned}
\]

and the Biswas–Ghosh statistic is based on

\[
B_{xy} = \frac{1}{nm}\sum_{i=1}^n \sum_{j=1}^m \sqrt{\|x_i-y_j\|},
\]

\[
B_{xx} = \frac{2}{n(n-1)}\sum_{i=1}^n \sum_{i<j} \sqrt{\|x_i-x_j\|},
\qquad
B_{yy} = \frac{2}{m(m-1)}\sum_{i=1}^m \sum_{i<j} \sqrt{\|y_i-y_j\|},
\]

with

\[
T_{\text{BG}} = \bigl(B_{xx}-B_{xy}\bigr)^2 + \bigl(B_{yy}-B_{xy}\bigr)^2.
\]

These procedures compare cross-sample and within-sample distances in different ways [2507.16630]. The studies characterize AZ as very strong in \(2\)D continuous settings and BG as particularly designed to work well in higher dimensions, including cases where other tests lose power [2605.12089].

### Kernel-based testing via MMD

The 2026 study adds a kernel-based procedure, Maximum Mean Discrepancy, to the MD2sample-centered comparison [2605.12089]. For a positive definite kernel \(k(\cdot,\cdot)\),

\[
\text{MMD}^2(F,G) = \mathbb{E} k(X,X') + \mathbb{E}k(Y,Y') - 2 \mathbb{E}k(X,Y),
\]

with unbiased empirical estimator

\[
\widehat{\text{MMD}^2} =
\frac{1}{n(n-1)}\sum_{i\neq j} k(x_i,x_j) +
\frac{1}{m(m-1)}\sum_{i\neq j} k(y_i,y_j) -
\frac{2}{nm}\sum_{i=1}^n\sum_{j=1}^m k(x_i,y_j).
\]

MD2sample is described there as implementing an aggregated MMD using \(10\) different bandwidths, usually Gaussian kernels with different \(\sigma\), following the “MMD aggregated two-sample test” of Schrab et al. 2023 [2605.12089]. In that study, MMD is identified as the single best general-purpose two-sample test for continuous data [2605.12089].

### Graph-based and ball-based procedures

MD2sample includes the Friedman–Rafsky MST test, Chen–Friedman tests CF1–CF4 via the gTests methodology, and Ball divergence via the Ball package [2507.16630]. Friedman–Rafsky constructs the minimum spanning tree on the pooled sample and evaluates how strongly the labels mix across edges; Chen–Friedman procedures use graph counts on MST or \(k\)-nearest-neighbor-type constructions; Ball divergence compares distributions of distances inside metric balls [2507.16630; 2605.12089]. These are omnibus procedures and are reported as competitive in complex shape and dependence changes, with CF2 sometimes strong in certain Dalitz-type or structured alternatives [2605.12089].

### Chi-square and discrete variants

For \(2\)-dimensional data, MD2sample includes chi-square tests based on equal-size or equal-probability binning, typically \(5 \times 5\) in the reported studies [2507.16630]. For pooled cell counts \(Z_{ij} = O_{ij}+M_{ij}\), sample totals \(N_1\), \(N_2\), and \(N=N_1+N_2\), the statistic given in the 2026 study is

\[
T_{\chi^2}
= \sum_{i,j} \frac{\big(O_{ij} - N_1 Z_{ij}/N\big)^2}{N_1 Z_{ij}/N}
+ \sum_{i,j} \frac{\big(M_{ij} - N_2 Z_{ij}/N\big)^2}{N_2 Z_{ij}/N}.
\]

The package merges bins until each contains at least \(5\) observations from the combined sample to satisfy chi-square validity, and the papers note that power is often maximized using a relatively small number of bins, regardless of sample size [2507.16630]. Discrete analogues of EDF, nearest-neighbor, and energy tests are also implemented for \(2\)D discrete data [2605.12089].

## 4. Simulation studies and empirical evaluation

The empirical characterization of MD2sample is grounded in extensive simulation studies [2605.12089]. The 2025 paper reports \(34\) case studies in \(2\)D, together with \(2\)D discrete and \(5\)D continuous scenarios [2507.16630]. The 2026 paper broadens the discussion to goodness-of-fit as well as two-sample settings and explicitly states that the studies were carried out using MD2sample and MDgof [2605.12089].

The simulated alternatives cover multivariate normal and \(t\) families, uniform distributions on rectangles, copulas including Frank, Clayton, Gumbel, Galambos, Joe, Husler–Reiss, and mixtures, as well as Dalitz plot examples motivated by high-energy physics [2605.12089; 2507.16630]. The case taxonomy distinguishes equal-marginal dependence changes from unequal-marginal alternatives such as mean shifts, variance changes, or changes in marginal family [2507.16630]. Discrete scenarios are produced either intrinsically or by binning continuous scenarios into \(5 \times 5\) grids [2605.12089].

The studies target nominal significance level \(\alpha = 0.05\) [2605.12089]. For the 2026 power comparisons, typical sample sizes are \(n = m = 100\) or \(n = m = 200\), with \(200\) used for reported power comparisons, and the alternative parameters are chosen so that the best method reaches approximately \(80\)–\(90\%\) power at \(\alpha=0.05\) [2605.12089]. Thousands of replications per scenario are used to estimate power reliably, and rejection frequencies are summarized both as mean power across scenarios and as the proportion of scenarios in which a method is within \(90\%\) of the best power for that scenario [2605.12089; 2507.16630].

A central methodological conclusion is repeated across both papers: there is no universally best test [2605.12089; 2507.16630]. A method can be excellent for mean shifts and poor for subtle dependence changes, or superb on some copula alternatives and almost powerless on others [2507.16630]. MD2sample’s comparative structure is therefore integral to its purpose rather than an accessory feature.

## 5. Main findings on power and recommended short lists

The reported recommendations depend on data type and dimension.

For continuous \(2\)-dimensional data, the 2026 study reports mean power \(90\) for MMD, \(87\) for AZ, \(79\) for ES chi-square, \(75\) for EP chi-square, \(77\) for BF, \(66\) for AD, \(63\) for NN5, \(59\) for CvM, \(57\) for Ball, and \(56\) for FR, CF1, and CF3 [2605.12089]. The corresponding “percentage close to best” values are \(91\%\) for MMD, \(85\%\) for AZ, \(53\%\) for ES, \(41\%\) for EP, and \(38\%\) for BF [2605.12089]. The same paper therefore characterizes MMD as the single most consistently powerful method in \(2\)D and AZ as the second-best, while noting that BG remains useful in specific scenarios such as complex Dalitz-type shapes [2605.12089].

The 2025 study, which predates the inclusion of MMD in the summarized comparison, gives a different ranking for continuous cases: AZ \(82.6\%\), ES \(80.8\%\), EP \(76.1\%\), BF \(71.5\%\), AD \(64.5\%\), NN5 \(63.2\%\), CvM \(58.1\%\), KS \(53.8\%\), and Ball \(53.0\%\), with BG at \(31.8\%\) mean power but sometimes essential [2507.16630]. In that study AZ is within \(90\%\) of the best power in \(74\%\) of cases, far above other methods [2507.16630]. This suggests that AZ was the most robust performer among the then-compared methods, whereas the 2026 study’s addition of aggregated MMD shifts the overall default recommendation toward MMD for continuous data.

For continuous \(5\)-dimensional data, the 2026 study reports mean power \(82\) for MMD, \(74\) for AZ, \(67\) for BF, \(57\) for AD, \(57\) for Ball, \(54\)–\(53\) for KS, CvM, K, and NN5, and \(38\) for BG [2605.12089]. Despite BG’s modest mean power, the smallest set of methods such that for every scenario at least one is best is reported as **BG + MMD + NN5** [2605.12089]. The interpretation given is that MMD dominates on average, while BG and NN5 are essential for certain pathological or high-dimensional alternatives [2605.12089].

For equal marginals, where only dependence changes, graph- and distance-based tests such as MMD, AZ, BG, NN5, and FR generally outperform EDF types [2605.12089]. With unequal marginals, EDF tests and binning-based chi-square become more competitive, but MMD and AZ remain very strong [2605.12089].

For \(2\)-dimensional discrete data, the 2026 study reports mean power \(79\) for chi-square, \(53\) for NN, \(49\) for AD, \(47\) for Kuiper, \(43\) for KS and CvM, and \(27\) and \(20\) for AZ and BF respectively [2605.12089]. The 2025 study gives closely aligned figures: chi-square \(79.4\%\), NN \(53.2\%\), AD \(49.2\%\), K \(47.4\%\), KS \(43.5\%\), CvM \(42.9\%\), AZ \(26.5\%\), and BF \(20.2\%\) [2507.16630]. Both sources therefore identify chi-square as clearly strongest on average for the studied discrete problems [2605.12089; 2507.16630].

The recommendations in the two papers differ in detail. The 2025 study recommends for continuous data the set **AZ**, **chi-square with small number of bins (ES, \(5 \times 5\) in \(2\)D)**, **AD**, **NN5**, and **BG**; for discrete data it recommends **chi-square**, **AZ**, **CvM**, and **KS** [2507.16630]. The 2026 study recommends for two-sample continuous \(d=2\) data **AZ**, **BG**, and **MMD**; for continuous \(d>2\), **BG**, **MMD**, and **NN5**; and for discrete \(d=2\), **AZ**, **AD**, **Kuiper**, and **chi-square** [2605.12089]. The difference reflects the broader method set and updated comparative evidence in the later paper.

## 6. Practical use, limitations, and relation to adjacent tools

The studies describe a standard MD2sample workflow. For continuous data, inputs are matrices \(x\) and \(y\) of sizes \(n \times d\) and \(m \times d\); for discrete \(2\)D data, inputs may be count vectors together with bin centers or cell coordinates [2605.12089]. Tests compute the chosen statistic, generate \(p\)-values by permutation or large-sample calibration as appropriate, and can be embedded in repeated simulation loops for type I error and power estimation [2507.16630; 2605.12089]. The package contains built-in case studies and stored power-study results, and it supports test combination with multiplicity correction [2507.16630; 2605.12089].

The reported guidance is pragmatic. If a single default test is needed for continuous data, the 2026 study recommends MMD [2605.12089]. If robustness across alternatives is the goal, the recommendation is to run a small complementary panel such as MMD + BG + AZ in \(2\)D or MMD + BG + NN5 in \(5\)D or higher, optionally combining \(p\)-values [2605.12089]. For discrete \(2\)D data, chi-square is the default, complemented by AD and Kuiper, with AZ as an optional addition [2605.12089]. The 2025 study similarly advises applying a battery of tests rather than relying on one method, especially when local structure, covariance stretch, or dependence changes are plausible [2507.16630].

Several limitations are explicit. The systematic simulations extend only to \(d=5\), so performance in much higher dimensions remains unstudied in these papers [2605.12089]. Discrete methods are implemented only for \(2\)D because higher-dimensional binning rapidly becomes impractical [2605.12089]. The discrete conclusions are specific to the chosen \(5 \times 5\) equal-size or related binning schemes, and the 2025 paper explicitly cautions that some methods, notably AZ, may have much better power under different binning choices [2507.16630]. Sample-size imbalance is not deeply explored, and computational cost remains nontrivial for full distance matrices, MST construction, and repeated permutations on large samples [2507.16630; 2605.12089].

MD2sample is also closely related to MDgof. In the 2026 study, MD2sample serves as the two-sample testing engine for hybrid goodness-of-fit procedures in which an observed sample is compared to a Monte Carlo sample from a fitted null model, with MC sample sizes equal to the data size or five times larger [2605.12089]. This places MD2sample at the intersection of classical two-sample testing and simulation-based goodness-of-fit assessment.

Taken together, the supplied papers define MD2sample as a research-oriented package whose distinguishing feature is not merely the number of implemented tests, but the empirical program accompanying them: a large simulation library, explicit power comparisons, and concrete short-list recommendations that acknowledge the absence of any uniformly dominant multivariate two-sample procedure [2605.12089; 2507.16630].

Source: https://www.emergentmind.com/topics/md2sample