#NEWLINE# def LOO( self , kern , X , Y , likelihood , posterior , Y_metadata = None , K = None , f_hat = None , W = None , Ki_W_i = None ) : #NEWLINE# #INDENT# '\n        Leave one out log predictive density as found in\n        "Bayesian leave-one-out cross-validation approximations for Gaussian latent variable models"\n        Vehtari et al. 2014.\n        ' #NEWLINE# Ki_f_init = np . zeros_like ( Y ) #NEWLINE# if ( K is None ) : #NEWLINE# #INDENT# K = kern . K ( X ) #NEWLINE# #UNINDENT# if ( f_hat is None ) : #NEWLINE# #INDENT# ( f_hat , _ ) = self . rasm_mode ( K , Y , likelihood , Ki_f_init , Y_metadata = Y_metadata ) #NEWLINE# #UNINDENT# if ( W is None ) : #NEWLINE# #INDENT# W = ( - likelihood . d2logpdf_df2 ( f_hat , Y , Y_metadata = Y_metadata ) ) #NEWLINE# #UNINDENT# if ( Ki_W_i is None ) : #NEWLINE# #INDENT# ( _ , _ , _ , Ki_W_i ) = self . _compute_B_statistics ( K , W , likelihood . log_concave ) #NEWLINE# #UNINDENT# logpdf_dfhat = likelihood . dlogpdf_df ( f_hat , Y , Y_metadata = Y_metadata ) #NEWLINE# if ( W . shape [ 1 ] == 1 ) : #NEWLINE# #INDENT# W = np . diagflat ( W ) #NEWLINE# #UNINDENT# var_site = ( 1.0 / np . diag ( W ) [ : , None ] ) #NEWLINE# mu_site = ( f_hat + ( var_site * logpdf_dfhat ) ) #NEWLINE# prec_site = ( 1.0 / var_site ) #NEWLINE# marginal_cov = Ki_W_i #NEWLINE# marginal_mu = marginal_cov . dot ( np . diagflat ( prec_site ) ) . dot ( mu_site ) #NEWLINE# marginal_var = np . diag ( p_ystar ) [ : , None ] #NEWLINE# posterior_cav_var = ( 1.0 / ( ( 1.0 / marginal_var ) - ( 1.0 / var_site ) ) ) #NEWLINE# posterior_cav_mean = ( posterior_cav_var * ( ( ( 1.0 / marginal_var ) * marginal_mu ) - ( ( 1.0 / var_site ) * Y ) ) ) #NEWLINE# flat_y = Y . flatten ( ) #NEWLINE# flat_mu = posterior_cav_mean . flatten ( ) #NEWLINE# flat_var = posterior_cav_var . flatten ( ) #NEWLINE# if ( Y_metadata is not None ) : #NEWLINE# #INDENT# Y_metadata_flat = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if ( Y_metadata is not None ) : #NEWLINE# #INDENT# for ( key , val ) in Y_metadata . items ( ) : #NEWLINE# #INDENT# Y_metadata_flat [ key ] = np . atleast_1d ( val ) . reshape ( ( - 1 ) , 1 ) #NEWLINE# #UNINDENT# #UNINDENT# zipped_values = [ ] #NEWLINE# for i in range ( Y . shape [ 0 ] ) : #NEWLINE# #INDENT# y_m = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( key , val ) in Y_metadata_flat . items ( ) : #NEWLINE# #INDENT# if ( np . isscalar ( val ) or ( val . shape [ 0 ] == 1 ) ) : #NEWLINE# #INDENT# y_m [ key ] = val #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# y_m [ key ] = val [ i ] #NEWLINE# #UNINDENT# #UNINDENT# zipped_values . append ( ( flat_y [ i ] , flat_mu [ i ] , flat_var [ i ] , y_m ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# zipped_values = zip ( flat_y , flat_mu , flat_var , ( [ None ] * Y . shape [ 0 ] ) ) #NEWLINE# #UNINDENT# def integral_generator( yi , mi , vi , yi_m ) : #NEWLINE# #INDENT# def f( fi_star ) : #NEWLINE# #INDENT# p_fi = np . exp ( ( ( likelihood . logpdf ( fi_star , yi , yi_m ) - ( 0.5 * np . log ( ( ( 2 * np . pi ) * vi ) ) ) ) - ( ( 0.5 * np . square ( ( mi - fi_star ) ) ) / vi ) ) ) #NEWLINE# return p_fi #NEWLINE# #UNINDENT# return f #NEWLINE# #UNINDENT# ( p_ystar , _ ) = zip ( * [ quad ( integral_generator ( y , m , v , yi_m ) , ( - np . inf ) , np . inf ) for ( y , m , v , yi_m ) in zipped_values ] ) #NEWLINE# p_ystar = np . array ( p_ystar ) . reshape ( ( - 1 ) , 1 ) #NEWLINE# return np . log ( p_ystar )