-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
307 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "OptimalTransport" | ||
uuid = "7e02d93a-ae51-4f58-b602-d97af76e3b33" | ||
authors = ["zsteve <[email protected]>"] | ||
version = "0.3.6" | ||
version = "0.3.7" | ||
|
||
[deps] | ||
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using OptimalTransport | ||
using Distributions | ||
|
||
using Literate: Literate | ||
using Pkg: Pkg | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
wasserstein(μ, ν; metric=Euclidean(), p=Val(1), kwargs...) | ||
Compute the `p`-Wasserstein distance with respect to the `metric` between measures `μ` and | ||
`ν`. | ||
Order `p` can be provided as a scalar of type `Real` or as a parameter of a value type | ||
`Val(p)`. For certain combinations of `metric` and `p`, such as `metric=Euclidean()` and | ||
`p=Val(2)`, the computations are more efficient if `p` is specified as a value type. The | ||
remaining keyword arguments are forwarded to [`ot_cost`](@ref). | ||
See also: [`squared2wasserstein`](@ref), [`ot_cost`](@ref) | ||
""" | ||
function wasserstein(μ, ν; metric=Euclidean(), p::Union{Real,Val}=Val(1), kwargs...) | ||
cost = ot_cost(p2distance(metric, p), μ, ν; kwargs...) | ||
return prt(cost, p) | ||
end | ||
|
||
# compute the cost function corresponding to a metric and exponent `p` | ||
p2distance(metric, ::Val{1}) = metric | ||
p2distance(metric, ::Val{P}) where {P} = (x, y) -> metric(x, y)^P | ||
p2distance(d::Euclidean, ::Val{2}) = SqEuclidean(d.thresh) | ||
p2distance(metric, p) = (x, y) -> metric(x, y)^p | ||
|
||
# compute the `p` root | ||
prt(x, ::Val{1}) = x | ||
prt(x, ::Val{2}) = sqrt(x) | ||
prt(x, ::Val{3}) = cbrt(x) | ||
prt(x, ::Val{P}) where {P} = x^(1 / P) | ||
prt(x, p) = x^(1 / p) | ||
|
||
""" | ||
squared2wasserstein(μ, ν; metric=Euclidean(), kwargs...) | ||
Compute the squared 2-Wasserstein distance with respect to the `metric` between measures `μ` | ||
and `ν`. | ||
The remaining keyword arguments are forwarded to [`ot_cost`](@ref). | ||
See also: [`wasserstein`](@ref), [`ot_cost`](@ref) | ||
""" | ||
function squared2wasserstein(μ, ν; metric=Euclidean(), kwargs...) | ||
return ot_cost(p2distance(metric, Val(2)), μ, ν; kwargs...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7e8cbe2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
7e8cbe2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/37976
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: