From 8d3308b76c9635d31972c07bd6baee90d83fde85 Mon Sep 17 00:00:00 2001 From: tjhgit Date: Tue, 13 Jan 2015 00:10:40 +0100 Subject: [PATCH] Added spline kernel (from P. Hennig) to GPy Had to modify the base_plots and model_plots.py, since I had troubles installing GPy in anaconda on debian linux due to the dependency on Tango. Why is Tango needed to represent colors that can also be typed in Hex format thus eliminating further dependencies. --- GPy/kern/__init__.py | 1 + GPy/kern/_src/spline.py | 47 ++++++++++++++++++++++++ GPy/plotting/matplot_dep/base_plots.py | 6 +-- GPy/plotting/matplot_dep/models_plots.py | 6 +-- 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 GPy/kern/_src/spline.py diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index c400277c..a455d05c 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -13,6 +13,7 @@ from _src.ODE_UYC import ODE_UYC from _src.ODE_st import ODE_st from _src.ODE_t import ODE_t from _src.poly import Poly +from _src.spline import Spline from _src.trunclinear import TruncLinear,TruncLinear_inf from _src.splitKern import SplitKern,DiffGenomeKern diff --git a/GPy/kern/_src/spline.py b/GPy/kern/_src/spline.py new file mode 100644 index 00000000..6832e0d5 --- /dev/null +++ b/GPy/kern/_src/spline.py @@ -0,0 +1,47 @@ +# Copyright (c) 2014, James Hensman +# Licensed under the BSD 3-clause license (see LICENSE.txt) + +import numpy as np +from kern import Kern +from ...core.parameterization import Param +from ...core.parameterization.transformations import Logexp +class Spline(Kern): + """ + Spline kernel + """ + + def __init__(self, input_dim, variance=1., c=1., active_dims=None, name='spline'): + super(Spline, self).__init__(input_dim, active_dims, name) + self.variance = Param('variance', variance, Logexp()) + self.c = Param('c', c) + self.link_parameters(self.variance,self.c) + + + def K(self, X, X2=None): + if X2 is None: X2=X + term1 = (X+8.)*(X2.T+8.)/16. + term2 = abs((X-X2.T)/16.)**3 + term3 = ((X+8.)/16.)**3 + ((X2.T+8.)/16.)**3 + return (self.variance**2 * (1. + (1.+self.c) * term1 + self.c/3. * (term2 - term3))) + + def Kdiag(self, X): + term1 = np.square(X+8.,X+8.)/16. + term3 = 2. * ((X+8.)/16.)**3 + return (self.variance**2 * (1. + (1.+self.c) * term1 - self.c/3. * term3))[:,0] + + def update_gradients_full(self, dL_dK, X, X2=None): + if X2 is None: X2=X + term1 = (X+8.)*(X2.T+8.)/16. + term2 = abs((X-X2.T)/16.)**3 + term3 = ((X+8.)/16.)**3 + ((X2.T+8.)/16.)**3 + self.variance.gradient = np.sum(dL_dK * (2*self.variance * (1. + (1.+self.c) * term1 + self.c/3. * ( term2 - term3)))) + self.c.gradient = np.sum(dL_dK * (self.variance**2* (term1 + 1./3.*(term2 - term3)))) + + def update_gradients_diag(self, dL_dKdiag, X): + raise NotImplementedError + + def gradients_X(self, dL_dK, X, X2=None): + raise NotImplementedError + + def gradients_X_diag(self, dL_dKdiag, X): + raise NotImplementedError diff --git a/GPy/plotting/matplot_dep/base_plots.py b/GPy/plotting/matplot_dep/base_plots.py index b4142342..742f9265 100644 --- a/GPy/plotting/matplot_dep/base_plots.py +++ b/GPy/plotting/matplot_dep/base_plots.py @@ -3,7 +3,7 @@ try: - import Tango + #import Tango import pylab as pb except: pass @@ -17,11 +17,11 @@ def ax_default(fignum, ax): fig = ax.figure return fig, ax -def meanplot(x, mu, color=Tango.colorsHex['darkBlue'], ax=None, fignum=None, linewidth=2,**kw): +def meanplot(x, mu, color='#3300FF', ax=None, fignum=None, linewidth=2,**kw): _, axes = ax_default(fignum, ax) return axes.plot(x,mu,color=color,linewidth=linewidth,**kw) -def gpplot(x, mu, lower, upper, edgecol=Tango.colorsHex['darkBlue'], fillcol=Tango.colorsHex['lightBlue'], ax=None, fignum=None, **kwargs): +def gpplot(x, mu, lower, upper, edgecol='#3300FF', fillcol='#33CCFF', ax=None, fignum=None, **kwargs): _, axes = ax_default(fignum, ax) mu = mu.flatten() diff --git a/GPy/plotting/matplot_dep/models_plots.py b/GPy/plotting/matplot_dep/models_plots.py index ed024c0a..5ff91498 100644 --- a/GPy/plotting/matplot_dep/models_plots.py +++ b/GPy/plotting/matplot_dep/models_plots.py @@ -2,7 +2,7 @@ # Licensed under the BSD 3-clause license (see LICENSE.txt) try: - import Tango +# import Tango import pylab as pb except: pass @@ -16,7 +16,7 @@ def plot_fit(model, plot_limits=None, which_data_rows='all', which_data_ycols='all', fixed_inputs=[], levels=20, samples=0, fignum=None, ax=None, resolution=None, plot_raw=False, - linecol=Tango.colorsHex['darkBlue'],fillcol=Tango.colorsHex['lightBlue'], Y_metadata=None, data_symbol='kx'): + linecol='#3300FF',fillcol='#00FFFF', Y_metadata=None, data_symbol='kx'): """ Plot the posterior of the GP. - In one dimension, the function is plotted with a shaded region identifying two standard deviations. @@ -107,7 +107,7 @@ def plot_fit(model, plot_limits=None, which_data_rows='all', if samples: #NOTE not tested with fixed_inputs Ysim = model.posterior_samples(Xgrid, samples) for yi in Ysim.T: - plots['posterior_samples'] = ax.plot(Xnew, yi[:,None], Tango.colorsHex['darkBlue'], linewidth=0.25) + plots['posterior_samples'] = ax.plot(Xnew, yi[:,None], '#3300FF', linewidth=0.25) #ax.plot(Xnew, yi[:,None], marker='x', linestyle='--',color=Tango.colorsHex['darkBlue']) #TODO apply this line for discrete outputs.