-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathconstant.jl
More file actions
158 lines (128 loc) · 3.99 KB
/
constant.jl
File metadata and controls
158 lines (128 loc) · 3.99 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
153
154
155
156
157
158
"""
ZeroKernel()
Zero kernel.
# Definition
For inputs ``x, x'``, the zero kernel is defined as
```math
k(x, x') = 0.
```
The output type depends on ``x`` and ``x'``.
See also: [`ConstantKernel`](@ref)
"""
struct ZeroKernel <: SimpleKernel end
@noparams ZeroKernel
# SimpleKernel interface
kappa(::ZeroKernel, ::Real) = false
metric(::ZeroKernel) = Delta()
# Optimizations
(::ZeroKernel)(x, y) = false
kernelmatrix(::ZeroKernel, x::AbstractVector) = Falses(length(x), length(x))
function kernelmatrix(::ZeroKernel, x::AbstractVector, y::AbstractVector)
validate_inputs(x, y)
return Falses(length(x), length(y))
end
function kernelmatrix!(K::AbstractMatrix, ::ZeroKernel, x::AbstractVector)
validate_inplace_dims(K, x)
return fill!(K, zero(eltype(K)))
end
function kernelmatrix!(
K::AbstractMatrix, ::ZeroKernel, x::AbstractVector, y::AbstractVector
)
validate_inplace_dims(K, x, y)
return fill!(K, zero(eltype(K)))
end
kernelmatrix_diag(::ZeroKernel, x::AbstractVector) = Falses(length(x))
function kernelmatrix_diag(::ZeroKernel, x::AbstractVector, y::AbstractVector)
validate_inputs(x, y)
return Falses(length(x))
end
function kernelmatrix_diag!(K::AbstractVector, ::ZeroKernel, x::AbstractVector)
validate_inplace_dims(K, x)
return fill!(K, zero(eltype(K)))
end
function kernelmatrix_diag!(
K::AbstractVector, ::ZeroKernel, x::AbstractVector, y::AbstractVector
)
validate_inplace_dims(K, x, y)
return fill!(K, zero(eltype(K)))
end
Base.show(io::IO, ::ZeroKernel) = print(io, "Zero Kernel")
"""
WhiteKernel()
White noise kernel.
# Definition
For inputs ``x, x'``, the white noise kernel is defined as
```math
k(x, x') = \\delta(x, x').
```
"""
struct WhiteKernel <: SimpleKernel end
@noparams WhiteKernel
"""
EyeKernel()
Alias of [`WhiteKernel`](@ref).
"""
const EyeKernel = WhiteKernel
kappa(κ::WhiteKernel, δₓₓ::Real) = δₓₓ
metric(::WhiteKernel) = Delta()
Base.show(io::IO, ::WhiteKernel) = print(io, "White Kernel")
"""
ConstantKernel(; c::Real=1.0)
Kernel of constant value `c`.
# Definition
For inputs ``x, x'``, the kernel of constant value ``c \\geq 0`` is defined as
```math
k(x, x') = c.
```
See also: [`ZeroKernel`](@ref)
"""
struct ConstantKernel{T<:Real} <: SimpleKernel
c::T
function ConstantKernel(c::Real)
@check_args(ConstantKernel, c, c >= zero(c), "c ≥ 0")
return new{typeof(c)}(c)
end
end
ConstantKernel(; c::Real=1.0) = ConstantKernel(c)
function ParameterHandling.flatten(::Type{T}, k::ConstantKernel{S}) where {T<:Real,S}
function unflatten_to_constantkernel(v::Vector{T})
return ConstantKernel(; c=S(exp(only(v))))
end
return T[log(k.c)], unflatten_to_constantkernel
end
# SimpleKernel interface
kappa(κ::ConstantKernel, ::Real) = κ.c
metric(::ConstantKernel) = Delta()
# Optimizations
(k::ConstantKernel)(x, y) = k.c
kernelmatrix(k::ConstantKernel, x::AbstractVector) = Fill(k.c, length(x), length(x))
function kernelmatrix(k::ConstantKernel, x::AbstractVector, y::AbstractVector)
validate_inputs(x, y)
return Fill(k.c, length(x), length(y))
end
function kernelmatrix!(K::AbstractMatrix, k::ConstantKernel, x::AbstractVector)
validate_inplace_dims(K, x)
return fill!(K, k.c)
end
function kernelmatrix!(
K::AbstractMatrix, k::ConstantKernel, x::AbstractVector, y::AbstractVector
)
validate_inplace_dims(K, x, y)
return fill!(K, k.c)
end
kernelmatrix_diag(k::ConstantKernel, x::AbstractVector) = Fill(k.c, length(x))
function kernelmatrix_diag(k::ConstantKernel, x::AbstractVector, y::AbstractVector)
validate_inputs(x, y)
return Fill(k.c, length(x))
end
function kernelmatrix_diag!(K::AbstractVector, k::ConstantKernel, x::AbstractVector)
validate_inplace_dims(K, x)
return fill!(K, k.c)
end
function kernelmatrix_diag!(
K::AbstractVector, k::ConstantKernel, x::AbstractVector, y::AbstractVector
)
validate_inplace_dims(K, x, y)
return fill!(K, k.c)
end
Base.show(io::IO, κ::ConstantKernel) = print(io, "Constant Kernel (c = ", κ.c, ")")