<s> from __future__ import division <EOL> from __future__ import absolute_import <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> from . import lik , cov <EOL> from copy import copy , deepcopy <EOL> from . tools import solve_chol , brentmin , cholupdate , jitchol <EOL> np . seterr ( all = '<STR_LIT:ignore>' ) <EOL> class postStruct ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . alpha = np . array ( [ ] ) <EOL> self . L = np . array ( [ ] ) <EOL> self . sW = np . array ( [ ] ) <EOL> def __repr__ ( self ) : <EOL> value = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" <EOL> return value <EOL> def __str__ ( self ) : <EOL> value = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + str ( self . alpha ) + "<STR_LIT:\n>" + "<STR_LIT>" + str ( self . L ) + "<STR_LIT>" + str ( self . sW ) <EOL> return value <EOL> class dnlZStruct ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , m , c , l ) : <EOL> self . mean = [ ] <EOL> self . cov = [ ] <EOL> self . lik = [ ] <EOL> if m . hyp is not None : <EOL> self . mean = [ <NUM_LIT:0> for i in range ( len ( m . hyp ) ) ] <EOL> if c . hyp is not None : <EOL> self . cov = [ <NUM_LIT:0> for i in range ( len ( c . hyp ) ) ] <EOL> if l . hyp is not None : <EOL> self . lik = [ <NUM_LIT:0> for i in range ( len ( l . hyp ) ) ] <EOL> def __str__ ( self ) : <EOL> value = "<STR_LIT>" + "<STR_LIT>" + str ( self . mean ) + "<STR_LIT:\n>" + "<STR_LIT>" + str ( self . cov ) + "<STR_LIT:\n>" + "<STR_LIT>" + str ( self . lik ) <EOL> return value <EOL> def __repr__ ( self ) : <EOL> value = "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" + "<STR_LIT>" <EOL> return value <EOL> class Inference ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> pass <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def _epComputeParams ( self , K , y , ttau , tnu , likfunc , m , inffunc ) : <EOL> n = len ( y ) <EOL> ssi = np . sqrt ( ttau ) <EOL> L = jitchol ( np . eye ( n ) + np . dot ( ssi , ssi . T ) * K ) . T <EOL> V = np . linalg . solve ( L . T , np . tile ( ssi , ( <NUM_LIT:1> , n ) ) * K ) <EOL> Sigma = K - np . dot ( V . T , V ) <EOL> mu = np . dot ( Sigma , tnu ) <EOL> Dsigma = np . reshape ( np . diag ( Sigma ) , ( np . diag ( Sigma ) . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> tau_n = old_div ( <NUM_LIT:1> , Dsigma ) - ttau <EOL> nu_n = old_div ( mu , Dsigma ) - tnu + m * tau_n <EOL> lZ = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) , old_div ( <NUM_LIT:1> , tau_n ) , inffunc ) <EOL> nlZ = np . log ( np . diag ( L ) ) . sum ( ) - lZ . sum ( ) - old_div ( np . dot ( tnu . T , np . dot ( Sigma , tnu ) ) , <NUM_LIT:2> ) - old_div ( np . dot ( ( nu_n - m * tau_n ) . T , ( old_div ( ( ttau / tau_n * ( nu_n - m * tau_n ) - <NUM_LIT:2> * tnu ) , ( ttau + tau_n ) ) ) ) , <NUM_LIT:2> ) + old_div ( ( old_div ( tnu ** <NUM_LIT:2> , ( tau_n + ttau ) ) ) . sum ( ) , <NUM_LIT> ) - old_div ( np . log ( <NUM_LIT:1.> + old_div ( ttau , tau_n ) ) . sum ( ) , <NUM_LIT> ) <EOL> return Sigma , mu , nlZ [ <NUM_LIT:0> ] , L <EOL> def _logdetA ( self , K , w , nargout ) : <EOL> '''<STR_LIT>''' <EOL> n = K . shape [ <NUM_LIT:0> ] <EOL> assert ( K . shape [ <NUM_LIT:0> ] == K . shape [ <NUM_LIT:1> ] ) <EOL> A = np . eye ( n ) + K * np . tile ( w . T , ( n , <NUM_LIT:1> ) ) <EOL> [ L , U , P ] = np . linalg . lu ( A ) <EOL> u = np . diag ( U ) <EOL> signU = np . prod ( np . sign ( u ) ) <EOL> detP = <NUM_LIT:1> <EOL> p = np . dot ( P , np . array ( list ( range ( n ) ) ) . T ) <EOL> for ii in range ( n ) : <EOL> if ii != p [ ii ] : <EOL> detP = - detP <EOL> j = [ jj for jj , val in enumerate ( p ) if val == ii ] <EOL> p [ ii , j [ <NUM_LIT:0> ] ] = p [ j [ <NUM_LIT:0> ] , ii ] <EOL> if signU != detP : <EOL> ldA = np . inf <EOL> else : <EOL> ldA = np . log ( np . abs ( u ) ) . sum ( ) <EOL> if nargout > <NUM_LIT:1> : <EOL> iA = inv ( U ) * inv ( L ) * P ; <EOL> if nargout > <NUM_LIT:2> : <EOL> mwiA = - np . tile ( w , ( <NUM_LIT:1> , n ) ) * iA <EOL> return ldA , iA , mwiA <EOL> else : <EOL> return ldA , iA <EOL> else : <EOL> return ldA <EOL> def _Psi_line ( self , s , dalpha , alpha , K , m , likfunc , y , inffunc ) : <EOL> '''<STR_LIT>''' <EOL> alpha = alpha + s * dalpha <EOL> f = np . dot ( K , alpha ) + m <EOL> [ lp , dlp , d2lp ] = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> W = - d2lp <EOL> Psi = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) <EOL> return Psi [ <NUM_LIT:0> ] , alpha , f , dlp , W <EOL> def _epfitcZ ( self , d , P , R , nn , gg , ttau , tnu , d0 , R0 , P0 , y , likfunc , m , inffunc ) : <EOL> '''<STR_LIT>''' <EOL> T = np . dot ( np . dot ( R , R0 ) , P ) <EOL> diag_sigma = d + np . array ( [ ( T * T ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> mu = nn + np . dot ( P . T , gg ) <EOL> tau_n = old_div ( <NUM_LIT:1.> , diag_sigma ) - ttau <EOL> nu_n = old_div ( mu , diag_sigma ) - tnu + m * tau_n <EOL> lZ = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) , old_div ( <NUM_LIT:1> , tau_n ) , inffunc , None , <NUM_LIT:1> ) <EOL> nu = gg . shape [ <NUM_LIT:0> ] <EOL> U = np . dot ( R0 , P0 ) . T * np . tile ( old_div ( <NUM_LIT:1> , np . sqrt ( d0 + old_div ( <NUM_LIT:1> , ttau ) ) ) , ( <NUM_LIT:1> , nu ) ) <EOL> L = jitchol ( np . eye ( nu ) + np . dot ( U . T , U ) ) . T <EOL> ld = <NUM_LIT> * np . log ( np . diag ( L ) ) . sum ( ) + ( np . log ( d0 + old_div ( <NUM_LIT:1> , ttau ) ) ) . sum ( ) + ( np . log ( ttau ) ) . sum ( ) <EOL> t = np . dot ( T , tnu ) ; tnu_Sigma_tnu = np . dot ( tnu . T , ( d * tnu ) ) + np . dot ( t . T , t ) <EOL> nlZ = old_div ( ld , <NUM_LIT> ) - lZ . sum ( ) - old_div ( tnu_Sigma_tnu , <NUM_LIT> ) - old_div ( np . dot ( ( nu_n - m * tau_n ) . T , ( old_div ( ( ttau / tau_n * ( nu_n - m * tau_n ) - <NUM_LIT> * tnu ) , ( ttau + tau_n ) ) ) ) , <NUM_LIT> ) + old_div ( ( old_div ( tnu ** <NUM_LIT:2> , ( tau_n + ttau ) ) ) . sum ( ) , <NUM_LIT> ) - old_div ( ( np . log ( <NUM_LIT:1> + old_div ( ttau , tau_n ) ) ) . sum ( ) , <NUM_LIT> ) <EOL> return nlZ , nu_n , tau_n <EOL> def _epfitcRefresh ( self , d0 , P0 , R0 , R0P0 , w , b ) : <EOL> '''<STR_LIT>''' <EOL> nu = R0 . shape [ <NUM_LIT:0> ] <EOL> rot180 = lambda A : np . rot90 ( np . rot90 ( A ) ) <EOL> chol_inv = lambda A : rot180 ( np . linalg . solve ( jitchol ( rot180 ( A ) ) , np . eye ( nu ) ) ) <EOL> t = old_div ( <NUM_LIT:1> , ( <NUM_LIT:1> + d0 * w ) ) <EOL> d = d0 * t <EOL> P = np . tile ( t . T , ( nu , <NUM_LIT:1> ) ) * P0 <EOL> T = np . tile ( ( w * t ) . T , ( nu , <NUM_LIT:1> ) ) * R0P0 <EOL> R = chol_inv ( np . eye ( nu ) + np . dot ( R0P0 , T . T ) ) <EOL> nn = d * b <EOL> gg = np . dot ( R0 . T , np . dot ( R . T , np . dot ( R , np . dot ( R0P0 , t * b ) ) ) ) <EOL> return d , P , R , nn , gg <EOL> def _epfitcUpdate ( self , d , P_i , R , nn , gg , w , b , ii , w_i , b_i , m , d0 , P0 , R0 ) : <EOL> dwi = w_i - w [ ii ] <EOL> dbi = b_i - b [ ii ] <EOL> hi = nn [ ii ] + m [ ii ] + np . dot ( P_i . T , gg ) <EOL> t = <NUM_LIT:1> + dwi * d [ ii ] <EOL> d [ ii ] = old_div ( d [ ii ] , t ) <EOL> nn [ ii ] = d [ ii ] * b_i <EOL> r = <NUM_LIT:1> + d0 [ ii ] * w [ ii ] <EOL> r = old_div ( ( r * r ) , dwi ) + r * d0 [ ii ] <EOL> v = np . dot ( R , np . dot ( R0 , P0 [ : , ii ] ) ) <EOL> v = np . reshape ( v , ( v . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> r = old_div ( <NUM_LIT:1> , ( r + np . dot ( v . T , v ) ) ) <EOL> if r > <NUM_LIT:0> : <EOL> R = cholupdate ( R , np . sqrt ( r ) * np . dot ( R . T , v ) , '<STR_LIT:->' ) <EOL> else : <EOL> R = cholupdate ( R , np . sqrt ( - r ) * np . dot ( R . T , v ) , '<STR_LIT:+>' ) <EOL> ttemp = np . dot ( R0 . T , np . dot ( R . T , np . dot ( R , np . dot ( R0 , P_i ) ) ) ) <EOL> gg = gg + ( old_div ( ( dbi - dwi * ( hi - m [ ii ] ) ) , t ) ) * np . reshape ( ttemp , ( ttemp . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> w [ ii ] = w_i ; b [ ii ] = b_i ; <EOL> P_i = old_div ( P_i , t ) <EOL> return d , P_i , R , nn , gg , w , b <EOL> def _mvmZ ( self , x , RVdd , t ) : <EOL> '''<STR_LIT>''' <EOL> Zx = t * x - np . dot ( RVdd . T , np . dot ( RVdd , x ) ) <EOL> return Zx <EOL> def _mvmK ( self , al , V , d0 ) : <EOL> '''<STR_LIT>''' <EOL> Kal = np . dot ( V . T , np . dot ( V , al ) ) + d0 * al <EOL> return Kal <EOL> def _Psi_lineFITC ( self , s , dalpha , alpha , V , d0 , m , likfunc , y , inffunc ) : <EOL> '''<STR_LIT>''' <EOL> alpha = alpha + s * dalpha <EOL> f = self . _mvmK ( alpha , V , d0 ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp <EOL> Psi = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) <EOL> return Psi [ <NUM_LIT:0> ] , alpha , f , dlp , W <EOL> def _fitcRefresh ( self , d0 , P0 , R0 , R0P0 , w ) : <EOL> '''<STR_LIT>''' <EOL> nu = R0 . shape [ <NUM_LIT:0> ] <EOL> rot180 = lambda A : np . rot90 ( np . rot90 ( A ) ) <EOL> chol_inv = lambda A : rot180 ( np . linalg . solve ( jitchol ( rot180 ( A ) ) , np . eye ( nu ) ) ) <EOL> t = old_div ( <NUM_LIT:1> , ( <NUM_LIT:1> + d0 * w ) ) <EOL> d = d0 * t <EOL> P = np . tile ( t . T , ( nu , <NUM_LIT:1> ) ) * P0 ; <EOL> T = np . tile ( ( w * t ) . T , ( nu , <NUM_LIT:1> ) ) * R0P0 ; <EOL> R = chol_inv ( np . eye ( nu ) + np . dot ( R0P0 , T . T ) ) <EOL> return d , P , R <EOL> class Exact ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . name = "<STR_LIT>" <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> if not isinstance ( likfunc , lik . Gauss ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> n , D = x . shape <EOL> K = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> sn2 = np . exp ( <NUM_LIT:2> * likfunc . hyp [ <NUM_LIT:0> ] ) <EOL> L = jitchol ( old_div ( K , sn2 ) + np . eye ( n ) ) . T <EOL> alpha = old_div ( solve_chol ( L , y - m ) , sn2 ) <EOL> post = postStruct ( ) <EOL> post . alpha = alpha <EOL> post . sW = old_div ( np . ones ( ( n , <NUM_LIT:1> ) ) , np . sqrt ( sn2 ) ) <EOL> post . L = L <EOL> if nargout > <NUM_LIT:1> : <EOL> nlZ = old_div ( np . dot ( ( y - m ) . T , alpha ) , <NUM_LIT> ) + np . log ( np . diag ( L ) ) . sum ( ) + n * np . log ( <NUM_LIT:2> * np . pi * sn2 ) / <NUM_LIT> <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> Q = old_div ( solve_chol ( L , np . eye ( n ) ) , sn2 ) - np . dot ( alpha , alpha . T ) <EOL> dnlZ . lik = [ sn2 * np . trace ( Q ) ] <EOL> if covfunc . hyp : <EOL> for ii in range ( len ( covfunc . hyp ) ) : <EOL> dnlZ . cov [ ii ] = old_div ( ( Q * covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = ii ) ) . sum ( ) , <NUM_LIT> ) <EOL> if meanfunc . hyp : <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dnlZ . mean [ ii ] = np . dot ( - meanfunc . getDerMatrix ( x , ii ) . T , alpha ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] , dnlZ <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post <EOL> class FITC_Exact ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> if not isinstance ( likfunc , lik . Gauss ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not isinstance ( covfunc , cov . FITCOfKernel ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> diagK , Kuu , Ku = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> n , D = x . shape <EOL> nu = Kuu . shape [ <NUM_LIT:0> ] <EOL> sn2 = np . exp ( <NUM_LIT:2> * likfunc . hyp [ <NUM_LIT:0> ] ) <EOL> snu2 = <NUM_LIT> * sn2 <EOL> Luu = jitchol ( Kuu + snu2 * np . eye ( nu ) ) . T <EOL> V = np . linalg . solve ( Luu . T , Ku ) <EOL> g_sn2 = diagK + sn2 - np . array ( [ ( V * V ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> Lu = jitchol ( np . eye ( nu ) + np . dot ( old_div ( V , np . tile ( g_sn2 . T , ( nu , <NUM_LIT:1> ) ) ) , V . T ) ) . T <EOL> r = old_div ( ( y - m ) , np . sqrt ( g_sn2 ) ) <EOL> be = np . linalg . solve ( Lu . T , np . dot ( V , old_div ( r , np . sqrt ( g_sn2 ) ) ) ) <EOL> iKuu = solve_chol ( Luu , np . eye ( nu ) ) <EOL> post = postStruct ( ) <EOL> post . alpha = np . linalg . solve ( Luu , np . linalg . solve ( Lu , be ) ) <EOL> post . L = solve_chol ( np . dot ( Lu , Luu ) , np . eye ( nu ) ) - iKuu <EOL> post . sW = old_div ( np . ones ( ( n , <NUM_LIT:1> ) ) , np . sqrt ( sn2 ) ) <EOL> if nargout > <NUM_LIT:1> : <EOL> nlZ = np . log ( np . diag ( Lu ) ) . sum ( ) + old_div ( ( np . log ( g_sn2 ) . sum ( ) + n * np . log ( <NUM_LIT:2> * np . pi ) + np . dot ( r . T , r ) - np . dot ( be . T , be ) ) , <NUM_LIT> ) <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> al = old_div ( r , np . sqrt ( g_sn2 ) ) - old_div ( np . dot ( V . T , np . linalg . solve ( Lu , be ) ) , g_sn2 ) <EOL> B = np . dot ( iKuu , Ku ) <EOL> w = np . dot ( B , al ) <EOL> W = np . linalg . solve ( Lu . T , old_div ( V , np . tile ( g_sn2 . T , ( nu , <NUM_LIT:1> ) ) ) ) <EOL> for ii in range ( len ( covfunc . hyp ) ) : <EOL> [ ddiagKi , dKuui , dKui ] = covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = ii ) <EOL> R = <NUM_LIT> * dKui - np . dot ( dKuui , B ) <EOL> v = ddiagKi - np . array ( [ ( R * B ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> dnlZ . cov [ ii ] = old_div ( ( np . dot ( ddiagKi . T , old_div ( <NUM_LIT:1.> , g_sn2 ) ) + np . dot ( w . T , ( np . dot ( dKuui , w ) - <NUM_LIT> * np . dot ( dKui , al ) ) ) - np . dot ( al . T , ( v * al ) ) - np . dot ( np . array ( [ ( W * W ) . sum ( axis = <NUM_LIT:0> ) ] ) , v ) - ( np . dot ( R , W . T ) * np . dot ( B , W . T ) ) . sum ( ) ) , <NUM_LIT> ) <EOL> dnlZ . cov [ ii ] = dnlZ . cov [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> dnlZ . lik = sn2 * ( ( old_div ( <NUM_LIT:1.> , g_sn2 ) ) . sum ( ) - ( np . array ( [ ( W * W ) . sum ( axis = <NUM_LIT:0> ) ] ) ) . sum ( ) - np . dot ( al . T , al ) ) <EOL> dKuui = <NUM_LIT:2> * snu2 <EOL> R = - dKuui * B <EOL> v = - np . array ( [ ( R * B ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> dnlZ . lik += old_div ( ( np . dot ( w . T , np . dot ( dKuui , w ) ) - np . dot ( al . T , ( v * al ) ) - np . dot ( np . array ( [ ( W * W ) . sum ( axis = <NUM_LIT:0> ) ] ) , v ) - ( np . dot ( R , W . T ) * np . dot ( B , W . T ) ) . sum ( ) ) , <NUM_LIT> ) <EOL> dnlZ . lik = list ( dnlZ . lik [ <NUM_LIT:0> ] ) <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dnlZ . mean [ ii ] = np . dot ( - meanfunc . getDerMatrix ( x , ii ) . T , al ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] , dnlZ <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post <EOL> class Laplace ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . last_alpha = None <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> tol = <NUM_LIT> <EOL> smax = <NUM_LIT:2> ; Nline = <NUM_LIT:20> ; thr = <NUM_LIT> <EOL> maxit = <NUM_LIT:20> <EOL> inffunc = self <EOL> K = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> n , D = x . shape <EOL> Psi_old = np . inf <EOL> if self . last_alpha is None : <EOL> alpha = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> f = np . dot ( K , alpha ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp <EOL> Psi_new = - lp . sum ( ) <EOL> else : <EOL> alpha = self . last_alpha <EOL> f = np . dot ( K , alpha ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp <EOL> Psi_new = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) <EOL> vargout = - likfunc . evaluate ( y , m , None , inffunc , None , <NUM_LIT:1> ) <EOL> Psi_def = vargout [ <NUM_LIT:0> ] <EOL> if Psi_def < Psi_new : <EOL> alpha = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> f = np . dot ( K , alpha ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp ; Psi_new = - lp . sum ( ) <EOL> isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> it = <NUM_LIT:0> <EOL> while ( Psi_old - Psi_new > tol ) and it < maxit : <EOL> Psi_old = Psi_new ; it += <NUM_LIT:1> <EOL> if isWneg : <EOL> W = np . maximum ( W , <NUM_LIT:0> ) <EOL> tol = <NUM_LIT> <EOL> sW = np . sqrt ( W ) ; L = jitchol ( np . eye ( n ) + np . dot ( sW , sW . T ) * K ) . T <EOL> b = W * ( f - m ) + dlp ; <EOL> dalpha = b - sW * solve_chol ( L , sW * np . dot ( K , b ) ) - alpha <EOL> vargout = brentmin ( <NUM_LIT:0> , smax , Nline , thr , self . _Psi_line , <NUM_LIT:4> , dalpha , alpha , K , m , likfunc , y , inffunc ) <EOL> s = vargout [ <NUM_LIT:0> ] <EOL> Psi_new = vargout [ <NUM_LIT:1> ] <EOL> Nfun = vargout [ <NUM_LIT:2> ] <EOL> alpha = vargout [ <NUM_LIT:3> ] <EOL> f = vargout [ <NUM_LIT:4> ] <EOL> dlp = vargout [ <NUM_LIT:5> ] <EOL> W = vargout [ <NUM_LIT:6> ] <EOL> isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> self . last_alpha = alpha <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:4> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] ; d3lp = vargout [ <NUM_LIT:3> ] <EOL> W = - d2lp ; isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> post = postStruct ( ) <EOL> post . alpha = alpha <EOL> post . sW = np . sqrt ( np . abs ( W ) ) * np . sign ( W ) <EOL> if isWneg : <EOL> [ ldA , iA , post . L ] = self . _logdetA ( K , W , <NUM_LIT:3> ) <EOL> nlZ = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) + old_div ( ldA , <NUM_LIT> ) <EOL> nlZ = nlZ [ <NUM_LIT:0> ] <EOL> else : <EOL> sW = post . sW <EOL> post . L = jitchol ( np . eye ( n ) + np . dot ( sW , sW . T ) * K ) . T <EOL> nlZ = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) + ( np . log ( np . diag ( post . L ) ) - np . reshape ( lp , ( lp . shape [ <NUM_LIT:0> ] , ) ) ) . sum ( ) <EOL> nlZ = nlZ [ <NUM_LIT:0> ] <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> if isWneg : <EOL> Z = - post . L <EOL> g = old_div ( np . atleast_2d ( ( iA * K ) . sum ( axis = <NUM_LIT:1> ) ) . T , <NUM_LIT:2> ) <EOL> else : <EOL> Z = np . tile ( sW , ( <NUM_LIT:1> , n ) ) * solve_chol ( post . L , np . diag ( np . reshape ( sW , ( sW . shape [ <NUM_LIT:0> ] , ) ) ) ) <EOL> C = np . linalg . solve ( post . L . T , np . tile ( sW , ( <NUM_LIT:1> , n ) ) * K ) <EOL> g = old_div ( np . atleast_2d ( ( np . diag ( K ) - ( C ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) . T ) ) . T , <NUM_LIT> ) <EOL> dfhat = g * d3lp <EOL> for ii in range ( len ( covfunc . hyp ) ) : <EOL> dK = covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = ii ) <EOL> dnlZ . cov [ ii ] = old_div ( ( Z * dK ) . sum ( ) , <NUM_LIT> ) - old_div ( np . dot ( alpha . T , np . dot ( dK , alpha ) ) , <NUM_LIT> ) <EOL> b = np . dot ( dK , dlp ) <EOL> dnlZ . cov [ ii ] -= np . dot ( dfhat . T , b - np . dot ( K , np . dot ( Z , b ) ) ) <EOL> dnlZ . cov [ ii ] = dnlZ . cov [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for ii in range ( len ( likfunc . hyp ) ) : <EOL> [ lp_dhyp , dlp_dhyp , d2lp_dhyp ] = likfunc . evaluate ( y , f , None , inffunc , ii , <NUM_LIT:3> ) <EOL> dnlZ . lik [ ii ] = - np . dot ( g . T , d2lp_dhyp ) - lp_dhyp . sum ( ) <EOL> b = np . dot ( K , dlp_dhyp ) <EOL> dnlZ . lik [ ii ] -= np . dot ( dfhat . T , b - np . dot ( K , np . dot ( Z , b ) ) ) <EOL> dnlZ . lik [ ii ] = dnlZ . lik [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dm = meanfunc . getDerMatrix ( x , ii ) <EOL> dnlZ . mean [ ii ] = - np . dot ( alpha . T , dm ) <EOL> dnlZ . mean [ ii ] -= np . dot ( dfhat . T , dm - np . dot ( K , np . dot ( Z , dm ) ) ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> ] , dnlZ <EOL> else : <EOL> return post , nlZ [ <NUM_LIT:0> ] <EOL> class FITC_Laplace ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . last_alpha = None <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> if not isinstance ( covfunc , cov . FITCOfKernel ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> tol = <NUM_LIT> <EOL> smax = <NUM_LIT:2> ; Nline = <NUM_LIT:100> ; thr = <NUM_LIT> <EOL> maxit = <NUM_LIT:20> <EOL> inffunc = Laplace ( ) <EOL> diagK , Kuu , Ku = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> if likfunc . hyp : <EOL> sn2 = np . exp ( <NUM_LIT> * likfunc . hyp [ - <NUM_LIT:1> ] ) <EOL> snu2 = <NUM_LIT> * sn2 <EOL> else : <EOL> snu2 = <NUM_LIT> <EOL> n , D = x . shape <EOL> nu = Kuu . shape [ <NUM_LIT:0> ] <EOL> rot180 = lambda A : np . rot90 ( np . rot90 ( A ) ) <EOL> chol_inv = lambda A : rot180 ( np . linalg . solve ( jitchol ( rot180 ( A ) ) , np . eye ( nu ) ) ) <EOL> R0 = chol_inv ( Kuu + snu2 * np . eye ( nu ) ) <EOL> V = np . dot ( R0 , Ku ) ; d0 = diagK - np . array ( [ ( V * V ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> Psi_old = np . inf <EOL> if self . last_alpha is None : <EOL> alpha = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> f = self . _mvmK ( alpha , V , d0 ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp ; Psi_new = - lp . sum ( ) <EOL> else : <EOL> alpha = self . last_alpha <EOL> f = self . _mvmK ( alpha , V , d0 ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp <EOL> Psi_new = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) <EOL> vargout = - likfunc . evaluate ( y , m , None , inffunc , None , <NUM_LIT:1> ) <EOL> Psi_def = vargout [ <NUM_LIT:0> ] <EOL> if Psi_def < Psi_new : <EOL> alpha = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> f = self . _mvmK ( alpha , V , d0 ) + m <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:3> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] <EOL> W = - d2lp ; Psi_new = - lp . sum ( ) <EOL> isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> it = <NUM_LIT:0> <EOL> while ( Psi_old - Psi_new > tol ) and it < maxit : <EOL> Psi_old = Psi_new <EOL> it += <NUM_LIT:1> <EOL> if isWneg : <EOL> W = np . maximum ( W , <NUM_LIT:0> ) <EOL> tol = <NUM_LIT> <EOL> b = W * ( f - m ) + dlp ; dd = old_div ( <NUM_LIT:1> , ( <NUM_LIT:1> + W * d0 ) ) <EOL> RV = np . dot ( chol_inv ( np . eye ( nu ) + np . dot ( V * np . tile ( ( W * dd ) . T , ( nu , <NUM_LIT:1> ) ) , V . T ) ) , V ) <EOL> dalpha = dd * b - ( W * dd ) * np . dot ( RV . T , np . dot ( RV , ( dd * b ) ) ) - alpha <EOL> vargout = brentmin ( <NUM_LIT:0> , smax , Nline , thr , self . _Psi_lineFITC , <NUM_LIT:4> , dalpha , alpha , V , d0 , m , likfunc , y , inffunc ) <EOL> s = vargout [ <NUM_LIT:0> ] ; Psi_new = vargout [ <NUM_LIT:1> ] ; Nfun = vargout [ <NUM_LIT:2> ] ; alpha = vargout [ <NUM_LIT:3> ] <EOL> f = vargout [ <NUM_LIT:4> ] ; dlp = vargout [ <NUM_LIT:5> ] ; W = vargout [ <NUM_LIT:6> ] <EOL> isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> self . last_alpha = alpha <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , None , <NUM_LIT:4> ) <EOL> lp = vargout [ <NUM_LIT:0> ] ; dlp = vargout [ <NUM_LIT:1> ] ; d2lp = vargout [ <NUM_LIT:2> ] ; d3lp = vargout [ <NUM_LIT:3> ] <EOL> W = - d2lp ; isWneg = np . any ( W < <NUM_LIT:0> ) <EOL> post = postStruct ( ) <EOL> post . alpha = np . dot ( R0 . T , np . dot ( V , alpha ) ) <EOL> post . sW = np . sqrt ( np . abs ( W ) ) * np . sign ( W ) <EOL> dd = old_div ( <NUM_LIT:1> , ( <NUM_LIT:1> + d0 * W ) ) <EOL> A = np . eye ( nu ) + np . dot ( V * np . tile ( ( W * dd ) . T , ( nu , <NUM_LIT:1> ) ) , V . T ) <EOL> R0tV = np . dot ( R0 . T , V ) ; B = R0tV * np . tile ( ( W * dd ) . T , ( nu , <NUM_LIT:1> ) ) <EOL> post . L = - np . dot ( B , R0tV . T ) <EOL> if np . any ( <NUM_LIT:1> + d0 * W < <NUM_LIT:0> ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> nlZ = old_div ( np . dot ( alpha . T , ( f - m ) ) , <NUM_LIT> ) - lp . sum ( ) - old_div ( np . log ( dd ) . sum ( ) , <NUM_LIT> ) + np . log ( np . diag ( jitchol ( A ) . T ) ) . sum ( ) <EOL> RV = np . dot ( chol_inv ( A ) , V ) <EOL> RVdd = RV * np . tile ( ( W * dd ) . T , ( nu , <NUM_LIT:1> ) ) <EOL> B = np . dot ( B , RV . T ) <EOL> post . L += np . dot ( B , B . T ) <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> [ d , P , R ] = self . _fitcRefresh ( d0 , Ku , R0 , V , W ) <EOL> g = old_div ( d , <NUM_LIT:2> ) + <NUM_LIT:0.5> * np . atleast_2d ( ( np . dot ( np . dot ( R , R0 ) , P ) ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) ) . T <EOL> t = old_div ( W , ( <NUM_LIT:1> + W * d0 ) ) <EOL> dfhat = g * d3lp <EOL> for ii in range ( len ( covfunc . hyp ) ) : <EOL> ddiagK , dKuu , dKu = covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = ii ) <EOL> dA = <NUM_LIT> * dKu . T - np . dot ( R0tV . T , dKuu ) <EOL> w = np . atleast_2d ( ( dA * R0tV . T ) . sum ( axis = <NUM_LIT:1> ) ) . T <EOL> v = ddiagK - w <EOL> dnlZ . cov [ ii ] = np . dot ( ddiagK . T , t ) - np . dot ( ( RVdd * RVdd ) . sum ( axis = <NUM_LIT:0> ) , v ) <EOL> dnlZ . cov [ ii ] -= ( np . dot ( RVdd , dA ) * np . dot ( RVdd , R0tV . T ) ) . sum ( ) <EOL> dnlZ . cov [ ii ] = <NUM_LIT:0.5> * dnlZ . cov [ ii ] - old_div ( np . dot ( alpha . T , np . dot ( dA , np . dot ( R0tV , alpha ) ) + v * alpha ) , <NUM_LIT> ) <EOL> b = np . dot ( dA , np . dot ( R0tV , dlp ) ) + v * dlp <EOL> KZb = self . _mvmK ( self . _mvmZ ( b , RVdd , t ) , V , d0 ) <EOL> dnlZ . cov [ ii ] -= np . dot ( dfhat . T , ( b - KZb ) ) <EOL> dnlZ . cov [ ii ] = dnlZ . cov [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for ii in range ( len ( likfunc . hyp ) ) : <EOL> vargout = likfunc . evaluate ( y , f , None , inffunc , ii , <NUM_LIT:3> ) <EOL> lp_dhyp = vargout [ <NUM_LIT:0> ] ; dlp_dhyp = vargout [ <NUM_LIT:1> ] ; d2lp_dhyp = vargout [ <NUM_LIT:2> ] <EOL> dnlZ . lik [ ii ] = - np . dot ( g . T , d2lp_dhyp ) - lp_dhyp . sum ( ) <EOL> b = self . _mvmK ( dlp_dhyp , V , d0 ) <EOL> dnlZ . lik [ ii ] -= np . dot ( dfhat . T , b - self . _mvmK ( self . _mvmZ ( b , RVdd , t ) , V , d0 ) ) <EOL> if ii == len ( likfunc . hyp ) - <NUM_LIT:1> : <EOL> snu = np . sqrt ( snu2 ) ; <EOL> T = chol_inv ( Kuu + snu2 * np . eye ( nu ) ) ; <EOL> T = np . dot ( T . T , np . dot ( T , snu * Ku ) ) ; <EOL> t = np . array ( [ ( T * T ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> z = np . dot ( alpha . T , np . dot ( T . T , np . dot ( T , alpha ) ) - t * alpha ) - np . dot ( np . array ( [ ( RVdd * RVdd ) . sum ( axis = <NUM_LIT:0> ) ] ) , t ) <EOL> z += ( np . dot ( RVdd , T . T ) ** <NUM_LIT:2> ) . sum ( ) <EOL> b = old_div ( ( t * dlp - np . dot ( T . T , np . dot ( T , dlp ) ) ) , <NUM_LIT> ) <EOL> KZb = self . _mvmK ( self . _mvmZ ( b , RVdd , t ) , V , d0 ) <EOL> z -= np . dot ( dfhat . T , b - KZb ) <EOL> dnlZ . lik [ ii ] += z <EOL> dnlZ . lik [ ii ] = dnlZ . lik [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dm = meanfunc . getDerMatrix ( x , ii ) <EOL> dnlZ . mean [ ii ] = - np . dot ( alpha . T , dm ) <EOL> Zdm = self . _mvmZ ( dm , RVdd , t ) <EOL> dnlZ . mean [ ii ] -= np . dot ( dfhat . T , ( dm - self . _mvmK ( Zdm , V , d0 ) ) ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] , dnlZ <EOL> else : <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> class EP ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> self . last_ttau = None <EOL> self . last_tnu = None <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> tol = <NUM_LIT> ; max_sweep = <NUM_LIT:10> ; min_sweep = <NUM_LIT:2> <EOL> n = x . shape [ <NUM_LIT:0> ] <EOL> inffunc = self <EOL> K = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> nlZ0 = - likfunc . evaluate ( y , m , np . reshape ( np . diag ( K ) , ( np . diag ( K ) . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) , inffunc ) . sum ( ) <EOL> if self . last_ttau is None : <EOL> ttau = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> tnu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> Sigma = K <EOL> mu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> nlZ = nlZ0 <EOL> else : <EOL> ttau = self . last_ttau <EOL> tnu = self . last_tnu <EOL> Sigma , mu , nlZ , L = self . _epComputeParams ( K , y , ttau , tnu , likfunc , m , inffunc ) <EOL> if nlZ > nlZ0 : <EOL> ttau = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> tnu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> Sigma = K <EOL> mu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> nlZ = nlZ0 <EOL> nlZ_old = np . inf ; sweep = <NUM_LIT:0> <EOL> while ( np . abs ( nlZ - nlZ_old ) > tol and sweep < max_sweep ) or ( sweep < min_sweep ) : <EOL> nlZ_old = nlZ ; sweep += <NUM_LIT:1> <EOL> rperm = range ( n ) <EOL> for ii in rperm : <EOL> tau_ni = old_div ( <NUM_LIT:1> , Sigma [ ii , ii ] ) - ttau [ ii ] <EOL> nu_ni = old_div ( mu [ ii ] , Sigma [ ii , ii ] ) + m [ ii ] * tau_ni - tnu [ ii ] <EOL> lZ , dlZ , d2lZ = likfunc . evaluate ( y [ ii ] , old_div ( nu_ni , tau_ni ) , old_div ( <NUM_LIT:1> , tau_ni ) , inffunc , None , <NUM_LIT:3> ) <EOL> ttau_old = copy ( ttau [ ii ] ) <EOL> ttau [ ii ] = old_div ( - d2lZ , ( <NUM_LIT:1.> + old_div ( d2lZ , tau_ni ) ) ) <EOL> ttau [ ii ] = max ( ttau [ ii ] , <NUM_LIT:0> ) <EOL> tnu [ ii ] = old_div ( ( dlZ + ( m [ ii ] - old_div ( nu_ni , tau_ni ) ) * d2lZ ) , ( <NUM_LIT:1.> + old_div ( d2lZ , tau_ni ) ) ) <EOL> ds2 = ttau [ ii ] - ttau_old <EOL> si = np . reshape ( Sigma [ : , ii ] , ( Sigma . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> Sigma = Sigma - ds2 / ( <NUM_LIT:1.> + ds2 * si [ ii ] ) * np . dot ( si , si . T ) <EOL> mu = np . dot ( Sigma , tnu ) <EOL> Sigma , mu , nlZ , L = self . _epComputeParams ( K , y , ttau , tnu , likfunc , m , inffunc ) <EOL> if sweep == max_sweep : <EOL> pass <EOL> self . last_ttau = ttau ; self . last_tnu = tnu <EOL> sW = np . sqrt ( ttau ) ; alpha = tnu - sW * solve_chol ( L , sW * np . dot ( K , tnu ) ) <EOL> post = postStruct ( ) <EOL> post . alpha = alpha <EOL> post . sW = sW <EOL> post . L = L <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> ssi = np . sqrt ( ttau ) <EOL> V = np . linalg . solve ( L . T , np . tile ( ssi , ( <NUM_LIT:1> , n ) ) * K ) <EOL> Sigma = K - np . dot ( V . T , V ) <EOL> mu = np . dot ( Sigma , tnu ) <EOL> Dsigma = np . reshape ( np . diag ( Sigma ) , ( np . diag ( Sigma ) . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> tau_n = old_div ( <NUM_LIT:1> , Dsigma ) - ttau <EOL> nu_n = old_div ( mu , Dsigma ) - tnu <EOL> F = np . dot ( alpha , alpha . T ) - np . tile ( sW , ( <NUM_LIT:1> , n ) ) * solve_chol ( L , np . diag ( np . reshape ( sW , ( sW . shape [ <NUM_LIT:0> ] , ) ) ) ) <EOL> for jj in range ( len ( covfunc . hyp ) ) : <EOL> dK = covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = jj ) <EOL> dnlZ . cov [ jj ] = old_div ( - ( F * dK ) . sum ( ) , <NUM_LIT> ) <EOL> for ii in range ( len ( likfunc . hyp ) ) : <EOL> dlik = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) , old_div ( <NUM_LIT:1> , tau_n ) , inffunc , ii ) <EOL> dnlZ . lik [ ii ] = - dlik . sum ( ) <EOL> junk , dlZ = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) , old_div ( <NUM_LIT:1> , tau_n ) , inffunc , None , <NUM_LIT:2> ) <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dm = meanfunc . getDerMatrix ( x , ii ) <EOL> dnlZ . mean [ ii ] = - np . dot ( dlZ . T , dm ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> ] , dnlZ <EOL> else : <EOL> return post , nlZ [ <NUM_LIT:0> ] <EOL> class FITC_EP ( Inference ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> self . last_ttau = None <EOL> self . last_tnu = None <EOL> def evaluate ( self , meanfunc , covfunc , likfunc , x , y , nargout = <NUM_LIT:1> ) : <EOL> if not isinstance ( covfunc , cov . FITCOfKernel ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> tol = <NUM_LIT> ; max_sweep = <NUM_LIT:10> ; min_sweep = <NUM_LIT:2> <EOL> inffunc = EP ( ) <EOL> diagK , Kuu , Ku = covfunc . getCovMatrix ( x = x , mode = '<STR_LIT:train>' ) <EOL> m = meanfunc . getMean ( x ) <EOL> if likfunc . hyp : <EOL> sn2 = np . exp ( <NUM_LIT> * likfunc . hyp [ - <NUM_LIT:1> ] ) <EOL> snu2 = <NUM_LIT> * sn2 <EOL> else : <EOL> snu2 = <NUM_LIT> <EOL> n , D = x . shape ; nu = Kuu . shape [ <NUM_LIT:0> ] <EOL> rot180 = lambda A : np . rot90 ( np . rot90 ( A ) ) <EOL> chol_inv = lambda A : rot180 ( np . linalg . solve ( jitchol ( rot180 ( A ) ) , np . eye ( nu ) ) ) <EOL> R0 = chol_inv ( Kuu + snu2 * np . eye ( nu ) ) <EOL> V = np . dot ( R0 , Ku ) ; d0 = diagK - np . array ( [ ( V * V ) . sum ( axis = <NUM_LIT:0> ) ] ) . T <EOL> nlZ0 = - <NUM_LIT:1.> * likfunc . evaluate ( y , m , np . reshape ( diagK , ( diagK . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) , inffunc ) . sum ( ) <EOL> if self . last_ttau is None : <EOL> ttau = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> tnu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> [ d , P , R , nn , gg ] = self . _epfitcRefresh ( d0 , Ku , R0 , V , ttau , tnu ) <EOL> nlZ = nlZ0 <EOL> else : <EOL> ttau = self . last_ttau <EOL> tnu = self . last_tnu <EOL> [ d , P , R , nn , gg ] = self . _epfitcRefresh ( d0 , Ku , R0 , V , ttau , tnu ) <EOL> nlZ = self . _epfitcZ ( d , P , R , nn , gg , ttau , tnu , d0 , R0 , Ku , y , likfunc , m , inffunc ) [ <NUM_LIT:0> ] <EOL> if nlZ > nlZ0 : <EOL> ttau = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> tnu = np . zeros ( ( n , <NUM_LIT:1> ) ) <EOL> [ d , P , R , nn , gg ] = self . _epfitcRefresh ( d0 , Ku , R0 , V , ttau , tnu ) <EOL> nlZ = nlZ0 <EOL> nlZ_old = np . inf ; sweep = <NUM_LIT:0> <EOL> while ( np . abs ( nlZ - nlZ_old ) > tol and sweep < max_sweep ) or ( sweep < min_sweep ) : <EOL> nlZ_old = nlZ <EOL> sweep += <NUM_LIT:1> <EOL> rperm = list ( range ( n ) ) <EOL> for ii in rperm : <EOL> p_i = np . reshape ( P [ : , ii ] , ( P . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> t = np . dot ( R , np . dot ( R0 , p_i ) ) <EOL> sigma_i = d [ ii ] + np . dot ( t . T , t ) ; mu_i = nn [ ii ] + np . dot ( p_i . T , gg ) <EOL> tau_ni = old_div ( <NUM_LIT:1> , sigma_i ) - ttau [ ii ] <EOL> nu_ni = old_div ( mu_i , sigma_i ) + m [ ii ] * tau_ni - tnu [ ii ] <EOL> vargout = likfunc . evaluate ( y [ ii ] , old_div ( nu_ni , tau_ni ) , old_div ( <NUM_LIT:1> , tau_ni ) , inffunc , None , <NUM_LIT:3> ) <EOL> lZ = vargout [ <NUM_LIT:0> ] ; dlZ = vargout [ <NUM_LIT:1> ] ; d2lZ = vargout [ <NUM_LIT:2> ] <EOL> ttau_i = old_div ( - d2lZ , ( <NUM_LIT:1.> + old_div ( d2lZ , tau_ni ) ) ) <EOL> ttau_i = max ( ttau_i , <NUM_LIT:0> ) <EOL> tnu_i = old_div ( ( dlZ + ( m [ ii ] - old_div ( nu_ni , tau_ni ) ) * d2lZ ) , ( <NUM_LIT:1.> + old_div ( d2lZ , tau_ni ) ) ) <EOL> [ d , P [ : , ii ] , R , nn , gg , ttau , tnu ] = self . _epfitcUpdate ( d , P [ : , ii ] , R , nn , gg , ttau , tnu , ii , ttau_i , tnu_i , m , d0 , Ku , R0 ) <EOL> [ d , P , R , nn , gg ] = self . _epfitcRefresh ( d0 , Ku , R0 , V , ttau , tnu ) <EOL> [ nlZ , nu_n , tau_n ] = self . _epfitcZ ( d , P , R , nn , gg , ttau , tnu , d0 , R0 , Ku , y , likfunc , m , inffunc ) <EOL> if sweep == max_sweep : <EOL> pass <EOL> self . last_ttau = ttau <EOL> self . last_tnu = tnu <EOL> post = postStruct ( ) <EOL> post . sW = np . sqrt ( ttau ) <EOL> dd = old_div ( <NUM_LIT:1> , ( d0 + old_div ( <NUM_LIT:1> , ttau ) ) ) <EOL> alpha = tnu / ttau * dd <EOL> RV = np . dot ( R , V ) <EOL> R0tV = np . dot ( R0 . T , V ) <EOL> alpha = alpha - np . dot ( RV . T , np . dot ( RV , alpha ) ) * dd <EOL> post . alpha = np . dot ( R0tV , alpha ) <EOL> B = R0tV * np . tile ( dd . T , ( nu , <NUM_LIT:1> ) ) ; L = np . dot ( B , R0tV . T ) ; B = np . dot ( B , RV . T ) <EOL> post . L = np . dot ( B , B . T ) - L <EOL> if nargout > <NUM_LIT:2> : <EOL> dnlZ = dnlZStruct ( meanfunc , covfunc , likfunc ) <EOL> RVdd = RV * np . tile ( dd . T , ( nu , <NUM_LIT:1> ) ) <EOL> for ii in range ( len ( covfunc . hyp ) ) : <EOL> ddiagK , dKuu , dKu = covfunc . getDerMatrix ( x = x , mode = '<STR_LIT:train>' , der = ii ) <EOL> dA = <NUM_LIT:2> * dKu . T - np . dot ( R0tV . T , dKuu ) <EOL> w = np . atleast_2d ( ( dA * R0tV . T ) . sum ( axis = <NUM_LIT:1> ) ) . T <EOL> v = ddiagK - w <EOL> z = np . dot ( dd . T , ( v + w ) ) - np . dot ( np . atleast_2d ( ( RVdd * RVdd ) . sum ( axis = <NUM_LIT:0> ) ) , v ) - ( np . dot ( RVdd , dA ) . T * np . dot ( R0tV , RVdd . T ) ) . sum ( ) <EOL> dnlZ . cov [ ii ] = old_div ( ( z - np . dot ( alpha . T , ( alpha * v ) ) - np . dot ( np . dot ( alpha . T , dA ) , np . dot ( R0tV , alpha ) ) ) , <NUM_LIT> ) <EOL> dnlZ . cov [ ii ] = dnlZ . cov [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for ii in range ( len ( likfunc . hyp ) ) : <EOL> dlik = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) + m , old_div ( <NUM_LIT:1> , tau_n ) , inffunc , ii , <NUM_LIT:1> ) <EOL> dnlZ . lik [ ii ] = - dlik . sum ( ) <EOL> if ii == len ( likfunc . hyp ) - <NUM_LIT:1> : <EOL> v = np . atleast_2d ( ( R0tV * R0tV ) . sum ( axis = <NUM_LIT:0> ) ) . T <EOL> z = ( np . dot ( RVdd , R0tV . T ) ** <NUM_LIT:2> ) . sum ( ) - np . dot ( np . atleast_2d ( ( RVdd * RVdd ) . sum ( axis = <NUM_LIT:0> ) ) , v ) <EOL> z = z + np . dot ( post . alpha . T , post . alpha ) - np . dot ( alpha . T , ( v * alpha ) ) <EOL> dnlZ . lik [ ii ] += snu2 * z <EOL> dnlZ . lik [ ii ] = dnlZ . lik [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> [ junk , dlZ ] = likfunc . evaluate ( y , old_div ( nu_n , tau_n ) , old_div ( <NUM_LIT:1> , tau_n ) , inffunc , None , <NUM_LIT:2> ) <EOL> for ii in range ( len ( meanfunc . hyp ) ) : <EOL> dm = meanfunc . getDerMatrix ( x , ii ) <EOL> dnlZ . mean [ ii ] = - np . dot ( dlZ . T , dm ) <EOL> dnlZ . mean [ ii ] = dnlZ . mean [ ii ] [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] , dnlZ <EOL> else : <EOL> return post , nlZ [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> pass </s>
