mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-05 14:55:15 +02:00
Added capability for sinc covariance via sympy kernel.
This commit is contained in:
parent
0d4fd01ae1
commit
e06d889d35
4 changed files with 72 additions and 1 deletions
|
|
@ -292,7 +292,8 @@ except ImportError:
|
||||||
if sympy_available:
|
if sympy_available:
|
||||||
from parts.sympykern import spkern
|
from parts.sympykern import spkern
|
||||||
from sympy.parsing.sympy_parser import parse_expr
|
from sympy.parsing.sympy_parser import parse_expr
|
||||||
|
from GPy.util.symbolic import sinc
|
||||||
|
|
||||||
def rbf_sympy(input_dim, ARD=False, variance=1., lengthscale=1.):
|
def rbf_sympy(input_dim, ARD=False, variance=1., lengthscale=1.):
|
||||||
"""
|
"""
|
||||||
Radial Basis Function covariance.
|
Radial Basis Function covariance.
|
||||||
|
|
@ -312,6 +313,27 @@ if sympy_available:
|
||||||
f = variance*sp.exp(-dist/(2*lengthscale**2))
|
f = variance*sp.exp(-dist/(2*lengthscale**2))
|
||||||
return kern(input_dim, [spkern(input_dim, f, name='rbf_sympy')])
|
return kern(input_dim, [spkern(input_dim, f, name='rbf_sympy')])
|
||||||
|
|
||||||
|
def sinc(input_dim, ARD=False, variance=1., lengthscale=1.):
|
||||||
|
"""
|
||||||
|
TODO: Not clear why this isn't working, suggests argument of sinc is not a number.
|
||||||
|
sinc covariance funciton
|
||||||
|
"""
|
||||||
|
X = [sp.var('x%i' % i) for i in range(input_dim)]
|
||||||
|
Z = [sp.var('z%i' % i) for i in range(input_dim)]
|
||||||
|
variance = sp.var('variance',positive=True)
|
||||||
|
if ARD:
|
||||||
|
lengthscales = [sp.var('lengthscale_%i' % i, positive=True) for i in range(input_dim)]
|
||||||
|
dist_string = ' + '.join(['(x%i-z%i)**2/lengthscale_%i**2' % (i, i, i) for i in range(input_dim)])
|
||||||
|
dist = parse_expr(dist_string)
|
||||||
|
f = variance*sinc(sp.pi*sp.sqrt(dist))
|
||||||
|
else:
|
||||||
|
lengthscale = sp.var('lengthscale',positive=True)
|
||||||
|
dist_string = ' + '.join(['(x%i-z%i)**2' % (i, i) for i in range(input_dim)])
|
||||||
|
dist = parse_expr(dist_string)
|
||||||
|
f = variance*sinc(sp.pi*sp.sqrt(dist)/lengthscale)
|
||||||
|
|
||||||
|
return kern(input_dim, [spkern(input_dim, f, name='sinc')])
|
||||||
|
|
||||||
def sympykern(input_dim, k,name=None):
|
def sympykern(input_dim, k,name=None):
|
||||||
"""
|
"""
|
||||||
A base kernel object, where all the hard work in done by sympy.
|
A base kernel object, where all the hard work in done by sympy.
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,17 @@ double DiracDelta(double x){
|
||||||
double DiracDelta(double x,int foo){
|
double DiracDelta(double x,int foo){
|
||||||
return 0.0;
|
return 0.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
double sinc(double x){
|
||||||
|
if (x==0)
|
||||||
|
return 1.0;
|
||||||
|
else
|
||||||
|
return sin(x)/x;
|
||||||
|
}
|
||||||
|
|
||||||
|
double sinc_grad(double x){
|
||||||
|
if (x==0)
|
||||||
|
return 0.0;
|
||||||
|
else
|
||||||
|
return (x*cos(x) - sin(x))/(x*x);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
double DiracDelta(double x);
|
double DiracDelta(double x);
|
||||||
double DiracDelta(double x, int foo);
|
double DiracDelta(double x, int foo);
|
||||||
|
|
||||||
|
double sinc(double x);
|
||||||
|
double sinc_grad(double x);
|
||||||
|
|
|
||||||
32
GPy/util/symbolic.py
Normal file
32
GPy/util/symbolic.py
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
from sympy import Function, S, oo, I, cos, sin
|
||||||
|
|
||||||
|
|
||||||
|
class sinc_grad(Function):
|
||||||
|
nargs = 1
|
||||||
|
|
||||||
|
def fdiff(self, argindex=1):
|
||||||
|
return ((2-x*x)*sin(self.args[0]) - 2*x*cos(x))/(x*x*x)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def eval(cls, x):
|
||||||
|
if x is S.Zero:
|
||||||
|
return S.Zero
|
||||||
|
else:
|
||||||
|
return (x*cos(x) - sin(x))/(x*x)
|
||||||
|
|
||||||
|
class sinc(Function):
|
||||||
|
|
||||||
|
nargs = 1
|
||||||
|
|
||||||
|
def fdiff(self, argindex=1):
|
||||||
|
return sinc_grad(self.args[0])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def eval(cls, x):
|
||||||
|
if x is S.Zero:
|
||||||
|
return S.One
|
||||||
|
else:
|
||||||
|
return sin(x)/x
|
||||||
|
|
||||||
|
def _eval_is_real(self):
|
||||||
|
return self.args[0].is_real
|
||||||
Loading…
Add table
Add a link
Reference in a new issue