-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from theogf/remove-transform
Removing transform field and creating TransformedKernel (and ScaledKernel)
- Loading branch information
Showing
33 changed files
with
437 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.json | ||
*.cov | ||
Manifest.toml | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
@inline metric(κ::Kernel) = κ.metric | ||
|
||
## Allows to iterate over kernels | ||
Base.length(::Kernel) = 1 | ||
Base.iterate(k::Kernel) = (k,nothing) | ||
Base.iterate(k::Kernel, ::Any) = nothing | ||
|
||
# default fallback for evaluating a kernel with two arguments (such as vectors etc) | ||
kappa(κ::Kernel, x, y) = kappa(κ, evaluate(metric(κ), transform(κ, x), transform(κ, y))) | ||
kappa(κ::Kernel, x, y) = kappa(κ, evaluate(metric(κ), x, y)) | ||
kappa(κ::TransformedKernel, x, y) = kappa(kernel(κ), apply(κ.transform,x), apply(κ.transform,y)) | ||
kappa(κ::TransformedKernel{<:BaseKernel,<:ScaleTransform}, x, y) = kappa(κ, _scale(κ.transform, metric(κ), x, y)) | ||
_scale(t::ScaleTransform, metric::Euclidean, x, y) = first(t.s) * evaluate(metric, x, y) | ||
_scale(t::ScaleTransform, metric::Union{SqEuclidean,DotProduct}, x, y) = first(t.s)^2 * evaluate(metric, x, y) | ||
_scale(t::ScaleTransform, metric, x, y) = evaluate(metric, apply(t, x), apply(t, y)) | ||
|
||
printshifted(io::IO,κ::Kernel,shift::Int) = print(io,"$κ") | ||
Base.show(io::IO,κ::Kernel) = print(io,nameof(typeof(κ))) | ||
|
||
### Syntactic sugar for creating matrices and using kernel functions | ||
for k in [:ExponentialKernel,:SqExponentialKernel,:GammaExponentialKernel,:MaternKernel,:Matern32Kernel,:Matern52Kernel,:LinearKernel,:PolynomialKernel,:ExponentiatedKernel,:ZeroKernel,:WhiteKernel,:ConstantKernel,:RationalQuadraticKernel,:GammaRationalQuadraticKernel] | ||
for k in subtypes(BaseKernel) | ||
@eval begin | ||
@inline (κ::$k)(d::Real) = kappa(κ,d) #TODO Add test | ||
@inline (κ::$k)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = kappa(κ, x, y) | ||
@inline (κ::$k)(X::AbstractMatrix{T},Y::AbstractMatrix{T};obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ,X,Y,obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T};obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ,X,obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T}, Y::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, Y, obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, obsdim=obsdim) | ||
end | ||
end | ||
|
||
### Transform generics | ||
@inline transform(κ::Kernel) = κ.transform | ||
@inline transform(κ::Kernel, x) = transform(transform(κ), x) | ||
@inline transform(κ::Kernel, x, obsdim::Int) = transform(transform(κ), x, obsdim) | ||
|
||
## Constructors for kernels without parameters | ||
for kernel in [:ExponentialKernel,:SqExponentialKernel,:Matern32Kernel,:Matern52Kernel,:ExponentiatedKernel] | ||
for k in nameof.(subtypes(BaseKernel)) | ||
@eval begin | ||
$kernel() = $kernel(IdentityTransform()) | ||
$kernel(ρ::Real) = $kernel(ScaleTransform(ρ)) | ||
$kernel(ρ::AbstractVector{<:Real}) = $kernel(ARDTransform(ρ)) | ||
@deprecate($k(ρ::Real;args...),transform($k(args...),ρ)) | ||
@deprecate($k(ρ::AbstractVector{<:Real};args...),transform($k(args...),ρ)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,49 @@ | ||
""" | ||
ZeroKernel([tr=IdentityTransform()]) | ||
ZeroKernel() | ||
Create a kernel always returning zero | ||
Create a kernel that always returning zero | ||
``` | ||
κ(x,y) = 0.0 | ||
``` | ||
The output type depends of `x` and `y` | ||
""" | ||
struct ZeroKernel{Tr} <: Kernel{Tr} | ||
transform::Tr | ||
end | ||
|
||
ZeroKernel() = ZeroKernel(IdentityTransform()) | ||
struct ZeroKernel <: BaseKernel end | ||
|
||
@inline kappa(κ::ZeroKernel, d::T) where {T<:Real} = zero(T) | ||
kappa(κ::ZeroKernel, d::T) where {T<:Real} = zero(T) | ||
|
||
metric(::ZeroKernel) = Delta() | ||
|
||
""" | ||
`WhiteKernel([tr=IdentityTransform()])` | ||
`WhiteKernel()` | ||
``` | ||
κ(x,y) = δ(x,y) | ||
``` | ||
Kernel function working as an equivalent to add white noise. | ||
""" | ||
struct WhiteKernel{Tr} <: Kernel{Tr} | ||
transform::Tr | ||
end | ||
|
||
WhiteKernel() = WhiteKernel(IdentityTransform()) | ||
struct WhiteKernel <: BaseKernel end | ||
|
||
@inline kappa(κ::WhiteKernel,δₓₓ::Real) = δₓₓ | ||
kappa(κ::WhiteKernel,δₓₓ::Real) = δₓₓ | ||
|
||
metric(::WhiteKernel) = Delta() | ||
|
||
""" | ||
`ConstantKernel([tr=IdentityTransform(),[c=1.0]])` | ||
`ConstantKernel(c=1.0)` | ||
``` | ||
κ(x,y) = c | ||
``` | ||
Kernel function always returning a constant value `c` | ||
""" | ||
struct ConstantKernel{Tr, Tc<:Real} <: Kernel{Tr} | ||
transform::Tr | ||
struct ConstantKernel{Tc<:Real} <: BaseKernel | ||
c::Tc | ||
function ConstantKernel(;c::T=1.0) where {T<:Real} | ||
new{T}(c) | ||
end | ||
end | ||
|
||
params(k::ConstantKernel) = (params(k.transform),k.c) | ||
opt_params(k::ConstantKernel) = (opt_params(k.transform),k.c) | ||
|
||
ConstantKernel(c::Real=1.0) = ConstantKernel(IdentityTransform(),c) | ||
|
||
ConstantKernel(t::Tr,c::Tc=1.0) where {Tr<:Transform,Tc<:Real} = ConstantKernel{Tr,Tc}(t,c) | ||
params(k::ConstantKernel) = (k.c,) | ||
opt_params(k::ConstantKernel) = (k.c,) | ||
|
||
@inline kappa(κ::ConstantKernel,x::Real) = κ.c | ||
kappa(κ::ConstantKernel,x::Real) = κ.c*one(x) | ||
|
||
metric(::ConstantKernel) = Delta() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.