-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexponential.jl
More file actions
152 lines (108 loc) · 3.69 KB
/
exponential.jl
File metadata and controls
152 lines (108 loc) · 3.69 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
142
143
144
145
146
147
148
149
150
151
152
"""
SqExponentialKernel(; metric=Euclidean())
Squared exponential kernel with respect to the `metric`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the squared exponential kernel is
defined as
```math
k(x, x') = \\exp\\bigg(- \\frac{d(x, x')^2}{2}\\bigg).
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
See also: [`GammaExponentialKernel`](@ref)
"""
struct SqExponentialKernel{M} <: SimpleKernel
metric::M
end
SqExponentialKernel(; metric=Euclidean()) = SqExponentialKernel(metric)
kappa(::SqExponentialKernel, d::Real) = exp(-d^2 / 2)
kappa(::SqExponentialKernel{<:Euclidean}, d²::Real) = exp(-d² / 2)
metric(k::SqExponentialKernel) = k.metric
metric(::SqExponentialKernel{<:Euclidean}) = SqEuclidean()
iskroncompatible(::SqExponentialKernel) = true
function Base.show(io::IO, k::SqExponentialKernel)
return print(io, "Squared Exponential Kernel (metric = ", k.metric, ")")
end
## Aliases ##
"""
RBFKernel()
Alias of [`SqExponentialKernel`](@ref).
"""
const RBFKernel = SqExponentialKernel
"""
GaussianKernel()
Alias of [`SqExponentialKernel`](@ref).
"""
const GaussianKernel = SqExponentialKernel
"""
SEKernel()
Alias of [`SqExponentialKernel`](@ref).
"""
const SEKernel = SqExponentialKernel
"""
ExponentialKernel(; metric=Euclidean())
Exponential kernel with respect to the `metric`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the exponential kernel is defined as
```math
k(x, x') = \\exp\\big(- d(x, x')\\big).
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
See also: [`GammaExponentialKernel`](@ref)
"""
struct ExponentialKernel{M} <: SimpleKernel
metric::M
end
ExponentialKernel(; metric=Euclidean()) = ExponentialKernel(metric)
kappa(::ExponentialKernel, d::Real) = exp(-d)
metric(k::ExponentialKernel) = k.metric
iskroncompatible(::ExponentialKernel) = true
function Base.show(io::IO, k::ExponentialKernel)
return print(io, "Exponential Kernel (metric = ", k.metric, ")")
end
## Aliases ##
"""
LaplacianKernel()
Alias of [`ExponentialKernel`](@ref).
"""
const LaplacianKernel = ExponentialKernel
"""
Matern12Kernel()
Alias of [`ExponentialKernel`](@ref).
"""
const Matern12Kernel = ExponentialKernel
"""
GammaExponentialKernel(; γ::Real=1.0, metric=Euclidean())
γ-exponential kernel with respect to the `metric` and with parameter `γ`.
# Definition
For inputs ``x, x'`` and metric ``d(\\cdot, \\cdot)``, the γ-exponential kernel[^RW] with
parameter ``\\gamma \\in (0, 2]``
is defined as
```math
k(x, x'; \\gamma) = \\exp\\big(- d(x, x')^{\\gamma}\\big).
```
By default, ``d`` is the Euclidean metric ``d(x, x') = \\|x - x'\\|_2``.
See also: [`ExponentialKernel`](@ref), [`SqExponentialKernel`](@ref)
[^RW]: C. E. Rasmussen & C. K. I. Williams (2006). Gaussian Processes for Machine Learning.
"""
struct GammaExponentialKernel{Tγ<:Real,M} <: SimpleKernel
γ::Vector{Tγ}
metric::M
function GammaExponentialKernel(γ::Real, metric; check_args::Bool=true)
@check_args(GammaExponentialKernel, (γ, zero(γ) < γ ≤ 2, "γ ∈ (0, 2]"))
return new{typeof(γ),typeof(metric)}([γ], metric)
end
end
function GammaExponentialKernel(;
gamma::Real=1.0, γ::Real=gamma, metric=Euclidean(), check_args::Bool=true
)
return GammaExponentialKernel(γ, metric; check_args)
end
@functor GammaExponentialKernel
kappa(κ::GammaExponentialKernel, d::Real) = exp(-d^only(κ.γ))
metric(k::GammaExponentialKernel) = k.metric
iskroncompatible(::GammaExponentialKernel) = true
function Base.show(io::IO, κ::GammaExponentialKernel)
return print(
io, "Gamma Exponential Kernel (γ = ", only(κ.γ), ", metric = ", κ.metric, ")"
)
end