# NOT RUN {
#Generate a dataset consisting of measures supported on discretized nested ellipses.
N<-5 #The number of measures generated
M<-20 #The number of points each ellipse is discretized into
C<-2 #The parameter of the Kantorovich-Rubinstein distance
data.list<-vector("list",N)
set.seed(42)
nest.vec<-rep(0,N) #A vector containing the number of ellipses in the data measures.
max.ell<-3 #The maximum number of ellipses in each measure. The number is chosen
#uniformly between 1 and this value.
#This loop actually generates the measures for the example.
for (i in 1:N){
pos.full<-matrix(0,0,2)
nesting.depth<-ceiling(runif(1,0,max.ell))
nest.vec[i]<-nesting.depth
for (k in 1:nesting.depth){
t.vec<-seq(0,2*pi,length.out=M)
pos<-cbind(cos(t.vec)*runif(1,0.2,1),sin(t.vec)*runif(1,0.2,1))/(3^(k-1))
theta<-runif(1,0,2*pi)
rotation<-matrix(c(cos(theta),sin(theta),-1*sin(theta),cos(theta)),2,2)
pos.full<-rbind(pos.full,pos%*%rotation)
}
W<-rep(1,M*nesting.depth)
data.list[[i]]<-transport::wpp((pos.full+1)/2,W)
}
#Using the multiscale method
system.time(bary.ms<-WSGeometry::kr_bary(data.list,C,method="multiscale",
support=c(8,8,3,10^-2),wmaxIter=100,return_type="mat",thresh=6*10^-4,threads=1))
# }
# NOT RUN {
#Using the fixed support method
support<-t(WSGeometry::grid_positions(20,20))
system.time(bary.fixed<-WSGeometry::kr_bary(data.list,C,method="fixed",
support=support,wmaxIter=100,return_type="wpp",thresh=6*10^-4,threads=1))
#Using the free support method
support<-t(WSGeometry::grid_positions(8,8))
system.time(bary.free<-WSGeometry::kr_bary(data.list,C,method="free",
support=support,wmaxIter=100,pmaxIter=25,return_type="wpp",thresh=8*10^-4,threads=1))
#The outputs can be conveniently visualised using the image function for the "mat" output
#and the plot-method for the wpp-objects provided by the transport package.
image(bary.ms)
plot(bary.fixed)
plot(bary.free)
# }
Run the code above in your browser using DataLab