Skip to content

Commit 16f1149

Browse files
Cyberfacewilltebbutttheogfdevmotion
authored
Adding Gibbskernel (#374)
* add gibbskernel.jl just sketching out the code and doc string * include gibbskernel.jl * some modifications for gibbskernel and added a small test * fix missing definition of ell function in test * fix gibbskernel test * add gibbskernel to test/runtests.jl * Update test/kernels/gibbskernel.jl Co-authored-by: willtebbutt <[email protected]> * Update src/kernels/gibbskernel.jl Co-authored-by: willtebbutt <[email protected]> * Update src/kernels/gibbskernel.jl Co-authored-by: willtebbutt <[email protected]> * implement changes based on PR discussion and update test * add brackets back in * fix unit test for gibbs kernel * update gibbskernel test: implement theogf's idea about using a non-trivial function for ell * ensure ell returns positive value in test added a broken test to seek help about why it's broken * gibbs kernel: fixed implementation and unittest bugs ironed out hopefully with lots of help from the devs * gibbs kernel: fix docstring * Update src/kernels/gibbskernel.jl remove space thanks @willtebbutt Co-authored-by: willtebbutt <[email protected]> * Update src/kernels/gibbskernel.jl thanks @theogf Co-authored-by: Théo Galy-Fajou <[email protected]> * add gibbskernel to docs * Update src/kernels/gibbskernel.jl thanks @devmotion Co-authored-by: David Widmann <[email protected]> * Update test/kernels/gibbskernel.jl thanks @devmotion Co-authored-by: David Widmann <[email protected]> * gibbskernel test: formatting * Fix format (hopefully) * Add newline character * Apply suggestions from code review * Export invsqrt2 * Version bump Co-authored-by: willtebbutt <[email protected]> Co-authored-by: Théo Galy-Fajou <[email protected]> Co-authored-by: David Widmann <[email protected]>
1 parent 6e7ca17 commit 16f1149

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

Project.toml

+1-1
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.10.20"
3+
version = "0.10.21"
44

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

docs/src/kernels.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CosineKernel
2727

2828
```@docs
2929
ExponentialKernel
30+
GibbsKernel
3031
LaplacianKernel
3132
SqExponentialKernel
3233
SEKernel

src/KernelFunctions.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export PiecewisePolynomialKernel
1717
export PeriodicKernel, NeuralNetworkKernel
1818
export KernelSum, KernelProduct, KernelTensorProduct
1919
export TransformedKernel, ScaledKernel, NormalizedKernel
20+
export GibbsKernel
2021

2122
export Transform,
2223
SelectTransform,
@@ -53,7 +54,7 @@ using Functors
5354
using LinearAlgebra
5455
using Requires
5556
using SpecialFunctions: loggamma, besselk, polygamma
56-
using IrrationalConstants: logtwo, twoπ
57+
using IrrationalConstants: logtwo, twoπ, invsqrt2
5758
using LogExpFunctions: softplus
5859
using StatsBase
5960
using TensorCore
@@ -94,6 +95,7 @@ include("basekernels/rational.jl")
9495
include("basekernels/sm.jl")
9596
include("basekernels/wiener.jl")
9697

98+
include("kernels/gibbskernel.jl")
9799
include("kernels/scaledkernel.jl")
98100
include("kernels/normalizedkernel.jl")
99101
include("matrix/kernelmatrix.jl")

src/kernels/gibbskernel.jl

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
GibbsKernel(; lengthscale)
3+
4+
# Definition
5+
6+
The Gibbs kernel is non-stationary generalisation of the squared exponential
7+
kernel. The lengthscale parameter ``l`` becomes a function of
8+
position ``l(x)``.
9+
10+
For a constant function``l(x) = c``, one recovers the standard squared exponential kernel
11+
with lengthscale `c`.
12+
13+
```math
14+
k(x, y; l) = \\sqrt{ \\left(\\frac{2 l(x) l(y)}{l(x)^2 + l(y)^2} \\right) }
15+
\\quad \\rm{exp} \\left( - \\frac{(x - y)^2}{l(x)^2 + l(y)^2} \\right)
16+
```
17+
18+
# References
19+
20+
Mark N. Gibbs. "Bayesian Gaussian Processes for Regression and Classication." PhD thesis, 1997
21+
22+
Christopher J. Paciorek and Mark J. Schervish. "Nonstationary Covariance Functions
23+
for Gaussian Process Regression". NeurIPS, 2003
24+
25+
Sami Remes, Markus Heinonen, Samuel Kaski. "Non-Stationary Spectral Kernels". arXiV:1705.08736, 2017
26+
27+
Sami Remes, Markus Heinonen, Samuel Kaski. "Neural Non-Stationary Spectral Kernel". arXiv:1811.10978, 2018
28+
"""
29+
struct GibbsKernel{T} <: Kernel
30+
lengthscale::T
31+
end
32+
33+
GibbsKernel(; lengthscale) = GibbsKernel(lengthscale)
34+
35+
function (k::GibbsKernel)(x, y)
36+
lengthscale = k.lengthscale
37+
lx = lengthscale(x)
38+
ly = lengthscale(y)
39+
l = invsqrt2 * hypot(lx, ly)
40+
kernel = (sqrt(lx * ly) / l) * with_lengthscale(SqExponentialKernel(), l)
41+
return kernel(x, y)
42+
end

test/kernels/gibbskernel.jl

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@testset "gibbskernel" begin
2+
x = randn()
3+
y = randn()
4+
5+
# this is the gibbs lengthscale function.
6+
ell(x) = exp(sum(sin, x))
7+
# create a gibbs kernel with our specific lengthscale function
8+
k_gibbs = GibbsKernel(ell)
9+
10+
@test k_gibbs(x, y)
11+
sqrt((2 * ell(x) * ell(y)) / (ell(x)^2 + ell(y)^2)) *
12+
exp(-(x - y)^2 / (ell(x)^2 + ell(y)^2))
13+
end

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ include("test_utils.jl")
126126
include("kernels/transformedkernel.jl")
127127
include("kernels/normalizedkernel.jl")
128128
include("kernels/neuralkernelnetwork.jl")
129+
include("kernels/gibbskernel.jl")
129130
end
130131
@info "Ran tests on Kernel"
131132
end

0 commit comments

Comments
 (0)