<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> from scipy import special <EOL> from . import links as L <EOL> from . import varfuncs as V <EOL> FLOAT_EPS = np . finfo ( float ) . eps <EOL> class Family ( object ) : <EOL> """<STR_LIT>""" <EOL> valid = [ - np . inf , np . inf ] <EOL> links = [ ] <EOL> def _setlink ( self , link ) : <EOL> """<STR_LIT>""" <EOL> self . _link = link <EOL> if not isinstance ( link , L . Link ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> validlink = link in self . links <EOL> validlink = max ( [ isinstance ( link , _ ) for _ in self . links ] ) <EOL> if not validlink : <EOL> errmsg = "<STR_LIT>" <EOL> raise ValueError ( errmsg % ( repr ( self . links ) , link ) ) <EOL> def _getlink ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _link <EOL> link = property ( _getlink , _setlink , doc = "<STR_LIT>" ) <EOL> def __init__ ( self , link , variance ) : <EOL> self . link = link ( ) <EOL> self . variance = variance <EOL> def starting_mu ( self , y ) : <EOL> r"""<STR_LIT>""" <EOL> return ( y + y . mean ( ) ) / <NUM_LIT> <EOL> def weights ( self , mu ) : <EOL> r"""<STR_LIT>""" <EOL> return <NUM_LIT:1.> / ( self . link . deriv ( mu ) ** <NUM_LIT:2> * self . variance ( mu ) ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def resid_dev ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def fitted ( self , lin_pred ) : <EOL> """<STR_LIT>""" <EOL> fits = self . link . inverse ( lin_pred ) <EOL> return fits <EOL> def predict ( self , mu ) : <EOL> """<STR_LIT>""" <EOL> return self . link ( mu ) <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class Poisson ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . log , L . identity , L . sqrt ] <EOL> variance = V . mu <EOL> valid = [ <NUM_LIT:0> , np . inf ] <EOL> safe_links = [ L . Log , ] <EOL> def __init__ ( self , link = L . log ) : <EOL> self . variance = Poisson . variance <EOL> self . link = link ( ) <EOL> def _clean ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return np . clip ( x , FLOAT_EPS , np . inf ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> return ( np . sign ( endog - mu ) * <EOL> np . sqrt ( <NUM_LIT:2> * ( endog * np . log ( endog_mu ) - ( endog - mu ) ) ) / scale ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r'''<STR_LIT>''' <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> return <NUM_LIT:2> * np . sum ( endog * freq_weights * np . log ( endog_mu ) ) / scale <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> loglike = np . sum ( freq_weights * ( endog * np . log ( mu ) - mu - <EOL> special . gammaln ( endog + <NUM_LIT:1> ) ) ) <EOL> return scale * loglike <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> r"""<STR_LIT>""" <EOL> return ( <NUM_LIT:3> / <NUM_LIT> ) * ( endog ** ( <NUM_LIT:2> / <NUM_LIT> ) - mu ** ( <NUM_LIT:2> / <NUM_LIT> ) ) / mu ** ( <NUM_LIT:1> / <NUM_LIT> ) <EOL> class Gaussian ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . log , L . identity , L . inverse_power ] <EOL> variance = V . constant <EOL> safe_links = links <EOL> def __init__ ( self , link = L . identity ) : <EOL> self . variance = Gaussian . variance <EOL> self . link = link ( ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return ( endog - mu ) / np . sqrt ( self . variance ( mu ) ) / scale <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return np . sum ( ( freq_weights * ( endog - mu ) ** <NUM_LIT:2> ) ) / scale <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( self . link , L . Power ) and self . link . power == <NUM_LIT:1> : <EOL> nobs2 = np . sum ( freq_weights , axis = <NUM_LIT:0> ) / <NUM_LIT> <EOL> SSR = np . sum ( ( endog - self . fitted ( mu ) ) ** <NUM_LIT:2> , axis = <NUM_LIT:0> ) <EOL> llf = - np . log ( SSR ) * nobs2 <EOL> llf -= ( <NUM_LIT:1> + np . log ( np . pi / nobs2 ) ) * nobs2 <EOL> return llf <EOL> else : <EOL> return np . sum ( freq_weights * ( ( endog * mu - mu ** <NUM_LIT:2> / <NUM_LIT:2> ) / scale - <EOL> endog ** <NUM_LIT:2> / ( <NUM_LIT:2> * scale ) - <NUM_LIT> * np . log ( <NUM_LIT:2> * np . pi * scale ) ) ) <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> r"""<STR_LIT>""" <EOL> return endog - mu <EOL> class Gamma ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . log , L . identity , L . inverse_power ] <EOL> variance = V . mu_squared <EOL> safe_links = [ L . Log , ] <EOL> def __init__ ( self , link = L . inverse_power ) : <EOL> self . variance = Gamma . variance <EOL> self . link = link ( ) <EOL> def _clean ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return np . clip ( x , FLOAT_EPS , np . inf ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> return <NUM_LIT:2> * np . sum ( freq_weights * ( ( endog - mu ) / mu - np . log ( endog_mu ) ) ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> return np . sign ( endog - mu ) * np . sqrt ( - <NUM_LIT:2> * ( - ( endog - mu ) / mu + <EOL> np . log ( endog_mu ) ) ) <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return - <NUM_LIT:1.> / scale * np . sum ( ( endog / mu + np . log ( mu ) + ( scale - <NUM_LIT:1> ) * <EOL> np . log ( endog ) + np . log ( scale ) + scale * <EOL> special . gammaln ( <NUM_LIT:1.> / scale ) ) * freq_weights ) <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> r"""<STR_LIT>""" <EOL> return <NUM_LIT:3> * ( endog ** ( <NUM_LIT:1> / <NUM_LIT> ) - mu ** ( <NUM_LIT:1> / <NUM_LIT> ) ) / mu ** ( <NUM_LIT:1> / <NUM_LIT> ) <EOL> class Binomial ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . logit , L . probit , L . cauchy , L . log , L . cloglog , L . identity ] <EOL> variance = V . binary <EOL> safe_links = [ L . Logit , L . CDFLink ] <EOL> def __init__ ( self , link = L . logit ) : <EOL> self . n = <NUM_LIT:1> <EOL> self . variance = V . Binomial ( n = self . n ) <EOL> self . link = link ( ) <EOL> def starting_mu ( self , y ) : <EOL> """<STR_LIT>""" <EOL> return ( y + <NUM_LIT> ) / <NUM_LIT:2> <EOL> def initialize ( self , endog , freq_weights ) : <EOL> '''<STR_LIT>''' <EOL> if ( endog . ndim > <NUM_LIT:1> and endog . shape [ <NUM_LIT:1> ] > <NUM_LIT:1> ) : <EOL> y = endog [ : , <NUM_LIT:0> ] <EOL> self . n = endog . sum ( <NUM_LIT:1> ) <EOL> return y * <NUM_LIT:1.> / self . n , self . n <EOL> else : <EOL> return endog , np . ones ( endog . shape [ <NUM_LIT:0> ] ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1> , scale = <NUM_LIT:1.> ) : <EOL> r'''<STR_LIT>''' <EOL> if np . shape ( self . n ) == ( ) and self . n == <NUM_LIT:1> : <EOL> one = np . equal ( endog , <NUM_LIT:1> ) <EOL> return - <NUM_LIT:2> * np . sum ( ( one * np . log ( mu + <NUM_LIT> ) + ( <NUM_LIT:1> - one ) * <EOL> np . log ( <NUM_LIT:1> - mu + <NUM_LIT> ) ) * freq_weights ) <EOL> else : <EOL> return <NUM_LIT:2> * np . sum ( self . n * freq_weights * <EOL> ( endog * np . log ( endog / mu + <NUM_LIT> ) + <EOL> ( <NUM_LIT:1> - endog ) * np . log ( ( <NUM_LIT:1> - endog ) / <EOL> ( <NUM_LIT:1> - mu ) + <NUM_LIT> ) ) ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> mu = self . link . _clean ( mu ) <EOL> if np . shape ( self . n ) == ( ) and self . n == <NUM_LIT:1> : <EOL> one = np . equal ( endog , <NUM_LIT:1> ) <EOL> return np . sign ( endog - mu ) * np . sqrt ( - <NUM_LIT:2> * <EOL> np . log ( one * mu + ( <NUM_LIT:1> - one ) * <EOL> ( <NUM_LIT:1> - mu ) ) ) / scale <EOL> else : <EOL> return ( np . sign ( endog - mu ) * <EOL> np . sqrt ( <NUM_LIT:2> * self . n * <EOL> ( endog * np . log ( endog / mu + <NUM_LIT> ) + <EOL> ( <NUM_LIT:1> - endog ) * np . log ( ( <NUM_LIT:1> - endog ) / ( <NUM_LIT:1> - mu ) + <EOL> <NUM_LIT> ) ) ) / scale ) <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> if np . shape ( self . n ) == ( ) and self . n == <NUM_LIT:1> : <EOL> return scale * np . sum ( ( endog * np . log ( mu / ( <NUM_LIT:1> - mu ) + <NUM_LIT> ) + <EOL> np . log ( <NUM_LIT:1> - mu ) ) * freq_weights ) <EOL> else : <EOL> y = endog * self . n <EOL> return scale * np . sum ( ( special . gammaln ( self . n + <NUM_LIT:1> ) - <EOL> special . gammaln ( y + <NUM_LIT:1> ) - <EOL> special . gammaln ( self . n - y + <NUM_LIT:1> ) + y * <EOL> np . log ( mu / ( <NUM_LIT:1> - mu ) ) + self . n * <EOL> np . log ( <NUM_LIT:1> - mu ) ) * freq_weights ) <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> '''<STR_LIT>''' <EOL> cox_snell = lambda x : ( special . betainc ( <NUM_LIT:2> / <NUM_LIT> , <NUM_LIT:2> / <NUM_LIT> , x ) <EOL> * special . beta ( <NUM_LIT:2> / <NUM_LIT> , <NUM_LIT:2> / <NUM_LIT> ) ) <EOL> return np . sqrt ( self . n ) * ( ( cox_snell ( endog ) - cox_snell ( mu ) ) / <EOL> ( mu ** ( <NUM_LIT:1> / <NUM_LIT> ) * ( <NUM_LIT:1> - mu ) ** ( <NUM_LIT:1> / <NUM_LIT> ) ) ) <EOL> class InverseGaussian ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . inverse_squared , L . inverse_power , L . identity , L . log ] <EOL> variance = V . mu_cubed <EOL> safe_links = [ L . inverse_squared , L . Log , ] <EOL> def __init__ ( self , link = L . inverse_squared ) : <EOL> self . variance = InverseGaussian . variance <EOL> self . link = link ( ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return np . sign ( endog - mu ) * np . sqrt ( ( endog - mu ) ** <NUM_LIT:2> / ( endog * mu ** <NUM_LIT:2> ) ) / scale <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return np . sum ( freq_weights * ( endog - mu ) ** <NUM_LIT:2> / ( endog * mu ** <NUM_LIT:2> ) ) / scale <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return - <NUM_LIT> * np . sum ( ( ( endog - mu ) ** <NUM_LIT:2> / ( endog * mu ** <NUM_LIT:2> * scale ) + <EOL> np . log ( scale * endog ** <NUM_LIT:3> ) + np . log ( <NUM_LIT:2> * np . pi ) ) * <EOL> freq_weights ) <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> r"""<STR_LIT>""" <EOL> return np . log ( endog / mu ) / np . sqrt ( mu ) <EOL> class NegativeBinomial ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . log , L . cloglog , L . identity , L . nbinom , L . Power ] <EOL> variance = V . nbinom <EOL> safe_links = [ L . Log , ] <EOL> def __init__ ( self , link = L . log , alpha = <NUM_LIT:1.> ) : <EOL> self . alpha = <NUM_LIT:1.> * alpha <EOL> self . variance = V . NegativeBinomial ( alpha = self . alpha ) <EOL> if isinstance ( link , L . NegativeBinomial ) : <EOL> self . link = link ( alpha = self . alpha ) <EOL> else : <EOL> self . link = link ( ) <EOL> def _clean ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return np . clip ( x , FLOAT_EPS , np . inf ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> iszero = np . equal ( endog , <NUM_LIT:0> ) <EOL> notzero = <NUM_LIT:1> - iszero <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> tmp = iszero * <NUM_LIT:2> * np . log ( <NUM_LIT:1> + self . alpha * mu ) / self . alpha <EOL> tmp += notzero * ( <NUM_LIT:2> * endog * np . log ( endog_mu ) - <NUM_LIT:2> / self . alpha * <EOL> ( <NUM_LIT:1> + self . alpha * endog ) * <EOL> np . log ( ( <NUM_LIT:1> + self . alpha * endog ) / <EOL> ( <NUM_LIT:1> + self . alpha * mu ) ) ) <EOL> return np . sum ( freq_weights * tmp ) / scale <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> iszero = np . equal ( endog , <NUM_LIT:0> ) <EOL> notzero = <NUM_LIT:1> - iszero <EOL> endog_mu = self . _clean ( endog / mu ) <EOL> tmp = iszero * <NUM_LIT:2> * np . log ( <NUM_LIT:1> + self . alpha * mu ) / self . alpha <EOL> tmp += notzero * ( <NUM_LIT:2> * endog * np . log ( endog_mu ) - <NUM_LIT:2> / self . alpha * <EOL> ( <NUM_LIT:1> + self . alpha * endog ) * <EOL> np . log ( ( <NUM_LIT:1> + self . alpha * endog ) / <EOL> ( <NUM_LIT:1> + self . alpha * mu ) ) ) <EOL> return np . sign ( endog - mu ) * np . sqrt ( tmp ) / scale <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> lin_pred = self . _link ( mu ) <EOL> constant = ( special . gammaln ( endog + <NUM_LIT:1> / self . alpha ) - <EOL> special . gammaln ( endog + <NUM_LIT:1> ) - special . gammaln ( <NUM_LIT:1> / self . alpha ) ) <EOL> exp_lin_pred = np . exp ( lin_pred ) <EOL> return np . sum ( ( endog * np . log ( self . alpha * exp_lin_pred / <EOL> ( <NUM_LIT:1> + self . alpha * exp_lin_pred ) ) - <EOL> np . log ( <NUM_LIT:1> + self . alpha * exp_lin_pred ) / <EOL> self . alpha + constant ) * freq_weights ) <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> """<STR_LIT>""" <EOL> hyp2f1 = lambda x : special . hyp2f1 ( <NUM_LIT:2> / <NUM_LIT> , <NUM_LIT:1> / <NUM_LIT> , <NUM_LIT:5> / <NUM_LIT> , x ) <EOL> return ( ( hyp2f1 ( - self . alpha * endog ) - hyp2f1 ( - self . alpha * mu ) + <EOL> <NUM_LIT> * ( endog ** ( <NUM_LIT:2> / <NUM_LIT> ) - mu ** ( <NUM_LIT:2> / <NUM_LIT> ) ) ) / <EOL> ( mu + self . alpha * mu ** <NUM_LIT:2> ) ** ( <NUM_LIT:1> / <NUM_LIT> ) ) <EOL> class Tweedie ( Family ) : <EOL> """<STR_LIT>""" <EOL> links = [ L . log , L . Power ] <EOL> variance = V . Power <EOL> safe_links = [ L . log , L . Power ] <EOL> def __init__ ( self , link = None , var_power = <NUM_LIT:1.> , link_power = <NUM_LIT:0> ) : <EOL> self . var_power = var_power <EOL> self . link_power = link_power <EOL> self . variance = V . Power ( power = var_power * <NUM_LIT:1.> ) <EOL> if link_power != <NUM_LIT:0> and not ( ( link is L . Power ) or ( link is None ) ) : <EOL> msg = '<STR_LIT>' <EOL> msg = msg . format ( link_power ) <EOL> raise ValueError ( msg ) <EOL> if ( link_power == <NUM_LIT:0> ) and ( ( link is None ) or ( link is L . Log ) ) : <EOL> self . link = L . log ( ) <EOL> elif link_power != <NUM_LIT:0> : <EOL> self . link = L . Power ( power = link_power * <NUM_LIT:1.> ) <EOL> else : <EOL> self . link = link ( ) <EOL> def _clean ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return np . clip ( x , <NUM_LIT:0> , np . inf ) <EOL> def deviance ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> p = self . var_power <EOL> if p == <NUM_LIT:1> : <EOL> dev = np . where ( endog == <NUM_LIT:0> , <EOL> mu , <EOL> endog * np . log ( endog / mu ) + ( mu - endog ) ) <EOL> elif p == <NUM_LIT:2> : <EOL> endog1 = np . clip ( endog , FLOAT_EPS , np . inf ) <EOL> dev = ( ( endog - mu ) / mu ) - np . log ( endog1 / mu ) <EOL> else : <EOL> dev = ( endog ** ( <NUM_LIT:2> - p ) / ( ( <NUM_LIT:1> - p ) * ( <NUM_LIT:2> - p ) ) - <EOL> endog * mu ** ( <NUM_LIT:1> - p ) / ( <NUM_LIT:1> - p ) + mu ** ( <NUM_LIT:2> - p ) / ( <NUM_LIT:2> - p ) ) <EOL> return np . sum ( <NUM_LIT:2> * freq_weights * dev ) <EOL> def resid_dev ( self , endog , mu , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> p = self . var_power <EOL> if p == <NUM_LIT:1> : <EOL> dev = np . where ( endog == <NUM_LIT:0> , <EOL> mu , <EOL> endog * np . log ( endog / mu ) + ( mu - endog ) ) <EOL> elif p == <NUM_LIT:2> : <EOL> endog1 = np . clip ( endog , FLOAT_EPS , np . inf ) <EOL> dev = ( ( endog - mu ) / mu ) - np . log ( endog1 / mu ) <EOL> else : <EOL> dev = ( endog ** ( <NUM_LIT:2> - p ) / ( ( <NUM_LIT:1> - p ) * ( <NUM_LIT:2> - p ) ) - <EOL> endog * mu ** ( <NUM_LIT:1> - p ) / ( <NUM_LIT:1> - p ) + mu ** ( <NUM_LIT:2> - p ) / ( <NUM_LIT:2> - p ) ) <EOL> return np . sign ( endog - mu ) * np . sqrt ( <NUM_LIT:2> * dev ) <EOL> def loglike ( self , endog , mu , freq_weights = <NUM_LIT:1.> , scale = <NUM_LIT:1.> ) : <EOL> r"""<STR_LIT>""" <EOL> return np . nan <EOL> def resid_anscombe ( self , endog , mu ) : <EOL> """<STR_LIT>""" <EOL> if self . var_power == <NUM_LIT:3> : <EOL> return ( np . log ( endog ) - np . log ( mu ) ) / np . sqrt ( mu ) <EOL> else : <EOL> c = ( <NUM_LIT> - self . var_power ) / <NUM_LIT> <EOL> return ( ( <NUM_LIT:1.> / c ) * ( endog ** c - mu ** c ) / <EOL> mu ** ( self . var_power / <NUM_LIT> ) ) </s>
