bundles / scipy 1.17.1 / scipy / stats / _stats_py / wasserstein_distance_nd
function
scipy.stats._stats_py:wasserstein_distance_nd
source: /scipy/stats/_stats_py.py :9387
Signature
def wasserstein_distance_nd ( u_values , v_values , u_weights = None , v_weights = None ) Summary
Compute the Wasserstein-1 distance between two N-D discrete distributions.
Extended Summary
The Wasserstein distance, also called the Earth mover's distance or the optimal transport distance, is a similarity metric between two probability distributions [1]. In the discrete case, the Wasserstein distance can be understood as the cost of an optimal transport plan to convert one distribution into the other. The cost is calculated as the product of the amount of probability mass being moved and the distance it is being moved. A brief and intuitive introduction can be found at [2].
Parameters
u_values: 2d array_likeA 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_likeA sample from or the support of a second distribution.
u_weights, v_weights: 1d array_like, optionalWeights 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: floatThe computed distance between the distributions.
Notes
Given two probability mass functions, and , the first Wasserstein distance between the distributions using the Euclidean norm is:
where is the set of (probability) distributions on whose marginals are and on the first and second factors respectively. For a given value , gives the probability of at position , and the same for .
This is also called the optimal transport problem or the Monge problem. Let the finite point sets and denote the support set of probability mass function and respectively. The Monge problem can be expressed as follows,
Let denote the transport plan, denote the distance matrix and,
The 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 in which is a positive value representing the amount of probability mass transported from to . Summing over the rows of should give the source distribution : holds for all and summing over the columns of should give the target distribution : holds for all . The distance matrix is a matrix , in which .
Given , , , the Monge problem can be transformed into a linear programming problem by taking as constraints and as minimization target (sum of costs) , where matrix has the form
By solving the dual form of the above linear programming problem (with solution ), the Wasserstein distance can be computed as .
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.
Array API Standard Support
wasserstein_distance_nd has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
Compute the Wasserstein distance between two three-dimensional samples, each with two observations.from scipy.stats import wasserstein_distance_nd wasserstein_distance_nd([[0, 2, 3], [1, 2, 5]], [[3, 2, 3], [4, 2, 5]])✓
wasserstein_distance_nd([[0, 2.75], [2, 209.3], [0, 0]], [[0.2, 0.322], [4.5, 25.1808]], [0.4, 5.2, 0.114], [0.8, 1.5])✓
See also
- wasserstein_distance
Compute the Wasserstein-1 distance between two 1D discrete distributions.
Aliases
-
scipy.stats.wasserstein_distance_nd