mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-04-27 05:46:24 +02:00
Trying to 'debug'
This commit is contained in:
parent
3f114aa020
commit
f9535c858a
3 changed files with 52 additions and 32 deletions
|
|
@ -5,13 +5,13 @@ from GPy.util.linalg import jitchol
|
|||
from functools import partial
|
||||
from GPy.likelihoods.likelihood import likelihood
|
||||
from GPy.util.linalg import pdinv,mdot
|
||||
|
||||
from scipy.stats import norm
|
||||
|
||||
|
||||
class Laplace(likelihood):
|
||||
"""Laplace approximation to a posterior"""
|
||||
|
||||
def __init__(self,data,likelihood_function):
|
||||
def __init__(self, data, likelihood_function):
|
||||
"""
|
||||
Laplace Approximation
|
||||
|
||||
|
|
@ -42,7 +42,13 @@ class Laplace(likelihood):
|
|||
GPy expects a likelihood to be gaussian, so need to caluclate the points Y^{squiggle} and Z^{squiggle}
|
||||
that makes the posterior match that found by a laplace approximation to a non-gaussian likelihood
|
||||
"""
|
||||
z_hat = N(f_hat|f_hat, hess_hat) / self.height_unnormalised
|
||||
#z_hat = N(f_hat|f_hat, hess_hat) / self.height_unnormalised
|
||||
normalised_approx = norm(loc=self.f_hat, scale=self.hess_hat)
|
||||
self.Z = normalised_approx.pdf(self.f_hat)/self.height_unnormalised
|
||||
#self.Y =
|
||||
#self.YYT =
|
||||
#self.covariance_matrix =
|
||||
#self.precision =
|
||||
|
||||
def fit_full(self, K):
|
||||
"""
|
||||
|
|
@ -51,11 +57,9 @@ class Laplace(likelihood):
|
|||
:K: Covariance matrix
|
||||
"""
|
||||
f = np.zeros((self.N, 1))
|
||||
print K.shape
|
||||
print f.shape
|
||||
print self.data.shape
|
||||
#K = np.diag(np.ones(self.N))
|
||||
(Ki, _, _, log_Kdet) = pdinv(K)
|
||||
obj_constant = (0.5 * log_Kdet) - ((0.5 * self.N) * np.log(2*np.pi))
|
||||
obj_constant = (0.5 * log_Kdet) - ((0.5 * self.N) * np.log(2 * np.pi))
|
||||
|
||||
#Find \hat(f) using a newton raphson optimizer for example
|
||||
#TODO: Add newton-raphson as subclass of optimizer class
|
||||
|
|
@ -77,11 +81,12 @@ class Laplace(likelihood):
|
|||
return np.squeeze(res)
|
||||
|
||||
self.f_hat = sp.optimize.fmin_ncg(obj, f, fprime=obj_grad, fhess=obj_hess)
|
||||
print self.f_hat
|
||||
|
||||
#At this point get the hessian matrix
|
||||
self.hess_hat = obj_hess(f_hat)
|
||||
self.hess_hat = obj_hess(self.f_hat)
|
||||
|
||||
#Need to add the constant as we previously were trying to avoid computing it (seems like a small overhead though...)
|
||||
self.height_unnormalised = obj(f_hat) #FIXME: Is it -1?
|
||||
self.height_unnormalised = obj(self.f_hat) #FIXME: Is it -1?
|
||||
|
||||
return _compute_GP_variables()
|
||||
return self._compute_GP_variables()
|
||||
|
|
|
|||
|
|
@ -15,27 +15,27 @@ class student_t(likelihood_function):
|
|||
dln p(yi|fi)_dfi
|
||||
d2ln p(yi|fi)_d2fifj
|
||||
"""
|
||||
def __init__(self, deg_free, sigma=1):
|
||||
def __init__(self, deg_free, sigma=2):
|
||||
self.v = deg_free
|
||||
self.sigma = 1
|
||||
self.sigma = sigma
|
||||
|
||||
def link_function(self, y, f):
|
||||
"""link_function $\ln p(y|f)$
|
||||
$$\ln p(y_{i}|f_{i}) = \ln \Gamma(\frac{v+1}{2}) - \ln \Gamma(\frac{v}{2})\sqrt{v \pi}\sigma - \frac{v+1}{2}\ln (1 + \frac{1}{v}\left(\frac{y_{i} - f_{i}}{\sigma}\right)^2$$
|
||||
|
||||
:y: datum number i
|
||||
:f: latent variable f
|
||||
:y: data
|
||||
:f: latent variables f
|
||||
:returns: float(likelihood evaluated for this point)
|
||||
|
||||
"""
|
||||
assert y.shape[0] == f.shape[0]
|
||||
e = y - f
|
||||
#print "Link ", y.shape, f.shape, e.shape
|
||||
objective = (gammaln((self.v + 1) * 0.5)
|
||||
- gammaln(self.v * 0.5)
|
||||
+ np.log(self.sigma * np.sqrt(self.v * np.pi))
|
||||
- (self.v + 1) * 0.5
|
||||
* np.log(1 + ((e**2 / self.sigma**2) / self.v))
|
||||
)
|
||||
- gammaln(self.v * 0.5)
|
||||
+ np.log(self.sigma * np.sqrt(self.v * np.pi))
|
||||
- (self.v + 1) * 0.5
|
||||
* np.log(1 + ((e**2 / self.sigma**2) / self.v))
|
||||
)
|
||||
return np.sum(objective)
|
||||
|
||||
def link_grad(self, y, f):
|
||||
|
|
@ -44,13 +44,13 @@ class student_t(likelihood_function):
|
|||
|
||||
$$\frac{d}{df}p(y_{i}|f_{i}) = \frac{(v + 1)(y - f)}{v \sigma^{2} + (y_{i} - f_{i})^{2}}$$
|
||||
|
||||
:y: datum number i
|
||||
:f: latent variable f
|
||||
:returns: float(gradient of likelihood evaluated at this point)
|
||||
:y: data
|
||||
:f: latent variables f
|
||||
:returns: gradient of likelihood evaluated at points
|
||||
|
||||
"""
|
||||
assert y.shape[0] == f.shape[0]
|
||||
e = y - f
|
||||
#print "Grad ", y.shape, f.shape, e.shape
|
||||
grad = ((self.v + 1) * e) / (self.v * (self.sigma**2) + (e**2))
|
||||
return grad
|
||||
|
||||
|
|
@ -63,10 +63,11 @@ class student_t(likelihood_function):
|
|||
|
||||
$$\frac{d^{2}p(y_{i}|f_{i})}{df^{2}} = \frac{(v + 1)(y - f)}{v \sigma^{2} + (y_{i} - f_{i})^{2}}$$
|
||||
|
||||
:y: datum number i
|
||||
:f: latent variable f
|
||||
:returns: float(second derivative of likelihood evaluated at this point)
|
||||
:y: data
|
||||
:f: latent variables f
|
||||
:returns: array which is diagonal of covariance matrix (second derivative of likelihood evaluated at points)
|
||||
"""
|
||||
assert y.shape[0] == f.shape[0]
|
||||
e = y - f
|
||||
hess = ((self.v + 1) * e) / ((((self.sigma**2)*self.v) + e**2)**2)
|
||||
hess = ((self.v + 1) * e) / ((((self.sigma**2) * self.v) + e**2)**2)
|
||||
return hess
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue