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

Finite Discrete Measure #95

Merged
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
30de49c
Initianning sikhorn divergence
davibarreira Jun 2, 2021
1a03325
Merge branch 'master' of https://github.com/JuliaOptimalTransport/Opt…
davibarreira Jun 2, 2021
4a1f380
Sinkhorn divergence implemented
davibarreira Jun 2, 2021
bdc1b5b
Added PyCall to test dependencies
davibarreira Jun 2, 2021
416dcb4
Added tests for sinkhorn divergence
davibarreira Jun 2, 2021
f593377
Added Sinkhorn Divergence to docs
davibarreira Jun 2, 2021
21d38a8
Creating FiniteDiscreteMeasure struct
davibarreira Jun 3, 2021
e17bba5
Modifications:
davibarreira Jun 3, 2021
10e8849
FixedDiscreteMeasure normalizes the weights to sum 1
davibarreira Jun 3, 2021
52b3c7a
FixedDiscreteMeasure checks if probabilities are positive
davibarreira Jun 3, 2021
7d2924d
Created tests for FiniteDiscreteMeasure
davibarreira Jun 3, 2021
7cf44a6
Added tests for sinkhorn divergence and finite discrete measure
davibarreira Jun 3, 2021
4764b00
Fixed the code for creating cost matrices in the sinkhorn_divergence
davibarreira Jun 3, 2021
98784c5
Added costmatrix.jl to tests
davibarreira Jun 3, 2021
1fb0fc1
Fixed docstring for costmatrix
davibarreira Jun 3, 2021
808d6ac
Fixed errors in the tests
davibarreira Jun 3, 2021
3415386
Minor fixes in the tests
davibarreira Jun 3, 2021
df97c0d
FiniteDiscreteMeasure implemented
davibarreira Jun 4, 2021
dc87b69
Formatted code
davibarreira Jun 4, 2021
cde2bff
Fixed docs
davibarreira Jun 4, 2021
e1911b8
Fixed docs
davibarreira Jun 4, 2021
ff67cf3
Update Project.toml
davibarreira Jun 4, 2021
77973db
Update test/runtests.jl
davibarreira Jun 4, 2021
b535601
Update src/OptimalTransport.jl
davibarreira Jun 4, 2021
85a265c
Update src/OptimalTransport.jl
davibarreira Jun 4, 2021
5a794ae
Update src/OptimalTransport.jl
davibarreira Jun 4, 2021
f280e33
Update test/entropic.jl
davibarreira Jun 4, 2021
171b8e5
Change from AbstractArray to AbstractVector
davibarreira Jun 5, 2021
31267ad
Update src/utils.jl
davibarreira Jun 5, 2021
64786bf
Update src/utils.jl
davibarreira Jun 5, 2021
62f6612
Update src/utils.jl
davibarreira Jun 5, 2021
e01c895
Update src/utils.jl
davibarreira Jun 5, 2021
df6964a
Using `isprobvec` when building FiniteDiscreteMeasure
davibarreira Jun 5, 2021
03dfd2e
export discretemeasure
davibarreira Jun 5, 2021
efe149b
removed multivariate tests until implementatio of RowVecs and ColVecs
davibarreira Jun 5, 2021
99db80a
Fixed test
davibarreira Jun 5, 2021
35dea14
Fixed tests and format
davibarreira Jun 5, 2021
25bd705
Added KernelFunctions to dependendcies
davibarreira Jun 5, 2021
624b382
Removed KernelFunctions as dependency.
davibarreira Jun 5, 2021
4e5eb35
Fixed tests and format
davibarreira Jun 5, 2021
844c3aa
Small correction in the docstring
davibarreira Jun 5, 2021
d6caa3a
Merge branch 'master' into finitediscretemeasure
davibarreira Jun 5, 2021
aaf5608
Trying to solve pkg compatibility
davibarreira Jun 5, 2021
15dd690
Changed from KernelFunctions to ArraysOfArrays
davibarreira Jun 5, 2021
7539e05
Removed ArraysOfArrays
davibarreira Jun 6, 2021
40afd46
Fixed tests to avoid using ArraysOfArrays or KernelFunctions
davibarreira Jun 6, 2021
fca14ae
Small fix on test
davibarreira Jun 6, 2021
be88027
Small fix on test
davibarreira Jun 6, 2021
8e221b0
Modified test to work with julia 1.0
davibarreira Jun 6, 2021
61a1862
Update test/utils.jl
davibarreira Jun 6, 2021
0d4d4cb
Update test/utils.jl
davibarreira Jun 6, 2021
f53dbc4
Update test/utils.jl
davibarreira Jun 6, 2021
2af2c01
Update src/utils.jl
davibarreira Jun 6, 2021
8eff9b6
Update docs/src/index.md
davibarreira Jun 6, 2021
55618c5
Update src/OptimalTransport.jl
davibarreira Jun 6, 2021
34733de
included utils in test
davibarreira Jun 6, 2021
26004bb
Update test/utils.jl
davibarreira Jun 6, 2021
7743f07
Update test/utils.jl
davibarreira Jun 6, 2021
d5d57c7
Update test/utils.jl
davibarreira Jun 6, 2021
7f7cce0
Merge branch 'master' into finitediscretemeasure
devmotion Jun 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tulip = "6dd1b50a-3aae-11e9-10b5-ef983d2400fa"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
davibarreira marked this conversation as resolved.
Show resolved Hide resolved

[targets]
test = ["ForwardDiff", "Pkg", "PythonOT", "Random", "SafeTestsets", "Test", "Tulip"]
5 changes: 5 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ sinkhorn_unbalanced2
```@docs
quadreg
```

