From 592414ce64bff32f567d04a262ab715411c5dcfc Mon Sep 17 00:00:00 2001 From: James Hensman Date: Wed, 1 Apr 2015 09:37:10 +0100 Subject: [PATCH] tests to probe the mean-function functionality --- GPy/testing/meanfunc_tests.py | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 GPy/testing/meanfunc_tests.py diff --git a/GPy/testing/meanfunc_tests.py b/GPy/testing/meanfunc_tests.py new file mode 100644 index 00000000..1d875377 --- /dev/null +++ b/GPy/testing/meanfunc_tests.py @@ -0,0 +1,56 @@ +# Copyright (c) 2015, James Hensman +# Licensed under the BSD 3-clause license (see LICENSE.txt) + +import unittest +import numpy as np +import GPy + +class MFtests(unittest.TestCase): + def simple_mean_function(): + """ + The simplest possible mean function. No parameters, just a simple Sinusoid. + """ + #create simple mean function + mf = GPy.core.Mapping(1,1) + mf.f = np.sin + mf.update_gradients = lambda a,b: None + + X = np.linspace(0,10,50).reshape(-1,1) + Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + + k =GPy.kern.RBF(1) + lik = GPy.likelihoods.Gaussian() + m = GPy.core.GP(X, Y, kernel=k, likelihood=lik, mean_function=mf) + self.assertTrue(m.checkgrad()) + + def test_parametric_mean_function(self): + """ + A linear mean function with parameters that we'll learn alongside the kernel + """ + + X = np.linspace(0,10,50).reshape(-1,1) + Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + 3*X + + mf = GPy.mappings.Linear(1,1) + + k =GPy.kern.RBF(1) + lik = GPy.likelihoods.Gaussian() + m = GPy.core.GP(X, Y, kernel=k, likelihood=lik, mean_function=mf) + self.assertTrue(m.checkgrad()) + + def test_svgp_mean_function(self): + + # an instance of the SVIGOP with a men function + X = np.linspace(0,10,500).reshape(-1,1) + Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + Y = np.where(Y>0, 1,0) # make aclassificatino problem + + mf = GPy.mappings.Linear(1,1) + Z = np.linspace(0,10,50).reshape(-1,1) + lik = GPy.likelihoods.Bernoulli() + k =GPy.kern.RBF(1) + GPy.kern.White(1, 1e-4) + m = GPy.core.SVGP(X, Y,Z=Z, kernel=k, likelihood=lik, mean_function=mf) + self.assertTrue(m.checkgrad()) + + +