  Nonparametric two-sample testing is a classical problem in inferential
statistics. While modern two-sample tests, such as the edge count test and its
variants, can handle multivariate and non-Euclidean data, contemporary
gargantuan datasets often exhibit heterogeneity due to the presence of latent
subpopulations. Direct application of these tests, without regulating for such
heterogeneity, may lead to incorrect statistical decisions. We develop a new
nonparametric testing procedure that accurately detects differences between the
two samples in the presence of unknown heterogeneity in the data generation
process. Our framework handles this latent heterogeneity through a composite
null that entertains the possibility that the two samples arise from a mixture
distribution with identical component distributions but with possibly different
mixing weights. In this regime, we study the asymptotic behavior of weighted
edge count test statistic and show that it can be effectively re-calibrated to
detect arbitrary deviations from the composite null. For practical
implementation we propose a Bootstrapped Weighted Edge Count test which
involves a bootstrap-based calibration procedure that can be easily implemented
across a wide range of heterogeneous regimes. A comprehensive simulation study
and an application to detecting aberrant user behaviors in online games
demonstrates the excellent non-asymptotic performance of the proposed test.
