<s> from __future__ import division <EOL> from __future__ import print_function <EOL> from builtins import str <EOL> from builtins import range <EOL> from past . utils import old_div <EOL> from builtins import object <EOL> import numpy as np <EOL> import math <EOL> import scipy . spatial . distance as spdist <EOL> class Kernel ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . hyp = [ ] <EOL> self . para = [ ] <EOL> def __repr__ ( self ) : <EOL> strvalue = str ( type ( self ) ) + '<STR_LIT>' + '<STR_LIT>' + '<STR_LIT>' <EOL> return strvalue <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def checkInputGetCovMatrix ( self , x , z , mode ) : <EOL> '''<STR_LIT>''' <EOL> if mode is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if x is None and z is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if mode == '<STR_LIT>' : <EOL> if x is None or z is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def checkInputGetDerMatrix ( self , x , z , mode , der ) : <EOL> '''<STR_LIT>''' <EOL> if mode is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if x is None and z is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if mode == '<STR_LIT>' : <EOL> if x is None or z is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if der is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def __add__ ( self , cov ) : <EOL> '''<STR_LIT>''' <EOL> return SumOfKernel ( self , cov ) <EOL> def __mul__ ( self , other ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( other , int ) or isinstance ( other , float ) : <EOL> return ScaleOfKernel ( self , other ) <EOL> elif isinstance ( other , Kernel ) : <EOL> return ProductOfKernel ( self , other ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> __rmul__ = __mul__ <EOL> def fitc ( self , inducingInput ) : <EOL> '''<STR_LIT>''' <EOL> return FITCOfKernel ( self , inducingInput ) <EOL> def _sq_dist ( self , a , b = None ) : <EOL> '''<STR_LIT>''' <EOL> n = a . shape [ <NUM_LIT:0> ] <EOL> D = a . shape [ <NUM_LIT:1> ] <EOL> m = n <EOL> if b is None : <EOL> b = a . transpose ( ) <EOL> else : <EOL> m = b . shape [ <NUM_LIT:0> ] <EOL> b = b . transpose ( ) <EOL> C = np . zeros ( ( n , m ) ) <EOL> for d in range ( <NUM_LIT:0> , D ) : <EOL> tt = a [ : , d ] <EOL> tt = tt . reshape ( n , <NUM_LIT:1> ) <EOL> tem = np . kron ( np . ones ( ( <NUM_LIT:1> , m ) ) , tt ) <EOL> tem = tem - np . kron ( np . ones ( ( n , <NUM_LIT:1> ) ) , b [ d , : ] ) <EOL> C = C + tem * tem <EOL> return C <EOL> class ProductOfKernel ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , cov1 , cov2 ) : <EOL> self . cov1 = cov1 <EOL> self . cov2 = cov2 <EOL> self . _hyp = cov1 . hyp + cov2 . hyp <EOL> def _setHyp ( self , hyp ) : <EOL> assert len ( hyp ) == len ( self . _hyp ) <EOL> len1 = len ( self . cov1 . hyp ) <EOL> self . _hyp = hyp <EOL> self . cov1 . hyp = self . _hyp [ : len1 ] <EOL> self . cov2 . hyp = self . _hyp [ len1 : ] <EOL> def _getHyp ( self ) : <EOL> return self . _hyp <EOL> hyp = property ( _getHyp , _setHyp ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> A = self . cov1 . getCovMatrix ( x , z , mode ) * self . cov2 . getCovMatrix ( x , z , mode ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if der < len ( self . cov1 . hyp ) : <EOL> A = self . cov1 . getDerMatrix ( x , z , mode , der ) * self . cov2 . getCovMatrix ( x , z , mode ) <EOL> elif der < len ( self . hyp ) : <EOL> der2 = der - len ( self . cov1 . hyp ) <EOL> A = self . cov2 . getDerMatrix ( x , z , mode , der2 ) * self . cov1 . getCovMatrix ( x , z , mode ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class SumOfKernel ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , cov1 , cov2 ) : <EOL> self . cov1 = cov1 <EOL> self . cov2 = cov2 <EOL> self . _hyp = cov1 . hyp + cov2 . hyp <EOL> def _setHyp ( self , hyp ) : <EOL> assert len ( hyp ) == len ( self . _hyp ) <EOL> len1 = len ( self . cov1 . hyp ) <EOL> self . _hyp = hyp <EOL> self . cov1 . hyp = self . _hyp [ : len1 ] <EOL> self . cov2 . hyp = self . _hyp [ len1 : ] <EOL> def _getHyp ( self ) : <EOL> return self . _hyp <EOL> hyp = property ( _getHyp , _setHyp ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> A = self . cov1 . getCovMatrix ( x , z , mode ) + self . cov2 . getCovMatrix ( x , z , mode ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if der < len ( self . cov1 . hyp ) : <EOL> A = self . cov1 . getDerMatrix ( x , z , mode , der ) <EOL> elif der < len ( self . hyp ) : <EOL> der2 = der - len ( self . cov1 . hyp ) <EOL> A = self . cov2 . getDerMatrix ( x , z , mode , der2 ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class ScaleOfKernel ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , cov , scalar ) : <EOL> self . cov = cov <EOL> if cov . hyp : <EOL> self . _hyp = [ scalar ] + cov . hyp <EOL> else : <EOL> self . _hyp = [ scalar ] <EOL> def _setHyp ( self , hyp ) : <EOL> assert len ( hyp ) == len ( self . _hyp ) <EOL> self . _hyp = hyp <EOL> self . cov . hyp = self . _hyp [ <NUM_LIT:1> : ] <EOL> def _getHyp ( self ) : <EOL> return self . _hyp <EOL> hyp = property ( _getHyp , _setHyp ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> A = sf2 * self . cov . getCovMatrix ( x , z , mode ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if der == <NUM_LIT:0> : <EOL> A = <NUM_LIT> * sf2 * self . cov . getCovMatrix ( x , z , mode ) <EOL> else : <EOL> A = sf2 * self . cov . getDerMatrix ( x , z , mode , der - <NUM_LIT:1> ) <EOL> return A <EOL> class FITCOfKernel ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , cov , inducingInput ) : <EOL> self . inducingInput = inducingInput <EOL> self . covfunc = cov <EOL> self . _hyp = cov . hyp <EOL> def _getHyp ( self ) : <EOL> return self . _hyp <EOL> def _setHyp ( self , hyp ) : <EOL> self . _hyp = hyp <EOL> self . covfunc . hyp = hyp <EOL> hyp = property ( _getHyp , _setHyp ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> xu = self . inducingInput <EOL> if not x is None : <EOL> try : <EOL> assert ( xu . shape [ <NUM_LIT:1> ] == x . shape [ <NUM_LIT:1> ] ) <EOL> except AssertionError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if mode == '<STR_LIT>' : <EOL> K = self . covfunc . getCovMatrix ( z = z , mode = '<STR_LIT>' ) <EOL> return K <EOL> elif mode == '<STR_LIT:train>' : <EOL> K = self . covfunc . getCovMatrix ( z = x , mode = '<STR_LIT>' ) <EOL> Kuu = self . covfunc . getCovMatrix ( x = xu , mode = '<STR_LIT:train>' ) <EOL> Ku = self . covfunc . getCovMatrix ( x = xu , z = x , mode = '<STR_LIT>' ) <EOL> return K , Kuu , Ku <EOL> elif mode == '<STR_LIT>' : <EOL> K = self . covfunc . getCovMatrix ( x = xu , z = z , mode = '<STR_LIT>' ) <EOL> return K <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> xu = self . inducingInput <EOL> if not x is None : <EOL> try : <EOL> assert ( xu . shape [ <NUM_LIT:1> ] == x . shape [ <NUM_LIT:1> ] ) <EOL> except AssertionError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if mode == '<STR_LIT>' : <EOL> K = self . covfunc . getDerMatrix ( z = z , mode = '<STR_LIT>' , der = der ) <EOL> return K <EOL> elif mode == '<STR_LIT:train>' : <EOL> K = self . covfunc . getDerMatrix ( z = x , mode = '<STR_LIT>' , der = der ) <EOL> Kuu = self . covfunc . getDerMatrix ( x = xu , mode = '<STR_LIT:train>' , der = der ) <EOL> Ku = self . covfunc . getDerMatrix ( x = xu , z = x , mode = '<STR_LIT>' , der = der ) <EOL> return K , Kuu , Ku <EOL> elif mode == '<STR_LIT>' : <EOL> K = self . covfunc . getDerMatrix ( x = xu , z = z , mode = '<STR_LIT>' , der = der ) <EOL> return K <EOL> class Gabor ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , log_p = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_p ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> p = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> dp = <NUM_LIT:2> * np . pi * np . sqrt ( A ) * ell / p <EOL> A = np . exp ( - <NUM_LIT:0.5> * A ) * np . cos ( dp ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> p = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> dp = <NUM_LIT:2> * np . pi * np . sqrt ( A ) * ell / p <EOL> if der == <NUM_LIT:0> : <EOL> A = np . exp ( - <NUM_LIT:0.5> * A ) * np . cos ( dp ) <EOL> A = dp * A <EOL> elif der == <NUM_LIT:1> : <EOL> A = np . exp ( - <NUM_LIT:0.5> * A ) * np . cos ( dp ) <EOL> A = np . tan ( dp ) * dp * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class SM ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , Q = <NUM_LIT:0> , hyps = [ ] , D = None ) : <EOL> if D : <EOL> self . hyp = np . random . random ( Q * ( <NUM_LIT:1> + <NUM_LIT:2> * D ) ) <EOL> else : <EOL> self . hyp = hyps <EOL> self . para = [ Q ] <EOL> def initSMhypers ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> x = np . atleast_2d ( x ) <EOL> y = np . atleast_2d ( y ) <EOL> ( n , D ) = x . shape <EOL> Q = self . para [ <NUM_LIT:0> ] <EOL> w = np . zeros ( Q ) <EOL> m = np . zeros ( ( D , Q ) ) <EOL> s = np . zeros ( ( D , Q ) ) <EOL> w [ : ] = old_div ( np . std ( y ) , Q ) <EOL> hypinit = np . zeros ( Q + <NUM_LIT:2> * D * Q ) <EOL> for i in range ( D ) : <EOL> xslice = np . atleast_2d ( x [ : , i ] ) . T <EOL> d2 = spdist . cdist ( xslice , xslice , '<STR_LIT>' ) <EOL> if n > <NUM_LIT:1> : <EOL> d2 [ d2 == <NUM_LIT:0> ] = d2 [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> else : <EOL> d2 [ d2 == <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> minshift = np . min ( np . min ( np . sqrt ( d2 ) ) ) <EOL> nyquist = old_div ( <NUM_LIT:0.5> , minshift ) <EOL> m [ i , : ] = nyquist * np . random . ranf ( ( <NUM_LIT:1> , Q ) ) <EOL> maxshift = np . max ( np . max ( np . sqrt ( d2 ) ) ) <EOL> s [ i , : ] = old_div ( <NUM_LIT:1.> , np . abs ( maxshift * np . random . ranf ( ( <NUM_LIT:1> , Q ) ) ) ) <EOL> hypinit [ : Q ] = np . log ( w ) <EOL> hypinit [ Q + np . arange ( <NUM_LIT:0> , Q * D ) ] = np . log ( m [ : ] ) . T <EOL> hypinit [ Q + Q * D + np . arange ( <NUM_LIT:0> , Q * D ) ] = np . log ( s [ : ] ) . T <EOL> self . hyp = list ( hypinit ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> Q = self . para [ <NUM_LIT:0> ] <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> else : <EOL> nn , D = x . shape <EOL> assert Q == old_div ( len ( self . hyp ) , ( <NUM_LIT:1> + <NUM_LIT:2> * D ) ) <EOL> w = np . exp ( self . hyp [ : Q ] ) <EOL> m = np . exp ( np . reshape ( self . hyp [ Q : Q + Q * D ] , ( D , Q ) ) ) <EOL> v = np . exp ( <NUM_LIT:2> * np . reshape ( self . hyp [ Q + Q * D : ] , ( D , Q ) ) ) <EOL> if mode == '<STR_LIT>' : <EOL> d2 = np . zeros ( ( nn , <NUM_LIT:1> , D ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> d2 = np . zeros ( ( nn , nn , D ) ) <EOL> for j in range ( D ) : <EOL> xslice = np . atleast_2d ( x [ : , j ] ) . T <EOL> d2 [ : , : , j ] = spdist . cdist ( xslice , xslice , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> d2 = np . zeros ( ( nn , z . shape [ <NUM_LIT:0> ] , D ) ) <EOL> for j in range ( D ) : <EOL> xslice = np . atleast_2d ( x [ : , j ] ) . T <EOL> zslice = np . atleast_2d ( z [ : , j ] ) . T <EOL> d2 [ : , : , j ] = spdist . cdist ( xslice , zslice , '<STR_LIT>' ) <EOL> d = np . sqrt ( d2 ) <EOL> k = lambda d2v_dm : np . exp ( - <NUM_LIT:2> * np . pi ** <NUM_LIT:2> * d2v_dm [ <NUM_LIT:0> ] ) * np . cos ( <NUM_LIT:2> * np . pi * d2v_dm [ <NUM_LIT:1> ] ) <EOL> km = lambda dm : - <NUM_LIT:2> * np . pi * np . tan ( <NUM_LIT:2> * np . pi * dm ) * dm <EOL> kv = lambda d2v : - d2v * ( <NUM_LIT:2> * np . pi ) ** <NUM_LIT:2> <EOL> A = <NUM_LIT:0.> <EOL> c = <NUM_LIT:1.> <EOL> qq = list ( range ( Q ) ) <EOL> for q in qq : <EOL> C = w [ q ] * c <EOL> for j in range ( D ) : <EOL> C = C * k ( ( d2 [ : , : , j ] * v [ j , q ] , d [ : , : , j ] * m [ j , q ] ) ) <EOL> A = A + C <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> Q = self . para [ <NUM_LIT:0> ] <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> else : <EOL> nn , D = x . shape <EOL> assert Q == old_div ( len ( self . hyp ) , ( <NUM_LIT:1> + <NUM_LIT:2> * D ) ) <EOL> w = np . exp ( self . hyp [ : Q ] ) <EOL> m = np . exp ( np . reshape ( self . hyp [ Q : Q + Q * D ] , ( D , Q ) ) ) <EOL> v = np . exp ( <NUM_LIT:2> * np . reshape ( self . hyp [ Q + Q * D : ] , ( D , Q ) ) ) <EOL> if mode == '<STR_LIT>' : <EOL> d2 = np . zeros ( ( nn , <NUM_LIT:1> , D ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> d2 = np . zeros ( ( nn , nn , D ) ) <EOL> for j in range ( D ) : <EOL> xslice = np . atleast_2d ( x [ : , j ] ) . T <EOL> d2 [ : , : , j ] = spdist . cdist ( xslice , xslice , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> d2 = np . zeros ( ( nn , z . shape [ <NUM_LIT:0> ] , D ) ) <EOL> for j in range ( D ) : <EOL> xslice = np . atleast_2d ( x [ : , j ] ) . T <EOL> zslice = np . atleast_2d ( z [ : , j ] ) . T <EOL> d2 [ : , : , j ] = spdist . cdist ( xslice , zslice , '<STR_LIT>' ) <EOL> d = np . sqrt ( d2 ) <EOL> k = lambda d2v_dm1 : np . exp ( - <NUM_LIT:2> * np . pi ** <NUM_LIT:2> * d2v_dm1 [ <NUM_LIT:0> ] ) * np . cos ( <NUM_LIT:2> * np . pi * d2v_dm1 [ <NUM_LIT:1> ] ) <EOL> km = lambda dm : - <NUM_LIT:2> * np . pi * np . tan ( <NUM_LIT:2> * np . pi * dm ) * dm <EOL> kv = lambda d2v : - d2v * ( <NUM_LIT:2> * np . pi ) ** <NUM_LIT:2> <EOL> A = <NUM_LIT:0.> <EOL> c = <NUM_LIT:1.> <EOL> qq = list ( range ( Q ) ) <EOL> if der < Q : <EOL> c = <NUM_LIT:1> <EOL> qq = [ der ] <EOL> elif der < Q + Q * D : <EOL> p = ( der - Q ) % D <EOL> q = old_div ( ( der - Q - p ) , D ) <EOL> c = km ( d [ : , : , p ] * m [ p , q ] ) <EOL> qq = [ q ] <EOL> elif der < <NUM_LIT:2> * Q * D + Q : <EOL> p = ( der - ( D + <NUM_LIT:1> ) * Q ) % D <EOL> q = old_div ( ( der - ( D + <NUM_LIT:1> ) * Q - p ) , D ) <EOL> c = kv ( d2 [ : , : , p ] * v [ p , q ] ) <EOL> qq = [ q ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for q in qq : <EOL> C = w [ q ] * c <EOL> for j in range ( D ) : <EOL> C = C * k ( ( d2 [ : , : , j ] * v [ j , q ] , d [ : , : , j ] * m [ j , q ] ) ) <EOL> A = A + C <EOL> return A <EOL> class Poly ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_c = <NUM_LIT:0.> , d = <NUM_LIT:2> , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_c , log_sigma ] <EOL> self . para = [ d ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> c = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> ord = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( ord - np . round ( ord ) ) < <NUM_LIT> : <EOL> ord = int ( round ( ord ) ) <EOL> assert ( ord >= <NUM_LIT:1.> ) <EOL> ord = int ( ord ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . reshape ( np . sum ( z * z , <NUM_LIT:1> ) , ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . dot ( x , x . T ) + np . eye ( n ) * <NUM_LIT> <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . dot ( x , z . T ) <EOL> A = sf2 * ( c + A ) ** ord <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> c = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> ord = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( ord - np . round ( ord ) ) < <NUM_LIT> : <EOL> ord = int ( round ( ord ) ) <EOL> assert ( ord >= <NUM_LIT:1.> ) <EOL> ord = int ( ord ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . reshape ( np . sum ( z * z , <NUM_LIT:1> ) , ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = np . dot ( x , x . T ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . dot ( x , z . T ) <EOL> if der == <NUM_LIT:0> : <EOL> A = c * ord * sf2 * ( c + A ) ** ( ord - <NUM_LIT:1> ) <EOL> elif der == <NUM_LIT:1> : <EOL> A = <NUM_LIT> * sf2 * ( c + A ) ** ord <EOL> elif der == <NUM_LIT:2> : <EOL> A = np . zeros_like ( A ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class PiecePoly ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , v = <NUM_LIT:2> , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_sigma ] <EOL> self . para = [ v ] <EOL> def ppmax ( self , A , B ) : <EOL> return np . maximum ( A , B * np . ones_like ( A ) ) <EOL> def func ( self , v , r , j ) : <EOL> if v == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> elif v == <NUM_LIT:1> : <EOL> return ( <NUM_LIT:1.> + ( j + <NUM_LIT:1> ) * r ) <EOL> elif v == <NUM_LIT:2> : <EOL> return ( <NUM_LIT:1.> + ( j + <NUM_LIT:2> ) * r + ( j * j + <NUM_LIT> * j + <NUM_LIT:3> ) / <NUM_LIT> * r * r ) <EOL> elif v == <NUM_LIT:3> : <EOL> return ( <NUM_LIT:1.> + ( j + <NUM_LIT:3> ) * r + ( <NUM_LIT> * j * j + <NUM_LIT> * j + <NUM_LIT> ) / <NUM_LIT> * r * r + ( j * j * j + <NUM_LIT> * j * j + <NUM_LIT> * j + <NUM_LIT> ) / <NUM_LIT> * r * r * r ) <EOL> else : <EOL> raise Exception ( [ "<STR_LIT>" + str ( v ) ] ) <EOL> def dfunc ( self , v , r , j ) : <EOL> if v == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> elif v == <NUM_LIT:1> : <EOL> return ( j + <NUM_LIT:1> ) <EOL> elif v == <NUM_LIT:2> : <EOL> return ( ( j + <NUM_LIT:2> ) + <NUM_LIT> * ( j * j + <NUM_LIT> * j + <NUM_LIT> ) / <NUM_LIT> * r ) <EOL> elif v == <NUM_LIT:3> : <EOL> return ( ( j + <NUM_LIT:3> ) + <NUM_LIT> * ( <NUM_LIT> * j * j + <NUM_LIT> * j + <NUM_LIT> ) / <NUM_LIT> * r + ( j * j * j + <NUM_LIT> * j * j + <NUM_LIT> * j + <NUM_LIT> ) / <NUM_LIT> * r * r ) <EOL> else : <EOL> raise Exception ( [ "<STR_LIT>" + str ( v ) ] ) <EOL> def pp ( self , r , j , v , func ) : <EOL> return func ( v , r , j ) * ( self . ppmax ( <NUM_LIT:1> - r , <NUM_LIT:0> ) ** ( j + v ) ) <EOL> def dpp ( self , r , j , v , func , dfunc ) : <EOL> return self . ppmax ( <NUM_LIT:1> - r , <NUM_LIT:0> ) ** ( j + v - <NUM_LIT:1> ) * r * ( ( j + v ) * self . func ( v , r , j ) - self . ppmax ( <NUM_LIT:1> - r , <NUM_LIT:0> ) * self . dfunc ( v , r , j ) ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> v = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( v - np . round ( v ) ) < <NUM_LIT> : <EOL> v = int ( round ( v ) ) <EOL> assert ( int ( v ) in range ( <NUM_LIT:4> ) ) <EOL> v = int ( v ) <EOL> j = np . floor ( <NUM_LIT:0.5> * D ) + v + <NUM_LIT:1> <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = np . sqrt ( spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . sqrt ( spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) ) <EOL> A = sf2 * self . pp ( A , j , v , self . func ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> v = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( v - np . round ( v ) ) < <NUM_LIT> : <EOL> v = int ( round ( v ) ) <EOL> assert ( int ( v ) in range ( <NUM_LIT:4> ) ) <EOL> v = int ( v ) <EOL> j = np . floor ( <NUM_LIT:0.5> * D ) + v + <NUM_LIT:1> <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = np . sqrt ( spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . sqrt ( spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) ) <EOL> if der == <NUM_LIT:0> : <EOL> A = sf2 * self . dpp ( A , j , v , self . func , self . dfunc ) <EOL> elif der == <NUM_LIT:1> : <EOL> A = <NUM_LIT> * sf2 * self . pp ( A , j , v , self . func ) <EOL> elif der == <NUM_LIT:2> : <EOL> A = np . zeros_like ( A ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class RBF ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> A = sf2 * np . exp ( - <NUM_LIT:0.5> * A ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> if der == <NUM_LIT:0> : <EOL> A = sf2 * np . exp ( - <NUM_LIT:0.5> * A ) * A <EOL> elif der == <NUM_LIT:1> : <EOL> A = <NUM_LIT> * sf2 * np . exp ( - <NUM_LIT:0.5> * A ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class RBFunit ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> A = np . exp ( - <NUM_LIT:0.5> * A ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> if der == <NUM_LIT:0> : <EOL> A = np . exp ( - <NUM_LIT:0.5> * A ) * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class RBFard ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , D = None , log_ell_list = None , log_sigma = <NUM_LIT:0.> ) : <EOL> if log_ell_list is None : <EOL> self . hyp = [ <NUM_LIT:0.> for i in range ( D ) ] + [ log_sigma ] <EOL> else : <EOL> self . hyp = log_ell_list + [ log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = old_div ( <NUM_LIT:1.> , np . exp ( self . hyp [ <NUM_LIT:0> : D ] ) ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ D ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> tem = np . dot ( np . diag ( ell ) , x . T ) . T <EOL> A = spdist . cdist ( tem , tem , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( np . dot ( np . diag ( ell ) , x . T ) . T , np . dot ( np . diag ( ell ) , z . T ) . T , '<STR_LIT>' ) <EOL> A = sf2 * np . exp ( - <NUM_LIT:0.5> * A ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = old_div ( <NUM_LIT:1.> , np . exp ( self . hyp [ <NUM_LIT:0> : D ] ) ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ D ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> tem = np . dot ( np . diag ( ell ) , x . T ) . T <EOL> A = spdist . cdist ( tem , tem , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = spdist . cdist ( np . dot ( np . diag ( ell ) , x . T ) . T , np . dot ( np . diag ( ell ) , z . T ) . T , '<STR_LIT>' ) <EOL> A = sf2 * np . exp ( - <NUM_LIT:0.5> * A ) <EOL> if der < D : <EOL> if mode == '<STR_LIT>' : <EOL> A = A * <NUM_LIT:0> <EOL> elif mode == '<STR_LIT:train>' : <EOL> tem = ( np . atleast_2d ( x [ : , der ] ) * ell [ der ] ) . T <EOL> A *= spdist . cdist ( tem , tem , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A *= spdist . cdist ( np . atleast_2d ( x [ : , der ] ) . T * ell [ der ] , np . atleast_2d ( z [ : , der ] ) . T * ell [ der ] , '<STR_LIT>' ) <EOL> elif der == D : <EOL> A = <NUM_LIT> * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Const ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = sf2 * np . ones ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = sf2 * np . ones ( ( n , n ) ) + np . eye ( n ) * <NUM_LIT> <EOL> elif mode == '<STR_LIT>' : <EOL> n , D = x . shape <EOL> nn , D = z . shape <EOL> A = sf2 * np . ones ( ( n , nn ) ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = sf2 * np . ones ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = sf2 * np . ones ( ( n , n ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> n , D = x . shape <EOL> nn , D = z . shape <EOL> A = sf2 * np . ones ( ( n , nn ) ) <EOL> if der == <NUM_LIT:0> : <EOL> A = <NUM_LIT> * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Linear ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . reshape ( np . sum ( z * z , <NUM_LIT:1> ) , ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . dot ( x , x . T ) + np . eye ( n ) * <NUM_LIT> <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . dot ( x , z . T ) <EOL> A = sf2 * A <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> sf2 = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> if der == <NUM_LIT:0> : <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . reshape ( np . sum ( z * z , <NUM_LIT:1> ) , ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . dot ( x , x . T ) + np . eye ( n ) * <NUM_LIT> <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . dot ( x , z . T ) <EOL> A = <NUM_LIT:2> * sf2 * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class LINard ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , D = None , log_ell_list = None ) : <EOL> if log_ell_list is None : <EOL> self . hyp = [ <NUM_LIT:0.> for i in range ( D ) ] <EOL> else : <EOL> self . hyp = log_ell_list <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . reshape ( np . sum ( z * z , <NUM_LIT:1> ) , ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . dot ( x , x . T ) + np . eye ( n ) * <NUM_LIT> <EOL> elif mode == '<STR_LIT>' : <EOL> z = np . dot ( z , np . diag ( old_div ( <NUM_LIT:1.> , ell ) ) ) <EOL> A = np . dot ( x , z . T ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> if der < D : <EOL> if mode == '<STR_LIT>' : <EOL> tem = np . atleast_2d ( z [ : , der ] ) . T <EOL> A = - <NUM_LIT> * tem * tem <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = - <NUM_LIT> * np . dot ( np . atleast_2d ( x [ : , der ] ) . T , np . atleast_2d ( x [ : , der ] ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> z = np . dot ( z , np . diag ( old_div ( <NUM_LIT:1.> , ell ) ) ) <EOL> A = - <NUM_LIT> * np . dot ( np . atleast_2d ( x [ : , der ] ) . T , np . atleast_2d ( z [ : , der ] ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Matern ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , d = <NUM_LIT:3> , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_sigma ] <EOL> self . para = [ d ] <EOL> def func ( self , d , t ) : <EOL> if d == <NUM_LIT:1> : <EOL> return <NUM_LIT:1> <EOL> elif d == <NUM_LIT:3> : <EOL> return <NUM_LIT:1> + t <EOL> elif d == <NUM_LIT:5> : <EOL> return <NUM_LIT:1> + t + t * t / <NUM_LIT> <EOL> elif d == <NUM_LIT:7> : <EOL> return <NUM_LIT:1> + t + <NUM_LIT> * t * t / <NUM_LIT> + t * t * t / <NUM_LIT> <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def dfunc ( self , d , t ) : <EOL> if d == <NUM_LIT:1> : <EOL> return <NUM_LIT:1> <EOL> elif d == <NUM_LIT:3> : <EOL> return t <EOL> elif d == <NUM_LIT:5> : <EOL> return ( old_div ( <NUM_LIT:1.> , <NUM_LIT> ) ) * ( t + t * t ) <EOL> elif d == <NUM_LIT:7> : <EOL> return ( old_div ( <NUM_LIT:1.> , <NUM_LIT> ) ) * ( t + <NUM_LIT> * t * t + t * t * t ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def mfunc ( self , d , t ) : <EOL> return self . func ( d , t ) * np . exp ( - <NUM_LIT:1.> * t ) <EOL> def dmfunc ( self , d , t ) : <EOL> return self . dfunc ( d , t ) * t * np . exp ( - <NUM_LIT:1.> * t ) <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> d = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( d - np . round ( d ) ) < <NUM_LIT> : <EOL> d = int ( round ( d ) ) <EOL> d = int ( d ) <EOL> try : <EOL> assert ( d in [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:7> ] ) <EOL> except AssertionError : <EOL> print ( "<STR_LIT>" ) <EOL> d = <NUM_LIT:3> <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> x = np . sqrt ( d ) * x / ell <EOL> A = np . sqrt ( spdist . cdist ( x , x , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> x = np . sqrt ( d ) * x / ell <EOL> z = np . sqrt ( d ) * z / ell <EOL> A = np . sqrt ( spdist . cdist ( x , z , '<STR_LIT>' ) ) <EOL> A = sf2 * self . mfunc ( d , A ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> d = self . para [ <NUM_LIT:0> ] <EOL> if np . abs ( d - np . round ( d ) ) < <NUM_LIT> : <EOL> d = int ( round ( d ) ) <EOL> d = int ( d ) <EOL> try : <EOL> assert ( d in [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:7> ] ) <EOL> except AssertionError : <EOL> print ( "<STR_LIT>" ) <EOL> d = <NUM_LIT:3> <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> x = np . sqrt ( d ) * x / ell <EOL> A = np . sqrt ( spdist . cdist ( x , x , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> x = np . sqrt ( d ) * x / ell <EOL> z = np . sqrt ( d ) * z / ell <EOL> A = np . sqrt ( spdist . cdist ( x , z , '<STR_LIT>' ) ) <EOL> A = sf2 * self . mfunc ( d , A ) <EOL> if der == <NUM_LIT:0> : <EOL> A = sf2 * self . dmfunc ( d , A ) <EOL> elif der == <NUM_LIT:1> : <EOL> A = <NUM_LIT:2> * sf2 * self . mfunc ( d , A ) <EOL> elif der == <NUM_LIT:2> : <EOL> A = np . zeros_like ( A ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Periodic ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , log_p = <NUM_LIT:0.> , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_p , log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> if not x is None : <EOL> assert x . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> , '<STR_LIT>' <EOL> if not z is None : <EOL> assert z . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> , '<STR_LIT>' <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> p = np . exp ( self . hyp [ <NUM_LIT:1> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:2> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = np . sqrt ( spdist . cdist ( x , x , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . sqrt ( spdist . cdist ( x , z , '<STR_LIT>' ) ) <EOL> A = np . pi * A / p <EOL> A = old_div ( np . sin ( A ) , ell ) <EOL> A = A * A <EOL> A = sf2 * np . exp ( - <NUM_LIT> * A ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if not x is None : <EOL> assert x . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> , '<STR_LIT>' <EOL> if not z is None : <EOL> assert z . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> , '<STR_LIT>' <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> p = np . exp ( self . hyp [ <NUM_LIT:1> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:2> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = np . sqrt ( spdist . cdist ( x , x , '<STR_LIT>' ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = np . sqrt ( spdist . cdist ( x , z , '<STR_LIT>' ) ) <EOL> A = np . pi * A / p <EOL> if der == <NUM_LIT:0> : <EOL> A = old_div ( np . sin ( A ) , ell ) <EOL> A = A * A <EOL> A = <NUM_LIT> * sf2 * np . exp ( - <NUM_LIT> * A ) * A <EOL> elif der == <NUM_LIT:1> : <EOL> R = old_div ( np . sin ( A ) , ell ) <EOL> A = <NUM_LIT:4> * sf2 / ell * np . exp ( - <NUM_LIT> * R * R ) * R * np . cos ( A ) * A <EOL> elif der == <NUM_LIT:2> : <EOL> A = old_div ( np . sin ( A ) , ell ) <EOL> A = A * A <EOL> A = <NUM_LIT> * sf2 * np . exp ( - <NUM_LIT> * A ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Noise ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_sigma = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_sigma ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> tol = <NUM_LIT> <EOL> s2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . eye ( n ) <EOL> elif mode == '<STR_LIT>' : <EOL> M = spdist . cdist ( x , z , '<STR_LIT>' ) <EOL> A = np . zeros_like ( M , dtype = np . float ) <EOL> A [ M < tol ] = <NUM_LIT:1.> <EOL> A = s2 * A <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> tol = <NUM_LIT> <EOL> s2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:0> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> A = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> n , D = x . shape <EOL> A = np . eye ( n ) <EOL> elif mode == '<STR_LIT>' : <EOL> M = spdist . cdist ( x , z , '<STR_LIT>' ) <EOL> A = np . zeros_like ( M , dtype = np . float ) <EOL> A [ M < tol ] = <NUM_LIT:1.> <EOL> if der == <NUM_LIT:0> : <EOL> A = <NUM_LIT> * s2 * A <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class RQ ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , log_ell = <NUM_LIT:0.> , log_sigma = <NUM_LIT:0.> , log_alpha = <NUM_LIT:0.> ) : <EOL> self . hyp = [ log_ell , log_sigma , log_alpha ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> alpha = np . exp ( self . hyp [ <NUM_LIT:2> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> D2 = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> D2 = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> D2 = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> A = sf2 * ( ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha ) ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> ell = np . exp ( self . hyp [ <NUM_LIT:0> ] ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ <NUM_LIT:1> ] ) <EOL> alpha = np . exp ( self . hyp [ <NUM_LIT:2> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> D2 = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> D2 = spdist . cdist ( old_div ( x , ell ) , old_div ( x , ell ) , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> D2 = spdist . cdist ( old_div ( x , ell ) , old_div ( z , ell ) , '<STR_LIT>' ) <EOL> if der == <NUM_LIT:0> : <EOL> A = sf2 * ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha - <NUM_LIT:1> ) * D2 <EOL> elif der == <NUM_LIT:1> : <EOL> A = <NUM_LIT> * sf2 * ( ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha ) ) <EOL> elif der == <NUM_LIT:2> : <EOL> K = ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) <EOL> A = sf2 * K ** ( - alpha ) * ( <NUM_LIT:0.5> * D2 / K - alpha * np . log ( K ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class RQard ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , D = None , log_ell_list = None , log_sigma = <NUM_LIT:0.> , log_alpha = <NUM_LIT:0.> ) : <EOL> if log_ell_list is None : <EOL> self . hyp = [ <NUM_LIT:0.> for i in range ( D ) ] + [ log_sigma , log_alpha ] <EOL> else : <EOL> self . hyp = log_ell_list + [ log_sigma , log_alpha ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> self . checkInputGetCovMatrix ( x , z , mode ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = old_div ( <NUM_LIT:1.> , np . exp ( self . hyp [ <NUM_LIT:0> : D ] ) ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ D ] ) <EOL> alpha = np . exp ( self . hyp [ D + <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> D2 = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> tmp = np . dot ( np . diag ( ell ) , x . T ) . T <EOL> D2 = spdist . cdist ( tmp , tmp , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> D2 = spdist . cdist ( np . dot ( np . diag ( ell ) , x . T ) . T , np . dot ( np . diag ( ell ) , z . T ) . T , '<STR_LIT>' ) <EOL> A = sf2 * ( ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha ) ) <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> self . checkInputGetDerMatrix ( x , z , mode , der ) <EOL> if not x is None : <EOL> n , D = x . shape <EOL> if not z is None : <EOL> nn , D = z . shape <EOL> ell = old_div ( <NUM_LIT:1.> , np . exp ( self . hyp [ <NUM_LIT:0> : D ] ) ) <EOL> sf2 = np . exp ( <NUM_LIT> * self . hyp [ D ] ) <EOL> alpha = np . exp ( self . hyp [ D + <NUM_LIT:1> ] ) <EOL> if mode == '<STR_LIT>' : <EOL> nn , D = z . shape <EOL> D2 = np . zeros ( ( nn , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> tmp = np . dot ( np . diag ( ell ) , x . T ) . T <EOL> D2 = spdist . cdist ( tmp , tmp , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> D2 = spdist . cdist ( np . dot ( np . diag ( ell ) , x . T ) . T , np . dot ( np . diag ( ell ) , z . T ) . T , '<STR_LIT>' ) <EOL> if der < D : <EOL> if mode == '<STR_LIT>' : <EOL> A = D2 * <NUM_LIT:0> <EOL> elif mode == '<STR_LIT:train>' : <EOL> tmp = old_div ( np . atleast_2d ( x [ : , der ] ) , ell [ der ] ) <EOL> A = sf2 * ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha - <NUM_LIT:1> ) * spdist . cdist ( tmp , tmp , '<STR_LIT>' ) <EOL> elif mode == '<STR_LIT>' : <EOL> A = sf2 * ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha - <NUM_LIT:1> ) * spdist . cdist ( old_div ( np . atleast_2d ( x [ : , der ] ) . T , ell [ der ] ) , old_div ( np . atleast_2d ( z [ : , der ] ) . T , ell [ der ] ) , '<STR_LIT>' ) <EOL> elif der == D : <EOL> A = <NUM_LIT> * sf2 * ( ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) ** ( - alpha ) ) <EOL> elif der == ( D + <NUM_LIT:1> ) : <EOL> K = ( <NUM_LIT:1.0> + <NUM_LIT:0.5> * D2 / alpha ) <EOL> A = sf2 * K ** ( - alpha ) * ( <NUM_LIT:0.5> * D2 / K - alpha * np . log ( K ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return A <EOL> class Pre ( Kernel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , M1 , M2 ) : <EOL> self . M1 = M1 <EOL> self . M2 = M2 <EOL> self . hyp = [ ] <EOL> def getCovMatrix ( self , x = None , z = None , mode = None ) : <EOL> if mode == '<STR_LIT>' : <EOL> A = self . M1 [ - <NUM_LIT:1> , : ] <EOL> A = np . reshape ( A , ( A . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> elif mode == '<STR_LIT:train>' : <EOL> A = self . M2 <EOL> elif mode == '<STR_LIT>' : <EOL> A = self . M1 [ : - <NUM_LIT:1> , : ] <EOL> return A <EOL> def getDerMatrix ( self , x = None , z = None , mode = None , der = None ) : <EOL> if not der is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> pass </s>
