kantorovich2D: A Function That Computes Kantorovich Distance between two 2D...

View source: R/ptlmapper.R

kantorovich2DR Documentation

A Function That Computes Kantorovich Distance between two 2D samples

Description

This function computes Kantorovich distance betwenn two 2D samples.

Usage

kantorovich2D(x, y, nbreaks = 32, lims = NULL, k1 = NULL, k2 = NULL)

Arguments

x

A vector of numerics.

y

A vector of numerics.

nbreaks

The number of breaks used to build histograms.

lims

vector of numerics of length 2 that specifies the range on which the histograms need to be built.

k1

A kernel obtains with the 'kde2d' function.

k2

A kernel obtains with the 'kde2d' function with the same n and lims as /k1/.

Value

The kantorovich distance between two 2D samples.

Examples

# # dataset
# x = cbind(rnorm(100000,0,1),rnorm(100000,0,1))
# y = cbind(rnorm(100000,0,2),rnorm(100000,0,2))
#
# # kantorovich2D algo
# nbreaks=32
# lims=c(min(x[,1], y[,1]),max(x[,1], y[,1]),min(x[,2], y[,2]), max(x[,2],y[,2]))
# k1 = MASS::kde2d(x[,1], x[,2], n=nbreaks, lims=lims)
# k2 = MASS::kde2d (y[,1], y[,2], n=nbreaks, lims=lims)
# binsizex = k1$x[2] - k1$x[1]
# binsizey = k1$y[2] - k1$y[1]
# diff = k1$z - k2$z
# cumsum2D = function(A){
#   t(apply(apply(A, 2, cumsum), 1, cumsum))
# }
# cumdiff = cumsum2D(diff)
# KD = binsizex*binsizey*binsizex*binsizey*sum(abs(cumdiff))
#
# # illustration
# layout(matrix(1:4, 2), respect=TRUE)
# persp(k1, phi = 30, theta = 20, d = 5)
# persp(k2, phi = 30, theta = 20, d = 5)
# kdiff = k1
# kdiff$z = diff
# persp(kdiff, phi = 30, theta = 20, d = 5)
# kcumdiff = k1
# kcumdiff$z = cumdiff
# persp(kcumdiff, phi = 30, theta = 20, d = 5)

fchuffar/ptlmapper documentation built on March 27, 2024, 3:28 p.m.