added fixed effect kernel

This commit is contained in:
Nicolo Fusi 2013-03-13 14:24:10 +00:00
parent e812368a87
commit 6da0feb7fc
4 changed files with 68 additions and 1 deletions

View file

@ -2,5 +2,5 @@
# Licensed under the BSD 3-clause license (see LICENSE.txt)
from constructors import rbf, Matern32, Matern52, exponential, linear, white, bias, finite_dimensional, spline, Brownian, rbf_sympy, sympykern, periodic_exponential, periodic_Matern32, periodic_Matern52, prod, prod_orthogonal, symmetric, coregionalise, rational_quadratic
from constructors import rbf, Matern32, Matern52, exponential, linear, white, bias, finite_dimensional, spline, Brownian, rbf_sympy, sympykern, periodic_exponential, periodic_Matern32, periodic_Matern52, prod, prod_orthogonal, symmetric, coregionalise, rational_quadratic, fixed
from kern import kern

View file

@ -12,6 +12,7 @@ from exponential import exponential as exponentialpart
from Matern32 import Matern32 as Matern32part
from Matern52 import Matern52 as Matern52part
from bias import bias as biaspart
from fixed import fixed as fixedpart
from finite_dimensional import finite_dimensional as finite_dimensionalpart
from spline import spline as splinepart
from Brownian import Brownian as Brownianpart
@ -296,3 +297,16 @@ def rational_quadratic(D,variance=1., lengthscale=1., power=1.):
"""
part = rational_quadraticpart(D,variance, lengthscale, power)
return kern(D, [part])
def fixed(D, K, variance=1.):
"""
Construct a fixed effect kernel.
Arguments
---------
D (int), obligatory
K (np.array), obligatory
variance (float)
"""
part = fixedpart(D, K, variance)
return kern(D, [part])

42
GPy/kern/fixed.py Normal file
View file

@ -0,0 +1,42 @@
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
from kernpart import kernpart
import numpy as np
import hashlib
class fixed(kernpart):
def __init__(self,D,K,variance=1.):
"""
:param D: the number of input dimensions
:type D: int
:param variance: the variance of the kernel
:type variance: float
"""
self.D = D
self.fixed_K = K
self.Nparam = 1
self.name = 'fixed'
self._set_params(np.array([variance]).flatten())
def _get_params(self):
return self.variance
def _set_params(self,x):
assert x.shape==(1,)
self.variance = x
def _get_param_names(self):
return ['variance']
def K(self,X,X2,target):
target += self.variance * self.fixed_K
def dK_dtheta(self,partial,X,X2,target):
target += (partial * self.fixed_K).sum()
def dK_dX(self, partial,X, X2, target):
pass
def dKdiag_dX(self,partial,X,target):
pass

View file

@ -15,6 +15,17 @@ class KernelTests(unittest.TestCase):
m = GPy.models.GP_regression(X,Y,K)
self.assertTrue(m.checkgrad())
def test_fixedkernel(self):
"""
Fixed effect kernel test
"""
X = np.random.rand(30, 4)
K = np.dot(X, X.T)
kernel = GPy.kern.fixed(4, K)
Y = np.ones((30,1))
m = GPy.models.GP_regression(X,Y,kernel=kernel)
self.assertTrue(m.checkgrad())
def test_coregionalisation(self):
X1 = np.random.rand(50,1)*8
X2 = np.random.rand(30,1)*5