## Auxiliary functions
```@docs
FiniteDiscreteMeasure
```
4 changes: 4 additions & 0 deletions src/OptimalTransport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module OptimalTransport

using LinearAlgebra: AbstractMatrix
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
using Distances
using LinearAlgebra
using IterativeSolvers, SparseArrays
Expand All @@ -17,8 +18,11 @@ export sinkhorn, sinkhorn2
export emd, emd2
export sinkhorn_stabilized, sinkhorn_stabilized_epsscaling, sinkhorn_barycenter
export sinkhorn_unbalanced, sinkhorn_unbalanced2
export sinkhorn_divergence
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
export quadreg
export ot_cost, ot_plan, wasserstein, squared2wasserstein
export FiniteDiscreteMeasure
export cost_matrix
davibarreira marked this conversation as resolved.
Show resolved Hide resolved

const MOI = MathOptInterface

Expand Down
50 changes: 50 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,53 @@ function checkbalanced(x::AbstractVecOrMat, y::AbstractVecOrMat)
throw(ArgumentError("source and target marginals are not balanced"))
return nothing
end

struct FiniteDiscreteMeasure{X<:AbstractArray,P<:AbstractVector}
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
support::X
p::P

function FiniteDiscreteMeasure{X,P}(support::X, p::P) where {X,P}
size(support, 1) == length(p) ||
error("number of rows of `support` and `p` must be equal")
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
sum(p) ≈ 1 || error("`p` must sum to 1")
all(p .>= 0) || error("values of `p` must be greater of equal than 0")
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
return new{X,P}(support, p)
end
end

"""
FiniteDiscreteMeasure(support::AbstractArray, p::AbstractVector)
Construct a finite discrete probability measure with support `support` and corresponding weights `p`.
"""
function FiniteDiscreteMeasure(support::AbstractArray, p::AbstractVector)
P = sum(p)
if size(support, 2) == 1
return if P ≈ 1
DiscreteNonParametric(vec(support), p)
else
DiscreteNonParametric(vec(support), p ./ P)
end
else
return if P ≈ 1
FiniteDiscreteMeasure{typeof(support),typeof(p)}(support, p)
else
FiniteDiscreteMeasure{typeof(support),typeof(p)}(support, p ./ P)
end
end
end
davibarreira marked this conversation as resolved.
Show resolved Hide resolved

"""
FiniteDiscreteMeasure(support::AbstractArray)
Construct a finite discrete probability measure with support `support` and equal probability for each point.
"""
function FiniteDiscreteMeasure(support::AbstractArray)
p = ones(size(support)[1]) ./ size(support)[1]
if size(support, 2) == 1
return DiscreteNonParametric(vec(support), p)
else
return FiniteDiscreteMeasure{typeof(support),typeof(p)}(support, p)
end
end

davibarreira marked this conversation as resolved.
Show resolved Hide resolved
Distributions.support(d::FiniteDiscreteMeasure) = d.support
Distributions.probs(d::FiniteDiscreteMeasure) = d.p
1 change: 1 addition & 0 deletions test/entropic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Distances
using ForwardDiff
using LogExpFunctions
using PythonOT: PythonOT
using Distributions
davibarreira marked this conversation as resolved.
Show resolved Hide resolved

using Random
using Test
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LinearAlgebra: symmetric
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
using OptimalTransport
using Pkg: Pkg
using SafeTestsets
Expand Down
50 changes: 50 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using OptimalTransport
using LinearAlgebra
using Random
using Test
using Distributions

Random.seed!(100)

Expand Down Expand Up @@ -95,4 +96,53 @@ Random.seed!(100)
x2, y2 .* hcat(rand(), ones(1, size(y2, 2) - 1))
)
end

@testset "finitediscretemeasure.jl" begin
@testset "Univariate Finite Discrete Measure" begin
n = 100
μsupp = rand(n)
νsupp = rand(n, 1)
μ = FiniteDiscreteMeasure(μsupp)
ν = FiniteDiscreteMeasure(νsupp, rand(n))
# check if it assigns equal probabilities to all entries
@test μ.p ≈ ones(n) ./ n
@test probs(μ) ≈ ones(n) ./ n
# check if it probabilities sum to 1
@test sum(ν.p) ≈ 1
@test sum(probs(ν)) ≈ 1
# check if probabilities are all positive (non-negative)
@test all(ν.p .>= 0)
@test all(probs(ν) .>= 0)
# check if it assigns to DiscreteNonParametric when Vector/Matrix is 1D
@test typeof(μ) <: DiscreteNonParametric
@test typeof(ν) <: DiscreteNonParametric
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
# check if support is correctly assinged
@test sort(μsupp) == μ.support
@test sort(μsupp) == support(μ)
@test sort(vec(νsupp)) == ν.support
@test sort(vec(νsupp)) == support(ν)
end
@testset "Multivariate Finite Discrete Measure" begin
n = 10
m = 3
μsupp = rand(n, m)
νsupp = rand(n, m)
μ = FiniteDiscreteMeasure(μsupp)
ν = FiniteDiscreteMeasure(νsupp, rand(n))
# check if it assigns equal probabilities to all entries
@test μ.p ≈ ones(n) ./ n
@test probs(μ) ≈ ones(n) ./ n
# check if it probabilities sum to 1
@test sum(ν.p) ≈ 1
@test sum(probs(ν)) ≈ 1
# check if probabilities are all positive (non-negative)
@test all(ν.p .>= 0)
@test all(probs(ν) .>= 0)
# check if support is correctly assinged
@test μsupp == μ.support
@test μsupp == support(μ)
@test νsupp == ν.support
@test νsupp == support(ν)
end
end
end