From 62d735e6a659f356c381fb45dce53e26e7b84338 Mon Sep 17 00:00:00 2001 From: Eric Kalosa-Kenyon Date: Wed, 26 May 2021 17:37:55 -0700 Subject: [PATCH 1/2] Fix GPy.priors.InverseGamma (#903) * fixed InverseGamma prior: beforehand, it was a child class of Gamma but it defined a broken __new__ method of its own. Now, it just inherits Gamma's __new__; also added a test that ensures the InverseGamma can be instantiated and integrated into a GPy model * overwrote misleading inherited methods in InverseGamma, deleted unnecessary repeated code --- GPy/core/parameterization/priors.py | 22 +++++++--------------- GPy/testing/prior_tests.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/GPy/core/parameterization/priors.py b/GPy/core/parameterization/priors.py index 6269436b..c4dfbc2a 100644 --- a/GPy/core/parameterization/priors.py +++ b/GPy/core/parameterization/priors.py @@ -371,24 +371,17 @@ class InverseGamma(Gamma): """ domain = _POSITIVE _instances = [] - def __new__(cls, a=1, b=.5): # Singleton: - if cls._instances: - cls._instances[:] = [instance for instance in cls._instances if instance()] - for instance in cls._instances: - if instance().a == a and instance().b == b: - return instance() - o = super(Prior, cls).__new__(cls, a, b) - cls._instances.append(weakref.ref(o)) - return cls._instances[-1]() - - def __init__(self, a, b): - self._a = float(a) - self._b = float(b) - self.constant = -gammaln(self.a) + a * np.log(b) def __str__(self): return "iGa({:.2g}, {:.2g})".format(self.a, self.b) + def summary(self): + return {} + + @staticmethod + def from_EV(E, V): + raise NotImplementedError + def lnpdf(self, x): return self.constant - (self.a + 1) * np.log(x) - self.b / x @@ -398,7 +391,6 @@ class InverseGamma(Gamma): def rvs(self, n): return 1. / np.random.gamma(scale=1. / self.b, shape=self.a, size=n) - class DGPLVM_KFDA(Prior): """ Implementation of the Discriminative Gaussian Process Latent Variable function using diff --git a/GPy/testing/prior_tests.py b/GPy/testing/prior_tests.py index 377ae504..83dfd0d6 100644 --- a/GPy/testing/prior_tests.py +++ b/GPy/testing/prior_tests.py @@ -55,6 +55,21 @@ class PriorTests(unittest.TestCase): m.randomize() self.assertTrue(m.checkgrad()) + def test_InverseGamma(self): + # Test that this prior object can be instantiated and performs its basic functions + # in integration. + xmin, xmax = 1, 2.5*np.pi + b, C, SNR = 1, 0, 0.1 + X = np.linspace(xmin, xmax, 500) + y = b*X + C + 1*np.sin(X) + y += 0.05*np.random.randn(len(X)) + X, y = X[:, None], y[:, None] + m = GPy.models.GPRegression(X, y) + InverseGamma = GPy.priors.InverseGamma(1, 1) + m.rbf.set_prior(InverseGamma) + m.randomize() + self.assertTrue(m.checkgrad()) + def test_incompatibility(self): xmin, xmax = 1, 2.5*np.pi b, C, SNR = 1, 0, 0.1 From 4089a662a9e64d3a1fc268a941b690f29a46ef1d Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Fri, 28 May 2021 12:47:48 +1000 Subject: [PATCH 2/2] docs: fix simple typo, symetric -> symmetric (#883) There is a small typo in doc/source/tuto_creating_new_kernels.rst. Should read `symmetric` rather than `symetric`. --- doc/source/tuto_creating_new_kernels.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/tuto_creating_new_kernels.rst b/doc/source/tuto_creating_new_kernels.rst index ec46aedc..9d909c3b 100644 --- a/doc/source/tuto_creating_new_kernels.rst +++ b/doc/source/tuto_creating_new_kernels.rst @@ -7,7 +7,7 @@ We will see in this tutorial how to create new kernels in GPy. We will also give Structure of a kernel in GPy ============================ -In GPy a kernel object is made of a list of kernpart objects, which correspond to symetric positive definite functions. More precisely, the kernel should be understood as the sum of the kernparts. In order to implement a new covariance, the following steps must be followed +In GPy a kernel object is made of a list of kernpart objects, which correspond to symmetric positive definite functions. More precisely, the kernel should be understood as the sum of the kernparts. In order to implement a new covariance, the following steps must be followed 1. implement the new covariance as a :py:class:`GPy.kern.src.kern.Kern` object 2. update the :py:mod:`GPy.kern.src` file