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 56 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
52 changes: 52 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ function checkbalanced(x::AbstractVecOrMat, y::AbstractVecOrMat)
throw(ArgumentError("source and target marginals are not balanced"))
return nothing
end

struct FiniteDiscreteMeasure{X<:AbstractVector,P<:AbstractVector}
support::X
p::P

function FiniteDiscreteMeasure{X,P}(support::X, p::P) where {X,P}
length(support) == length(p) || error("length of `support` and `p` must be equal")
isprobvec(p) || error("`p` must be a probability vector")
return new{X,P}(support, p)
end
end

"""
discretemeasure(
support::AbstractVector,
probs::AbstractVector{<:Real}=fill(inv(length(support)), length(support))
)

Construct a finite discrete probability measure with `support` and corresponding
`probabilities`. If the probability vector argument is not passed, then
equal probability is assigned to each entry in the support.

# Examples
```julia
using KernelFunctions
# rows correspond to samples
μ = discretemeasure(RowVecs(rand(7,3)), normalize!(rand(10),1))

# columns correspond to samples, each with equal probability
ν = discretemeasure(ColVecs(rand(3,12)))
```

!!! note
If `support` is a 1D vector, the constructed measure will be sorted,
e.g. for `mu = discretemeasure([3, 1, 2],[0.5, 0.2, 0.3])`, then
`mu.support` will be `[1, 2, 3]` and `mu.p` will be `[0.2, 0.3, 0.5]`.
"""
function discretemeasure(
support::AbstractVector{<:Real},
probs::AbstractVector{<:Real}=fill(inv(length(support)), length(support)),
)
return DiscreteNonParametric(support, probs)
end
function discretemeasure(
support::AbstractVector,
probs::AbstractVector{<:Real}=fill(inv(length(support)), length(support)),
)
return FiniteDiscreteMeasure{typeof(support),typeof(probs)}(support, probs)
end

Distributions.support(d::FiniteDiscreteMeasure) = d.support
Distributions.probs(d::FiniteDiscreteMeasure) = d.p
51 changes: 51 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ using OptimalTransport
using LinearAlgebra
using Random
using Test
using Distributions

include("../src/utils.jl")
davibarreira marked this conversation as resolved.
Show resolved Hide resolved

Random.seed!(100)

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

@testset "FiniteDiscreteMeasure" begin
@testset "Univariate Finite Discrete Measure" begin
n = 100
m = 80
μsupp = rand(n)
νsupp = rand(m)
μprobs = normalize!(rand(n), 1)

μ = discretemeasure(μsupp, μprobs)
ν = discretemeasure(νsupp)
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
# check if it vectors are indeed probabilities
@test isprobvec(μ.p)
@test isprobvec(probs(μ))
@test ν.p == ones(m) ./ m
@test probs(ν) == ones(m) ./ m

# check if it assigns to DiscreteNonParametric when Vector/Matrix is 1D
@test μ isa DiscreteNonParametric
@test ν isa DiscreteNonParametric

# 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(m) for i in 1:n]
νsupp = [rand(m) for i in 1:n]
μprobs = normalize!(rand(n), 1)
μ = discretemeasure(μsupp, μprobs)
ν = discretemeasure(νsupp)
davibarreira marked this conversation as resolved.
Show resolved Hide resolved
# check if it vectors are indeed probabilities
@test isprobvec(μ.p)
@test isprobvec(probs(μ))
@test ν.p == ones(n) ./ n
@test probs(ν) == ones(n) ./ n

# check if support is correctly assinged
@test μsupp == μ.support
@test μsupp == support(μ)
@test νsupp == ν.support
@test νsupp == support(ν)
end
end
end