Skip to content

Commit

Permalink
Add rng field to KLIEP and LSIF
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Feb 6, 2022
1 parent 6aa066f commit d58f3ec
Show file tree
Hide file tree
Showing 14 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.4.3"
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Expand Down
1 change: 1 addition & 0 deletions src/DensityRatioEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using StatsBase
using Statistics
using LinearAlgebra
using Parameters
using Random

# implement fit for estimators
import StatsBase: fit
Expand Down
5 changes: 3 additions & 2 deletions src/kliep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Kullback-Leibler importance estimation procedure (KLIEP).
* Júlio Hoffimann ([email protected])
"""
@with_kw struct KLIEP{T} <: DensityRatioEstimator
@with_kw struct KLIEP{T,RNG} <: DensityRatioEstimator
σ::T=2.0
b::Int=10
rng::RNG=Random.GLOBAL_RNG
end

default_optlib(dre::Type{<:KLIEP}) = OptimLib
Expand All @@ -49,7 +50,7 @@ end

# constants involved in KLIEP optimization
function _kliep_consts(x_nu, x_de, dre)
x_ba = sample(x_nu, min(length(x_nu), dre.b), replace=false)
x_ba = sample(dre.rng, x_nu, min(length(x_nu), dre.b), replace=false)
K_nu = gaussian_gramian(x_nu, x_ba, σ=dre.σ)
K_de = gaussian_gramian(x_de, x_ba, σ=dre.σ)

Expand Down
5 changes: 3 additions & 2 deletions src/lsif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ Importance Estimation
* Júlio Hoffimann ([email protected])
"""
@with_kw struct LSIF{T} <: DensityRatioEstimator
@with_kw struct LSIF{T,RNG} <: DensityRatioEstimator
σ::T=2.0
b::Int=10
λ::T=0.001
rng::RNG=Random.GLOBAL_RNG
end

default_optlib(dre::Type{<:LSIF}) = OptimLib
Expand All @@ -50,7 +51,7 @@ function _densratiofunc(x_nu, x_de, dre::LSIF,
end

function _lsif_consts(x_nu, x_de, dre)
x_ba = sample(x_nu, min(length(x_nu), dre.b), replace=false)
x_ba = sample(dre.rng, x_nu, min(length(x_nu), dre.b), replace=false)
K_nu = gaussian_gramian(x_nu, x_ba, σ=dre.σ)
K_de = gaussian_gramian(x_de, x_ba, σ=dre.σ)

Expand Down
Binary file modified test/data/KLIEP-ConvexLib-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/KLIEP-ConvexLib-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/KLIEP-OptimLib-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/KLIEP-OptimLib-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/LSIF-JuMPLib-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/LSIF-JuMPLib-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/LSIF-OptimLib-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/data/LSIF-OptimLib-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/kliep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
x_nu, x_de = rand(rng, d_nu, 1_000), rand(rng, d_de, 500)

# estimated density ratio
σ, b = 1.0, 100
= densratio(x_nu, x_de, KLIEP=σ, b=b), optlib=optlib)
kliep = KLIEP=1.0, b=100, rng=rng)
= densratio(x_nu, x_de, kliep, optlib=optlib)

# simplex constraints
@test abs(mean(r̂) - 1) 1e-2
Expand Down
2 changes: 1 addition & 1 deletion test/lsif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
x_nu, x_de = rand(rng, d_nu, 1000), rand(rng, d_de, 500)

# estimated density ratio
lsif = LSIF=1.0, b=100)
lsif = LSIF=1.0, b=100, rng=rng)
= densratio(x_nu, x_de, lsif, optlib=optlib)

# simplex constraints
Expand Down

0 comments on commit d58f3ec

Please sign in to comment.