# 2-Wasserstein Distance calculation

## Background

The 2-Wasserstein distance W is a metric to describe the distance between two distributions, representing 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$$ using $$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 quantiles from the original decomposition of $$d$$.

## Three Implementations

The package waddR 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 Cpp of the function wasserstein1d from the R package transport, which computes the original 2-Wasserstein distance $$W$$.

wasserstein_metric(x, y)
#>  2.028542

The corresponding value of the squared 2-Wasserstein distance $$d$$ is then:

wasserstein_metric(x, y)**2
#>  4.114983

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 distance in the testing procedures.

squared_wass_approx(x, y)
#>  4.179803

The third function, squared_wass_decomp, approximates the squared 2-Wasserstein distance by addding 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 #>  4.180458 #> #>$location
#>  4.114983
#>
#> $size #>  0.002307 #> #>$shape
#>  0.06316766

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

## Session Info

sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.5 LTS
#>
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
#>
#> locale:
#>   LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
#>   LC_TIME=en_US.UTF-8        LC_COLLATE=C
#>   LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
#>   LC_PAPER=en_US.UTF-8       LC_NAME=C
#>   LC_ADDRESS=C               LC_TELEPHONE=C
#>  LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#>  stats     graphics  grDevices utils     datasets  methods   base
#>
#> other attached packages:
#>
#> loaded via a namespace (and not attached):
#>   MatrixGenerics_1.2.0        Biobase_2.50.0
#>   httr_1.4.2                  bit64_4.0.5
#>   splines_4.0.3               Formula_1.2-4
#>   assertthat_0.2.1            statmod_1.4.35
#>   stats4_4.0.3                BiocFileCache_1.14.0
#>  latticeExtra_0.6-29         blob_1.2.1
#>  GenomeInfoDbData_1.2.4      yaml_2.2.1
#>  backports_1.1.10            pillar_1.4.6
#>  RSQLite_2.2.1               lattice_0.20-41
#>  glue_1.4.2                  digest_0.6.27
#>  GenomicRanges_1.42.0        RColorBrewer_1.1-2
#>  XVector_0.30.0              checkmate_2.0.0
#>  minqa_1.2.4                 colorspace_1.4-1
#>  htmltools_0.5.0             Matrix_1.2-18
#>  pkgconfig_2.0.3             zlibbioc_1.36.0
#>  purrr_0.3.4                 scales_1.1.1
#>  jpeg_0.1-8.1                lme4_1.1-25
#>  BiocParallel_1.24.0         arm_1.11-2
#>  tibble_3.0.4                htmlTable_2.1.0
#>  generics_0.0.2              IRanges_2.24.0
#>  ggplot2_3.3.2               ellipsis_0.3.1
#>  SummarizedExperiment_1.20.0 nnet_7.3-14
#>  BiocGenerics_0.36.0         survival_3.2-7
#>  magrittr_1.5                crayon_1.3.4
#>  memoise_1.1.0               evaluate_0.14
#>  nlme_3.1-150                MASS_7.3-53
#>  foreign_0.8-80              data.table_1.13.2
#>  tools_4.0.3                 lifecycle_0.2.0
#>  matrixStats_0.57.0          stringr_1.4.0
#>  S4Vectors_0.28.0            munsell_0.5.0
#>  cluster_2.1.0               DelayedArray_0.16.0
#>  compiler_4.0.3              GenomeInfoDb_1.26.0
#>  rlang_0.4.8                 nloptr_1.2.2.2
#>  grid_4.0.3                  RCurl_1.98-1.2
#>  rstudioapi_0.11             htmlwidgets_1.5.2
#>  rappdirs_0.3.1              SingleCellExperiment_1.12.0
#>  bitops_1.0-6                base64enc_0.1-3
#>  rmarkdown_2.5               boot_1.3-25
#>  gtable_0.3.0                abind_1.4-5
#>  DBI_1.1.0                   curl_4.3
#>  R6_2.4.1                    gridExtra_2.3
#>  knitr_1.30                  dplyr_1.0.2
#>  bit_4.0.4                   Hmisc_4.4-1
#>  stringi_1.5.3               parallel_4.0.3
#>  Rcpp_1.0.5                  vctrs_0.3.4
#>  rpart_4.1-15                png_0.1-7
#>  coda_0.19-4                 dbplyr_1.4.4
#>  tidyselect_1.1.0            xfun_0.18