2013-01-25 18:14:28 +00:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pb
|
|
|
|
|
from .. import kern
|
|
|
|
|
from ..core import model
|
|
|
|
|
from ..util.linalg import pdinv,mdot
|
2013-02-01 17:58:21 +00:00
|
|
|
from ..util.plot import gpplot,x_frame1D,x_frame2D, Tango
|
2013-02-01 13:17:17 +00:00
|
|
|
from ..likelihoods import EP
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
class GP(model):
|
|
|
|
|
"""
|
2013-01-29 12:07:19 +00:00
|
|
|
Gaussian Process model for regression and EP
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
:param X: input observations
|
|
|
|
|
:param kernel: a GPy kernel, defaults to rbf+white
|
2013-01-31 12:00:57 +00:00
|
|
|
:parm likelihood: a GPy likelihood
|
2013-01-25 18:14:28 +00:00
|
|
|
:param normalize_X: whether to normalize the input data before computing (predictions will be in original scales)
|
|
|
|
|
:type normalize_X: False|True
|
|
|
|
|
:param normalize_Y: whether to normalize the input data before computing (predictions will be in original scales)
|
|
|
|
|
:type normalize_Y: False|True
|
|
|
|
|
:param Xslices: how the X,Y data co-vary in the kernel (i.e. which "outputs" they correspond to). See (link:slicing)
|
|
|
|
|
:rtype: model object
|
2013-01-28 17:47:08 +00:00
|
|
|
:param epsilon_ep: convergence criterion for the Expectation Propagation algorithm, defaults to 0.1
|
|
|
|
|
:param powerep: power-EP parameters [$\eta$,$\delta$], defaults to [1.,1.]
|
|
|
|
|
:type powerep: list
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
.. Note:: Multiple independent outputs are allowed using columns of Y
|
|
|
|
|
|
|
|
|
|
"""
|
2013-02-08 14:00:13 +00:00
|
|
|
#FIXME normalize vs normalise
|
2013-02-01 17:12:45 +00:00
|
|
|
def __init__(self, X, likelihood, kernel, normalize_X=False, Xslices=None):
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
# parse arguments
|
|
|
|
|
self.Xslices = Xslices
|
|
|
|
|
self.X = X
|
|
|
|
|
assert len(self.X.shape)==2
|
2013-01-31 12:00:57 +00:00
|
|
|
self.N, self.Q = self.X.shape
|
|
|
|
|
assert isinstance(kernel, kern.kern)
|
2013-01-25 18:14:28 +00:00
|
|
|
self.kern = kernel
|
|
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
#here's some simple normalisation for the inputs
|
2013-01-25 18:14:28 +00:00
|
|
|
if normalize_X:
|
|
|
|
|
self._Xmean = X.mean(0)[None,:]
|
|
|
|
|
self._Xstd = X.std(0)[None,:]
|
|
|
|
|
self.X = (X.copy() - self._Xmean) / self._Xstd
|
|
|
|
|
if hasattr(self,'Z'):
|
|
|
|
|
self.Z = (self.Z - self._Xmean) / self._Xstd
|
|
|
|
|
else:
|
|
|
|
|
self._Xmean = np.zeros((1,self.X.shape[1]))
|
|
|
|
|
self._Xstd = np.ones((1,self.X.shape[1]))
|
|
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
self.likelihood = likelihood
|
2013-02-01 13:17:17 +00:00
|
|
|
#assert self.X.shape[0] == self.likelihood.Y.shape[0]
|
|
|
|
|
#self.N, self.D = self.likelihood.Y.shape
|
|
|
|
|
assert self.X.shape[0] == self.likelihood.data.shape[0]
|
|
|
|
|
self.N, self.D = self.likelihood.data.shape
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
model.__init__(self)
|
|
|
|
|
|
2013-03-01 11:13:55 +00:00
|
|
|
def dL_dZ(self):
|
|
|
|
|
"""
|
|
|
|
|
TODO: one day we might like to learn Z by gradient methods?
|
|
|
|
|
"""
|
|
|
|
|
return np.zeros_like(self.Z)
|
|
|
|
|
|
2013-01-25 18:14:28 +00:00
|
|
|
def _set_params(self,p):
|
2013-01-31 12:00:57 +00:00
|
|
|
self.kern._set_params_transformed(p[:self.kern.Nparam])
|
2013-02-06 09:52:54 +00:00
|
|
|
#self.likelihood._set_params(p[self.kern.Nparam:]) # test by Nicolas
|
|
|
|
|
self.likelihood._set_params(p[self.kern.Nparam_transformed():]) # test by Nicolas
|
|
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
|
2013-03-06 16:00:14 +00:00
|
|
|
self.K = self.kern.K(self.X,slices1=self.Xslices,slices2=self.Xslices)
|
2013-02-01 13:45:55 +00:00
|
|
|
self.K += self.likelihood.covariance_matrix
|
2013-01-31 12:00:57 +00:00
|
|
|
|
2013-01-28 17:47:08 +00:00
|
|
|
self.Ki, self.L, self.Li, self.K_logdet = pdinv(self.K)
|
2013-01-25 18:14:28 +00:00
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
#the gradient of the likelihood wrt the covariance matrix
|
2013-01-31 15:02:34 +00:00
|
|
|
if self.likelihood.YYT is None:
|
|
|
|
|
alpha = np.dot(self.Ki,self.likelihood.Y)
|
|
|
|
|
self.dL_dK = 0.5*(np.dot(alpha,alpha.T)-self.D*self.Ki)
|
2013-01-31 12:00:57 +00:00
|
|
|
else:
|
2013-01-31 15:02:34 +00:00
|
|
|
tmp = mdot(self.Ki, self.likelihood.YYT, self.Ki)
|
2013-01-31 12:00:57 +00:00
|
|
|
self.dL_dK = 0.5*(tmp - self.D*self.Ki)
|
|
|
|
|
|
2013-01-25 18:14:28 +00:00
|
|
|
def _get_params(self):
|
2013-01-31 12:00:57 +00:00
|
|
|
return np.hstack((self.kern._get_params_transformed(), self.likelihood._get_params()))
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
def _get_param_names(self):
|
2013-01-31 12:00:57 +00:00
|
|
|
return self.kern._get_param_names_transformed() + self.likelihood._get_param_names()
|
2013-01-25 18:14:28 +00:00
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
def update_likelihood_approximation(self):
|
2013-01-29 12:07:19 +00:00
|
|
|
"""
|
|
|
|
|
Approximates a non-gaussian likelihood using Expectation Propagation
|
2013-01-31 12:00:57 +00:00
|
|
|
|
|
|
|
|
For a Gaussian (or direct: TODO) likelihood, no iteration is required:
|
|
|
|
|
this function does nothing
|
2013-01-29 12:07:19 +00:00
|
|
|
"""
|
2013-02-01 13:45:55 +00:00
|
|
|
self.likelihood.fit_full(self.kern.K(self.X))
|
2013-02-01 13:32:13 +00:00
|
|
|
self._set_params(self._get_params()) # update the GP
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
def _model_fit_term(self):
|
|
|
|
|
"""
|
|
|
|
|
Computes the model fit using YYT if it's available
|
|
|
|
|
"""
|
2013-01-31 15:02:34 +00:00
|
|
|
if self.likelihood.YYT is None:
|
|
|
|
|
return -0.5*np.sum(np.square(np.dot(self.Li,self.likelihood.Y)))
|
2013-01-25 18:14:28 +00:00
|
|
|
else:
|
2013-01-31 15:02:34 +00:00
|
|
|
return -0.5*np.sum(np.multiply(self.Ki, self.likelihood.YYT))
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
def log_likelihood(self):
|
|
|
|
|
"""
|
2013-01-31 12:00:57 +00:00
|
|
|
The log marginal likelihood of the GP.
|
|
|
|
|
|
|
|
|
|
For an EP model, can be written as the log likelihood of a regression
|
|
|
|
|
model for a new variable Y* = v_tilde/tau_tilde, with a covariance
|
2013-01-25 18:14:28 +00:00
|
|
|
matrix K* = K + diag(1./tau_tilde) plus a normalization term.
|
|
|
|
|
"""
|
2013-01-31 15:02:34 +00:00
|
|
|
return -0.5*self.D*self.K_logdet + self._model_fit_term() + self.likelihood.Z
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
def _log_likelihood_gradients(self):
|
|
|
|
|
"""
|
|
|
|
|
The gradient of all parameters.
|
|
|
|
|
|
|
|
|
|
For the kernel parameters, use the chain rule via dL_dK
|
|
|
|
|
|
|
|
|
|
For the likelihood parameters, pass in alpha = K^-1 y
|
|
|
|
|
"""
|
2013-03-06 16:00:14 +00:00
|
|
|
return np.hstack((self.kern.dK_dtheta(partial=self.dL_dK,X=self.X,slices1=self.Xslices,slices2=self.Xslices), self.likelihood._gradients(partial=np.diag(self.dL_dK))))
|
2013-01-31 12:00:57 +00:00
|
|
|
|
2013-02-01 13:17:17 +00:00
|
|
|
def _raw_predict(self,_Xnew,slices=None, full_cov=False):
|
2013-01-31 12:00:57 +00:00
|
|
|
"""
|
|
|
|
|
Internal helper function for making predictions, does not account
|
|
|
|
|
for normalisation or likelihood
|
|
|
|
|
"""
|
|
|
|
|
Kx = self.kern.K(self.X,_Xnew, slices1=self.Xslices,slices2=slices)
|
2013-01-31 15:02:34 +00:00
|
|
|
mu = np.dot(np.dot(Kx.T,self.Ki),self.likelihood.Y)
|
2013-01-31 12:00:57 +00:00
|
|
|
KiKx = np.dot(self.Ki,Kx)
|
|
|
|
|
if full_cov:
|
|
|
|
|
Kxx = self.kern.K(_Xnew, slices1=slices,slices2=slices)
|
2013-02-04 12:01:27 +00:00
|
|
|
var = Kxx - np.dot(KiKx.T,Kx) #NOTE this won't work for plotting
|
2013-01-25 18:14:28 +00:00
|
|
|
else:
|
2013-01-31 12:00:57 +00:00
|
|
|
Kxx = self.kern.Kdiag(_Xnew, slices=slices)
|
|
|
|
|
var = Kxx - np.sum(np.multiply(KiKx,Kx),0)
|
2013-02-05 18:57:30 +00:00
|
|
|
var = var[:,None]
|
|
|
|
|
return mu, var
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def predict(self,Xnew, slices=None, full_cov=False):
|
|
|
|
|
"""
|
|
|
|
|
Predict the function(s) at the new point(s) Xnew.
|
|
|
|
|
|
|
|
|
|
Arguments
|
|
|
|
|
---------
|
|
|
|
|
:param Xnew: The points at which to make a prediction
|
|
|
|
|
:type Xnew: np.ndarray, Nnew x self.Q
|
|
|
|
|
:param slices: specifies which outputs kernel(s) the Xnew correspond to (see below)
|
|
|
|
|
:type slices: (None, list of slice objects, list of ints)
|
|
|
|
|
:param full_cov: whether to return the folll covariance matrix, or just the diagonal
|
|
|
|
|
:type full_cov: bool
|
|
|
|
|
:rtype: posterior mean, a Numpy array, Nnew x self.D
|
2013-02-05 14:23:51 +00:00
|
|
|
:rtype: posterior variance, a Numpy array, Nnew x 1 if full_cov=False, Nnew x Nnew otherwise
|
|
|
|
|
:rtype: lower and upper boundaries of the 95% confidence intervals, Numpy arrays, Nnew x self.D
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
.. Note:: "slices" specifies how the the points X_new co-vary wich the training points.
|
|
|
|
|
|
|
|
|
|
- If None, the new points covary throigh every kernel part (default)
|
|
|
|
|
- If a list of slices, the i^th slice specifies which data are affected by the i^th kernel part
|
|
|
|
|
- If a list of booleans, specifying which kernel parts are active
|
|
|
|
|
|
|
|
|
|
If full_cov and self.D > 1, the return shape of var is Nnew x Nnew x self.D. If self.D == 1, the return shape is Nnew x Nnew.
|
|
|
|
|
This is to allow for different normalisations of the output dimensions.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
#normalise X values
|
|
|
|
|
Xnew = (Xnew.copy() - self._Xmean) / self._Xstd
|
2013-02-05 14:23:51 +00:00
|
|
|
mu, var = self._raw_predict(Xnew, slices, full_cov)
|
2013-01-25 18:14:28 +00:00
|
|
|
|
2013-01-31 12:00:57 +00:00
|
|
|
#now push through likelihood TODO
|
2013-02-05 18:57:30 +00:00
|
|
|
mean, _025pm, _975pm = self.likelihood.predictive_values(mu, var)
|
2013-01-25 18:14:28 +00:00
|
|
|
|
2013-02-05 18:57:30 +00:00
|
|
|
return mean, var, _025pm, _975pm
|
2013-01-25 18:14:28 +00:00
|
|
|
|
|
|
|
|
|
2013-02-05 18:57:30 +00:00
|
|
|
def plot_f(self, samples=0, plot_limits=None, which_data='all', which_functions='all', resolution=None, full_cov=False):
|
2013-02-01 13:17:17 +00:00
|
|
|
"""
|
|
|
|
|
Plot the GP's view of the world, where the data is normalised and the likelihood is Gaussian
|
|
|
|
|
|
|
|
|
|
:param samples: the number of a posteriori samples to plot
|
|
|
|
|
:param which_data: which if the training data to plot (default all)
|
|
|
|
|
:type which_data: 'all' or a slice object to slice self.X, self.Y
|
|
|
|
|
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
|
|
|
|
|
:param which_functions: which of the kernel functions to plot (additively)
|
|
|
|
|
:type which_functions: list of bools
|
|
|
|
|
:param resolution: the number of intervals to sample the GP on. Defaults to 200 in 1D and 50 (a 50x50 grid) in 2D
|
|
|
|
|
|
|
|
|
|
Plot the posterior of the GP.
|
|
|
|
|
- In one dimension, the function is plotted with a shaded region identifying two standard deviations.
|
|
|
|
|
- In two dimsensions, a contour-plot shows the mean predicted function
|
|
|
|
|
- In higher dimensions, we've no implemented this yet !TODO!
|
|
|
|
|
|
|
|
|
|
Can plot only part of the data and part of the posterior functions using which_data and which_functions
|
2013-01-31 15:04:24 +00:00
|
|
|
Plot the data's view of the world, with non-normalised values and GP predictions passed through the likelihood
|
|
|
|
|
"""
|
2013-02-01 15:14:11 +00:00
|
|
|
if which_functions=='all':
|
|
|
|
|
which_functions = [True]*self.kern.Nparts
|
|
|
|
|
if which_data=='all':
|
|
|
|
|
which_data = slice(None)
|
|
|
|
|
|
2013-02-01 17:58:21 +00:00
|
|
|
if self.X.shape[1] == 1:
|
|
|
|
|
Xnew, xmin, xmax = x_frame1D(self.X, plot_limits=plot_limits)
|
2013-02-05 18:57:30 +00:00
|
|
|
if samples == 0:
|
|
|
|
|
m,v = self._raw_predict(Xnew, slices=which_functions)
|
|
|
|
|
gpplot(Xnew,m,m-2*np.sqrt(v),m+2*np.sqrt(v))
|
|
|
|
|
pb.plot(self.X[which_data],self.likelihood.Y[which_data],'kx',mew=1.5)
|
|
|
|
|
else:
|
|
|
|
|
m,v = self._raw_predict(Xnew, slices=which_functions,full_cov=True)
|
|
|
|
|
Ysim = np.random.multivariate_normal(m.flatten(),v,samples)
|
|
|
|
|
gpplot(Xnew,m,m-2*np.sqrt(np.diag(v)[:,None]),m+2*np.sqrt(np.diag(v))[:,None])
|
|
|
|
|
for i in range(samples):
|
|
|
|
|
pb.plot(Xnew,Ysim[i,:],Tango.coloursHex['darkBlue'],linewidth=0.25)
|
2013-02-01 17:58:21 +00:00
|
|
|
pb.plot(self.X[which_data],self.likelihood.Y[which_data],'kx',mew=1.5)
|
|
|
|
|
pb.xlim(xmin,xmax)
|
2013-02-08 14:00:13 +00:00
|
|
|
ymin,ymax = min(np.append(self.likelihood.Y,m-2*np.sqrt(np.diag(v)[:,None]))), max(np.append(self.likelihood.Y,m+2*np.sqrt(np.diag(v)[:,None])))
|
|
|
|
|
ymin, ymax = ymin - 0.1*(ymax - ymin), ymax + 0.1*(ymax - ymin)
|
|
|
|
|
pb.ylim(ymin,ymax)
|
|
|
|
|
if hasattr(self,'Z'):
|
|
|
|
|
pb.plot(self.Z,self.Z*0+pb.ylim()[0],'r|',mew=1.5,markersize=12)
|
|
|
|
|
|
2013-02-05 14:23:51 +00:00
|
|
|
elif self.X.shape[1] == 2:
|
2013-02-01 17:58:21 +00:00
|
|
|
resolution = resolution or 50
|
2013-02-05 14:23:51 +00:00
|
|
|
Xnew, xmin, xmax, xx, yy = x_frame2D(self.X, plot_limits,resolution)
|
2013-02-01 17:58:21 +00:00
|
|
|
m,v = self._raw_predict(Xnew, slices=which_functions)
|
2013-02-05 12:27:12 +00:00
|
|
|
m = m.reshape(resolution,resolution).T
|
|
|
|
|
pb.contour(xx,yy,m,vmin=m.min(),vmax=m.max(),cmap=pb.cm.jet)
|
|
|
|
|
pb.scatter(Xorig[:,0],Xorig[:,1],40,Yorig,linewidth=0,cmap=pb.cm.jet,vmin=m.min(), vmax=m.max())
|
2013-02-01 17:58:21 +00:00
|
|
|
pb.xlim(xmin[0],xmax[0])
|
|
|
|
|
pb.ylim(xmin[1],xmax[1])
|
|
|
|
|
else:
|
|
|
|
|
raise NotImplementedError, "Cannot define a frame with more than two input dimensions"
|
2013-02-01 13:17:17 +00:00
|
|
|
|
2013-03-01 11:13:55 +00:00
|
|
|
def plot(self,samples=0,plot_limits=None,which_data='all',which_functions='all',resolution=None,levels=20):
|
|
|
|
|
"""
|
|
|
|
|
TODO: Docstrings!
|
|
|
|
|
:param levels: for 2D plotting, the number of contour levels to use
|
|
|
|
|
|
|
|
|
|
"""
|
2013-02-05 18:57:30 +00:00
|
|
|
# TODO include samples
|
2013-02-01 15:14:11 +00:00
|
|
|
if which_functions=='all':
|
|
|
|
|
which_functions = [True]*self.kern.Nparts
|
|
|
|
|
if which_data=='all':
|
|
|
|
|
which_data = slice(None)
|
2013-02-01 17:58:21 +00:00
|
|
|
|
|
|
|
|
if self.X.shape[1] == 1:
|
2013-02-08 14:00:13 +00:00
|
|
|
|
|
|
|
|
Xu = self.X * self._Xstd + self._Xmean #NOTE self.X are the normalized values now
|
|
|
|
|
|
|
|
|
|
Xnew, xmin, xmax = x_frame1D(Xu, plot_limits=plot_limits)
|
2013-02-05 14:23:51 +00:00
|
|
|
m, var, lower, upper = self.predict(Xnew, slices=which_functions)
|
2013-02-01 17:58:21 +00:00
|
|
|
gpplot(Xnew,m, lower, upper)
|
2013-02-08 14:00:13 +00:00
|
|
|
pb.plot(Xu[which_data],self.likelihood.data[which_data],'kx',mew=1.5)
|
2013-02-07 13:04:29 +00:00
|
|
|
ymin,ymax = min(np.append(self.likelihood.data,lower)), max(np.append(self.likelihood.data,upper))
|
|
|
|
|
ymin, ymax = ymin - 0.1*(ymax - ymin), ymax + 0.1*(ymax - ymin)
|
2013-02-01 17:58:21 +00:00
|
|
|
pb.xlim(xmin,xmax)
|
|
|
|
|
pb.ylim(ymin,ymax)
|
2013-02-08 14:00:13 +00:00
|
|
|
if hasattr(self,'Z'):
|
|
|
|
|
Zu = self.Z*self._Xstd + self._Xmean
|
|
|
|
|
pb.plot(Zu,Zu*0+pb.ylim()[0],'r|',mew=1.5,markersize=12)
|
2013-03-11 11:40:17 +00:00
|
|
|
if self.has_uncertain_inputs:
|
|
|
|
|
pb.errorbar(self.X[:,0], pb.ylim()[0]+np.zeros(self.N), xerr=2*np.sqrt(self.X_uncertainty.flatten()))
|
2013-02-05 18:57:30 +00:00
|
|
|
|
2013-02-08 14:00:13 +00:00
|
|
|
elif self.X.shape[1]==2: #FIXME
|
2013-02-01 17:58:21 +00:00
|
|
|
resolution = resolution or 50
|
2013-02-05 12:27:12 +00:00
|
|
|
Xnew, xx, yy, xmin, xmax = x_frame2D(self.X, plot_limits,resolution)
|
|
|
|
|
x, y = np.linspace(xmin[0],xmax[0],resolution), np.linspace(xmin[1],xmax[1],resolution)
|
2013-02-05 14:23:51 +00:00
|
|
|
m, var, lower, upper = self.predict(Xnew, slices=which_functions)
|
2013-02-05 12:27:12 +00:00
|
|
|
m = m.reshape(resolution,resolution).T
|
2013-03-01 11:40:18 +00:00
|
|
|
pb.contour(x,y,m,levels,vmin=m.min(),vmax=m.max(),cmap=pb.cm.jet)
|
2013-02-05 12:27:12 +00:00
|
|
|
Yf = self.likelihood.Y.flatten()
|
|
|
|
|
pb.scatter(self.X[:,0], self.X[:,1], 40, Yf, cmap=pb.cm.jet,vmin=m.min(),vmax=m.max(), linewidth=0.)
|
2013-02-01 17:58:21 +00:00
|
|
|
pb.xlim(xmin[0],xmax[0])
|
|
|
|
|
pb.ylim(xmin[1],xmax[1])
|
2013-03-11 11:40:17 +00:00
|
|
|
if hasattr(self,'Z'):
|
|
|
|
|
pb.plot(self.Z[:,0],self.Z[:,1],'wo')
|
|
|
|
|
|
2013-02-01 17:58:21 +00:00
|
|
|
else:
|
|
|
|
|
raise NotImplementedError, "Cannot define a frame with more than two input dimensions"
|