Skip to content

Commit 536bdcb

Browse files
authored
Add RationalKernel and rename GammaRationalQuadraticKernel (#242)
1 parent c7e1f5e commit 536bdcb

File tree

10 files changed

+290
-179
lines changed

10 files changed

+290
-179
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "KernelFunctions"
22
uuid = "ec8451be-7e33-11e9-00cf-bbf324bd1392"
3-
version = "0.9.4"
3+
version = "0.9.5"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

docs/src/kernels.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ LinearKernel
8888
PolynomialKernel
8989
```
9090

91-
### Rational Quadratic Kernels
91+
### Rational Kernels
9292

9393
```@docs
94+
RationalKernel
9495
RationalQuadraticKernel
95-
GammaRationalQuadraticKernel
96+
GammaRationalKernel
9697
```
9798

9899
### Spectral Mixture Kernels

src/KernelFunctions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export ExponentiatedKernel
1313
export FBMKernel
1414
export MaternKernel, Matern12Kernel, Matern32Kernel, Matern52Kernel
1515
export LinearKernel, PolynomialKernel
16-
export RationalQuadraticKernel, GammaRationalQuadraticKernel
16+
export RationalKernel, RationalQuadraticKernel, GammaRationalKernel
1717
export GaborKernel, PiecewisePolynomialKernel
1818
export PeriodicKernel, NeuralNetworkKernel
1919
export KernelSum, KernelProduct, KernelTensorProduct
@@ -84,7 +84,7 @@ include(joinpath("basekernels", "nn.jl"))
8484
include(joinpath("basekernels", "periodic.jl"))
8585
include(joinpath("basekernels", "piecewisepolynomial.jl"))
8686
include(joinpath("basekernels", "polynomial.jl"))
87-
include(joinpath("basekernels", "rationalquad.jl"))
87+
include(joinpath("basekernels", "rational.jl"))
8888
include(joinpath("basekernels", "sm.jl"))
8989
include(joinpath("basekernels", "wiener.jl"))
9090

src/basekernels/exponential.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,30 @@ For inputs ``x, x' \\in \\mathbb{R}^d``, the γ-exponential kernel[^RW] with par
9898
k(x, x'; \\gamma) = \\exp\\big(- \\|x - x'\\|_2^{\\gamma}\\big).
9999
```
100100
101+
!!! warning
102+
The default value of parameter `γ` will be changed to `1.0` in the next breaking release
103+
of KernelFunctions.
104+
101105
See also: [`ExponentialKernel`](@ref), [`SqExponentialKernel`](@ref)
102106
103107
[^RW]: C. E. Rasmussen & C. K. I. Williams (2006). Gaussian Processes for Machine Learning.
104108
"""
105109
struct GammaExponentialKernel{Tγ<:Real} <: SimpleKernel
106110
γ::Vector{Tγ}
107-
function GammaExponentialKernel(; gamma::Real=2.0, γ::Real=gamma)
108-
@check_args(GammaExponentialKernel, γ, zero(γ) < γ 2, "γ ∈ (0, 2]")
109-
return new{typeof(γ)}([γ])
111+
# function GammaExponentialKernel(; gamma::Real=1.0, γ::Real=gamma)
112+
function GammaExponentialKernel(; gamma=nothing, γ=gamma)
113+
γ2 = if γ === nothing
114+
Base.depwarn(
115+
"the default value of parameter `γ` of the `GammaExponentialKernel` will " *
116+
"be changed to `1.0` in the next breaking release of KernelFunctions",
117+
:GammaExponentialKernel,
118+
)
119+
2.0
120+
else
121+
γ
122+
end
123+
@check_args(GammaExponentialKernel, γ2, zero(γ2) < γ2 2, "γ ∈ (0, 2]")
124+
return new{typeof(γ2)}([γ2])
110125
end
111126
end
112127

src/basekernels/rational.jl

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""
2+
RationalKernel(; α::Real=2.0)
3+
4+
Rational kernel with shape parameter `α`.
5+
6+
# Definition
7+
8+
For inputs ``x, x' \\in \\mathbb{R}^d``, the rational kernel with shape parameter
9+
``\\alpha > 0`` is defined as
10+
```math
11+
k(x, x'; \\alpha) = \\bigg(1 + \\frac{\\|x - x'\\|_2}{\\alpha}\\bigg)^{-\\alpha}.
12+
```
13+
14+
The [`ExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
15+
16+
See also: [`GammaRationalKernel`](@ref)
17+
"""
18+
struct RationalKernel{Tα<:Real} <: SimpleKernel
19+
α::Vector{Tα}
20+
function RationalKernel(; alpha::T=2.0, α::T=alpha) where {T}
21+
@check_args(RationalKernel, α, α > zero(T), "α > 0")
22+
return new{T}([α])
23+
end
24+
end
25+
26+
@functor RationalKernel
27+
28+
function kappa::RationalKernel, d::Real)
29+
return (one(d) + d / first.α))^(-first.α))
30+
end
31+
32+
metric(::RationalKernel) = Euclidean()
33+
34+
function Base.show(io::IO, κ::RationalKernel)
35+
return print(io, "Rational Kernel (α = $(first.α)))")
36+
end
37+
38+
"""
39+
RationalQuadraticKernel(; α::Real=2.0)
40+
41+
Rational-quadratic kernel with shape parameter `α`.
42+
43+
# Definition
44+
45+
For inputs ``x, x' \\in \\mathbb{R}^d``, the rational-quadratic kernel with shape parameter
46+
``\\alpha > 0`` is defined as
47+
```math
48+
k(x, x'; \\alpha) = \\bigg(1 + \\frac{\\|x - x'\\|_2^2}{2\\alpha}\\bigg)^{-\\alpha}.
49+
```
50+
51+
The [`SqExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
52+
53+
See also: [`GammaRationalKernel`](@ref)
54+
"""
55+
struct RationalQuadraticKernel{Tα<:Real} <: SimpleKernel
56+
α::Vector{Tα}
57+
function RationalQuadraticKernel(; alpha::T=2.0, α::T=alpha) where {T}
58+
@check_args(RationalQuadraticKernel, α, α > zero(T), "α > 0")
59+
return new{T}([α])
60+
end
61+
end
62+
63+
@functor RationalQuadraticKernel
64+
65+
function kappa::RationalQuadraticKernel, d²::T) where {T<:Real}
66+
return (one(T) +/ (2 * first.α)))^(-first.α))
67+
end
68+
69+
metric(::RationalQuadraticKernel) = SqEuclidean()
70+
71+
function Base.show(io::IO, κ::RationalQuadraticKernel)
72+
return print(io, "Rational Quadratic Kernel (α = $(first.α)))")
73+
end
74+
75+
"""
76+
GammaRationalKernel(; α::Real=2.0, γ::Real=2.0)
77+
78+
γ-rational kernel with shape parameters `α` and `γ`.
79+
80+
# Definition
81+
82+
For inputs ``x, x' \\in \\mathbb{R}^d``, the γ-rational kernel with shape
83+
parameters ``\\alpha > 0`` and ``\\gamma \\in (0, 2]`` is defined as
84+
```math
85+
k(x, x'; \\alpha, \\gamma) = \\bigg(1 + \\frac{\\|x - x'\\|_2^{\\gamma}}{\\alpha}\\bigg)^{-\\alpha}.
86+
```
87+
88+
The [`GammaExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
89+
90+
!!! warning
91+
The default value of parameter `γ` will be changed to `1.0` in the next breaking release
92+
of KernelFunctions.
93+
94+
See also: [`RationalKernel`](@ref), [`RationalQuadraticKernel`](@ref)
95+
"""
96+
struct GammaRationalKernel{Tα<:Real,Tγ<:Real} <: SimpleKernel
97+
α::Vector{Tα}
98+
γ::Vector{Tγ}
99+
# function GammaRationalKernel(;
100+
# alpha::Real=2.0, gamma::Real=1.0, α::Real=alpha, γ::Real=gamma
101+
# )
102+
function GammaRationalKernel(; alpha::Real=2.0, gamma=nothing, α::Real=alpha, γ=gamma)
103+
γ2 = if γ === nothing
104+
Base.depwarn(
105+
"the default value of parameter `γ` of the `GammaRationalKernel` will " *
106+
"be changed to `1.0` in the next breaking release of KernelFunctions",
107+
:GammaRationalKernel,
108+
)
109+
2.0
110+
else
111+
γ
112+
end
113+
@check_args(GammaRationalKernel, α, α > zero(α), "α > 0")
114+
@check_args(GammaRationalKernel, γ2, zero(γ2) < γ2 2, "γ ∈ (0, 2]")
115+
return new{typeof(α),typeof(γ2)}([α], [γ2])
116+
end
117+
end
118+
119+
@functor GammaRationalKernel
120+
121+
function kappa::GammaRationalKernel, d::Real)
122+
return (one(d) + d^first.γ) / first.α))^(-first.α))
123+
end
124+
125+
metric(::GammaRationalKernel) = Euclidean()
126+
127+
function Base.show(io::IO, κ::GammaRationalKernel)
128+
return print(io, "Gamma Rational Kernel (α = $(first.α)), γ = $(first.γ)))")
129+
end

src/basekernels/rationalquad.jl

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/deprecations.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
@deprecate transform(k::TransformedKernel, t::Transform) k.kernel t k.transform
33
@deprecate transform(k::Kernel, ρ::Real) k ScaleTransform(ρ)
44
@deprecate transform(k::Kernel, ρ::AbstractVector) k ARDTransform(ρ)
5+
6+
# TODO: Remove deprecations in the constructors and docstrings of `GammaExponentialKernel`
7+
# and `GammaRationalKernel`
8+
Base.@deprecate_binding GammaRationalQuadraticKernel GammaRationalKernel

0 commit comments

Comments
 (0)