transport_cost.numeric | R Documentation |
Calculate the optimal transport cost.
## S3 method for class 'numeric' transport_cost(x, y, wx, wy, p = 1, sorted = FALSE, threshold = 1e-15, ...) transport_cost(x, ...) ## S3 method for class 'otgridtransport' transport_cost(x, threshold = 1e-15, ...) ## S3 method for class 'otgrid' transport_cost(x, ...) ## S3 method for class 'data.frame' transport_cost(x, costm, ...)
x |
a vector of points; a data frame with columns |
y |
second vector of points. |
wx |
weight vector of the first vector of points. |
wy |
weight vector of the second vector of points. |
p |
the power ≥q 1 of the cost function. |
sorted |
logical value indicating whether or not |
threshold |
small value that indicates when a value is considered to be zero. |
... |
further arguments (for |
costm |
cost matrix of the transport |
In case of two-dimensional grids, the pivot measure is used to calculate the optimal transport cost.
For one-dimensional optimal transport, the cost function is given by c(x, y) = | x - y |^p. In this case, the north-west-corner algorithm is used.
the optimal transport cost or, in case of two-dimensional case, an object of class "otgridtransport"
with element cost
that contains it.
pivot measure pivot_measure
## one-dimensional example set.seed(1) a <- 1:5 wa <- rep(1/5, 5) b <- 1:6 wb <- runif(6) wb <- wb / sum(wb) transport_cost(a, b, wa, wb, p = 1) ## two-dimensional example x <- otgrid(cbind(0:1, 1:0)) y <- otgrid(cbind(1:0, 0:1)) # first calculate pivot manually pm <- pivot_measure(x, y) pm <- transport_cost(pm) print(pm$cost) # or just pm2 <- transport_cost(x, y) print(pm2$cost) # or from transport plan and cost matrix costm <- transport_costmat(pm) tp <- transport_df(pm) print(transport_cost(tp$df, costm))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.