wasserstein_1d#
- class braintools.fit.losses.wasserstein_1d(p, q, x)#
Compute the Wasserstein-1 distance between two densities on a shared uniform grid.
\[W_1(p, q) = \int \left| F_p(x) - F_q(x) \right| \, dx \approx \Delta x \sum_k \left| F_p(x_k) - F_q(x_k) \right|,\]where \(F_p\) and \(F_q\) are the cumulative sums of
pandqnormalised to end at 1, so unnormalised histograms are accepted.- Parameters:
- Returns:
Scalar distance, in the units of
x(a Quantity ifxis one).- Return type:
- Raises:
ValueError – If
pis not 1-D, the shapes differ, orxhas fewer than 2 points.
See also
ks_distanceThe non-smooth counterpart.
fcd_wassersteinWasserstein distance between two FCD distributions.
Notes
Ported from
brainmass.objectives.wasserstein_1dwith identical numerics. It converges toscipy.stats.wasserstein_distance()as the grid is refined. Works underjax.jitandjax.vmapand is differentiable inpandq.Examples
>>> import jax.numpy as jnp >>> from braintools.fit import losses >>> x = jnp.arange(5.) >>> p = jnp.array([1., 0., 0., 0., 0.]) >>> q = jnp.array([0., 0., 1., 0., 0.]) >>> float(losses.wasserstein_1d(p, q, x)) 2.0