| swdist | R Documentation | 
Sliced Wasserstein (SW) Distance is a popular alternative to the standard Wasserstein distance due to its computational 
efficiency on top of nice theoretical properties. For the d-dimensional probability 
measures \mu and \nu, the SW distance is defined as 
\mathcal{SW}_p (\mu, \nu) = 
\left( \int_{\mathbb{S}^{d-1}} \mathcal{W}_p^p (
\langle \theta, \mu\rangle, \langle \theta, \nu \rangle) d\lambda (\theta) \right)^{1/p},
where \mathbb{S}^{d-1} is the (d-1)-dimensional unit hypersphere and 
\lambda is the uniform distribution on \mathbb{S}^{d-1}. Practically, 
it is computed via Monte Carlo integration.
swdist(X, Y, p = 2, ...)
| X | an  | 
| Y | an  | 
| p | an exponent for the order of the distance (default: 2). | 
| ... | extra parameters including 
 | 
a named list containing
\mathcal{SW}_p distance value.
a length-num_proj vector of projected univariate distances.
rabin_2012_WassersteinBarycenterItsT4transport
#-------------------------------------------------------------------
#  Sliced-Wasserstein Distance between Two Bivariate Normal
#
# * class 1 : samples from Gaussian with mean=(-1, -1)
# * class 2 : samples from Gaussian with mean=(+1, +1)
#-------------------------------------------------------------------
# SMALL EXAMPLE
set.seed(100)
m = 20
n = 30
X = matrix(rnorm(m*2, mean=-1),ncol=2) # m obs. for X
Y = matrix(rnorm(n*2, mean=+1),ncol=2) # n obs. for Y
# COMPUTE THE SLICED-WASSERSTEIN DISTANCE
outsw <- swdist(X, Y, num_proj=100)
# VISUALIZE
# prepare ingredients for plotting
plot_x = 1:1000
plot_y = base::cumsum(outsw$projdist)/plot_x
# draw
opar <- par(no.readonly=TRUE)
plot(plot_x, plot_y, type="b", cex=0.1, lwd=2,
     xlab="number of MC samples", ylab="distance",
     main="Effect of MC Sample Size")
abline(h=outsw$distance, col="red", lwd=2)
legend("bottomright", legend="SW Distance", 
       col="red", lwd=2)
par(opar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.