mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-18 13:55:14 +02:00
some tidying in the likelihood classes
This commit is contained in:
parent
3a558d8244
commit
7dfbcebb87
6 changed files with 364 additions and 369 deletions
|
|
@ -18,12 +18,10 @@ class EP:
|
|||
self.likelihood_function = likelihood_function
|
||||
self.epsilon = epsilon
|
||||
self.eta, self.delta = power_ep
|
||||
self.jitter = 1e-12 # TODO: is this needed?
|
||||
self.is_heteroscedastic = True
|
||||
|
||||
"""
|
||||
Initial values - Likelihood approximation parameters:
|
||||
p(y|f) = t(f|tau_tilde,v_tilde)
|
||||
"""
|
||||
#Initial values - Likelihood approximation parameters:
|
||||
#p(y|f) = t(f|tau_tilde,v_tilde)
|
||||
self.tau_tilde = np.zeros(self.N)
|
||||
self.v_tilde = np.zeros(self.N)
|
||||
|
||||
|
|
@ -32,8 +30,11 @@ class EP:
|
|||
mu_tilde = self.v_tilde/self.tau_tilde #When calling EP, this variable is used instead of Y in the GP model
|
||||
sigma_sum = 1./self.tau_ + 1./self.tau_tilde
|
||||
mu_diff_2 = (self.v_/self.tau_ - mu_tilde)**2
|
||||
Z_ep = np.sum(np.log(self.Z_hat)) + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) #Normalization constant
|
||||
self.Y, self.beta, self.Z = self.tau_tilde[:,None], mu_tilde[:,None], Z_ep
|
||||
self.Z = np.sum(np.log(self.Z_hat)) + 0.5*np.sum(np.log(sigma_sum)) + 0.5*np.sum(mu_diff_2/sigma_sum) #Normalization constant, aka Z_ep
|
||||
|
||||
self.Y = mu_tilde[:,None]
|
||||
self.precsion = self.tau_tilde
|
||||
self.covariance_matrix = np.diag(1./self.precision)
|
||||
|
||||
def fit_full(self,K):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import numpy as np
|
|||
|
||||
class Gaussian:
|
||||
def __init__(self,data,variance=1.,normalize=False):
|
||||
self.is_heteroscedastic = False
|
||||
self.data = data
|
||||
self.N,D = data.shape
|
||||
self.Z = 0. # a correction factor which accounts for the approximation made
|
||||
|
|
@ -19,6 +20,7 @@ class Gaussian:
|
|||
self.YYT = np.dot(self.Y,self.Y.T)
|
||||
self._set_params(np.asarray(variance))
|
||||
|
||||
|
||||
def _get_params(self):
|
||||
return np.asarray(self._variance)
|
||||
|
||||
|
|
@ -27,7 +29,8 @@ class Gaussian:
|
|||
|
||||
def _set_params(self,x):
|
||||
self._variance = x
|
||||
self.variance = np.eye(self.N)*self._variance
|
||||
self.covariance_matrix = np.eye(self.N)*self._variance
|
||||
self.precision = 1./self._variance
|
||||
|
||||
def fit(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -8,18 +8,18 @@ import scipy as sp
|
|||
import pylab as pb
|
||||
from ..util.plot import gpplot
|
||||
|
||||
class likelihood:
|
||||
class likelihood_function:
|
||||
"""
|
||||
Likelihood class for doing Expectation propagation
|
||||
|
||||
:param Y: observed output (Nx1 numpy.darray)
|
||||
..Note:: Y values allowed depend on the likelihood used
|
||||
..Note:: Y values allowed depend on the likelihood_function used
|
||||
"""
|
||||
def __init__(self,location=0,scale=1):
|
||||
self.location = location
|
||||
self.scale = scale
|
||||
|
||||
class probit(likelihood):
|
||||
class probit(likelihood_function):
|
||||
"""
|
||||
Probit likelihood
|
||||
Y is expected to take values in {-1,1}
|
||||
|
|
@ -29,7 +29,7 @@ class probit(likelihood):
|
|||
$$
|
||||
"""
|
||||
def __init__(self,location=0,scale=1):
|
||||
likelihood.__init__(self,Y,location,scale)
|
||||
likelihood_function.__init__(self,Y,location,scale)
|
||||
|
||||
def moments_match(self,data_i,tau_i,v_i):
|
||||
"""
|
||||
|
|
@ -64,7 +64,7 @@ class probit(likelihood):
|
|||
def _log_likelihood_gradients():
|
||||
return np.zeros(0) # there are no parameters of whcih to compute the gradients
|
||||
|
||||
class poisson(likelihood):
|
||||
class poisson(likelihood_function):
|
||||
"""
|
||||
Poisson likelihood
|
||||
Y is expected to take values in {0,1,2,...}
|
||||
|
|
@ -75,7 +75,7 @@ class poisson(likelihood):
|
|||
"""
|
||||
def __init__(self,Y,location=0,scale=1):
|
||||
assert len(Y[Y<0]) == 0, "Output cannot have negative values"
|
||||
likelihood.__init__(self,Y,location,scale)
|
||||
likelihood_function.__init__(self,Y,location,scale)
|
||||
|
||||
def moments_match(self,i,tau_i,v_i):
|
||||
"""
|
||||
|
|
@ -160,7 +160,7 @@ class poisson(likelihood):
|
|||
if Z is not None:
|
||||
pb.plot(Z,Z*0+pb.ylim()[0],'k|',mew=1.5,markersize=12)
|
||||
|
||||
class gaussian(likelihood):
|
||||
class gaussian(likelihood_function):
|
||||
"""
|
||||
Gaussian likelihood
|
||||
Y is expected to take values in (-inf,inf)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue