Skip to content

Commit ab5f908

Browse files
authored
Update and extend documentation (#8)
1 parent 170b862 commit ab5f908

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

docs/src/api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API
22

3-
## Exact optimal transport (Kantorovich) problem
3+
## Exact optimal transport
44

55
```@docs
66
emd

src/lib.jl

+64-37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
emd(μ, ν, C; kwargs...)
33
4-
Compute the optimal transport map for the Monge-Kantorovich problem with source and target
4+
Compute the optimal transport plan for the Monge-Kantorovich problem with source and target
55
marginals `μ` and `ν` and cost matrix `C` of size `(length(μ), length(ν))`.
66
7-
The optimal transport map `γ` is of the same size as `C` and solves
7+
The optimal transport plan `γ` is of the same size as `C` and solves
88
```math
99
\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle.
1010
```
@@ -20,9 +20,7 @@ julia> μ = [0.5, 0.2, 0.3];
2020
2121
julia> ν = [0.0, 1.0];
2222
23-
julia> C = [0.0 1.0;
24-
2.0 0.0;
25-
0.5 1.5];
23+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
2624
2725
julia> emd(μ, ν, C)
2826
3×2 Matrix{Float64}:
@@ -59,9 +57,7 @@ julia> μ = [0.5, 0.2, 0.3];
5957
6058
julia> ν = [0.0, 1.0];
6159
62-
julia> C = [0.0 1.0;
63-
2.0 0.0;
64-
0.5 1.5];
60+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
6561
6662
julia> emd2(μ, ν, C)
6763
0.95
@@ -76,11 +72,11 @@ end
7672
"""
7773
sinkhorn(μ, ν, C, ε; kwargs...)
7874
79-
Compute the optimal transport map for the entropic regularization optimal transport problem
75+
Compute the optimal transport plan for the entropic regularization optimal transport problem
8076
with source and target marginals `μ` and `ν`, cost matrix `C` of size
8177
`(length(μ), length(ν))`, and entropic regularization parameter `ε`.
8278
83-
The optimal transport map `γ` is of the same size as `C` and solves
79+
The optimal transport plan `γ` is of the same size as `C` and solves
8480
```math
8581
\\inf_{\\gamma \\in \\Pi(\\mu, \\nu)} \\langle \\gamma, C \\rangle
8682
+ \\varepsilon \\Omega(\\gamma),
@@ -94,14 +90,12 @@ Transport package. Keyword arguments are listed in the documentation of the Pyth
9490
9591
# Examples
9692
97-
```jldoctest
93+
```jldoctest sinkhorn
9894
julia> μ = [0.5, 0.2, 0.3];
9995
10096
julia> ν = [0.0, 1.0];
10197
102-
julia> C = [0.0 1.0;
103-
2.0 0.0;
104-
0.5 1.5];
98+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
10599
106100
julia> sinkhorn(μ, ν, C, 0.01)
107101
3×2 Matrix{Float64}:
@@ -110,6 +104,18 @@ julia> sinkhorn(μ, ν, C, 0.01)
110104
0.0 0.3
111105
```
112106
107+
It is possible to provide multiple target marginals as columns of a matrix. In this case the
108+
optimal transport costs are returned:
109+
110+
```jldoctest sinkhorn
111+
julia> ν = [0.0 0.5; 1.0 0.5];
112+
113+
julia> round.(sinkhorn(μ, ν, C, 0.01); sigdigits=6)
114+
2-element Vector{Float64}:
115+
0.95
116+
0.45
117+
```
118+
113119
See also: [`sinkhorn2`](@ref)
114120
"""
115121
function sinkhorn(μ, ν, C, ε; kwargs...)
@@ -137,20 +143,29 @@ Transport package. Keyword arguments are listed in the documentation of the Pyth
137143
138144
# Examples
139145
140-
```jldoctest
146+
```jldoctest sinkhorn2
141147
julia> μ = [0.5, 0.2, 0.3];
142148
143149
julia> ν = [0.0, 1.0];
144150
145-
julia> C = [0.0 1.0;
146-
2.0 0.0;
147-
0.5 1.5];
151+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
148152
149153
julia> round.(sinkhorn2(μ, ν, C, 0.01); sigdigits=6)
150154
1-element Vector{Float64}:
151155
0.95
152156
```
153157
158+
It is possible to provide multiple target marginals as columns of a matrix.
159+
160+
```jldoctest sinkhorn2
161+
julia> ν = [0.0 0.5; 1.0 0.5];
162+
163+
julia> round.(sinkhorn2(μ, ν, C, 0.01); sigdigits=6)
164+
2-element Vector{Float64}:
165+
0.95
166+
0.45
167+
```
168+
154169
See also: [`sinkhorn`](@ref)
155170
"""
156171
function sinkhorn2(μ, ν, C, ε; kwargs...)
@@ -160,12 +175,12 @@ end
160175
"""
161176
sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...)
162177
163-
Compute the optimal transport map for the unbalanced entropic regularization optimal
178+
Compute the optimal transport plan for the unbalanced entropic regularization optimal
164179
transport problem with source and target marginals `μ` and `ν`, cost matrix `C` of size
165180
`(length(μ), length(ν))`, entropic regularization parameter `ε`, and marginal relaxation
166181
term `λ`.
167182
168-
The optimal transport map `γ` is of the same size as `C` and solves
183+
The optimal transport plan `γ` is of the same size as `C` and solves
169184
```math
170185
\\inf_{\\gamma} \\langle \\gamma, C \\rangle
171186
+ \\varepsilon \\Omega(\\gamma)
@@ -182,14 +197,12 @@ Python function.
182197
183198
# Examples
184199
185-
```jldoctest
200+
```jldoctest sinkhorn_unbalanced
186201
julia> μ = [0.5, 0.2, 0.3];
187202
188203
julia> ν = [0.0, 1.0];
189204
190-
julia> C = [0.0 1.0;
191-
2.0 0.0;
192-
0.5 1.5];
205+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
193206
194207
julia> sinkhorn_unbalanced(μ, ν, C, 0.01, 1_000)
195208
3×2 Matrix{Float64}:
@@ -198,6 +211,18 @@ julia> sinkhorn_unbalanced(μ, ν, C, 0.01, 1_000)
198211
0.0 0.29983
199212
```
200213
214+
It is possible to provide multiple target marginals as columns of a matrix. In this case the
215+
optimal transport costs are returned:
216+
217+
```jldoctest sinkhorn_unbalanced
218+
julia> ν = [0.0 0.5; 1.0 0.5];
219+
220+
julia> round.(sinkhorn_unbalanced(μ, ν, C, 0.01, 1_000); sigdigits=6)
221+
2-element Vector{Float64}:
222+
0.949709
223+
0.449411
224+
```
225+
201226
See also: [`sinkhorn_unbalanced2`](@ref)
202227
"""
203228
function sinkhorn_unbalanced(μ, ν, C, ε, λ; kwargs...)
@@ -231,20 +256,29 @@ Python function.
231256
232257
# Examples
233258
234-
```jldoctest
259+
```jldoctest sinkhorn_unbalanced2
235260
julia> μ = [0.5, 0.2, 0.3];
236261
237262
julia> ν = [0.0, 1.0];
238263
239-
julia> C = [0.0 1.0;
240-
2.0 0.0;
241-
0.5 1.5];
264+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
242265
243266
julia> round.(sinkhorn_unbalanced2(μ, ν, C, 0.01, 1_000); sigdigits=6)
244267
1-element Vector{Float64}:
245268
0.949709
246269
```
247270
271+
It is possible to provide multiple target marginals as columns of a matrix:
272+
273+
```jldoctest sinkhorn_unbalanced2
274+
julia> ν = [0.0 0.5; 1.0 0.5];
275+
276+
julia> round.(sinkhorn_unbalanced2(μ, ν, C, 0.01, 1_000); sigdigits=6)
277+
2-element Vector{Float64}:
278+
0.949709
279+
0.449411
280+
```
281+
248282
See also: [`sinkhorn_unbalanced`](@ref)
249283
"""
250284
function sinkhorn_unbalanced2(μ, ν, C, ε, λ; kwargs...)
@@ -263,7 +297,7 @@ The Wasserstein barycenter is a histogram and solves
263297
```math
264298
\\inf_{a} \\sum_{i} W_{\\varepsilon,C}(a, a_i),
265299
```
266-
where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C}(a, a_i)}``
300+
where the histograms ``a_i`` are columns of matrix `A` and ``W_{\\varepsilon,C}(a, a_i)``
267301
is the optimal transport cost for the entropically regularized optimal transport problem
268302
with marginals ``a`` and ``a_i``, cost matrix ``C``, and entropic regularization parameter
269303
``\\varepsilon``. Optionally, weights of the histograms ``a_i`` can be provided with the
@@ -287,11 +321,4 @@ julia> isapprox(sum(barycenter(A, C, 0.01; method="sinkhorn_stabilized")), 1; at
287321
true
288322
```
289323
"""
290-
function barycenter(A, C, ε; kwargs...)
291-
return pot.barycenter(
292-
PyCall.PyReverseDims(permutedims(A)),
293-
PyCall.PyReverseDims(permutedims(C)),
294-
ε;
295-
kwargs...,
296-
)
297-
end
324+
barycenter(A, C, ε; kwargs...) = pot.barycenter(A, C, ε; kwargs...)

src/smooth.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ julia> μ = [0.5, 0.2, 0.3];
4242
4343
julia> ν = [0.0, 1.0];
4444
45-
julia> C = [0.0 1.0;
46-
2.0 0.0;
47-
0.5 1.5];
45+
julia> C = [0.0 1.0; 2.0 0.0; 0.5 1.5];
4846
4947
julia> smooth_ot_dual(μ, ν, C, 0.01)
5048
3×2 Matrix{Float64}:

0 commit comments

Comments
 (0)