2012-11-29 16:39:20 +00:00
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
# Licensed under the BSD 3-clause license (see LICENSE.txt)
2012-11-29 16:31:48 +00:00
import numpy as np
from kern import kern
2013-06-17 16:47:36 +01:00
import parts
2012-11-30 17:32:32 +00:00
2013-07-15 20:12:54 +01:00
def rbf_inv ( input_dim , variance = 1. , inv_lengthscale = None , ARD = False ) :
"""
Construct an RBF kernel
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param ARD : Auto Relevance Determination ( one lengthscale per dimension )
: type ARD : Boolean
"""
part = parts . rbf_inv . RBFInv ( input_dim , variance , inv_lengthscale , ARD )
return kern ( input_dim , [ part ] )
2013-06-05 17:40:43 +01:00
def rbf ( input_dim , variance = 1. , lengthscale = None , ARD = False ) :
2012-11-29 16:31:48 +00:00
"""
Construct an RBF kernel
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
2012-11-29 16:31:48 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
2013-01-18 16:03:20 +00:00
: param ARD : Auto Relevance Determination ( one lengthscale per dimension )
: type ARD : Boolean
2012-11-29 16:31:48 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . rbf . RBF ( input_dim , variance , lengthscale , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def linear ( input_dim , variances = None , ARD = False ) :
2012-11-29 16:31:48 +00:00
"""
Construct a linear kernel .
Arguments
- - - - - - - - -
2013-06-05 13:02:03 +01:00
input_dimD ( int ) , obligatory
2013-01-28 16:21:32 +00:00
variances ( np . ndarray )
ARD ( boolean )
2012-11-29 16:31:48 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . linear . Linear ( input_dim , variances , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-08-16 18:28:16 +01:00
def mlp ( input_dim , variance = 1. , weight_variance = None , bias_variance = 100. , ARD = False ) :
"""
Construct an MLP kernel
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
: param variance : the variance of the kernel
: type variance : float
: param weight_scale : the lengthscale of the kernel
: type weight_scale : vector of weight variances for input weights in neural network ( length 1 if kernel is isotropic )
: param bias_variance : the variance of the biases in the neural network .
: type bias_variance : float
: param ARD : Auto Relevance Determination ( allows for ARD version of covariance )
: type ARD : Boolean
"""
part = parts . mlp . MLP ( input_dim , variance , weight_variance , bias_variance , ARD )
return kern ( input_dim , [ part ] )
2013-08-29 19:26:00 +02:00
# def gibbs(input_dim,variance=1., mapping=None):
# """
# Gibbs and MacKay non-stationary covariance function.
# .. math::
# r = sqrt((x_i - x_j)'*(x_i - x_j))
# k(x_i, x_j) = \sigma^2*Z*exp(-r^2/(l(x)*l(x) + l(x')*l(x')))
# Z = \sqrt{2*l(x)*l(x')/(l(x)*l(x) + l(x')*l(x')}
# where :math:`l(x)` is a function giving the length scale as a function of space.
# This is the non stationary kernel proposed by Mark Gibbs in his 1997
# thesis. It is similar to an RBF but has a length scale that varies
# with input location. This leads to an additional term in front of
# the kernel.
# The parameters are :math:`\sigma^2`, the process variance, and the parameters of l(x) which is a function that can be specified by the user, by default an multi-layer peceptron is used is used.
# :param input_dim: the number of input dimensions
# :type input_dim: int
# :param variance: the variance :math:`\sigma^2`
# :type variance: float
# :param mapping: the mapping that gives the lengthscale across the input space.
# :type mapping: GPy.core.Mapping
# :param ARD: Auto Relevance Determination. If equal to "False", the kernel is isotropic (ie. one weight variance parameter \sigma^2_w), otherwise there is one weight variance parameter per dimension.
# :type ARD: Boolean
# :rtype: Kernpart object
# """
# part = parts.gibbs.Gibbs(input_dim,variance,mapping)
# return kern(input_dim, [part])
2013-08-17 09:07:09 +02:00
def poly ( input_dim , variance = 1. , weight_variance = None , bias_variance = 1. , degree = 2 , ARD = False ) :
"""
Construct a polynomial kernel
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
: param variance : the variance of the kernel
: type variance : float
: param weight_scale : the lengthscale of the kernel
: type weight_scale : vector of weight variances for input weights .
: param bias_variance : the variance of the biases .
: type bias_variance : float
: param degree : the degree of the polynomial
: type degree : int
: param ARD : Auto Relevance Determination ( allows for ARD version of covariance )
: type ARD : Boolean
"""
part = parts . poly . POLY ( input_dim , variance , weight_variance , bias_variance , degree , ARD )
return kern ( input_dim , [ part ] )
2013-06-05 17:40:43 +01:00
def white ( input_dim , variance = 1. ) :
2012-11-29 16:31:48 +00:00
"""
Construct a white kernel .
Arguments
- - - - - - - - -
2013-06-05 13:02:03 +01:00
input_dimD ( int ) , obligatory
2012-11-29 16:31:48 +00:00
variance ( float )
"""
2013-06-17 16:47:36 +01:00
part = parts . white . White ( input_dim , variance )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def exponential ( input_dim , variance = 1. , lengthscale = None , ARD = False ) :
2012-11-29 16:31:48 +00:00
"""
2013-01-18 16:03:20 +00:00
Construct an exponential kernel
2012-11-29 16:31:48 +00:00
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
2013-01-18 16:03:20 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param ARD : Auto Relevance Determination ( one lengthscale per dimension )
: type ARD : Boolean
2012-11-29 16:31:48 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . exponential . Exponential ( input_dim , variance , lengthscale , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def Matern32 ( input_dim , variance = 1. , lengthscale = None , ARD = False ) :
2012-11-29 16:31:48 +00:00
"""
Construct a Matern 3 / 2 kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
2013-01-18 16:03:20 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param ARD : Auto Relevance Determination ( one lengthscale per dimension )
: type ARD : Boolean
2012-11-29 16:31:48 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . Matern32 . Matern32 ( input_dim , variance , lengthscale , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def Matern52 ( input_dim , variance = 1. , lengthscale = None , ARD = False ) :
2012-11-29 16:31:48 +00:00
"""
Construct a Matern 5 / 2 kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality of the kernel , obligatory
: type input_dim : int
2013-01-18 16:03:20 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param ARD : Auto Relevance Determination ( one lengthscale per dimension )
: type ARD : Boolean
2012-11-29 16:31:48 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . Matern52 . Matern52 ( input_dim , variance , lengthscale , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def bias ( input_dim , variance = 1. ) :
2012-11-29 16:31:48 +00:00
"""
Construct a bias kernel .
Arguments
- - - - - - - - -
2013-06-05 13:02:03 +01:00
input_dim ( int ) , obligatory
2012-11-29 16:31:48 +00:00
variance ( float )
"""
2013-06-17 16:47:36 +01:00
part = parts . bias . Bias ( input_dim , variance )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def finite_dimensional ( input_dim , F , G , variances = 1. , weights = None ) :
2012-11-29 16:31:48 +00:00
"""
Construct a finite dimensional kernel .
2013-06-05 13:02:03 +01:00
input_dim : int - the number of input dimensions
2012-11-29 16:31:48 +00:00
F : np . array of functions with shape ( n , ) - the n basis functions
G : np . array with shape ( n , n ) - the Gram matrix associated to F
variances : np . ndarray with shape ( n , )
"""
2013-06-17 16:47:36 +01:00
part = parts . finite_dimensional . FiniteDimensional ( input_dim , F , G , variances , weights )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def spline ( input_dim , variance = 1. ) :
2012-11-29 16:31:48 +00:00
"""
Construct a spline kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : Dimensionality of the kernel
: type input_dim : int
2012-11-29 16:31:48 +00:00
: param variance : the variance of the kernel
: type variance : float
"""
2013-06-17 16:47:36 +01:00
part = parts . spline . Spline ( input_dim , variance )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-29 16:31:48 +00:00
2013-06-05 17:40:43 +01:00
def Brownian ( input_dim , variance = 1. ) :
2012-11-29 16:31:48 +00:00
"""
Construct a Brownian motion kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : Dimensionality of the kernel
: type input_dim : int
2012-11-29 16:31:48 +00:00
: param variance : the variance of the kernel
: type variance : float
"""
2013-06-17 16:47:36 +01:00
part = parts . Brownian . Brownian ( input_dim , variance )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2012-11-30 17:32:32 +00:00
2013-04-23 17:13:43 +01:00
try :
import sympy as sp
from sympykern import spkern
from sympy . parsing . sympy_parser import parse_expr
sympy_available = True
except ImportError :
sympy_available = False
if sympy_available :
2013-06-05 17:40:43 +01:00
def rbf_sympy ( input_dim , ARD = False , variance = 1. , lengthscale = 1. ) :
2013-04-23 17:13:43 +01:00
"""
Radial Basis Function covariance .
"""
2013-06-05 17:40:43 +01:00
X = [ sp . var ( ' x %i ' % i ) for i in range ( input_dim ) ]
Z = [ sp . var ( ' z %i ' % i ) for i in range ( input_dim ) ]
2013-04-23 17:13:43 +01:00
rbf_variance = sp . var ( ' rbf_variance ' , positive = True )
if ARD :
2013-06-05 17:40:43 +01:00
rbf_lengthscales = [ sp . var ( ' rbf_lengthscale_ %i ' % i , positive = True ) for i in range ( input_dim ) ]
dist_string = ' + ' . join ( [ ' (x %i -z %i )**2/rbf_lengthscale_ %i **2 ' % ( i , i , i ) for i in range ( input_dim ) ] )
2013-04-23 17:13:43 +01:00
dist = parse_expr ( dist_string )
f = rbf_variance * sp . exp ( - dist / 2. )
else :
rbf_lengthscale = sp . var ( ' rbf_lengthscale ' , positive = True )
2013-06-05 17:40:43 +01:00
dist_string = ' + ' . join ( [ ' (x %i -z %i )**2 ' % ( i , i ) for i in range ( input_dim ) ] )
2013-04-23 17:13:43 +01:00
dist = parse_expr ( dist_string )
f = rbf_variance * sp . exp ( - dist / ( 2 * rbf_lengthscale * * 2 ) )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ spkern ( input_dim , f ) ] )
2013-04-23 17:13:43 +01:00
2013-06-05 17:40:43 +01:00
def sympykern ( input_dim , k ) :
2013-04-23 17:13:43 +01:00
"""
A kernel from a symbolic sympy representation
"""
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ spkern ( input_dim , k ) ] )
2013-04-23 17:13:43 +01:00
del sympy_available
2013-01-21 13:29:25 +00:00
2013-06-05 17:40:43 +01:00
def periodic_exponential ( input_dim = 1 , variance = 1. , lengthscale = None , period = 2 * np . pi , n_freq = 10 , lower = 0. , upper = 4 * np . pi ) :
2013-01-21 13:29:25 +00:00
"""
Construct an periodic exponential kernel
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality , only defined for input_dim = 1
: type input_dim : int
2013-01-21 13:29:25 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param period : the period
: type period : float
: param n_freq : the number of frequencies considered for the periodic subspace
: type n_freq : int
"""
2013-06-17 16:47:36 +01:00
part = parts . periodic_exponential . PeriodicExponential ( input_dim , variance , lengthscale , period , n_freq , lower , upper )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-01-21 13:29:25 +00:00
2013-06-05 17:40:43 +01:00
def periodic_Matern32 ( input_dim , variance = 1. , lengthscale = None , period = 2 * np . pi , n_freq = 10 , lower = 0. , upper = 4 * np . pi ) :
2013-01-21 13:29:25 +00:00
"""
Construct a periodic Matern 3 / 2 kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality , only defined for input_dim = 1
: type input_dim : int
2013-01-21 13:29:25 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param period : the period
: type period : float
: param n_freq : the number of frequencies considered for the periodic subspace
: type n_freq : int
"""
2013-06-17 16:47:36 +01:00
part = parts . periodic_Matern32 . PeriodicMatern32 ( input_dim , variance , lengthscale , period , n_freq , lower , upper )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-01-21 13:29:25 +00:00
2013-06-05 17:40:43 +01:00
def periodic_Matern52 ( input_dim , variance = 1. , lengthscale = None , period = 2 * np . pi , n_freq = 10 , lower = 0. , upper = 4 * np . pi ) :
2013-01-21 13:29:25 +00:00
"""
Construct a periodic Matern 5 / 2 kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : dimensionality , only defined for input_dim = 1
: type input_dim : int
2013-01-21 13:29:25 +00:00
: param variance : the variance of the kernel
: type variance : float
: param lengthscale : the lengthscale of the kernel
: type lengthscale : float
: param period : the period
: type period : float
: param n_freq : the number of frequencies considered for the periodic subspace
: type n_freq : int
"""
2013-07-08 13:06:02 +01:00
part = parts . periodic_Matern52 . PeriodicMatern52 ( input_dim , variance , lengthscale , period , n_freq , lower , upper )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-01-31 10:57:43 +00:00
2013-05-10 17:48:11 +01:00
def prod ( k1 , k2 , tensor = False ) :
2013-02-20 17:44:40 +00:00
"""
2013-06-05 13:02:03 +01:00
Construct a product kernel over input_dim from two kernels over input_dim
2013-02-20 17:44:40 +00:00
: param k1 , k2 : the kernels to multiply
: type k1 , k2 : kernpart
2013-08-16 18:28:16 +01:00
: param tensor : The kernels are either multiply as functions defined on the same input space ( default ) or on the product of the input spaces
: type tensor : Boolean
2013-02-20 17:44:40 +00:00
: rtype : kernel object
"""
2013-08-02 16:40:16 +01:00
part = parts . prod . Prod ( k1 , k2 , tensor )
2013-06-05 13:02:03 +01:00
return kern ( part . input_dim , [ part ] )
2013-03-05 15:58:03 +00:00
def symmetric ( k ) :
"""
2013-08-16 18:28:16 +01:00
Construct a symmetric kernel from an existing kernel
2013-03-05 15:58:03 +00:00
"""
k_ = k . copy ( )
2013-06-17 16:47:36 +01:00
k_ . parts = [ symmetric . Symmetric ( p ) for p in k . parts ]
2013-03-05 15:58:03 +00:00
return k_
2013-08-27 10:53:32 +02:00
def coregionalise ( output_dim , rank = 1 , W = None , kappa = None ) :
"""
Coregionalisation kernel .
Used for computing covariance functions of the form
. . math : :
2013-08-29 19:26:00 +02:00
k_2 ( x , y ) = \mathbf { B } k ( x , y )
2013-08-27 10:53:32 +02:00
where
. . math : :
2013-08-29 19:26:00 +02:00
\mathbf { B } = \mathbf { W } \mathbf { W } ^ \top + kappa \mathbf { I }
2013-08-27 10:53:32 +02:00
: param output_dim : the number of output dimensions
: type output_dim : int
: param rank : the rank of the coregionalisation matrix .
: type rank : int
: param W : a low rank matrix that determines the correlations between the different outputs , together with kappa it forms the coregionalisation matrix B .
: type W : ndarray
: param kappa : a diagonal term which allows the outputs to behave independently .
: rtype : kernel object
. . Note : see coregionalisation examples in GPy . examples . regression for some usage .
"""
p = parts . coregionalise . Coregionalise ( output_dim , rank , W , kappa )
2013-03-06 13:15:15 +00:00
return kern ( 1 , [ p ] )
2013-06-05 17:40:43 +01:00
def rational_quadratic ( input_dim , variance = 1. , lengthscale = 1. , power = 1. ) :
2013-03-11 17:48:38 +00:00
"""
Construct rational quadratic kernel .
2013-06-05 13:02:03 +01:00
: param input_dim : the number of input dimensions
: type input_dim : int ( input_dim = 1 is the only value currently supported )
2013-03-11 17:48:38 +00:00
: param variance : the variance : math : ` \sigma ^ 2 `
: type variance : float
: param lengthscale : the lengthscale : math : ` \ell `
: type lengthscale : float
: rtype : kern object
"""
2013-06-17 16:47:36 +01:00
part = parts . rational_quadratic . RationalQuadratic ( input_dim , variance , lengthscale , power )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-03-13 14:24:10 +00:00
2013-06-17 16:47:36 +01:00
def fixed ( input_dim , K , variance = 1. ) :
2013-03-13 14:24:10 +00:00
"""
2013-06-05 16:14:30 +01:00
Construct a Fixed effect kernel .
2013-03-13 14:24:10 +00:00
2013-08-16 18:28:16 +01:00
: param input_dim : the number of input dimensions
: type input_dim : int ( input_dim = 1 is the only value currently supported )
: param K : the variance : math : ` \sigma ^ 2 `
: type K : np . array
: param variance : kernel variance
: type variance : float
: rtype : kern object
2013-03-13 14:24:10 +00:00
"""
2013-06-17 16:47:36 +01:00
part = parts . fixed . Fixed ( input_dim , K , variance )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-04-05 11:09:14 +01:00
2013-06-05 17:40:43 +01:00
def rbfcos ( input_dim , variance = 1. , frequencies = None , bandwidths = None , ARD = False ) :
2013-04-05 11:09:14 +01:00
"""
construct a rbfcos kernel
"""
2013-06-17 16:47:36 +01:00
part = parts . rbfcos . RBFCos ( input_dim , variance , frequencies , bandwidths , ARD )
2013-06-05 17:40:43 +01:00
return kern ( input_dim , [ part ] )
2013-04-23 17:13:43 +01:00
2013-06-17 16:47:36 +01:00
def independent_outputs ( k ) :
2013-04-23 17:13:43 +01:00
"""
Construct a kernel with independent outputs from an existing kernel
"""
for sl in k . input_slices :
assert ( sl . start is None ) and ( sl . stop is None ) , " cannot adjust input slices! (TODO) "
2013-07-08 13:06:02 +01:00
_parts = [ parts . independent_outputs . IndependentOutputs ( p ) for p in k . parts ]
return kern ( k . input_dim + 1 , _parts )
2013-07-08 14:50:13 +01:00
def hierarchical ( k ) :
"""
2013-08-16 18:28:16 +01:00
TODO THis can ' t be right! Construct a kernel with independent outputs from an existing kernel
2013-07-08 14:50:13 +01:00
"""
# for sl in k.input_slices:
# assert (sl.start is None) and (sl.stop is None), "cannot adjust input slices! (TODO)"
_parts = [ parts . hierarchical . Hierarchical ( k . parts ) ]
2013-07-08 15:29:37 +01:00
return kern ( k . input_dim + len ( k . parts ) , _parts )