-
Notifications
You must be signed in to change notification settings - Fork 3
/
kliep.jl
63 lines (47 loc) · 1.74 KB
/
kliep.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
KLIEP(σ=1.0, b=100)
Kullback-Leibler importance estimation procedure (KLIEP).
## Parameters
* `σ` - Bandwidth of Gaussian kernel (default to `2.0`)
* `b` - Maximum number of radial basis functions (default to `10`)
## References
* Sugiyama et al. 2008. Direct importance estimation for
covariate shift adaptation.
### Author
* Júlio Hoffimann (julio.hoffimann@gmail.com)
"""
@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
available_optlib(dre::Type{<:KLIEP}) = [OptimLib, ConvexLib]
function _densratio(x_nu, x_de, dre::KLIEP, optlib::Type{<:OptimizationLibrary})
K_nu, K_de, x_ba = _kliep_consts(x_nu, x_de, dre)
α = _kliep_coeffs(K_nu, K_de, dre, optlib)
K_de * α
end
function _densratiofunc(x_nu, x_de, dre::KLIEP, optlib::Type{<:OptimizationLibrary})
K_nu, K_de, x_ba = _kliep_consts(x_nu, x_de, dre)
α = _kliep_coeffs(K_nu, K_de, dre, optlib)
function r(x)
K = gaussian_gramian([x], x_ba, σ=dre.σ)
dot(K, α)
end
end
# constants involved in KLIEP optimization
function _kliep_consts(x_nu, x_de, dre)
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.σ)
K_nu, K_de, x_ba
end
"""
_kliep_coeffs(K_nu, K_de, dre, optlib)
Return the coefficients of KLIEP basis expansion.
"""
_kliep_coeffs(K_nu, K_de, dre::KLIEP, optlib::Type{<:OptimizationLibrary}) = @error "not implemented"