diff --git a/GPy/core/gp.py b/GPy/core/gp.py index decca6b8..8fbc3719 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -604,3 +604,10 @@ class GP(Model): mu_star, var_star = self._raw_predict(x_test) return self.likelihood.log_predictive_density_sampling(y_test, mu_star, var_star, Y_metadata=Y_metadata, num_samples=num_samples) + def LOO(self): + """ + Evaluate the approximate leave one out error using the current state of the inference method + """ + return self.inference_method.LOO(self.kern, self.X, self.Y, self.likelihood, self.posterior, Y_metadata=self.Y_metadata) + + diff --git a/GPy/inference/latent_function_inference/__init__.py b/GPy/inference/latent_function_inference/__init__.py index eabc6cc9..f0a87443 100644 --- a/GPy/inference/latent_function_inference/__init__.py +++ b/GPy/inference/latent_function_inference/__init__.py @@ -41,6 +41,13 @@ class LatentFunctionInference(object): """ pass + def LOO(self, kern, X, Y, likelihood, posterior, *args, **kwargs): + """ + Leave one out error for the inference algorithm + """ + raise NotImplementedError + + class InferenceMethodList(LatentFunctionInference, list): def on_optimization_start(self): diff --git a/GPy/testing/model_tests.py b/GPy/testing/model_tests.py index e4411e23..2185f08b 100644 --- a/GPy/testing/model_tests.py +++ b/GPy/testing/model_tests.py @@ -406,7 +406,16 @@ class MiscTests(unittest.TestCase): warp_f.plot(X.min()-10, X.max()+10) plt.show() - + def test_LOO_laplace(self): + import GPy + gauss = GPy.likelihoods.Gaussian() + kern = GPy.kern.RBF(self.X.shape[1]) + GPy.kern.White(self.X.shape[1]) + exact = GPy.inference.latent_function_inference.ExactGaussianInference() + m_exact = GPy.core.GP(X=self.X, Y=self.Y, kernel=kern.copy(), likelihood=gauss.copy(), inference_method=exact) + laplace = GPy.inference.latent_function_inference.Laplace() + m_laplace = GPy.core.GP(X=self.X, Y=self.Y, kernel=kern.copy(), likelihood=gauss.copy(), inference_method=laplace) + m_laplace[:] = m_exact[:] + np.testing.assert_almost_equal(m_exact.LOO(), m_laplace.LOO()) class GradientTests(np.testing.TestCase): def setUp(self):