- Parameters:
- u_values : 2d array_like
A sample from a probability distribution or the support (set of all
possible values) of a probability distribution. Each element along
axis 0 is an observation or possible value, and axis 1 represents the
dimensionality of the distribution; i.e., each row is a vector
observation or possible value.
- v_values : 2d array_like
A sample from or the support of a second distribution.
- u_weights, v_weights : 1d array_like, optional
Weights or counts corresponding with the sample or probability masses
corresponding with the support values. Sum of elements must be positive
and finite. If unspecified, each value is assigned the same weight.
- Returns:
- distance : float
The computed distance between the distributions.
Notes
Given two probability mass functions, u
and v, the first Wasserstein distance between the distributions
using the Euclidean norm is:
l1(u,v)=π∈Γ(u,v)inf∫∥x−y∥2dπ(x,y) where Γ(u,v) is the set of (probability) distributions on
Rn×Rn whose marginals are u and
v on the first and second factors respectively. For a given value
x, u(x) gives the probability of u at position
x, and the same for v(x).
This is also called the optimal transport problem or the Monge problem.
Let the finite point sets {xi} and {yj} denote
the support set of probability mass function u and v
respectively. The Monge problem can be expressed as follows,
Let Γ denote the transport plan, D denote the
distance matrix and,
x=vec(Γ)c=vec(D)b=[uv] The vec() function denotes the Vectorization function
that transforms a matrix into a column vector by vertically stacking
the columns of the matrix.
The transport plan Γ is a matrix [γij] in
which γij is a positive value representing the amount of
probability mass transported from u(xi) to v(yi).
Summing over the rows of Γ should give the source distribution
u : ∑jγij=u(xi) holds for all i
and summing over the columns of Γ should give the target
distribution v: ∑iγij=v(yj) holds for all
j.
The distance matrix D is a matrix [dij], in which
dij=d(xi,yj).
Given Γ, D, b, the Monge problem can be
transformed into a linear programming problem by
taking Ax=b as constraints and z=cTx as minimization
target (sum of costs) , where matrix A has the form
10⋮010⋮010⋮001⋮0……⋱………⋱…10⋮000⋮101⋮010⋮001⋮001⋮0……⋱………⋱…01⋮0……⋮1……⋮………⋮…00⋮110⋮000⋮101⋮0……⋱………⋱…00⋮100⋮1 By solving the dual form of the above linear programming problem (with
solution y∗), the Wasserstein distance l1(u,v) can
be computed as bTy∗.
The above solution is inspired by Vincent Herrmann’s blog [3] . For a
more thorough explanation, see [4] .
The input distributions can be empirical, therefore coming from samples
whose values are effectively inputs of the function, or they can be seen as
generalized functions, in which case they are weighted sums of Dirac delta
functions located at the specified values.
References
Examples
0.114
],
[
0.8
,
1.5
])
174.15840245217169