Skip to content

Commit 62d735e

Browse files
authored
Fix GPy.priors.InverseGamma (SheffieldML#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
1 parent 88d4fbf commit 62d735e

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

GPy/core/parameterization/priors.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -371,24 +371,17 @@ class InverseGamma(Gamma):
371371
"""
372372
domain = _POSITIVE
373373
_instances = []
374-
def __new__(cls, a=1, b=.5): # Singleton:
375-
if cls._instances:
376-
cls._instances[:] = [instance for instance in cls._instances if instance()]
377-
for instance in cls._instances:
378-
if instance().a == a and instance().b == b:
379-
return instance()
380-
o = super(Prior, cls).__new__(cls, a, b)
381-
cls._instances.append(weakref.ref(o))
382-
return cls._instances[-1]()
383-
384-
def __init__(self, a, b):
385-
self._a = float(a)
386-
self._b = float(b)
387-
self.constant = -gammaln(self.a) + a * np.log(b)
388374

389375
def __str__(self):
390376
return "iGa({:.2g}, {:.2g})".format(self.a, self.b)
391377

378+
def summary(self):
379+
return {}
380+
381+
@staticmethod
382+
def from_EV(E, V):
383+
raise NotImplementedError
384+
392385
def lnpdf(self, x):
393386
return self.constant - (self.a + 1) * np.log(x) - self.b / x
394387

@@ -398,7 +391,6 @@ def lnpdf_grad(self, x):
398391
def rvs(self, n):
399392
return 1. / np.random.gamma(scale=1. / self.b, shape=self.a, size=n)
400393

401-
402394
class DGPLVM_KFDA(Prior):
403395
"""
404396
Implementation of the Discriminative Gaussian Process Latent Variable function using

GPy/testing/prior_tests.py

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ def test_Gamma(self):
5555
m.randomize()
5656
self.assertTrue(m.checkgrad())
5757

58+
def test_InverseGamma(self):
59+
# Test that this prior object can be instantiated and performs its basic functions
60+
# in integration.
61+
xmin, xmax = 1, 2.5*np.pi
62+
b, C, SNR = 1, 0, 0.1
63+
X = np.linspace(xmin, xmax, 500)
64+
y = b*X + C + 1*np.sin(X)
65+
y += 0.05*np.random.randn(len(X))
66+
X, y = X[:, None], y[:, None]
67+
m = GPy.models.GPRegression(X, y)
68+
InverseGamma = GPy.priors.InverseGamma(1, 1)
69+
m.rbf.set_prior(InverseGamma)
70+
m.randomize()
71+
self.assertTrue(m.checkgrad())
72+
5873
def test_incompatibility(self):
5974
xmin, xmax = 1, 2.5*np.pi
6075
b, C, SNR = 1, 0, 0.1

0 commit comments

Comments
 (0)