<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> from bnpy . allocmodel import AllocModel <EOL> from bnpy . suffstats import SuffStatBag <EOL> from bnpy . util import NumericUtil <EOL> from bnpy . util import gammaln , digamma , EPS <EOL> class DPMixModel ( AllocModel ) : <EOL> def __init__ ( self , inferType , priorDict = None ) : <EOL> if inferType == '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . inferType = inferType <EOL> if priorDict is None : <EOL> self . alpha0 = <NUM_LIT:1.0> <EOL> self . alpha1 = <NUM_LIT:1.0> <EOL> self . truncType = '<STR_LIT:z>' <EOL> else : <EOL> self . set_prior ( priorDict ) <EOL> self . K = <NUM_LIT:0> <EOL> def set_prior ( self , PriorParamDict ) : <EOL> self . alpha1 = <NUM_LIT:1.0> <EOL> self . alpha0 = PriorParamDict [ '<STR_LIT>' ] <EOL> self . truncType = PriorParamDict [ '<STR_LIT>' ] <EOL> def set_helper_params ( self ) : <EOL> '''<STR_LIT>''' <EOL> DENOM = digamma ( self . qalpha0 + self . qalpha1 ) <EOL> self . ElogV = digamma ( self . qalpha1 ) - DENOM <EOL> self . Elog1mV = digamma ( self . qalpha0 ) - DENOM <EOL> if self . truncType == '<STR_LIT:v>' : <EOL> self . qalpha1 [ - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> self . qalpha0 [ - <NUM_LIT:1> ] = EPS <EOL> self . ElogV [ - <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> self . Elog1mV [ - <NUM_LIT:1> ] = np . log ( <NUM_LIT> ) <EOL> self . Elogw = self . ElogV . copy ( ) <EOL> self . Elogw [ <NUM_LIT:1> : ] += self . Elog1mV [ : - <NUM_LIT:1> ] . cumsum ( ) <EOL> def get_keys_for_memoized_local_params ( self ) : <EOL> '''<STR_LIT>''' <EOL> return list ( ) <EOL> def calc_local_params ( self , Data , LP , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> lpr = LP [ '<STR_LIT>' ] <EOL> lpr += self . Elogw <EOL> NumericUtil . inplaceExpAndNormalizeRows ( lpr ) <EOL> LP [ '<STR_LIT>' ] = lpr <EOL> assert np . allclose ( lpr . sum ( axis = <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> return LP <EOL> def get_global_suff_stats ( self , Data , LP , <EOL> doPrecompEntropy = False , <EOL> doPrecompMergeEntropy = False , mPairIDs = None ) : <EOL> '''<STR_LIT>''' <EOL> Nvec = np . sum ( LP [ '<STR_LIT>' ] , axis = <NUM_LIT:0> ) <EOL> SS = SuffStatBag ( K = Nvec . size , D = Data . dim ) <EOL> SS . setField ( '<STR_LIT:N>' , Nvec , dims = ( '<STR_LIT>' ) ) <EOL> if doPrecompEntropy : <EOL> ElogqZ_vec = self . E_logqZ ( LP ) <EOL> SS . setELBOTerm ( '<STR_LIT>' , ElogqZ_vec , dims = ( '<STR_LIT>' ) ) <EOL> if doPrecompMergeEntropy : <EOL> Hmerge = np . zeros ( ( self . K , self . K ) ) <EOL> for jj in range ( self . K ) : <EOL> compIDs = np . arange ( jj + <NUM_LIT:1> , self . K ) <EOL> Rcombo = LP [ '<STR_LIT>' ] [ : , jj ] [ : , np . newaxis ] + LP [ '<STR_LIT>' ] [ : , compIDs ] <EOL> Hmerge [ jj , compIDs ] = np . sum ( Rcombo * np . log ( Rcombo + EPS ) , axis = <NUM_LIT:0> ) <EOL> SS . setMergeTerm ( '<STR_LIT>' , Hmerge , dims = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return SS <EOL> def update_global_params_VB ( self , SS , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> self . K = SS . K <EOL> qalpha1 = self . alpha1 + SS . N <EOL> qalpha0 = self . alpha0 * np . ones ( self . K ) <EOL> qalpha0 [ : - <NUM_LIT:1> ] += SS . N [ : : - <NUM_LIT:1> ] . cumsum ( ) [ : : - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> self . qalpha1 = qalpha1 <EOL> self . qalpha0 = qalpha0 <EOL> self . set_helper_params ( ) <EOL> def update_global_params_soVB ( self , SS , rho , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> assert self . K == SS . K <EOL> qalpha1 = self . alpha1 + SS . N <EOL> qalpha0 = self . alpha0 * np . ones ( self . K ) <EOL> qalpha0 [ : - <NUM_LIT:1> ] += SS . N [ : : - <NUM_LIT:1> ] . cumsum ( ) [ : : - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> self . qalpha1 = rho * qalpha1 + ( <NUM_LIT:1> - rho ) * self . qalpha1 <EOL> self . qalpha0 = rho * qalpha0 + ( <NUM_LIT:1> - rho ) * self . qalpha0 <EOL> self . set_helper_params ( ) <EOL> def set_global_params ( self , hmodel = None , K = None , qalpha1 = None , <EOL> qalpha0 = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if hmodel is not None : <EOL> self . K = hmodel . allocModel . K <EOL> self . qalpha1 = hmodel . allocModel . qalpha1 <EOL> self . qalpha0 = hmodel . allocModel . qalpha0 <EOL> self . set_helper_params ( ) <EOL> return <EOL> if type ( qalpha1 ) != np . ndarray or qalpha1 . size != K or qalpha0 . size != K : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . K = K <EOL> self . qalpha1 = qalpha1 <EOL> self . qalpha0 = qalpha0 <EOL> self . set_helper_params ( ) <EOL> def calc_evidence ( self , Data , SS , LP = None ) : <EOL> '''<STR_LIT:U+0020>''' <EOL> evV = self . E_logpV ( ) - self . E_logqV ( ) <EOL> if SS . hasELBOTerm ( '<STR_LIT>' ) : <EOL> evZq = np . sum ( SS . getELBOTerm ( '<STR_LIT>' ) ) <EOL> else : <EOL> evZq = np . sum ( self . E_logqZ ( LP ) ) <EOL> if SS . hasAmpFactor ( ) : <EOL> evZ = self . E_logpZ ( SS ) - SS . ampF * evZq <EOL> else : <EOL> evZ = self . E_logpZ ( SS ) - evZq <EOL> return evZ + evV <EOL> def E_logpZ ( self , SS ) : <EOL> return np . inner ( SS . N , self . Elogw ) <EOL> def E_logqZ ( self , LP ) : <EOL> return np . sum ( LP [ '<STR_LIT>' ] * np . log ( LP [ '<STR_LIT>' ] + EPS ) , axis = <NUM_LIT:0> ) <EOL> def E_logpV ( self ) : <EOL> logNormC = gammaln ( self . alpha0 + self . alpha1 ) - gammaln ( self . alpha0 ) - gammaln ( self . alpha1 ) <EOL> logBetaPDF = ( self . alpha1 - <NUM_LIT:1> ) * self . ElogV + ( self . alpha0 - <NUM_LIT:1> ) * self . Elog1mV <EOL> if self . truncType == '<STR_LIT:z>' : <EOL> return self . K * logNormC + logBetaPDF . sum ( ) <EOL> elif self . truncType == '<STR_LIT:v>' : <EOL> return self . K * logNormC + logBetaPDF [ : - <NUM_LIT:1> ] . sum ( ) <EOL> def E_logqV ( self ) : <EOL> logNormC = gammaln ( self . qalpha0 + self . qalpha1 ) - gammaln ( self . qalpha0 ) - gammaln ( self . qalpha1 ) <EOL> logBetaPDF = ( self . qalpha1 - <NUM_LIT:1> ) * self . ElogV + ( self . qalpha0 - <NUM_LIT:1> ) * self . Elog1mV <EOL> if self . truncType == '<STR_LIT:z>' : <EOL> return logNormC . sum ( ) + logBetaPDF . sum ( ) <EOL> elif self . truncType == '<STR_LIT:v>' : <EOL> return logNormC [ : - <NUM_LIT:1> ] . sum ( ) + logBetaPDF [ : - <NUM_LIT:1> ] . sum ( ) <EOL> def get_info_string ( self ) : <EOL> '''<STR_LIT>''' <EOL> msgPattern = '<STR_LIT>' <EOL> return msgPattern % ( self . K , self . alpha0 ) <EOL> def to_dict ( self ) : <EOL> return dict ( qalpha1 = self . qalpha1 , qalpha0 = self . qalpha0 ) <EOL> def from_dict ( self , myDict ) : <EOL> self . inferType = myDict [ '<STR_LIT>' ] <EOL> self . K = myDict [ '<STR_LIT>' ] <EOL> self . qalpha1 = myDict [ '<STR_LIT>' ] <EOL> self . qalpha0 = myDict [ '<STR_LIT>' ] <EOL> if self . qalpha0 . ndim == <NUM_LIT:0> : <EOL> self . qalpha0 = self . qalpha1 [ np . newaxis ] <EOL> if self . qalpha0 . ndim == <NUM_LIT:0> : <EOL> self . qalpha0 = self . qalpha0 [ np . newaxis ] <EOL> self . set_helper_params ( ) <EOL> def get_prior_dict ( self ) : <EOL> return dict ( alpha1 = self . alpha1 , alpha0 = self . alpha0 , K = self . K , <EOL> truncType = self . truncType ) </s>
