kantorovich: A Function That Computes Kantorovich Distance

View source: R/ptlmapper.R

kantorovichR Documentation

A Function That Computes Kantorovich Distance

Description

This function computes Kantorovich distance betwenn two vector of numerics, or two histograms.

Usage

kantorovich(x, y, nbreaks = 100, lims = NULL, h1 = NULL, h2 = 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.

h1

An histogram.

h2

An histogram with the same breaks value as h1.

Value

The kantorovich distance between two samples.

Examples

layout(matrix(1:3, 3))
x = rnorm(100000,0,1)
y = rnorm(100000,0,2)
lims = c(min(x, y),max(x, y))
H = hist(lims, plot = FALSE, breaks = 200);
tmp_breaks = H$breaks
h1 <-  hist(x, plot = FALSE, breaks = tmp_breaks);
h2 <-  hist(y, plot = FALSE, breaks = tmp_breaks);
diff = h1$density - h2$density
cumdiff = cumsum(diff)

plot(h1$density, main="Distributions", type="l", col=2, xlab="", ylab="density")
lines(h2$density, type="l", col=4, lwd=3)
legend("topright", col=c(2,4),  legend=c("P", "Q"), lwd=2)
abline(h=0, lty=2)
axis(2, at=0)

plot(diff, type="l", main="Difference", lwd=3, xlab="", ylab="diff")
abline(h=0, lty=2)
axis(2, at=0)

plot(cumdiff, type="l", main="Cumulative Sum", lwd=3, xlab="", ylab="cumsum")
polygon(x= 1:length(cumdiff), y = cumdiff, col = "grey")
lines(cumdiff, type="l", lwd=3)
abline(h=0, lty=2)
axis(2, at=0)

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