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\).
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.
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.
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\).
The corresponding value of the squared 2-Wasserstein distance \(d\) is then computed as:
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.
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.
The waddR
package
Two-sample tests to check for differences between two distributions
Detection of differential gene expression distributions in scRNAseq data
sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.5 LTS
#>
#> Matrix products: default
#> BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB LC_COLLATE=C
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] waddR_1.12.0
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.9 lattice_0.20-45
#> [3] assertthat_0.2.1 digest_0.6.30
#> [5] SingleCellExperiment_1.20.0 utf8_1.2.2
#> [7] BiocFileCache_2.6.0 R6_2.5.1
#> [9] GenomeInfoDb_1.34.0 stats4_4.2.1
#> [11] RSQLite_2.2.18 evaluate_0.17
#> [13] coda_0.19-4 httr_1.4.4
#> [15] pillar_1.8.1 zlibbioc_1.44.0
#> [17] rlang_1.0.6 curl_4.3.3
#> [19] minqa_1.2.5 nloptr_2.0.3
#> [21] jquerylib_0.1.4 blob_1.2.3
#> [23] S4Vectors_0.36.0 Matrix_1.5-1
#> [25] rmarkdown_2.17 splines_4.2.1
#> [27] lme4_1.1-31 BiocParallel_1.32.0
#> [29] stringr_1.4.1 RCurl_1.98-1.9
#> [31] bit_4.0.4 DelayedArray_0.24.0
#> [33] compiler_4.2.1 xfun_0.34
#> [35] pkgconfig_2.0.3 BiocGenerics_0.44.0
#> [37] eva_0.2.6 htmltools_0.5.3
#> [39] tidyselect_1.2.0 SummarizedExperiment_1.28.0
#> [41] tibble_3.1.8 GenomeInfoDbData_1.2.9
#> [43] IRanges_2.32.0 codetools_0.2-18
#> [45] matrixStats_0.62.0 fansi_1.0.3
#> [47] withr_2.5.0 dplyr_1.0.10
#> [49] dbplyr_2.2.1 MASS_7.3-58.1
#> [51] bitops_1.0-7 rappdirs_0.3.3
#> [53] grid_4.2.1 nlme_3.1-160
#> [55] jsonlite_1.8.3 arm_1.13-1
#> [57] lifecycle_1.0.3 DBI_1.1.3
#> [59] magrittr_2.0.3 cli_3.4.1
#> [61] stringi_1.7.8 cachem_1.0.6
#> [63] XVector_0.38.0 bslib_0.4.0
#> [65] filelock_1.0.2 generics_0.1.3
#> [67] vctrs_0.5.0 boot_1.3-28
#> [69] tools_4.2.1 bit64_4.0.5
#> [71] Biobase_2.58.0 glue_1.6.2
#> [73] purrr_0.3.5 MatrixGenerics_1.10.0
#> [75] abind_1.4-5 parallel_4.2.1
#> [77] fastmap_1.1.0 yaml_2.3.6
#> [79] GenomicRanges_1.50.0 memoise_2.0.1
#> [81] knitr_1.40 sass_0.4.2