From 346f9dd8bd3207959b87ded258e55aeb094f1ea3 Mon Sep 17 00:00:00 2001 From: James Hensman Date: Fri, 1 Feb 2013 10:05:22 +0000 Subject: [PATCH] added a likelihood atom class and also some import tidying in the EP.py file --- GPy/likelihoods/EP.py | 12 ++++-------- GPy/likelihoods/Gaussian.py | 3 ++- GPy/likelihoods/likelihood.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 GPy/likelihoods/likelihood.py diff --git a/GPy/likelihoods/EP.py b/GPy/likelihoods/EP.py index c52fd8bf..ff612a6d 100644 --- a/GPy/likelihoods/EP.py +++ b/GPy/likelihoods/EP.py @@ -1,11 +1,9 @@ import numpy as np -import random from scipy import stats, linalg -from ..core import model from ..util.linalg import pdinv,mdot,jitchol -from ..util.plot import gpplot +from likelihood import likelihood -class EP: +class EP(likelihood): def __init__(self,data,likelihood_function,epsilon=1e-3,power_ep=[1.,1.]): """ Expectation Propagation @@ -70,8 +68,7 @@ class EP: self.np1 = [self.tau_tilde.copy()] self.np2 = [self.v_tilde.copy()] while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: - update_order = np.arange(self.N) - random.shuffle(update_order) + update_order = np.random.permutation(self.N) for i in update_order: #Cavity distribution parameters self.tau_[i] = 1./self.Sigma[i,i] - self.eta*self.tau_tilde[i] @@ -243,8 +240,7 @@ class EP: self.np1 = [self.tau_tilde.copy()] self.np2 = [self.v_tilde.copy()] while epsilon_np1 > self.epsilon or epsilon_np2 > self.epsilon: - update_order = np.arange(self.N) - random.shuffle(update_order) + update_order = np.random.permutation(self.N) for i in update_order: #Cavity distribution parameters self.tau_[i] = 1./self.Sigma_diag[i] - self.eta*self.tau_tilde[i] diff --git a/GPy/likelihoods/Gaussian.py b/GPy/likelihoods/Gaussian.py index 5b461537..cb040d50 100644 --- a/GPy/likelihoods/Gaussian.py +++ b/GPy/likelihoods/Gaussian.py @@ -1,6 +1,7 @@ import numpy as np +from likelihood import likelihood -class Gaussian: +class Gaussian(likelihood): def __init__(self,data,variance=1.,normalize=False): self.is_heteroscedastic = False self.data = data diff --git a/GPy/likelihoods/likelihood.py b/GPy/likelihoods/likelihood.py new file mode 100644 index 00000000..6ec57c07 --- /dev/null +++ b/GPy/likelihoods/likelihood.py @@ -0,0 +1,35 @@ +import numpy as np + +class likelihood: + """ + The atom for a likelihood class + + This object interfaces the GP and the data. The most basic likelihood + (Gaussian) inherits directly from this, as does the EP algorithm + + Some things must be defined for this to work properly: + self.Y : the effective Gaussian target of the GP + self.N, self.D : Y.shape + self.covariance_matrix : the effective (noise) covariance of the GP targets + self.Z : a factor which gets added to the likelihood (0 for a Gaussian, Z_EP for EP) + self.is_heteroscedastic : enables significant computational savings in GP + self.precision : a scalar or vector representation of the effective target precision + self.YYT : (optional) = np.dot(self.Y, self.Y.T) enables computational savings for D>N + """ + def __init__(self,data): + raise ValueError, "this class is not to be instantiated" + + def _get_params(self): + raise NotImplementedError + + def _get_param_names(self): + raise NotImplementedError + + def _set_params(self,x): + raise NotImplementedError + + def fit(self): + raise NotImplementedError + + def _gradients(self,partial): + raise NotImplementedError