# \donttest{
#----------------------------------------------------------------------
# Binned from Gaussian and Uniform
#
# Create two types of histograms with the same binning. One is from
# the standard normal and the other from uniform distribution in [-5,5].
#----------------------------------------------------------------------
# GENERATE 20 HISTOGRAMS
set.seed(100)
hist20 = list()
bk = seq(from=-10, to=10, length.out=20) # common breaks
for (i in 1:10){
hist20[[i]] = hist(stats::rnorm(100), breaks=bk, plot=FALSE)
hist20[[i+10]] = hist(stats::runif(100, min=-5, max=5), breaks=bk, plot=FALSE)
}
# COMPUTE THE PAIRWISE DISTANCE
pdmat = array(0,c(20,20))
for (i in 1:19){
for (j in (i+1):20){
pdmat[i,j] = histdist(hist20[[i]], hist20[[j]], p=2)$distance
pdmat[j,i] = pdmat[i,j]
}
}
# VISUALIZE
opar <- par(no.readonly=TRUE)
par(pty="s")
image(pdmat, axes=FALSE, main="Pairwise 2-Wasserstein Distance between Histograms")
par(opar)
# }
Run the code above in your browser using DataLab