#NEWLINE# def log_marginal_likelihood( self , theta = None , eval_gradient = False ) : #NEWLINE# #INDENT# 'Returns log-marginal likelihood of theta for training data.\n\n        Parameters\n        ----------\n        theta : array-like, shape = (n_kernel_params,) or None\n            Kernel hyperparameters for which the log-marginal likelihood is\n            evaluated. If None, the precomputed log_marginal_likelihood\n            of ``self.kernel_.theta`` is returned.\n\n        eval_gradient : bool, default: False\n            If True, the gradient of the log-marginal likelihood with respect\n            to the kernel hyperparameters at position theta is returned\n            additionally. If True, theta must not be None.\n\n        Returns\n        -------\n        log_likelihood : float\n            Log-marginal likelihood of theta for training data.\n\n        log_likelihood_gradient : array, shape = (n_kernel_params,), optional\n            Gradient of the log-marginal likelihood with respect to the kernel\n            hyperparameters at position theta.\n            Only returned when eval_gradient is True.\n        ' #NEWLINE# if ( theta is None ) : #NEWLINE# #INDENT# if eval_gradient : #NEWLINE# #INDENT# raise ValueError ( 'Gradient can only be evaluated for theta!=None' ) #NEWLINE# #UNINDENT# return self . log_marginal_likelihood_value_ #NEWLINE# #UNINDENT# kernel = self . kernel_ . clone_with_theta ( theta ) #NEWLINE# if eval_gradient : #NEWLINE# #INDENT# ( K , K_gradient ) = kernel ( self . X_train_ , eval_gradient = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# K = kernel ( self . X_train_ ) #NEWLINE# #UNINDENT# K [ np . diag_indices_from ( K ) ] += self . alpha #NEWLINE# try : #NEWLINE# #INDENT# L = cholesky ( K , lower = True ) #NEWLINE# #UNINDENT# except np . linalg . LinAlgError : #NEWLINE# #INDENT# return ( ( ( - np . inf ) , np . zeros_like ( theta ) ) if eval_gradient else ( - np . inf ) ) #NEWLINE# #UNINDENT# y_train = self . y_train_ #NEWLINE# if ( y_train . ndim == 1 ) : #NEWLINE# #INDENT# y_train = y_train [ : , np . newaxis ] #NEWLINE# #UNINDENT# alpha = cho_solve ( ( L , True ) , y_train ) #NEWLINE# log_likelihood_dims = ( ( - 0.5 ) * np . einsum ( 'ik,ik->k' , y_train , alpha ) ) #NEWLINE# log_likelihood_dims -= np . log ( np . diag ( L ) ) . sum ( ) #NEWLINE# log_likelihood_dims -= ( ( K . shape [ 0 ] / 2 ) * np . log ( ( 2 * np . pi ) ) ) #NEWLINE# log_likelihood = log_likelihood_dims . sum ( ( - 1 ) ) #NEWLINE# if eval_gradient : #NEWLINE# #INDENT# tmp = np . einsum ( 'ik,jk->ijk' , alpha , alpha ) #NEWLINE# tmp -= cho_solve ( ( L , True ) , np . eye ( K . shape [ 0 ] ) ) [ : , : , np . newaxis ] #NEWLINE# log_likelihood_gradient_dims = ( 0.5 * np . einsum ( 'ijl,ijk->kl' , tmp , K_gradient ) ) #NEWLINE# log_likelihood_gradient = log_likelihood_gradient_dims . sum ( ( - 1 ) ) #NEWLINE# #UNINDENT# if eval_gradient : #NEWLINE# #INDENT# return ( log_likelihood , log_likelihood_gradient ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return log_likelihood