<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from scipy import stats , special <EOL> from . import link_functions <EOL> from . likelihood import Likelihood <EOL> from . . core . parameterization import Param <EOL> from paramz . transformations import Logexp <EOL> from scipy import stats <EOL> class Gaussian ( Likelihood ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , gp_link = None , variance = <NUM_LIT:1.> , name = '<STR_LIT>' ) : <EOL> if gp_link is None : <EOL> gp_link = link_functions . Identity ( ) <EOL> if not isinstance ( gp_link , link_functions . Identity ) : <EOL> print ( "<STR_LIT>" ) <EOL> super ( Gaussian , self ) . __init__ ( gp_link , name = name ) <EOL> self . variance = Param ( '<STR_LIT>' , variance , Logexp ( ) ) <EOL> self . link_parameter ( self . variance ) <EOL> if isinstance ( gp_link , link_functions . Identity ) : <EOL> self . log_concave = True <EOL> def betaY ( self , Y , Y_metadata = None ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return Y / self . gaussian_variance ( Y_metadata ) <EOL> def gaussian_variance ( self , Y_metadata = None ) : <EOL> return self . variance <EOL> def update_gradients ( self , grad ) : <EOL> self . variance . gradient = grad <EOL> def exact_inference_gradients ( self , dL_dKdiag , Y_metadata = None ) : <EOL> return dL_dKdiag . sum ( ) <EOL> def _preprocess_values ( self , Y ) : <EOL> """<STR_LIT>""" <EOL> return Y <EOL> def moments_match_ep ( self , data_i , tau_i , v_i , Y_metadata_i = None ) : <EOL> """<STR_LIT>""" <EOL> sigma2_hat = <NUM_LIT:1.> / ( <NUM_LIT:1.> / self . variance + tau_i ) <EOL> mu_hat = sigma2_hat * ( data_i / self . variance + v_i ) <EOL> sum_var = self . variance + <NUM_LIT:1.> / tau_i <EOL> Z_hat = <NUM_LIT:1.> / np . sqrt ( <NUM_LIT> * np . pi * sum_var ) * np . exp ( - <NUM_LIT> * ( data_i - v_i / tau_i ) ** <NUM_LIT> / sum_var ) <EOL> return Z_hat , mu_hat , sigma2_hat <EOL> def predictive_values ( self , mu , var , full_cov = False , Y_metadata = None ) : <EOL> if full_cov : <EOL> if var . ndim == <NUM_LIT:2> : <EOL> var += np . eye ( var . shape [ <NUM_LIT:0> ] ) * self . variance <EOL> if var . ndim == <NUM_LIT:3> : <EOL> var += np . atleast_3d ( np . eye ( var . shape [ <NUM_LIT:0> ] ) * self . variance ) <EOL> else : <EOL> var += self . variance <EOL> return mu , var <EOL> def predictive_mean ( self , mu , sigma ) : <EOL> return mu <EOL> def predictive_variance ( self , mu , sigma , predictive_mean = None ) : <EOL> return self . variance + sigma ** <NUM_LIT:2> <EOL> def predictive_quantiles ( self , mu , var , quantiles , Y_metadata = None ) : <EOL> return [ stats . norm . ppf ( q / <NUM_LIT> ) * np . sqrt ( var + self . variance ) + mu for q in quantiles ] <EOL> def pdf_link ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> return np . exp ( np . sum ( np . log ( stats . norm . pdf ( y , link_f , np . sqrt ( self . variance ) ) ) ) ) <EOL> def logpdf_link ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> ln_det_cov = np . log ( self . variance ) <EOL> return - ( <NUM_LIT:1.0> / ( <NUM_LIT:2> * self . variance ) ) * ( ( y - link_f ) ** <NUM_LIT:2> ) - <NUM_LIT:0.5> * ln_det_cov - <NUM_LIT:0.5> * np . log ( <NUM_LIT> * np . pi ) <EOL> def dlogpdf_dlink ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> s2_i = <NUM_LIT:1.0> / self . variance <EOL> grad = s2_i * y - s2_i * link_f <EOL> return grad <EOL> def d2logpdf_dlink2 ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> N = y . shape [ <NUM_LIT:0> ] <EOL> D = link_f . shape [ <NUM_LIT:1> ] <EOL> hess = - ( <NUM_LIT:1.0> / self . variance ) * np . ones ( ( N , D ) ) <EOL> return hess <EOL> def d3logpdf_dlink3 ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> N = y . shape [ <NUM_LIT:0> ] <EOL> D = link_f . shape [ <NUM_LIT:1> ] <EOL> d3logpdf_dlink3 = np . zeros ( ( N , D ) ) <EOL> return d3logpdf_dlink3 <EOL> def dlogpdf_link_dvar ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> e = y - link_f <EOL> s_4 = <NUM_LIT:1.0> / ( self . variance ** <NUM_LIT:2> ) <EOL> dlik_dsigma = - <NUM_LIT:0.5> / self . variance + <NUM_LIT:0.5> * s_4 * np . square ( e ) <EOL> return dlik_dsigma <EOL> def dlogpdf_dlink_dvar ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> s_4 = <NUM_LIT:1.0> / ( self . variance ** <NUM_LIT:2> ) <EOL> dlik_grad_dsigma = - s_4 * y + s_4 * link_f <EOL> return dlik_grad_dsigma <EOL> def d2logpdf_dlink2_dvar ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> s_4 = <NUM_LIT:1.0> / ( self . variance ** <NUM_LIT:2> ) <EOL> N = y . shape [ <NUM_LIT:0> ] <EOL> D = link_f . shape [ <NUM_LIT:1> ] <EOL> d2logpdf_dlink2_dvar = np . ones ( ( N , D ) ) * s_4 <EOL> return d2logpdf_dlink2_dvar <EOL> def dlogpdf_link_dtheta ( self , f , y , Y_metadata = None ) : <EOL> dlogpdf_dtheta = np . zeros ( ( self . size , f . shape [ <NUM_LIT:0> ] , f . shape [ <NUM_LIT:1> ] ) ) <EOL> dlogpdf_dtheta [ <NUM_LIT:0> , : , : ] = self . dlogpdf_link_dvar ( f , y , Y_metadata = Y_metadata ) <EOL> return dlogpdf_dtheta <EOL> def dlogpdf_dlink_dtheta ( self , f , y , Y_metadata = None ) : <EOL> dlogpdf_dlink_dtheta = np . zeros ( ( self . size , f . shape [ <NUM_LIT:0> ] , f . shape [ <NUM_LIT:1> ] ) ) <EOL> dlogpdf_dlink_dtheta [ <NUM_LIT:0> , : , : ] = self . dlogpdf_dlink_dvar ( f , y , Y_metadata = Y_metadata ) <EOL> return dlogpdf_dlink_dtheta <EOL> def d2logpdf_dlink2_dtheta ( self , f , y , Y_metadata = None ) : <EOL> d2logpdf_dlink2_dtheta = np . zeros ( ( self . size , f . shape [ <NUM_LIT:0> ] , f . shape [ <NUM_LIT:1> ] ) ) <EOL> d2logpdf_dlink2_dtheta [ <NUM_LIT:0> , : , : ] = self . d2logpdf_dlink2_dvar ( f , y , Y_metadata = Y_metadata ) <EOL> return d2logpdf_dlink2_dtheta <EOL> def _mean ( self , gp ) : <EOL> """<STR_LIT>""" <EOL> return self . gp_link . transf ( gp ) <EOL> def _variance ( self , gp ) : <EOL> """<STR_LIT>""" <EOL> return self . variance <EOL> def samples ( self , gp , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> orig_shape = gp . shape <EOL> gp = gp . flatten ( ) <EOL> gp = gp . flatten ( ) <EOL> Ysim = np . array ( [ np . random . normal ( self . gp_link . transf ( gpj ) , scale = np . sqrt ( self . variance ) , size = <NUM_LIT:1> ) for gpj in gp ] ) <EOL> return Ysim . reshape ( orig_shape ) <EOL> def log_predictive_density ( self , y_test , mu_star , var_star , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> v = var_star + self . variance <EOL> return - <NUM_LIT:0.5> * np . log ( <NUM_LIT:2> * np . pi ) - <NUM_LIT:0.5> * np . log ( v ) - <NUM_LIT:0.5> * np . square ( y_test - mu_star ) / v <EOL> def variational_expectations ( self , Y , m , v , gh_points = None , Y_metadata = None ) : <EOL> if not isinstance ( self . gp_link , link_functions . Identity ) : <EOL> return super ( Gaussian , self ) . variational_expectations ( Y = Y , m = m , v = v , gh_points = gh_points , Y_metadata = Y_metadata ) <EOL> lik_var = float ( self . variance ) <EOL> F = - <NUM_LIT:0.5> * np . log ( <NUM_LIT:2> * np . pi ) - <NUM_LIT:0.5> * np . log ( lik_var ) - <NUM_LIT:0.5> * ( np . square ( Y ) + np . square ( m ) + v - <NUM_LIT:2> * m * Y ) / lik_var <EOL> dF_dmu = ( Y - m ) / lik_var <EOL> dF_dv = np . ones_like ( v ) * ( - <NUM_LIT:0.5> / lik_var ) <EOL> dF_dtheta = - <NUM_LIT:0.5> / lik_var + <NUM_LIT:0.5> * ( np . square ( Y ) + np . square ( m ) + v - <NUM_LIT:2> * m * Y ) / ( lik_var ** <NUM_LIT:2> ) <EOL> return F , dF_dmu , dF_dv , dF_dtheta . reshape ( <NUM_LIT:1> , Y . shape [ <NUM_LIT:0> ] , Y . shape [ <NUM_LIT:1> ] ) <EOL> class HeteroscedasticGaussian ( Gaussian ) : <EOL> def __init__ ( self , Y_metadata , gp_link = None , variance = <NUM_LIT:1.> , name = '<STR_LIT>' ) : <EOL> if gp_link is None : <EOL> gp_link = link_functions . Identity ( ) <EOL> if not isinstance ( gp_link , link_functions . Identity ) : <EOL> print ( "<STR_LIT>" ) <EOL> super ( HeteroscedasticGaussian , self ) . __init__ ( gp_link , np . ones ( Y_metadata [ '<STR_LIT>' ] . shape ) * variance , name ) <EOL> def exact_inference_gradients ( self , dL_dKdiag , Y_metadata = None ) : <EOL> return dL_dKdiag [ Y_metadata [ '<STR_LIT>' ] ] <EOL> def gaussian_variance ( self , Y_metadata = None ) : <EOL> return self . variance [ Y_metadata [ '<STR_LIT>' ] . flatten ( ) ] <EOL> def predictive_values ( self , mu , var , full_cov = False , Y_metadata = None ) : <EOL> _s = self . variance [ Y_metadata [ '<STR_LIT>' ] . flatten ( ) ] <EOL> if full_cov : <EOL> if var . ndim == <NUM_LIT:2> : <EOL> var += np . eye ( var . shape [ <NUM_LIT:0> ] ) * _s <EOL> if var . ndim == <NUM_LIT:3> : <EOL> var += np . atleast_3d ( np . eye ( var . shape [ <NUM_LIT:0> ] ) * _s ) <EOL> else : <EOL> var += _s <EOL> return mu , var <EOL> def predictive_quantiles ( self , mu , var , quantiles , Y_metadata = None ) : <EOL> _s = self . variance [ Y_metadata [ '<STR_LIT>' ] . flatten ( ) ] <EOL> return [ stats . norm . ppf ( q / <NUM_LIT> ) * np . sqrt ( var + _s ) + mu for q in quantiles ] </s>
