<s> import numpy as np <EOL> from scipy import stats , special <EOL> import scipy as sp <EOL> from . . core . parameterization import Param <EOL> from . import link_functions <EOL> from . likelihood import Likelihood <EOL> class Gamma ( Likelihood ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , gp_link = None , beta = <NUM_LIT:1.> ) : <EOL> if gp_link is None : <EOL> gp_link = link_functions . Log ( ) <EOL> super ( Gamma , self ) . __init__ ( gp_link , '<STR_LIT>' ) <EOL> self . beta = Param ( '<STR_LIT>' , beta ) <EOL> self . link_parameter ( self . beta ) <EOL> self . beta . fix ( ) <EOL> def pdf_link ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> assert np . atleast_1d ( link_f ) . shape == np . atleast_1d ( y ) . shape <EOL> alpha = link_f * self . beta <EOL> objective = ( y ** ( alpha - <NUM_LIT:1.> ) * np . exp ( - self . beta * y ) * self . beta ** alpha ) / special . gamma ( alpha ) <EOL> return np . exp ( np . sum ( np . log ( objective ) ) ) <EOL> def logpdf_link ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> alpha = link_f * self . beta <EOL> log_objective = alpha * np . log ( self . beta ) - np . log ( special . gamma ( alpha ) ) + ( alpha - <NUM_LIT:1> ) * np . log ( y ) - self . beta * y <EOL> return log_objective <EOL> def dlogpdf_dlink ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> grad = self . beta * np . log ( self . beta * y ) - special . psi ( self . beta * link_f ) * self . beta <EOL> return grad <EOL> def d2logpdf_dlink2 ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> hess = - special . polygamma ( <NUM_LIT:1> , self . beta * link_f ) * ( self . beta ** <NUM_LIT:2> ) <EOL> return hess <EOL> def d3logpdf_dlink3 ( self , link_f , y , Y_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> d3lik_dlink3 = - special . polygamma ( <NUM_LIT:2> , self . beta * link_f ) * ( self . beta ** <NUM_LIT:3> ) <EOL> return d3lik_dlink3 </s>
