diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index 6852384c..5d8a7d15 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -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 diff --git a/GPy/kern/constructors.py b/GPy/kern/constructors.py index 983674b0..ec34242b 100644 --- a/GPy/kern/constructors.py +++ b/GPy/kern/constructors.py @@ -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]) diff --git a/GPy/kern/fixed.py b/GPy/kern/fixed.py new file mode 100644 index 00000000..8732ec8f --- /dev/null +++ b/GPy/kern/fixed.py @@ -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 diff --git a/GPy/testing/kernel_tests.py b/GPy/testing/kernel_tests.py index f1762db8..0f6d8772 100644 --- a/GPy/testing/kernel_tests.py +++ b/GPy/testing/kernel_tests.py @@ -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