-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathrational.jl
More file actions
141 lines (108 loc) · 4.3 KB
/
rational.jl
File metadata and controls
141 lines (108 loc) · 4.3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
RationalKernel(; α::Real=2.0, metric=Euclidean())
Rational kernel with shape parameter `α` and given `metric`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the rational kernel with shape parameter ``\\alpha > 0`` is defined as
```math
k(x, x'; \\alpha) = \\bigg(1 + \\frac{d(x, x')}{\\alpha}\\bigg)^{-\\alpha}.
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
The [`ExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
See also: [`GammaRationalKernel`](@ref)
"""
struct RationalKernel{Tα<:Real,M} <: SimpleKernel
α::Vector{Tα}
metric::M
function RationalKernel(α::Real, metric)
@check_args(RationalKernel, α, α > zero(α), "α > 0")
return new{typeof(α),typeof(metric)}([α], metric)
end
end
function RationalKernel(; alpha::Real=2.0, α::Real=alpha, metric=Euclidean())
return RationalKernel(α, metric)
end
@functor RationalKernel
function kappa(κ::RationalKernel, d::Real)
return (one(d) + d / only(κ.α))^(-only(κ.α))
end
metric(k::RationalKernel) = k.metric
function Base.show(io::IO, κ::RationalKernel)
return print(io, "Rational Kernel (α = ", only(κ.α), ", metric = ", κ.metric, ")")
end
"""
RationalQuadraticKernel(; α::Real=2.0, metric=Euclidean())
Rational-quadratic kernel with respect to the `metric` and with shape parameter `α`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the rational-quadratic kernel with
shape parameter ``\\alpha > 0`` is defined as
```math
k(x, x'; \\alpha) = \\bigg(1 + \\frac{d(x, x')^2}{2\\alpha}\\bigg)^{-\\alpha}.
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
The [`SqExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
See also: [`GammaRationalKernel`](@ref)
"""
struct RationalQuadraticKernel{Tα<:Real,M} <: SimpleKernel
α::Vector{Tα}
metric::M
function RationalQuadraticKernel(; alpha::Real=2.0, α::Real=alpha, metric=Euclidean())
@check_args(RationalQuadraticKernel, α, α > zero(α), "α > 0")
return new{typeof(α),typeof(metric)}([α], metric)
end
end
@functor RationalQuadraticKernel
function kappa(κ::RationalQuadraticKernel, d::Real)
return (one(d) + d^2 / (2 * only(κ.α)))^(-only(κ.α))
end
function kappa(κ::RationalQuadraticKernel{<:Real,<:Euclidean}, d²::Real)
return (one(d²) + d² / (2 * only(κ.α)))^(-only(κ.α))
end
metric(k::RationalQuadraticKernel) = k.metric
metric(::RationalQuadraticKernel{<:Real,<:Euclidean}) = SqEuclidean()
function Base.show(io::IO, κ::RationalQuadraticKernel)
return print(
io, "Rational Quadratic Kernel (α = ", only(κ.α), ", metric = ", κ.metric, ")"
)
end
"""
GammaRationalKernel(; α::Real=2.0, γ::Real=1.0, metric=Euclidean())
γ-rational kernel with respect to the `metric` with shape parameters `α` and `γ`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the γ-rational kernel with shape
parameters ``\\alpha > 0`` and ``\\gamma \\in (0, 2]`` is defined as
```math
k(x, x'; \\alpha, \\gamma) = \\bigg(1 + \\frac{d(x, x')^{\\gamma}}{\\alpha}\\bigg)^{-\\alpha}.
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
The [`GammaExponentialKernel`](@ref) is recovered in the limit as ``\\alpha \\to \\infty``.
See also: [`RationalKernel`](@ref), [`RationalQuadraticKernel`](@ref)
"""
struct GammaRationalKernel{Tα<:Real,Tγ<:Real,M} <: SimpleKernel
α::Vector{Tα}
γ::Vector{Tγ}
metric::M
function GammaRationalKernel(;
alpha::Real=2.0, gamma::Real=1.0, α::Real=alpha, γ::Real=gamma, metric=Euclidean()
)
@check_args(GammaRationalKernel, α, α > zero(α), "α > 0")
@check_args(GammaRationalKernel, γ, zero(γ) < γ ≤ 2, "γ ∈ (0, 2]")
return new{typeof(α),typeof(γ),typeof(metric)}([α], [γ], metric)
end
end
@functor GammaRationalKernel
function kappa(κ::GammaRationalKernel, d::Real)
return (one(d) + d^only(κ.γ) / only(κ.α))^(-only(κ.α))
end
metric(k::GammaRationalKernel) = k.metric
function Base.show(io::IO, κ::GammaRationalKernel)
return print(
io,
"Gamma Rational Kernel (α = ",
only(κ.α),
", γ = ",
only(κ.γ),
", metric = ",
κ.metric,
")",
)
end