Skip to content

Add Wasserstein distance (aka Earth Mover's distance) to linfa-nn#440

Merged
relf merged 3 commits into
rust-ml:masterfrom
mrsanor:earthmover-dist
May 26, 2026
Merged

Add Wasserstein distance (aka Earth Mover's distance) to linfa-nn#440
relf merged 3 commits into
rust-ml:masterfrom
mrsanor:earthmover-dist

Conversation

@mrsanor
Copy link
Copy Markdown
Contributor

@mrsanor mrsanor commented Apr 21, 2026

Add Earth Mover's Distance, aka. Wasserstein to linfa-nn

@mrsanor mrsanor changed the title feat(linfa-nn): Added Earth Mover's Distance Add Earth Mover's Distance to linfa-nn Apr 21, 2026
Comment thread algorithms/linfa-nn/src/distance.rs Outdated
assert_abs_diff_eq!(ab, 0.9, epsilon = 1e-5);

let a = arr1(&[0.35, 0.15, 0.15, 0.10, 0.25]);
let b = arr1(&[0.1, 0.20, 0.05, 0.20, 0.45]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight variations in input data, maybe add a comment to explain what we should see as a result. You can also test against scipy examples

Comment thread algorithms/linfa-nn/src/distance.rs Outdated
let a = arr2(&[[0.3, 0.2, 0.15, 0.10, 0.25], [0.35, 0.15, 0.15, 0.10, 0.25]]);
let b = arr2(&[[0.1, 0.2, 0.05, 0.20, 0.45], [0.1, 0.20, 0.05, 0.20, 0.45]]);
let ab = dist.distance(a.view(), b.view());
assert_abs_diff_eq!(ab, 0.9 + 0.95, epsilon = 1e-5);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use previous a, b, ab variables

Copy link
Copy Markdown
Member

@relf relf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. Some minor requests...

Comment thread algorithms/linfa-nn/src/distance.rs Outdated
serde(crate = "serde_crate")
)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EarthMoverDist;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go with WassersteinDist as you put it first above and it will be consistent with scipy impl.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to WassersteinDist and changed the tests to be according to SciPy where relevant since the APIs are a bit different

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I was not aware of the difference of API. Maybe specify once the version of scipy you checked against.

@mrsanor mrsanor force-pushed the earthmover-dist branch from 79ab352 to 7b26ce4 Compare May 22, 2026 09:35
@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.56%. Comparing base (ad45826) to head (4ee6643).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #440      +/-   ##
==========================================
+ Coverage   77.54%   77.56%   +0.02%     
==========================================
  Files         106      106              
  Lines        7578     7585       +7     
==========================================
+ Hits         5876     5883       +7     
  Misses       1702     1702              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@relf relf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rebase your changes on current master?

@mrsanor mrsanor force-pushed the earthmover-dist branch from caf48be to 4ee6643 Compare May 25, 2026 10:36
@mrsanor
Copy link
Copy Markdown
Contributor Author

mrsanor commented May 25, 2026

Could you rebase your changes on current master?

Done, and I added the SciPy version I compared against to comments

Copy link
Copy Markdown
Member

@relf relf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@relf relf changed the title Add Earth Mover's Distance to linfa-nn Add Wasserstein distance (aka Earth Mover's distance) to linfa-nn May 26, 2026
@relf relf merged commit 60382a3 into rust-ml:master May 26, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants