From 2b40ee6f7e952b11405d6e2434995c68c9ac71da Mon Sep 17 00:00:00 2001 From: Ricardo Andrade Date: Thu, 31 Jan 2013 15:30:57 +0000 Subject: [PATCH] predictive_values implemented in EP --- GPy/likelihoods/EP.py | 11 ++++++++++- GPy/likelihoods/Gaussian.py | 10 ++++++++++ GPy/likelihoods/likelihood_functions.py | 8 ++++---- GPy/models/GP.py | 3 ++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/GPy/likelihoods/EP.py b/GPy/likelihoods/EP.py index 3e975436..b557a62f 100644 --- a/GPy/likelihoods/EP.py +++ b/GPy/likelihoods/EP.py @@ -18,7 +18,6 @@ class EP: self.likelihood_function = likelihood_function self.epsilon = epsilon self.eta, self.delta = power_ep - self.jitter = 1e-12 # TODO: is this needed? """ Initial values - Likelihood approximation parameters: @@ -27,6 +26,16 @@ class EP: self.tau_tilde = np.zeros(self.N) self.v_tilde = np.zeros(self.N) + 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 _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 diff --git a/GPy/likelihoods/Gaussian.py b/GPy/likelihoods/Gaussian.py index fe954b78..37132cf0 100644 --- a/GPy/likelihoods/Gaussian.py +++ b/GPy/likelihoods/Gaussian.py @@ -29,6 +29,16 @@ class Gaussian: self._variance = x self.variance = np.eye(self.N)*self._variance + 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(self): """ No approximations needed diff --git a/GPy/likelihoods/likelihood_functions.py b/GPy/likelihoods/likelihood_functions.py index b94929d3..68fd276a 100644 --- a/GPy/likelihoods/likelihood_functions.py +++ b/GPy/likelihoods/likelihood_functions.py @@ -49,7 +49,7 @@ class probit(likelihood): def predictive_values(self,mu,var,all=False): """ - 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() @@ -57,7 +57,7 @@ class probit(likelihood): if all: p_05 = np.zeros([mu.size]) p_95 = np.ones([mu.size]) - return mean, mean*(1-mean),p_05,p_95 + return mean, p_05, p_95 else: return mean @@ -136,14 +136,14 @@ class poisson(likelihood): def predictive_values(self,mu,var,all=False): """ - 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 + return mean,p_05,p_95 else: return mean diff --git a/GPy/models/GP.py b/GPy/models/GP.py index b663ad3e..d20aa290 100644 --- a/GPy/models/GP.py +++ b/GPy/models/GP.py @@ -164,9 +164,10 @@ class GP(model): """ #normalise X values Xnew = (Xnew.copy() - self._Xmean) / self._Xstd - mu, var, phi = self._raw_predict(Xnew, slices, full_cov=full_cov) + mu, var = self._raw_predict(Xnew, slices, full_cov=full_cov) #now push through likelihood TODO + mean, _5pc, _95pc = self.likelihood.predictive_values(mu, var) return mean, _5pc, _95pc