-
Notifications
You must be signed in to change notification settings - Fork 36
Adding Gibbskernel #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Gibbskernel #374
Changes from 14 commits
d50f2b4
2ca62f4
2b83286
e4d1414
f9f066a
d6b53b9
698c453
3a98028
0f757a3
2e1df2d
b5c6faa
19e6665
21de7d4
4ddafbe
9fd1d14
5531851
78faeac
ae8b3d4
6b91a23
a914957
6eb4099
79aa4f5
0942447
de3e535
0caf2fd
cfbca3d
1fb7903
f577700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,44 @@ | ||||||||||||||||||
""" | ||||||||||||||||||
GibbsKernel(x, y) | ||||||||||||||||||
|
||||||||||||||||||
# Definition | ||||||||||||||||||
|
||||||||||||||||||
The Gibbs kernel is non-stationary generalisation of the Squared-Exponential | ||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
kernel. The lengthscale parameter ``l`` becomes a function of | ||||||||||||||||||
position ``l(x)``. | ||||||||||||||||||
|
||||||||||||||||||
``l(x) = 1.`` then you recover the standard Squared-Exponential kernel | ||||||||||||||||||
with constant lengthscale. | ||||||||||||||||||
|
||||||||||||||||||
```math | ||||||||||||||||||
k(x, y) = \\sqrt{ \\left(\\frac{2 l(x) l(y)}{l(x)^2 + l(y)^2} \\right) } | ||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
\\quad \\rm{exp} \\left( - \\frac{(x - y)^2}{l(x)^2 + l(y)^2} \\right) | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
[1] - Mark N. Gibbs. "Bayesian Gaussian Processes for Regression and Classication." | ||||||||||||||||||
PhD thesis, 1997 | ||||||||||||||||||
|
||||||||||||||||||
[2] - Christopher J. Paciorek and Mark J. Schervish. "Nonstationary Covariance | ||||||||||||||||||
Functions for Gaussian Process Regression". NEURIPS, 2003 | ||||||||||||||||||
|
||||||||||||||||||
[3] - Sami Remes, Markus Heinonen, Samuel Kaski. | ||||||||||||||||||
"Non-Stationary Spectral Kernels". arXiV:1705.08736, 2017 | ||||||||||||||||||
|
||||||||||||||||||
[4] - Sami Remes, Markus Heinonen, Samuel Kaski. | ||||||||||||||||||
"Neural Non-Stationary Spectral Kernel". arXiv:1811.10978, 2018 | ||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
Cyberface marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
struct GibbsKernel{T} <: Kernel | ||||||||||||||||||
lengthscale::T | ||||||||||||||||||
end | ||||||||||||||||||
|
||||||||||||||||||
GibbsKernel(; lengthscale) = GibbsKernel(lengthscale) | ||||||||||||||||||
|
||||||||||||||||||
function (k::GibbsKernel)(x, y) | ||||||||||||||||||
lengthscale = k.lengthscale | ||||||||||||||||||
lx = lengthscale(x) | ||||||||||||||||||
ly = lengthscale(y) | ||||||||||||||||||
l = hypot(lx, ly) | ||||||||||||||||||
kernel = (sqrt(2 * lx * ly) / l) * with_lengthscale(SqExponentialKernel(), l) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this should more correspond to the exact formula There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without promotion to Float64:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct though because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't it be
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes but in the latex formula the factor 2 is not there, so this way we cancel it out! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the 2 disappears because we already have l(x) + l(y) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah the 2 would be there when l(x) = l(y) = constant. I think I finally understand most of the things here but I am confused by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure what 2 you refer to (and there is no l(x) + l(y) term anywhere 🤔). The 2 in the square root outside of the squared exponential kernel disappears because we already scale the square root of the denominator by 1/sqrt(2).
It will be squared by the squared exponential kernel. A squared exponential kernel with lengthscale l is of the form There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the And when Also thanks for explaining to me about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, if they are constant you get which is exactly the formula in the docstring (and the other issue). |
||||||||||||||||||
return kernel(x, y) | ||||||||||||||||||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
@testset "gibbskernel" begin | ||||||||||||||||||||||||||||||||||||||||||||||
x = randn(2) | ||||||||||||||||||||||||||||||||||||||||||||||
y = randn(2) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# generate random number of standard SqExponentialKernel lengthscale | ||||||||||||||||||||||||||||||||||||||||||||||
# taking exp() to ensure ell is positive | ||||||||||||||||||||||||||||||||||||||||||||||
ell(x) = exp(sum(sin, x)) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# this is the gibbs lengthscale function. | ||||||||||||||||||||||||||||||||||||||||||||||
# to check that we can recover the stationary SqExponentialKernel | ||||||||||||||||||||||||||||||||||||||||||||||
# with a constant lengthscale we just set this function | ||||||||||||||||||||||||||||||||||||||||||||||
# equal to a constant. | ||||||||||||||||||||||||||||||||||||||||||||||
l_func(x) = ell(1) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# in order to compare to the Gibbs kernel we compute the | ||||||||||||||||||||||||||||||||||||||||||||||
# equivalent lengthscale l(x)^2 + l(y)^2. | ||||||||||||||||||||||||||||||||||||||||||||||
# See the denominator of the exponential term in the gibbs kernel. | ||||||||||||||||||||||||||||||||||||||||||||||
lengthscale = hypot(l_func(x), l_func(y)) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# create a SqExponentialKernel with a lengthscale | ||||||||||||||||||||||||||||||||||||||||||||||
k = with_lengthscale(SqExponentialKernel(), lengthscale) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# create a gibbs kernel with our constant lengthscale function | ||||||||||||||||||||||||||||||||||||||||||||||
k_gibbs = GibbsKernel(l_func) | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
That's more what I meant to use instead of a constant. |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
# check they are equal | ||||||||||||||||||||||||||||||||||||||||||||||
@test k_gibbs(x, y) == k(x, y) | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the test it would be great if you could be as faithful as possible to the latex formula, it makes it easier to see if it's doing the right thing! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sure what you mean, could you elaborate or sketch out an idea of what you would like to see? Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n.b. probably better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I see what you mean. I've added your suggestion but actually this test is failing and I'm not sure why! I don't think I fully understand the implementation in https://github.com/Cyberface/KernelFunctions.jl/blob/gibbskernel/src/kernels/gibbskernel.jl#L37 so will take a look a bit later again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure the using
This returns a single number - shouldn't this be a 2x2 matrix? Assuming the Sorry for my confusion! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No no, this the L2 norm! So it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I get you now - I thought this was supposed to give the covariance matrix but that is the This is just the covariance between two points in n-dimensions. Hopefully I got this correct :) |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
k_gibbs = GibbsKernel(ell) | ||||||||||||||||||||||||||||||||||||||||||||||
@test k_gibbs(x, y) ≈ sqrt((2 * ell(x) * ell(y)) / (ell(x)^2 + ell(y)^2)) * exp(- norm(x - y)^2 / (ell(x)^2 + ell(y)^2)) | ||||||||||||||||||||||||||||||||||||||||||||||
Cyberface marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.