<s> from modshogun import GMM , RealFeatures <EOL> from numpy import zeros <EOL> from numpy . ma . extras import unique <EOL> from numpy . random import randint <EOL> from kameleon_mcmc . distribution . Discrete import Discrete <EOL> from kameleon_mcmc . distribution . Gaussian import Gaussian <EOL> from kameleon_mcmc . distribution . MixtureDistribution import MixtureDistribution <EOL> from kameleon_mcmc . mcmc . samplers . StandardMetropolis import StandardMetropolis <EOL> class GMMMetropolis ( StandardMetropolis ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , distribution , num_components , num_sample_discard = <NUM_LIT:1000> , <EOL> num_samples_gmm = <NUM_LIT:1000> , num_samples_when_to_switch = <NUM_LIT> , num_runs_em = <NUM_LIT:1> ) : <EOL> StandardMetropolis . __init__ ( self , distribution ) <EOL> self . num_components = num_components <EOL> self . num_sample_discard = num_sample_discard <EOL> self . num_samples_gmm = num_samples_gmm <EOL> self . num_samples_when_to_switch = num_samples_when_to_switch <EOL> self . num_runs_em = num_runs_em <EOL> self . proposal = None <EOL> def __str__ ( self ) : <EOL> s = self . __class__ . __name__ + "<STR_LIT>" <EOL> s += "<STR_LIT>" + str ( self . num_components ) <EOL> s += "<STR_LIT>" + str ( self . num_sample_discard ) <EOL> s += "<STR_LIT>" + str ( self . num_samples_gmm ) <EOL> s += "<STR_LIT>" + str ( self . num_runs_em ) <EOL> s += "<STR_LIT:U+002CU+0020>" + StandardMetropolis . __str__ ( self ) <EOL> s += "<STR_LIT:]>" <EOL> return s <EOL> def adapt ( self , mcmc_chain , step_output ) : <EOL> if mcmc_chain . iteration == self . num_samples_when_to_switch : <EOL> iter_no = mcmc_chain . iteration <EOL> inds = randint ( iter_no - self . num_sample_discard , size = self . num_samples_gmm ) + self . num_sample_discard <EOL> unique_inds = unique ( inds ) <EOL> self . proposal = self . fit_gmm ( mcmc_chain . samples [ unique_inds ] ) <EOL> def construct_proposal ( self , y ) : <EOL> if self . proposal is not None : <EOL> return self . proposal <EOL> else : <EOL> return StandardMetropolis . construct_proposal ( self , y ) <EOL> def fit_gmm ( self , samples ) : <EOL> """<STR_LIT>""" <EOL> features = RealFeatures ( samples . T ) <EOL> gmms = [ ] <EOL> log_likelihoods = zeros ( self . num_runs_em ) <EOL> for i in range ( self . num_runs_em ) : <EOL> gmm = GMM ( self . num_components ) <EOL> gmm . set_features ( features ) <EOL> log_likelihoods [ i ] = gmm . train_em ( ) <EOL> gmms . append ( gmm ) <EOL> max_idx = log_likelihoods . argmax ( ) <EOL> components = [ ] <EOL> for i in range ( self . num_components ) : <EOL> mu = gmms [ max_idx ] . get_nth_mean ( i ) <EOL> Sigma = gmms [ max_idx ] . get_nth_cov ( i ) <EOL> components . append ( Gaussian ( mu , Sigma ) ) <EOL> pie = gmms [ max_idx ] . get_coef ( ) <EOL> proposal = MixtureDistribution ( components [ <NUM_LIT:0> ] . dimension , <EOL> self . num_components , components , <EOL> Discrete ( pie ) ) <EOL> return proposal </s>
