mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-18 13:55:14 +02:00
Merge branch 'newGP' of github.com:SheffieldML/GPy into newGP
Conflicts: GPy/likelihoods/EP.py
This commit is contained in:
commit
64280d7eb6
7 changed files with 136 additions and 136 deletions
|
|
@ -16,6 +16,8 @@ class EP(likelihood):
|
|||
self.likelihood_function = likelihood_function
|
||||
self.epsilon = epsilon
|
||||
self.eta, self.delta = power_ep
|
||||
self.data = data
|
||||
self.N = self.data.size
|
||||
self.is_heteroscedastic = True
|
||||
|
||||
#Initial values - Likelihood approximation parameters:
|
||||
|
|
@ -23,6 +25,24 @@ class EP(likelihood):
|
|||
self.tau_tilde = np.zeros(self.N)
|
||||
self.v_tilde = np.zeros(self.N)
|
||||
|
||||
#initial values for the GP variables
|
||||
self.Y = np.zeros((self.N,1))
|
||||
self.covariance_matrix = np.eye(self.N)
|
||||
self.Z = 0
|
||||
self.YYT = None
|
||||
|
||||
def predictive_values(self,mu,var):
|
||||
return self.likelihood_function.predictive_values(mu,var)
|
||||
|
||||
def _get_params(self):
|
||||
return np.zeros(0)
|
||||
def _get_param_names(self):
|
||||
return []
|
||||
def _set_params(self,p):
|
||||
pass # TODO: the EP likelihood might want to take some parameters...
|
||||
def _gradients(self,partial):
|
||||
return np.zeros(0) # TODO: the EP likelihood might want to take some parameters...
|
||||
|
||||
def _compute_GP_variables(self):
|
||||
#Variables to be called from GP
|
||||
mu_tilde = self.v_tilde/self.tau_tilde #When calling EP, this variable is used instead of Y in the GP model
|
||||
|
|
@ -31,7 +51,8 @@ class EP(likelihood):
|
|||
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.precision = self.tau_tilde[:,None]
|
||||
self.YYT = np.dot(self.Y,self.Y.T)
|
||||
self.precision = self.tau_tilde
|
||||
self.covariance_matrix = np.diag(1./self.precision)
|
||||
|
||||
def fit_full(self,K):
|
||||
|
|
@ -41,6 +62,8 @@ class EP(likelihood):
|
|||
"""
|
||||
#Prior distribution parameters: p(f|X) = N(f|0,K)
|
||||
|
||||
self.tau_tilde = np.zeros(self.N)
|
||||
self.v_tilde = np.zeros(self.N)
|
||||
#Initial values - Posterior distribution parameters: q(f|X,Y) = N(f|mu,Sigma)
|
||||
self.mu = np.zeros(self.N)
|
||||
self.Sigma = K.copy()
|
||||
|
|
@ -73,8 +96,9 @@ class EP(likelihood):
|
|||
#Cavity distribution parameters
|
||||
self.tau_[i] = 1./self.Sigma[i,i] - self.eta*self.tau_tilde[i]
|
||||
self.v_[i] = self.mu[i]/self.Sigma[i,i] - self.eta*self.v_tilde[i]
|
||||
print 1./self.Sigma[i,i],self.tau_tilde[i]
|
||||
#Marginal moments
|
||||
self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i])
|
||||
self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood_function.moments_match(self.data[i],self.tau_[i],self.v_[i])
|
||||
#Site parameters update
|
||||
Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma[i,i])
|
||||
Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma[i,i])
|
||||
|
|
@ -85,6 +109,7 @@ class EP(likelihood):
|
|||
self.Sigma = self.Sigma - Delta_tau/(1.+ Delta_tau*self.Sigma[i,i])*np.dot(si,si.T)
|
||||
self.mu = np.dot(self.Sigma,self.v_tilde)
|
||||
self.iterations += 1
|
||||
print self.tau_tilde[i]
|
||||
#Sigma recomptutation with Cholesky decompositon
|
||||
Sroot_tilde_K = np.sqrt(self.tau_tilde)[:,None]*K
|
||||
B = np.eye(self.N) + np.sqrt(self.tau_tilde)[None,:]*Sroot_tilde_K
|
||||
|
|
@ -105,7 +130,7 @@ class EP(likelihood):
|
|||
For nomenclature see ... 2013.
|
||||
"""
|
||||
|
||||
#TODO: this doesn;t work with uncertain inputs!
|
||||
#TODO: this doesn;t work with uncertain inputs!
|
||||
|
||||
"""
|
||||
Prior approximation parameters:
|
||||
|
|
@ -246,7 +271,7 @@ class EP(likelihood):
|
|||
self.tau_[i] = 1./self.Sigma_diag[i] - self.eta*self.tau_tilde[i]
|
||||
self.v_[i] = self.mu[i]/self.Sigma_diag[i] - self.eta*self.v_tilde[i]
|
||||
#Marginal moments
|
||||
self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood.moments_match(i,self.tau_[i],self.v_[i])
|
||||
self.Z_hat[i], mu_hat[i], sigma2_hat[i] = self.likelihood_function.moments_match(data[i],self.tau_[i],self.v_[i])
|
||||
#Site parameters update
|
||||
Delta_tau = self.delta/self.eta*(1./sigma2_hat[i] - 1./self.Sigma_diag[i])
|
||||
Delta_v = self.delta/self.eta*(mu_hat[i]/sigma2_hat[i] - self.mu[i]/self.Sigma_diag[i])
|
||||
|
|
|
|||
|
|
@ -33,7 +33,17 @@ class Gaussian(likelihood):
|
|||
self.covariance_matrix = np.eye(self.N)*self._variance
|
||||
self.precision = 1./self._variance
|
||||
|
||||
def fit(self):
|
||||
def predictive_values(self,mu,var):
|
||||
"""
|
||||
Un-normalise the prediction and add the likelihood variance, then return the 5%, 95% interval
|
||||
"""
|
||||
mean = mu*self._std + self._mean
|
||||
true_var = (var + self._variance)*self._std**2
|
||||
_5pc = mean + mean - 2.*np.sqrt(var)
|
||||
_95pc = mean + 2.*np.sqrt(var)
|
||||
return mean, _5pc, _95pc
|
||||
|
||||
def fit_full(self):
|
||||
"""
|
||||
No approximations needed
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from scipy import stats
|
|||
import scipy as sp
|
||||
import pylab as pb
|
||||
from ..util.plot import gpplot
|
||||
#from . import EP
|
||||
|
||||
class likelihood_function:
|
||||
"""
|
||||
|
|
@ -28,8 +29,6 @@ class probit(likelihood_function):
|
|||
L(x) = \\Phi (Y_i*f_i)
|
||||
$$
|
||||
"""
|
||||
def __init__(self,location=0,scale=1):
|
||||
likelihood_function.__init__(self,Y,location,scale)
|
||||
|
||||
def moments_match(self,data_i,tau_i,v_i):
|
||||
"""
|
||||
|
|
@ -47,24 +46,18 @@ class probit(likelihood_function):
|
|||
sigma2_hat = 1./tau_i - (phi/((tau_i**2+tau_i)*Z_hat))*(z+phi/Z_hat)
|
||||
return Z_hat, mu_hat, sigma2_hat
|
||||
|
||||
def predictive_values(self,mu,var,all=False):
|
||||
def predictive_values(self,mu,var):
|
||||
"""
|
||||
Compute mean, variance, and conficence interval (percentiles 5 and 95) of the prediction
|
||||
Compute mean, and conficence interval (percentiles 5 and 95) of the prediction
|
||||
"""
|
||||
mu = mu.flatten()
|
||||
var = var.flatten()
|
||||
mean = stats.norm.cdf(mu/np.sqrt(1+var))
|
||||
if all:
|
||||
p_05 = np.zeros([mu.size])
|
||||
p_95 = np.ones([mu.size])
|
||||
return mean, mean*(1-mean),p_05,p_95
|
||||
else:
|
||||
return mean
|
||||
p_05 = np.zeros([mu.size])
|
||||
p_95 = np.ones([mu.size])
|
||||
return mean, p_05, p_95
|
||||
|
||||
def _log_likelihood_gradients():
|
||||
return np.zeros(0) # there are no parameters of whcih to compute the gradients
|
||||
|
||||
class poisson(likelihood_function):
|
||||
class Poisson(likelihood_function):
|
||||
"""
|
||||
Poisson likelihood
|
||||
Y is expected to take values in {0,1,2,...}
|
||||
|
|
@ -73,10 +66,6 @@ class poisson(likelihood_function):
|
|||
L(x) = \exp(\lambda) * \lambda**Y_i / Y_i!
|
||||
$$
|
||||
"""
|
||||
def __init__(self,Y,location=0,scale=1):
|
||||
assert len(Y[Y<0]) == 0, "Output cannot have negative values"
|
||||
likelihood_function.__init__(self,Y,location,scale)
|
||||
|
||||
def moments_match(self,i,tau_i,v_i):
|
||||
"""
|
||||
Moments match of the marginal approximation in EP algorithm
|
||||
|
|
@ -134,52 +123,12 @@ class poisson(likelihood_function):
|
|||
sigma2_hat = m2 - mu_hat**2 # Second central moment
|
||||
return float(Z_hat), float(mu_hat), float(sigma2_hat)
|
||||
|
||||
def predictive_values(self,mu,var,all=False):
|
||||
def predictive_values(self,mu,var):
|
||||
"""
|
||||
Compute mean, variance, and conficence interval (percentiles 5 and 95) of the prediction
|
||||
Compute mean, and conficence interval (percentiles 5 and 95) of the prediction
|
||||
"""
|
||||
mean = np.exp(mu*self.scale + self.location)
|
||||
if all:
|
||||
tmp = stats.poisson.ppf(np.array([.05,.95]),mu)
|
||||
p_05 = tmp[:,0]
|
||||
p_95 = tmp[:,1]
|
||||
return mean,mean,p_05,p_95
|
||||
else:
|
||||
return mean
|
||||
|
||||
def _log_likelihood_gradients():
|
||||
raise NotImplementedError
|
||||
|
||||
def plot(self,X,mu,var,phi,X_obs,Z=None,samples=0):
|
||||
assert X_obs.shape[1] == 1, 'Number of dimensions must be 1'
|
||||
gpplot(X,phi,phi.flatten())
|
||||
pb.plot(X_obs,self.Y,'kx',mew=1.5)
|
||||
if samples:
|
||||
phi_samples = np.vstack([np.random.poisson(phi.flatten(),phi.size) for s in range(samples)])
|
||||
pb.plot(X,phi_samples.T, alpha = 0.4, c='#3465a4', linewidth = 0.8)
|
||||
if Z is not None:
|
||||
pb.plot(Z,Z*0+pb.ylim()[0],'k|',mew=1.5,markersize=12)
|
||||
|
||||
class gaussian(likelihood_function):
|
||||
"""
|
||||
Gaussian likelihood
|
||||
Y is expected to take values in (-inf,inf)
|
||||
"""
|
||||
def moments_match(self,i,tau_i,v_i):
|
||||
"""
|
||||
Moments match of the marginal approximation in EP algorithm
|
||||
|
||||
:param i: number of observation (int)
|
||||
:param tau_i: precision of the cavity distribution (float)
|
||||
:param v_i: mean/variance of the cavity distribution (float)
|
||||
"""
|
||||
mu = v_i/tau_i
|
||||
sigma = np.sqrt(1./tau_i)
|
||||
s = 1. if self.Y[i] == 0 else 1./self.Y[i]
|
||||
sigma2_hat = 1./(1./sigma**2 + 1./s**2)
|
||||
mu_hat = sigma2_hat*(mu/sigma**2 + self.Y[i]/s**2)
|
||||
Z_hat = 1./np.sqrt(2*np.pi) * 1./np.sqrt(sigma**2+s**2) * np.exp(-.5*(mu-self.Y[i])**2/(sigma**2 + s**2))
|
||||
return Z_hat, mu_hat, sigma2_hat
|
||||
|
||||
def _log_likelihood_gradients():
|
||||
raise NotImplementedError
|
||||
tmp = stats.poisson.ppf(np.array([.05,.95]),mu)
|
||||
p_05 = tmp[:,0]
|
||||
p_95 = tmp[:,1]
|
||||
return mean,p_05,p_95
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue