Skip to content

Commit

Permalink
Add emd_1d and emd2_1d (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
devmotion and github-actions[bot] authored Jun 1, 2021
1 parent 53806cf commit af649a0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PythonOT"
uuid = "3c485715-4278-42b2-9b5f-8f00e43c12ef"
authors = ["David Widmann"]
version = "0.1.2"
version = "0.1.3"

[deps]
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
```@docs
emd
emd2
emd_1d
emd2_1d
```

## Regularized optimal transport
Expand Down
2 changes: 2 additions & 0 deletions src/PythonOT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using PyCall: PyCall

export emd,
emd2,
emd_1d,
emd2_1d,
sinkhorn,
sinkhorn2,
barycenter,
Expand Down
72 changes: 72 additions & 0 deletions src/lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,78 @@ function emd2(μ, ν, C; kwargs...)
return pot.lp.emd2(μ, ν, PyCall.PyReverseDims(permutedims(C)); kwargs...)
end

"""
emd_1d(xsource, xtarget; kwargs...)
Compute the optimal transport plan for the Monge-Kantorovich problem with univariate
discrete measures with support `xsource` and `xtarget` as source and target marginals.
This function is a wrapper of the function
[`emd_1d`](https://pythonot.github.io/all.html#ot.emd_1d) in the Python Optimal Transport
package. Keyword arguments are listed in the documentation of the Python function.
# Examples
```jldoctest
julia> xsource = [0.2, 0.5];
julia> xtarget = [0.8, 0.3];
julia> emd_1d(xsource, xtarget)
2×2 Matrix{Float64}:
0.0 0.5
0.5 0.0
julia> histogram_source = [0.8, 0.2];
julia> histogram_target = [0.7, 0.3];
julia> emd_1d(xsource, xtarget; a=histogram_source, b=histogram_target)
2×2 Matrix{Float64}:
0.5 0.3
0.2 0.0
```
See also: [`emd`](@ref), [`emd2_1d`](@ref)
"""
function emd_1d(xsource, xtarget; kwargs...)
return pot.lp.emd_1d(xsource, xtarget; kwargs...)
end

"""
emd2_1d(xsource, xtarget; kwargs...)
Compute the optimal transport cost for the Monge-Kantorovich problem with univariate
discrete measures with support `xsource` and `xtarget` as source and target marginals.
This function is a wrapper of the function
[`emd2_1d`](https://pythonot.github.io/all.html#ot.emd2_1d) in the Python Optimal Transport
package. Keyword arguments are listed in the documentation of the Python function.
# Examples
```jldoctest
julia> xsource = [0.2, 0.5];
julia> xtarget = [0.8, 0.3];
julia> round(emd2_1d(xsource, xtarget); sigdigits=6)
0.05
julia> histogram_source = [0.8, 0.2];
julia> histogram_target = [0.7, 0.3];
julia> round(emd2_1d(xsource, xtarget; a=histogram_source, b=histogram_target); sigdigits=6)
0.201
```
See also: [`emd2`](@ref), [`emd2_1d`](@ref)
"""
function emd2_1d(xsource, xtarget; kwargs...)
return pot.lp.emd2_1d(xsource, xtarget; kwargs...)
end

"""
sinkhorn(μ, ν, C, ε; kwargs...)
Expand Down

2 comments on commit af649a0

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

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/37932

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:

git tag -a v0.1.3 -m "<description of version>" af649a02f7fc53d4f80dea35a485cc23b44da8c9
git push origin v0.1.3

Please sign in to comment.