Repository navigation
[MRG] Use the weights w in ot.dist for all metrics - #884
raashish1601 wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #884 +/- ##
=======================================
Coverage 97.06% 97.06%
=======================================
Files 129 129
Lines 26509 26531 +22
=======================================
+ Hits 25730 25753 +23
+ Misses 779 778 -1 🚀 New features to boost your workflow:
|
cedricvincentcuaz
left a comment
There was a problem hiding this comment.
Dear @raashish1601,
Thank you for your PR. Could you take into account my comments before i merge in the master branch ?
Best,
Cédric
| # sum_k w_k (x1_k - x2_k)^2 is the squared distance between the | ||
| # samples scaled by sqrt(w) | ||
| x1 = x1 * nx.sqrt(w)[None, :] | ||
| x2 = x2 * nx.sqrt(w)[None, :] |
There was a problem hiding this comment.
As euclidean_distances includes a test if X is Y, which tests whether both variables point to the same Python object - it is problematic here to define two new variables x1 and x2.
There was a problem hiding this comment.
You should instead preserve the function behaviour by doing:
sqrt_w = nx.sqrt(w)
if x1 is x2:
x1 = x1 * sqrt_w[None, :]
x2 = x1
else:
x1 = x1 * sqrt_w[None, :]
x2 = x2 * sqrt_w[None, :]
| if not squared: | ||
| c = nx.sqrt(c) | ||
|
|
||
| if X is Y: |
There was a problem hiding this comment.
This test should actually come sooner to avoid redundant computations as in this case b2=a2
|
|
||
| """ | ||
| if nx is None: | ||
| nx = get_backend(x1, x2) |
There was a problem hiding this comment.
should add w in this backend test. get_backend can handle w=None so no need for any case differentiation here
| x1 = x1 - nx.mean(x1, axis=1)[:, None] | ||
| x2 = x2 - nx.mean(x2, axis=1)[:, None] | ||
| else: | ||
| x1 = x1 - (nx.dot(x1, w) / nx.sum(w))[:, None] |
There was a problem hiding this comment.
nx.sum(w) should be computed only once and tested for zero value in which case we can raise a ValueError
| x1 = x1 - (nx.dot(x1, w) / nx.sum(w))[:, None] | ||
| x2 = x2 - (nx.dot(x2, w) / nx.sum(w))[:, None] | ||
| if w is not None: | ||
| x1 = x1 * nx.sqrt(w)[None, :] |
There was a problem hiding this comment.
compute sqrt only once
| @pytest.mark.parametrize( | ||
| "metric", ["sqeuclidean", "euclidean", "cosine", "correlation"] | ||
| ) | ||
| def test_dist_weighted_backends(nx, metric): |
There was a problem hiding this comment.
these 2 tests can be merged, with different cases depending on the backend if nx.__name__ == "jax": fixing a lower tolerance than the other backends.
|
|
||
| #### Closed issues | ||
|
|
||
| - Fix `ot.dist` ignoring the weights `w` for the `sqeuclidean`, `euclidean`, `cosine` and `correlation` metrics and for the other metrics computed with `scipy.spatial.distance.cdist` (PR #884) |
There was a problem hiding this comment.
move to the end of the itemize please to try to follow PR order
Types of changes
Motivation and context / Related issue
ot.distsilently ignoredwfor every metric exceptcityblockandminkowski, so it returned the unweighted distance:This affected the backend implementations of
sqeuclidean,euclidean,cosineandcorrelation, and the numpy fallback tocdist, which only passedwforminkowski/wminkowski. Withbackend="scipy"the weights were used, so the result depended on the backend.test_distalready lists these metrics as "those that support weights" but did not check the values. #859 fixed the same problem forcityblock.Changes:
sqeuclidean/euclidean: scale both point sets bysqrt(w)beforeeuclidean_distances.cosine: same scaling.correlation: center with the weighted mean, then the same scaling. This matches the scipy definitions.wtocdistwhenever it is given (scipy still raises for metrics that do not accept weights).Unweighted results are unchanged.
How has this been tested (if it applies)
Added
test_dist_weighted_vs_cdist(numpy, compared withscipy.spatial.distance.cdistfor sqeuclidean, euclidean, cosine, correlation, braycurtis and canberra) andtest_dist_weighted_backends(all backends, for the four backend metrics). All new cases fail on master.pytest test/test_utils.pypasses (numpy and torch backends installed locally), and ruff 0.5.2 lint and format are clean.PR checklist