A robot-data report can compare two range distributions and obtain a distance of zero even though their mass sits at very different positions. That result may come from sending the histogram counts to an interface that expects observation values. The calculation then answers a different question correctly.
This article uses an original three-position example to separate support coordinates, counts, and density heights. The coordinates are invented range values for an offline representation check. They do not describe a deployed sensor, measured obstacle field, or navigation decision.
Write the distribution as position-and-mass pairs
Use support positions [1,4,9], interpreted as meters for the example. Distribution A has counts [6,2,0], and distribution B has counts [0,2,6]. Each contains eight observations. A therefore places three quarters of its mass at one meter and one quarter at four meters; B places one quarter at four meters and three quarters at nine meters.
The middle position has the same mass in both distributions. The remaining three quarters move from one meter to nine meters, a distance of eight meters. That transport has cost 0.75 times 8, or 6 meters. The number measures the normalized distributions in this example; it is not a total travel distance for eight separate robot motions.
SciPy's wasserstein_distance interface separates the support or sample values from their associated weights [1]. For this data, both value arguments receive the position list. The count arrays belong in the weight arguments.
import numpy as np
from scipy.stats import wasserstein_distance
positions = np.array([1.0, 4.0, 9.0])
counts_a = np.array([6, 2, 0])
counts_b = np.array([0, 2, 6])
distance = wasserstein_distance(
positions, positions, counts_a, counts_b)
assert distance == 6.0
The executed fixture used SciPy 1.18.1, NumPy 2.5.2, and Python 3.14.7. The cited SciPy page was labeled version 1.18.0. The numerical result was also checked by expanding the integer counts into repeated observations, rather than relying only on one weighted call.
Trace the zero produced by the wrong representation
Now evaluate wasserstein_distance(counts_a,counts_b), omitting both weight arguments. This treats [6,2,0] and [0,2,6] as two lists of observation values. Both lists contain exactly the same three numbers, each with equal weight. Their order differs, but their empirical distributions do not.
The returned distance is zero. It does not assert that the original range histograms agree. The range coordinates [1,4,9] never reached this second call, so the calculation cannot distinguish where the counts were located.
This failure is particularly easy to overlook when each report stores only a short vector called histogram. A vector does not establish whether its entries are observations, counts indexed by bins, normalized masses, or density heights. The input contract must retain the associated positions and units.
Check the result with expanded observations
For this small integer-count fixture, A expands to [1,1,1,1,1,1,4,4]. B expands to [4,4,9,9,9,9,9,9]. These are now actual observation-value lists for the synthetic distribution, so they may be passed without separate weights.
samples_a = np.repeat(positions, counts_a)
samples_b = np.repeat(positions, counts_b)
assert wasserstein_distance(samples_a, samples_b) == distance
Pairing the sorted samples makes the arithmetic visible. The first two differences are three meters each, the next four are eight meters each, and the final two are five meters each. Their sum is 48 meters; dividing by eight observations gives six meters.
The expansion is a diagnostic for these small integer counts. It is not a recommendation to allocate repeated arrays for large histograms or fractional weights. The compact weighted representation already states the distribution directly.
Use normalization controls deliberately
Multiplying A's counts by eleven and B's counts by three leaves this computed distance at six meters. The relative mass within each distribution is unchanged. Consequently, this comparison does not preserve a difference in total observation count as a separate discrepancy.
If a report also needs to distinguish eight valid range samples from eighty-eight, record those totals separately. A normalized shape comparison cannot substitute for a count-completeness check. Conversely, a zero shape distance need not imply equal sample volume.
Adding twenty meters to both support lists also leaves the distance at six meters. Converting both lists from meters to millimeters changes the result to 6,000 millimeters. These controls test the coordinate representation: common translation preserves separations, whereas a unit conversion scales them. Applying a conversion to only one input changes the comparison itself.
Do not use density height as probability mass
A second representation issue appears with unequal histogram bin widths. Choose edges [0,2,6,12], giving widths [2,4,6] and centers [1,4,9]. Applying numpy.histogram with density=True to the expanded samples gives A heights [0.375,0.0625,0] and B heights [0,0.0625,0.125].
NumPy documents density output as a density whose integral is one, rather than a probability mass vector [2]. For these bins, multiplying height by width recovers masses [0.75,0.25,0] and [0,0.25,0.75]. Those are the weights needed for the center-supported approximation in this fixture.
edges = np.array([0.0, 2.0, 6.0, 12.0])
widths = np.diff(edges)
density_a, _ = np.histogram(samples_a, bins=edges, density=True)
density_b, _ = np.histogram(samples_b, bins=edges, density=True)
corrected = wasserstein_distance(
positions, positions, density_a * widths, density_b * widths)
assert corrected == 6.0
Passing the density heights themselves as weights instead gives approximately 5.904762 meters. That plausible-looking value comes from different normalized masses. It is not the desired answer for the stated count distributions.
Keep the approximation visible
In this constructed example, every sample already lies at its bin center, so center-supported masses reproduce the raw-sample distance exactly. Real samples spread within bins need not satisfy that equality. Once only counts and edges remain, their individual positions inside each bin are no longer available.
Preserve raw values when the intended analysis requires their original resolution. When using a binned approximation, retain edges, representative positions, masses, units, and the convention used to choose those positions. A correct function call cannot recover information discarded before the call.
The useful audit question is therefore which distribution each pair of arrays encodes. With positions and masses recorded separately, a range-comparison result can be reproduced and its limitations understood without mistaking a permutation of count values for an unchanged spatial distribution.
Sources
[1] SciPy, wasserstein_distance: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.wasserstein_distance.html
[2] NumPy, histogram: https://numpy.org/doc/stable/reference/generated/numpy.histogram.html