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
This commit is contained in:
Eric Kalosa-Kenyon 2021-05-26 17:37:55 -07:00 committed by GitHub
parent 88d4fbf2c0
commit 62d735e6a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View file

@ -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