Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasserstein and squared2wasserstein #91

Merged
merged 7 commits into from
Jun 2, 2021
Merged

Conversation

devmotion
Copy link
Member

Add wasserstein and squared2wasserstein functions that compute the p-Wasserstein distance and the squared 2-Wasserstein distance.

#88 and #85 will add support for more types of marginals.

@coveralls
Copy link

coveralls commented Jun 1, 2021

Pull Request Test Coverage Report for Build 897793775

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.078%

Totals Coverage Status
Change from base Build 897582936: 0.2%
Covered Lines: 343
Relevant Lines: 357

💛 - Coveralls

@devmotion devmotion requested review from davibarreira and zsteve June 1, 2021 15:23
Copy link
Member

@davibarreira davibarreira left a comment

Choose a reason for hiding this comment

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

All good. But I wonder if squared2wasserstein is really necessary.

@@ -17,10 +17,13 @@ export emd, emd2
export sinkhorn_stabilized, sinkhorn_stabilized_epsscaling, sinkhorn_barycenter
export sinkhorn_unbalanced, sinkhorn_unbalanced2
export quadreg
export ot_cost, ot_plan
export ot_cost, ot_plan, wasserstein, squared2wasserstein

Copy link
Member

Choose a reason for hiding this comment

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

Why do you think a squared2wasserstein is necessary? Why not only wasserstein? The ot_cost would already calculate the squared case if the user wished so, or he could just square the output of the `wasserstein

Copy link
Member Author

Choose a reason for hiding this comment

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

The squared version is useful when working with e.g. normal distributions. And it is quite expensive to first take the square root and then square the result again. Therefore also e.g. Distances contains a dedicated SqEuclidean distance and Distributions a pdfL2squared.

Copy link
Member

Choose a reason for hiding this comment

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

Understood.

src/exact.jl Outdated

See also: [`ot_cost`](@ref)
"""
ot_plan
Copy link
Member

Choose a reason for hiding this comment

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

Why defining this docstring like this? Isn't it redundant since a very similar docstring is present in the following functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

The main motivation is to explain what ot_cost and ot_plan are designed for without focusing on the special cases such as e.g. for univariate distributions or discrete 1D measures. The common practice is to only define docstrings for the special cases if they are really necessary and e.g. have special keyword arguments or return types.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Perhaps in another PR, we should reduce some of the repeated info in the other repeated functions.

ν = Normal(randn(), randexp())
for p in (1, 2, 3, randexp()), metric in (Euclidean(), TotalVariation())
# without additional keyword arguments
w = wasserstein(μ, ν; p=Val(p), metric=metric)
Copy link
Member

Choose a reason for hiding this comment

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

is it necessary to use p=Val(p) when using the wasserstein function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, otherwise one can't dispatch on p to use optimized cost functions such as SqEuclidean.

Copy link
Member

@davibarreira davibarreira Jun 1, 2021

Choose a reason for hiding this comment

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

Hmm. I understand, although it's a bit unfortunate, cause it's a bit odd from a user stand point.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the latest commit I added support for Real-valued ps and a note in the docstring that explains that sometimes Val(p) might be more efficient.

@codecov-commenter
Copy link

codecov-commenter commented Jun 1, 2021

Codecov Report

Merging #91 (261e3d9) into master (3572ba3) will increase coverage by 0.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #91      +/-   ##
==========================================
+ Coverage   95.91%   96.07%   +0.16%     
==========================================
  Files           2        3       +1     
  Lines         343      357      +14     
==========================================
+ Hits          329      343      +14     
  Misses         14       14              
Impacted Files Coverage Δ
src/OptimalTransport.jl 95.20% <ø> (ø)
src/exact.jl 97.84% <ø> (ø)
src/wasserstein.jl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3572ba3...261e3d9. Read the comment docs.

@devmotion devmotion merged commit 7e8cbe2 into master Jun 2, 2021
@devmotion devmotion deleted the dw/wasserstein branch June 2, 2021 00:38
@coveralls
Copy link

coveralls commented Sep 25, 2024

Pull Request Test Coverage Report for Build 896480278

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 12 of 12 (100.0%) changed or added relevant lines in 1 file are covered.
  • 10 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.2%) to 96.066%

Files with Coverage Reduction New Missed Lines %
src/OptimalTransport.jl 10 95.74%
Totals Coverage Status
Change from base Build 889071712: 0.2%
Covered Lines: 293
Relevant Lines: 305

💛 - Coveralls

@coveralls
Copy link

coveralls commented Sep 25, 2024

Pull Request Test Coverage Report for Build 897793775

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.078%

Totals Coverage Status
Change from base Build 897582936: 0.2%
Covered Lines: 343
Relevant Lines: 357

💛 - Coveralls

@coveralls
Copy link

Pull Request Test Coverage Report for Build 896486751

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 12 of 12 (100.0%) changed or added relevant lines in 1 file are covered.
  • 10 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.2%) to 96.066%

Files with Coverage Reduction New Missed Lines %
src/OptimalTransport.jl 10 95.74%
Totals Coverage Status
Change from base Build 889071712: 0.2%
Covered Lines: 293
Relevant Lines: 305

💛 - Coveralls

@coveralls
Copy link

Pull Request Test Coverage Report for Build 897702779

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 14 of 14 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.078%

Totals Coverage Status
Change from base Build 897582936: 0.2%
Covered Lines: 343
Relevant Lines: 357

💛 - Coveralls

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.

4 participants