# 2-Wasserstein distance calculation

## Background

The 2-Wasserstein distance \(W\) is a metric to describe the distance between two distributions, representing e.g. two different conditions \(A\) and \(B\).

For continuous distributions, it is given by

\[W := W(F_A, F_B) = \bigg( \int_0^1 \big|F_A^{-1}(u) - F_B^{-1}(u) \big|^2 du \bigg)^\frac{1}{2},\]

where \(F_A\) and \(F_B\) are the corresponding cumulative distribution functions (CDFs) and \(F_A^{-1}\) and \(F_B^{-1}\) the respective quantile functions.

We specifically consider the squared 2-Wasserstein distance \(d := W^2\) which offers the following decomposition into location, size, and shape terms: \[d := d(F_A, F_B) = \int_0^1 \big|F^{-1}(u) - F^{-1}(u) \big|^2 du = \underbrace{\big(\mu_A - \mu_B\big)^2}_{\text{location}} + \underbrace{\big(\sigma_A - \sigma_B\big)^2}_{\text{size}} + \underbrace{2\sigma_A \sigma_B \big(1 - \rho^{A,B}\big)}_{\text{shape}},\]

where \(\mu_A\) and \(\mu_B\) are the respective means, \(\sigma_A\) and \(\sigma_B\) are the respective standard deviations, and \(\rho^{A,B}\) is the Pearson correlation of the points in the quantile-quantile plot of \(F_A\) and \(F_B\).

## Usage in two-sample setting

In case the distributions \(F_A\) and \(F_B\) are not explicitly given and information is only availbale in the form of samples from \(F_A\) and \(F_B\), respectively, we use the corresponding empirical CDFs \(\hat{F}_A\) and \(\hat{F}_B\). Then, the 2-Wasserstein distance is computed by

\[d(\hat{F}_A, \hat{F}_B) \approx \frac{1}{K} \sum_{k=1}^K \big(Q_A^{\alpha_k} - Q_B^{\alpha_k} \big) \approx \big(\hat{\mu}_A - \hat{\mu}_B\big)^2 + \big(\hat{\sigma}_A - \hat{\sigma}_B\big)^2 + 2\hat{\sigma}_A \hat{\sigma}_B \big(1 - \hat{\rho}^{A,B}\big).\]

Here, \(Q_A\) and \(Q_B\) denote equidistant quantiles of \(F_A\) and \(F_B\), respectively, at the levels \(\alpha_k := \frac{k-0.5}{K}, k = 1, \dots , K\), where we use \(K:=1000\) in our implementation. Moreover, \(\hat{\mu}_A, \hat{\mu}_B, \hat{\sigma}_A, \hat{\sigma}_B\) and \(\hat{\rho}_{A,B}\) denote the empirical versions of the corresponding quantities.

## Three implementations

The `waddR` package offers three functions to compute the 2-Wasserstein distance in two-sample settings.

We will use samples from normal distributions to illustrate them.

    library(waddR)

    set.seed(24)
    x <- rnorm(100,mean=0,sd=1)
    y <- rnorm(100,mean=2,sd=1)

The first function, `wasserstein_metric`, offers a faster reimplementation in C++ of the `wasserstein1d` function from the R package `transport`, which is able to compute general \(p\)-Wasserstein distances. For \(p=2\), we obtain the 2-Wasserstein distance \(W\).

    wasserstein_metric(x,y,p=2)
    #> [1] 2.044457

The corresponding value of the squared 2-Wasserstein distance \(d\) is then computed as:

    wasserstein_metric(x,y,p=2)^2
    #> [1] 4.179803

The second function, `squared_wass_approx`, computes the squared 2-Wasserstein distance by calculating the mean squared difference of the equidistant quantiles (first approximation in the previous formula). This function is currently used to compute the 2-Wasserstein distances in the testing procedures.

    squared_wass_approx(x,y)
    #> [1] 4.179803

The third function, `squared_wass_decomp`, approximates the squared 2-Wasserstein distance using the location, size, and shape terms from the above decomposition (second apporximation in the previous formula). It also returns the respective decomposition values.

    squared_wass_decomp(x,y)
    #> $distance
    #> [1] 4.180458
    #>
    #> $location
    #> [1] 4.114983
    #>
    #> $size
    #> [1] 0.002307
    #>
    #> $shape
    #> [1] 0.06316766

In the considered example, the decomposition results suggest that the two distributions differ with respect to location (mean), but not in terms of size and shape, thus confirming the underlying normal model.

## See also

- The `waddR` package

- Two-sample tests to check for differences between two distributions

- Detection of differential gene expression distributions in scRNAseq data

## Session Info

    sessionInfo()
    #> R version 4.6.0 Patched (2026-04-24 r89963)
    #> Platform: aarch64-apple-darwin23
    #> Running under: macOS Tahoe 26.3.1
    #>
    #> Matrix products: default
    #> BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib
    #> LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
    #>
    #> locale:
    #> [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    #>
    #> time zone: America/New_York
    #> tzcode source: internal
    #>
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base
    #>
    #> other attached packages:
    #> [1] waddR_1.26.0
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] SummarizedExperiment_1.42.0 xfun_0.57
    #>  [3] bslib_0.10.0                httr2_1.2.2
    #>  [5] Biobase_2.72.0              lattice_0.22-9
    #>  [7] Rdpack_2.6.6                vctrs_0.7.3
    #>  [9] tools_4.6.0                 generics_0.1.4
    #> [11] stats4_4.6.0                curl_7.1.0
    #> [13] parallel_4.6.0              tibble_3.3.1
    #> [15] RSQLite_2.4.6               blob_1.3.0
    #> [17] pkgconfig_2.0.3             Matrix_1.7-5
    #> [19] arm_1.15-3                  dbplyr_2.5.2
    #> [21] S4Vectors_0.50.0            lifecycle_1.0.5
    #> [23] compiler_4.6.0              Seqinfo_1.2.0
    #> [25] codetools_0.2-20            eva_0.2.6
    #> [27] htmltools_0.5.9             sass_0.4.10
    #> [29] yaml_2.3.12                 nloptr_2.2.1
    #> [31] pillar_1.11.1               jquerylib_0.1.4
    #> [33] MASS_7.3-65                 BiocParallel_1.46.0
    #> [35] SingleCellExperiment_1.34.0 cachem_1.1.0
    #> [37] DelayedArray_0.38.0         reformulas_0.4.4
    #> [39] boot_1.3-32                 abind_1.4-8
    #> [41] nlme_3.1-169                tidyselect_1.2.1
    #> [43] digest_0.6.39               purrr_1.2.2
    #> [45] dplyr_1.2.1                 splines_4.6.0
    #> [47] fastmap_1.2.0               grid_4.6.0
    #> [49] cli_3.6.6                   SparseArray_1.12.0
    #> [51] magrittr_2.0.5              S4Arrays_1.12.0
    #> [53] withr_3.0.2                 filelock_1.0.3
    #> [55] rappdirs_0.3.4              bit64_4.8.0
    #> [57] rmarkdown_2.31              XVector_0.52.0
    #> [59] matrixStats_1.5.0           bit_4.6.0
    #> [61] otel_0.2.0                  lme4_2.0-1
    #> [63] memoise_2.0.1               coda_0.19-4.1
    #> [65] evaluate_1.0.5              knitr_1.51
    #> [67] rbibutils_2.4.1             GenomicRanges_1.64.0
    #> [69] IRanges_2.46.0              BiocFileCache_3.2.0
    #> [71] rlang_1.2.0                 Rcpp_1.1.1-1.1
    #> [73] glue_1.8.1                  DBI_1.3.0
    #> [75] BiocGenerics_0.58.0         minqa_1.2.8
    #> [77] jsonlite_2.0.0              R6_2.6.1
    #> [79] MatrixGenerics_1.24.0