#NEWLINE# def rasm_mode( self , K , Y , likelihood , Ki_f_init , Y_metadata = None , * args , **kwargs ) : #NEWLINE# #INDENT# Ki_f = Ki_f_init . copy ( ) #NEWLINE# f = np . dot ( K , Ki_f ) #NEWLINE# def obj( Ki_f , f ) : #NEWLINE# #INDENT# ll = ( ( ( - 0.5 ) * np . dot ( Ki_f . T , f ) ) + np . sum ( likelihood . logpdf_sum ( f , Y , Y_metadata = Y_metadata ) ) ) #NEWLINE# if np . isnan ( ll ) : #NEWLINE# #INDENT# return ( - np . inf ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ll #NEWLINE# #UNINDENT# #UNINDENT# difference = np . inf #NEWLINE# iteration = 0 #NEWLINE# I = np . eye ( K . shape [ 0 ] ) #NEWLINE# while ( ( difference > self . _mode_finding_tolerance ) and ( iteration < self . _mode_finding_max_iter ) ) : #NEWLINE# #INDENT# W = ( - likelihood . d2logpdf_df2 ( f , Y , Y_metadata = Y_metadata ) ) #NEWLINE# W [ np . diag_indices_from ( W ) ] = np . clip ( np . diag ( W ) , 1e-06 , 1e+30 ) #NEWLINE# W_f = np . dot ( W , f ) #NEWLINE# grad = likelihood . dlogpdf_df ( f , Y , Y_metadata = Y_metadata ) #NEWLINE# b = ( W_f + grad ) #NEWLINE# ( K_Wi_i , _ , _ , _ ) = self . _compute_B_statistics ( K , W , likelihood . log_concave , * args , ** kwargs ) #NEWLINE# full_step_Ki_f = np . dot ( ( I - np . dot ( K_Wi_i , K ) ) , b ) #NEWLINE# dKi_f = ( full_step_Ki_f - Ki_f ) #NEWLINE# def inner_obj( step_size ) : #NEWLINE# #INDENT# Ki_f_trial = ( Ki_f + ( step_size * dKi_f ) ) #NEWLINE# f_trial = np . dot ( K , Ki_f_trial ) #NEWLINE# return ( - obj ( Ki_f_trial , f_trial ) ) #NEWLINE# #UNINDENT# step = optimize . brent ( inner_obj , tol = 0.0001 , maxiter = 12 ) #NEWLINE# Ki_f_new = ( Ki_f + ( step * dKi_f ) ) #NEWLINE# f_new = np . dot ( K , Ki_f_new ) #NEWLINE# difference = ( np . abs ( np . sum ( ( f_new - f ) ) ) + np . abs ( np . sum ( ( Ki_f_new - Ki_f ) ) ) ) #NEWLINE# Ki_f = Ki_f_new #NEWLINE# f = f_new #NEWLINE# iteration += 1 #NEWLINE# #UNINDENT# if ( W > self . _mode_finding_tolerance ) : #NEWLINE# #INDENT# if ( not self . bad_fhat ) : #NEWLINE# #INDENT# warnings . warn ( 'Not perfect f_hat fit difference: {}' . format ( difference ) ) #NEWLINE# #UNINDENT# self . _previous_Ki_fhat = np . zeros_like ( Y ) #NEWLINE# self . bad_fhat = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if self . bad_fhat : #NEWLINE# #INDENT# self . bad_fhat = False #NEWLINE# warnings . warn ( 'f_hat now fine again' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( iteration > self . _mode_finding_max_iter ) : #NEWLINE# #INDENT# warnings . warn ( "didn't find the best" ) #NEWLINE# #UNINDENT# return ( f , Ki_f )