<s> import numpy as np <EOL> from . . util . univariate_Gaussian import std_norm_pdf , std_norm_cdf <EOL> from . import link_functions <EOL> from . likelihood import Likelihood <EOL> class Bernoulli ( Likelihood ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , gp_link = None ) : <EOL> if gp_link is None : <EOL> gp_link = link_functions . Probit ( ) <EOL> super ( Bernoulli , self ) . __init__ ( gp_link , '<STR_LIT>' ) <EOL> if isinstance ( gp_link , ( link_functions . Heaviside , link_functions . Probit ) ) : <EOL> self . log_concave = True <EOL> def _preprocess_values ( self , Y ) : <EOL> """<STR_LIT>""" <EOL> Y_prep = Y . copy ( ) <EOL> Y1 = Y [ Y . flatten ( ) == <NUM_LIT:1> ] . size <EOL> Y2 = Y [ Y . flatten ( ) == <NUM_LIT:0> ] . size <EOL> assert Y1 + Y2 == Y . size , '<STR_LIT>' <EOL> Y_prep [ Y . flatten ( ) == <NUM_LIT:0> ] = - <NUM_LIT:1> <EOL> return Y_prep <EOL> def moments_match_ep ( self , Y_i , tau_i , v_i , Y_metadata_i = None ) : <EOL> """<STR_LIT>""" <EOL> if Y_i == <NUM_LIT:1> : <EOL> sign = <NUM_LIT:1.> <EOL> elif Y_i == <NUM_LIT:0> or Y_i == - <NUM_LIT:1> : <EOL> sign = - <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if isinstance ( self . gp_link , link_functions . Probit ) : <EOL> z = sign * v_i / np . sqrt ( tau_i ** <NUM_LIT:2> + tau_i ) <EOL> Z_hat = std_norm_cdf ( z ) <EOL> Z_hat = np . where ( Z_hat == <NUM_LIT:0> , <NUM_LIT> , Z_hat ) <EOL> phi = std_norm_pdf ( z ) <EOL> mu_hat = v_i / tau_i + sign * phi / ( Z_hat * np . sqrt ( tau_i ** <NUM_LIT:2> + tau_i ) ) <EOL> sigma2_hat = <NUM_LIT:1.> / tau_i - ( phi / ( ( tau_i ** <NUM_LIT:2> + tau_i ) * Z_hat ) ) * ( z + phi / Z_hat ) <EOL> elif isinstance ( self . gp_link , link_functions . Heaviside ) : <EOL> a = sign * v_i / np . sqrt ( tau_i ) <EOL> Z_hat = np . max ( <NUM_LIT> , std_norm_cdf ( z ) ) <EOL> N = std_norm_pdf ( a ) <EOL> mu_hat = v_i / tau_i + sign * N / Z_hat / np . sqrt ( tau_i ) <EOL> sigma2_hat = ( <NUM_LIT:1.> - a * N / Z_hat - np . square ( N / Z_hat ) ) / tau_i <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( self . gp_link . __name__ ) ) <EOL> return Z_hat , mu_hat , sigma2_hat <EOL> def variational_expectations ( self , Y , m , v , gh_points = None , Y_metadata = None ) : <EOL> if isinstance ( self . gp_link , link_functions . Probit ) : <EOL> if gh_points is None : <EOL> gh_x , gh_w = self . _gh_points ( ) <EOL> else : <EOL> gh_x , gh_w = gh_points <EOL> gh_w = gh_w / np . sqrt ( np . pi ) <EOL> shape = m . shape <EOL> m , v , Y = m . flatten ( ) , v . flatten ( ) , Y . flatten ( ) <EOL> Ysign = np . where ( Y == <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> X = gh_x [ None , : ] * np . sqrt ( <NUM_LIT> * v [ : , None ] ) + ( m * Ysign ) [ : , None ] <EOL> p = std_norm_cdf ( X ) <EOL> p = np . clip ( p , <NUM_LIT> , <NUM_LIT:1.> - <NUM_LIT> ) <EOL> N = std_norm_pdf ( X ) <EOL> F = np . log ( p ) . dot ( gh_w ) <EOL> NoverP = N / p <EOL> dF_dm = ( NoverP * Ysign [ : , None ] ) . dot ( gh_w ) <EOL> dF_dv = - <NUM_LIT:0.5> * ( NoverP ** <NUM_LIT:2> + NoverP * X ) . dot ( gh_w ) <EOL> return F . reshape ( * shape ) , dF_dm . reshape ( * shape ) , dF_dv . reshape ( * shape ) , None <EOL> else : <EOL> raise NotImplementedError <EOL> def predictive_mean ( self , mu , variance , Y_metadata = None ) : <EOL> if isinstance ( self . gp_link , link_functions . Probit ) : <EOL> return std_norm_cdf ( mu / np . sqrt ( <NUM_LIT:1> + variance ) ) <EOL> elif isinstance ( self . gp_link , link_functions . Heaviside ) : <EOL> return std_norm_cdf ( mu / np . sqrt ( variance ) ) <EOL> else : <EOL> raise NotImplementedError <EOL> def predictive_variance ( self , mu , variance , pred_mean , Y_metadata = None ) : <EOL> if isinstance ( self . gp_link , link_functions . Heaviside ) : <EOL> return <NUM_LIT:0.> <EOL> else : <EOL> return np . nan <EOL> def pdf_link ( self , inv_link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> return np . where ( y == <NUM_LIT:1> , inv_link_f , <NUM_LIT:1.> - inv_link_f ) <EOL> def logpdf_link ( self , inv_link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> p = np . where ( y == <NUM_LIT:1> , inv_link_f , <NUM_LIT:1.> - inv_link_f ) <EOL> return np . log ( np . clip ( p , <NUM_LIT> , np . inf ) ) <EOL> def dlogpdf_dlink ( self , inv_link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> ff = np . clip ( inv_link_f , <NUM_LIT> , <NUM_LIT:1> - <NUM_LIT> ) <EOL> denom = np . where ( y == <NUM_LIT:1> , ff , - ( <NUM_LIT:1> - ff ) ) <EOL> return <NUM_LIT:1.> / denom <EOL> def d2logpdf_dlink2 ( self , inv_link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> arg = np . where ( y == <NUM_LIT:1> , inv_link_f , <NUM_LIT:1.> - inv_link_f ) <EOL> ret = - <NUM_LIT:1.> / np . square ( np . clip ( arg , <NUM_LIT> , <NUM_LIT> ) ) <EOL> if np . any ( np . isinf ( ret ) ) : <EOL> stop <EOL> return ret <EOL> def d3logpdf_dlink3 ( self , inv_link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> assert np . atleast_1d ( inv_link_f ) . shape == np . atleast_1d ( y ) . shape <EOL> state = np . seterr ( divide = '<STR_LIT:ignore>' ) <EOL> d3logpdf_dlink3 = np . where ( y == <NUM_LIT:1> , <NUM_LIT> / ( inv_link_f ** <NUM_LIT:3> ) , - <NUM_LIT> / ( ( <NUM_LIT:1.> - inv_link_f ) ** <NUM_LIT:3> ) ) <EOL> np . seterr ( ** state ) <EOL> return d3logpdf_dlink3 <EOL> def predictive_quantiles ( self , mu , var , quantiles , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> p = self . predictive_mean ( mu , var ) <EOL> return [ np . asarray ( p > ( q / <NUM_LIT> ) , dtype = np . int32 ) for q in quantiles ] <EOL> def samples ( self , gp , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> orig_shape = gp . shape <EOL> gp = gp . flatten ( ) <EOL> ns = np . ones_like ( gp , dtype = int ) <EOL> Ysim = np . random . binomial ( ns , self . gp_link . transf ( gp ) ) <EOL> return Ysim . reshape ( orig_shape ) <EOL> def exact_inference_gradients ( self , dL_dKdiag , Y_metadata = None ) : <EOL> return np . zeros ( self . size ) </s>
