def MM_E_step(x, K, opts, tmp_mu, tmp_v, tmp_PI, xpos, xneg): #PS=np.zeros([K,size(x)]) #D=np.zeros([K,size(x)]) # storages probability of samples wrt distributions PS = np.zeros([K, x.shape[0]]) D = np.zeros([K, x.shape[0] ]) # storages probability of samples wrt distributions tmp_a = np.zeros( K) #it will remain zero for non-gamma or inv gamma distributions tmp_b = np.zeros( K) #it will remain zero for non-gamma or inv gamma distributions for k in range(K): if opts['Components_Model'][k] == 'Gauss': Nobj = scipy.stats.norm(tmp_mu[k], np.power(tmp_v[k], 0.5)) PS[k, :] = Nobj.pdf(x) elif opts['Components_Model'][k] == 'Gamma': tmp_a[k] = alb.alphaGm(tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaGm(tmp_mu[k], tmp_v[k]) PS[k, :] = alb.gam_self(x, tmp_a[k], tmp_b[k]) PS[k, xneg] = 0 elif opts['Components_Model'][k] == '-Gamma': tmp_a[k] = alb.alphaGm(-1 * tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaGm(-1 * tmp_mu[k], tmp_v[k]) PS[k, :] = alb.gam_self(-1 * x, tmp_a[k], tmp_b[k]) PS[k, xpos] = 0 elif opts['Components_Model'][k] == 'InvGamma': tmp_a[k] = alb.alphaIG(tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaIG(tmp_mu[k], tmp_v[k]) PS[k, :] = alb.invgam(x, tmp_a[k], tmp_b[k]) PS[k, xneg] = 0 elif opts['Components_Model'][k] == '-InvGamma': tmp_a[k] = alb.alphaIG(-1 * tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaIG(-1 * tmp_mu[k], tmp_v[k]) PS[k, :] = alb.invgam(-1 * x, tmp_a[k], tmp_b[k]) PS[k, xpos] = 0 elif opts['Components_Model'][k] == 'Beta': tmp_a[k] = alb.a_beta_distr(tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.b_beta_distr(tmp_mu[k], tmp_v[k]) PS[k, :] = scipy.stats.beta.pdf(x, tmp_a[k], tmp_b[k]) PS[np.isnan(PS)] = 0 PS[np.isinf(PS)] = 0 D = np.multiply(PS, np.matrix(tmp_PI).T) resp = np.divide(D, np.matrix(np.sum(D, 0))) N = np.sum(resp, 1) tmp_PI = np.divide(N, np.sum(resp)).T if 0: dum = np.add(np.log(PS), np.log(tmp_PI).T) dum[np.isinf(dum)] = 0 dum[np.isinf(dum)] = 0 Exp_lik = np.sum(np.multiply(resp, dum)) else: dum = np.multiply(tmp_PI.T, PS) #add(np.log(PS),np.log(tmp_PI).T) dum[np.isinf(dum)] = 1 dum[np.isinf(dum)] = 1 dum[dum == 0] = 1 Exp_lik = np.sum(np.log(dum)) return PS, resp, tmp_PI, N, Exp_lik
def init_ML(x, opts): tol = opts['tol'] maxiters = opts['maxits'] K = opts['Number_of_Components'] #Exp_lik=np.zeros(maxiters+1) opts_MM = copy.deepcopy(opts) #opts_MM['maxits']=np.int(1) Model = alb.Mix_Mod_MethodOfMoments(x, opts_MM) Exp_lik = Model['Likelihood'] tmp_mu = Model['mu1'] tmp_v = Model['variances'] tmp_PI = Model['Mixing Prop.'] param1 = np.zeros(K) param2 = np.zeros(K) for k in range(K): if opts['Components_Model'][k] == 'Gauss': param1[k] = tmp_mu[k] param2[k] = tmp_v[k] elif opts['Components_Model'][k] == 'Gamma': param1[k] = alb.alphaGm(tmp_mu[k], tmp_v[k]) param2[k] = np.divide(1., alb.betaGm(tmp_mu[k], tmp_v[k])) elif opts['Components_Model'][k] == '-Gamma': param1[k] = alb.alphaGm(-1 * tmp_mu[k], tmp_v[k]) param2[k] = np.divide(1., alb.betaGm(-1 * tmp_mu[k], tmp_v[k])) elif opts['Components_Model'][k] == 'InvGamma': param1[k] = alb.alphaIG(tmp_mu[k], tmp_v[k]) param2[k] = alb.betaIG(tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == '-InvGamma': param1[k] = alb.alphaIG(-1 * tmp_mu[k], tmp_v[k]) param2[k] = alb.betaIG(-1 * tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == 'Beta': param1[k] = alb.a_beta_distr(tmp_mu[k], tmp_v[k]) param2[k] = alb.b_beta_distr(tmp_mu[k], tmp_v[k]) return param1, param2, maxiters, tol, K, tmp_PI, Exp_lik
def Mix_Mod_MethodOfMoments( x, opts={ 'Number_of_Components': 3, 'Components_Model': ['Gauss', 'Gamma', '-Gamma'], 'init_params': [0, 1, 3, 1, -3, 1], 'maxits': np.int(100), 'tol': 0.00001, 'init_pi': np.true_divide(np.ones(3), 3) }): tmp_mu, tmp_v, maxiters, tol, K, tmp_PI, Exp_lik = init_MM(x, opts) #indexes of samples to assign 0 prob wrt each positive definite distr. #xneg=find(x<pow(10,-14)) #xpos=find(x>-pow(10,-14)) xneg = np.argwhere(x < pow(10, -14))[:, 0] xpos = np.argwhere(x > -pow(10, -14))[:, 0] #ITERATE flag = 0 it = 0 while flag == 0: # E-step PS, resp, tmp_PI, N, Exp_lik[it] = MM_E_step(x, K, opts, tmp_mu, tmp_v, tmp_PI, xpos, xneg) #M-step tmp_mu, tm_v = MM_M_step(x, K, tmp_mu, tmp_v, resp, N) #convergence criterium if it > 0: if (abs((Exp_lik[it] - Exp_lik[it - 1]) / Exp_lik[it - 1]) < tol) | (it > maxiters - 1): flag = 1 #print(it) it = it + 1 #gather output tmp_a = np.zeros( K) #it will remain zero for non-gamma or inv gamma distributions tmp_b = np.zeros( K) #it will remain zero for non-gamma or inv gamma distributions tmp_c = np.zeros(K) for k in range(K): if opts['Components_Model'][k] == 'Gamma': tmp_a[k] = alb.alphaGm(tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaGm(tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == '-Gamma': tmp_a[k] = alb.alphaGm(-1 * tmp_mu[k], tmp_v[k]) tmp_b[k] = alb.betaGm(-1 * tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == 'InvGamma': tmp_a[k] = alb.alphaIG(tmp_mu[k], tmp_v[k]) tmp_c[k] = alb.betaIG(tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == '-InvGamma': tmp_a[k] = alb.alphaIG(-1 * tmp_mu[k], tmp_v[k]) tmp_c[k] = alb.betaIG(-1 * tmp_mu[k], tmp_v[k]) elif opts['Components_Model'][k] == 'Beta': tmp_a[k] = alb.a_beta_distr(tmp_mu[k], tmp_v[k]) tmp_c[k] = alb.b_beta_distr(tmp_mu[k], tmp_v[k]) output_dict = { 'means': tmp_mu, 'mu1': tmp_mu, 'variances': tmp_v, 'taus1': np.divide(1, tmp_v), 'Mixing Prop.': np.asarray(tmp_PI)[0], 'Likelihood': Exp_lik[0:it], 'its': it, 'Final responsibilities': resp, 'opts': opts, 'shapes': tmp_a, 'scales': tmp_c, 'rates': np.divide(1., tmp_b) } return output_dict
def SIN_init_VB_MM(data, opts): K = opts['Number_of_Components'] opts2 = copy.deepcopy(opts) opts2['maxits'] = 10 #SET PRIORS #set mixing priors. mmm = 3 #(the mean of non gauss component) vvv = 10 #(the variance of the component)\ m0 = np.zeros(K) tau0 = np.zeros(K) b0 = np.zeros(K) c0 = np.zeros(K) b_0 = np.zeros(K) c_0 = np.zeros(K) d_0 = np.zeros(K) e_0 = np.zeros(K) Erate = np.zeros(K) Eshape = np.zeros(K) loga_0 = np.zeros(K) Escale = np.zeros(K) for k in range(K): if opts['Components_Model'][k] == 'Gamma' or ( opts['Components_Model'][k] == '-Gamma'): #set GAMMA prior on rates (shape and rate) Erate[k] = np.true_divide(1, betaGm(mmm, vvv)) d_0[k] = copy.deepcopy(Erate[k]) e_0[k] = 1 Erate[k] = np.true_divide(d_0[k], e_0[k]) #set shapes conditional prior (fancy) Eshape[k] = alphaGm(mmm, vvv) dum_v = np.copy(Eshape[k]) #allow variance on shape to be of size of mean shape dum_p = np.true_divide(1, dum_v) #from laplace approx b=prec/psi'(map(s)) b_0[k] = np.true_divide(dum_p, sp.polygamma(1, Eshape[k])) c_0[k] = copy.deepcopy(b_0[k]) loga_0[k] = ((b_0[k] * sp.polygamma(0, Eshape[k])) - (c_0[k] * np.log(Erate[k]))) if opts['Components_Model'][k] == 'InvGamma' or ( opts['Components_Model'][k] == '-InvGamma'): #set GAMMA prior on scale (shape d and rate e) Escale[k] = betaIG(mmm, vvv) d_0[k] = copy.deepcopy(Escale[k]) #shape e_0[k] = 1 #rate Escale[k] = np.true_divide(d_0[k], e_0[k]) #set component 2 and 3 shape conditional prior (fancy) Eshape[k] = alphaIG(mmm, vvv) dum_v = np.copy(Eshape[k]) #allow variance on shape to be of size of mean shape dum_p = np.true_divide(1, dum_v) b_0[k] = np.true_divide(dum_p, sp.polygamma(1, Eshape[k])) #from laplace approx b=prec/psi'(map(s)) c_0[k] = copy.deepcopy(b_0[k]) loga_0[k] = (-(b_0[k] * sp.polygamma(0, Eshape[k])) + (c_0[k] * np.log(Escale[k]))) Prior = { 'lambda_0': 5, 'm_0': 0, 'tau_0': 100, 'c0': 0.001, 'b0': 100, 'd_0': d_0, 'e_0': e_0, 'loga_0': loga_0, 'b_0': b_0, 'c_0': c_0 } # #SET POSTERIORS initializations using ML mixture models init_ML = Mix_Mod_MethodOfMoments(data, opts2) resp = init_ML['Final responsibilities'] lambdap = np.sum(resp, 1) Escales = np.zeros(K) for k in range(K): if opts['Components_Model'][k] == 'Gauss': m0[k] = init_ML['means'][k] tau0[k] = np.mean(np.absolute(init_ML['means'])) #hyperparam. on precission init_prec = np.true_divide(1, init_ML['variances'][k]) init_var_prec = np.var(np.true_divide(1, init_ML['variances']), ddof=1) c0[k] = alphaGm(init_prec, init_var_prec) #shape b0[k] = betaGm(init_prec, init_var_prec) #scale if (opts['Components_Model'][k] == 'Gamma') or (opts['Components_Model'][k] == '-Gamma'): ##hyperparam. on rates init_rates = np.true_divide( 1, betaGm(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) ) # , np.true_divide(1,alb.betaGm(np.absolute(ML_param[4]), ML_param[5])) ] ; dum_var_r = np.multiply( 0.1, init_rates) #(init_rates)* 0.1;# var(init_rates); d_0[k] = alphaGm(init_rates, dum_var_r) #shape e_0[k] = np.true_divide(1, betaGm(init_rates, dum_var_r)) #rate Erate[k] = np.true_divide(d_0[k], e_0[k]) # == init_rates #hyperparam. on shapes init_shapes = alphaGm(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) b_0[k] = np.sum(resp[k, :]) c_0[k] = b_0[k] #loga_0=((b_0* sp.polygamma(0,init_shapes)-(c_0*log(Erates))); loga_0[k] = np.multiply(b_0[k], sp.polygamma( 0, init_shapes)) - (np.multiply(c_0[k], np.log(Erate[k]))) #MAP_shapes=invpsi((loga_0+ (c_0 .* log(Erates))) ./ b_0) # == init_shapes if (opts['Components_Model'][k] == 'InvGamma') or (opts['Components_Model'][k] == '-InvGamma'): #hyperparam. on scales (inverse gamma) --> scale is r in the text, #r ~ inv gamma distr init_scales = betaIG(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) dum_var_sc = np.multiply(0.1, init_scales) d_0[k] = alphaGm(init_scales, dum_var_sc) #gamma shape e_0[k] = np.divide(1., betaGm(init_scales, dum_var_sc)) #gamma rate Escales[k] = np.divide(d_0[k], e_0[k]) #hyperparam. on shapes init_shapes = alphaIG(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) sumgam = np.sum(resp, 1) b_0[k] = sumgam[k] c_0[k] = copy.deepcopy(b_0[k]) loga_0[k] = -np.multiply(b_0[k], sp.polygamma(0, init_shapes)) + ( np.multiply(c_0[k], np.log(Escales[k]))) #MAP_shapes=invpsi((-loga_0+ (c_0 .* log(Escales))) ./ b_0) # == init_shapes shapes = np.zeros(K) rates = np.zeros(K) scales = np.zeros(K) for k in range(K): if (opts['Components_Model'][k] == 'Gamma') or (opts['Components_Model'][k] == '-Gamma'): shapes[k] = alphaGm(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) rates[k] = np.divide( 1, betaGm(np.absolute(init_ML['means'][k]), init_ML['variances'][k])) if (opts['Components_Model'][k] == 'InvGamma') or (opts['Components_Model'][k] == '-InvGamma'): shapes[k] = alphaIG(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) scales[k] = betaIG(np.absolute(init_ML['means'][k]), init_ML['variances'][k]) #Save posterior expectations for initialization of VB mixModel Posterior = { 'gammas': resp, 'pi': init_ML['Mixing Prop.'], 'mus': init_ML['means'], 'tau1s': np.true_divide(1, init_ML['variances']), 'shapes': shapes, 'rates': rates, 'scales': scales, 'lambda': lambdap, 'm_0': m0, 'tau_0': tau0, 'c0': c0, 'b0': b0, 'd_0': d_0, 'e_0': e_0, 'loga_0': loga_0, 'b_0': b_0, 'c_0': c_0 } return Prior, Posterior
def SIN_init_VB_MM(data, opts): import scipy.special as sp import copy #SIN_init_VB_MM does # - fit a mixture model using ML (EM + MM algorithms (mmfit.m)) # - initialize VB parameters of mixture model using EM fit as # initial posteriors #inputs: -data : vector normalized to mean zero and unit std # -opts: list with options and values # -MM = GIM or GGM( default =GIM) # -MLMMits = max number of iterations allowed to ML algorithm before initialize VB (default =1) # -MLMMtol = tolerance for convergence of ML algorithm before initialize VB #ouptput: mix1 is a list containg the initialized priors, and the posterior estimations given the ML initialization #example:opts=[];opts.append({'MM': 'GIM', 'MLMMits': 1, 'MLMMtol': 10^-5}); #mix=SIN_init_VB_MM(data,opts); #From matlab...IN PROGRESS if 'MM' not in opts[0]: MM = 'GIM' else: MM = opts[0]['MM'] if 'MLMMits' not in opts[0]: MLMMits = 1 else: MLMMits = opts[0]['MLMMits'] if 'MLMMtol' not in opts[0]: MLMMtol = 0.00001 else: MLMMtol = opts[0]['MLMMtol'] #SET PRIORS #set mixing priors. prior = [] mmm = 10 #(the mean of component) vvv = 10 #(the variance of the component) if MM == 'GGM': #set GAMMA prior on rates (shape and rate) Erate = np.true_divide(1, alb.betaGm(mmm, vvv)) d_0 = copy.deepcopy(Erate) e_0 = 1 Erate = np.true_divide(d_0, e_0) #set shapes conditional prior (fancy) Eshape = alb.alphaGm(mmm, vvv) dum_v = np.copy(Eshape) #allow variance on shape to be of size of mean shape dum_p = np.true_divide(1, dum_v) #from laplace approx b=prec/psi'(map(s)) b_0 = np.true_divide(dum_p, sp.polygamma(1, Eshape)) c_0 = copy.deepcopy(b_0) loga_0 = ((b_0 * sp.polygamma(0, Eshape)) - (c_0 * log(Erate))) elif MM == 'GIM': #set GAMMA prior on scale (shape d and rate e) Escale = alb.betaIG(mmm, vvv) d_0 = copy.deepcopy(Escale) #shape e_0 = 1 #rate Escale = np.true_divide(d_0, e_0) #set component 2 and 3 shape conditional prior (fancy) Eshape = alb.alphaIG(mmm, vvv) dum_v = np.copy(Eshape) #allow variance on shape to be of size of mean shape dum_p = np.true_divide(1, dum_v) b_0 = np.true_divide(dum_p, sp.polygamma(1, Eshape)) #from laplace approx b=prec/psi'(map(s)) c_0 = copy.deepcopy(b_0) loga_0 = (-(b_0 * sp.polygamma(0, Eshape)) + (c_0 * np.log(Escale))) prior.append({ 'lambda_0': 5, 'm_0': 0, 'tau_0': 100, 'c0': 0.001, 'b0': 100, 'd_0': d_0, 'e_0': e_0, 'loga_0': loga_0, 'b_0': b_0, 'c_0': c_0 }) prior = prior[0] #SET POSTERIORS initializations using ML mixture models if MM == 'GGM': mmtype = 2 elif MM == 'GIM': mmtype = 3 else: mmtype = 1 #dummy, never used gmm ML = [] [mu1, v1, mu2, v2, mu3, v3, pipi, lik, numits, resp] = alb.mmfit3(data, MLMMits, MLMMtol, mmtype) ML_param = [mu1, v1, mu2, v2, mu3, v3] ML.append({'init': ML_param, 'pi': pipi, 'LIK': lik}) ML = ML[0] #mix1.ML #INIT POSTERIORS BASED IN ML MIX MODEL post = [] #[dum; b]=max(resp) q = resp.argmax(1) gammas = np.copy(resp) #lambda=sum(resp,2)' lambdap = resp.sum(0) #COMPONENT 1: Gaussian component #hyperparam. on mean m0 = ML_param[0] tau0 = np.true_divide( 1, np.true_divide(ML_param[0] + ML_param[2] + np.absolute(ML_param[4]), 3)) #hyperparam. on precission init_prec = np.true_divide(1, ML_param[1]) init_var_prec = np.var([ np.true_divide(1, ML_param[1]), np.true_divide(1, ML_param[3]), np.true_divide(1, ML_param[5]) ], ddof=1) c0 = alb.alphaGm(init_prec, init_var_prec) #shape b0 = alb.betaGm(init_prec, init_var_prec) #scale #COMPONENTS 2 AND 3: gamma or inverse gamma if MM == 'GGM': #hyperparam. on rates init_rates = [ np.true_divide(1, alb.betaGm(np.absolute(ML_param[2]), ML_param[3])), np.true_divide(1, alb.betaGm(np.absolute(ML_param[4]), ML_param[5])) ] dum_var_r = np.multiply( 0.1, init_rates) #(init_rates)* 0.1;# var(init_rates); d_0 = alb.alphaGm(init_rates, dum_var_r) #shape e_0 = np.true_divide(1, alb.betaGm(init_rates, dum_var_r)) #rate Erates = np.true_divide(d_0, e_0) # == init_rates #hyperparam. on shapes init_shapes = [ alb.alphaGm(np.absolute(ML_param[2]), ML_param[3]), alb.alphaGm(np.absolute(ML_param[4]), ML_param[5]) ] #b_0=[1 1];c_0=b_0; #b_0=sum(resp(2:3,:),2)';c_0=b_0; b_0 = resp.sum(0)[1:3] c_0 = b_0 #loga_0=((b_0* sp.polygamma(0,init_shapes)-(c_0*log(Erates))); loga_0 = np.multiply(b_0, sp.polygamma(0, init_shapes)) - (np.multiply( c_0, np.log(Erates))) #MAP_shapes=invpsi((loga_0+ (c_0 .* log(Erates))) ./ b_0) # == init_shapes elif MM == 'GIM': #hyperparam. on scales (inverse gamma) --> scale is r in the text, #r ~ inv gamma distr init_scales = [ alb.betaIG(np.absolute(ML_param[2]), ML_param[3]), alb.betaIG(np.absolute(ML_param[4]), ML_param[5]) ] dum_var_sc = np.multiply(0.1, init_scales) #var(init_scales); d_0 = alb.alphaGm(init_scales, dum_var_sc) #gamma shape e_0 = np.divide(1, alb.betaGm(init_scales, dum_var_sc)) #gamma rate Escales = np.divide(d_0, e_0) # == init_scales #hyperparam. on shapes init_shapes = [ alb.alphaIG(np.absolute(ML_param[2]), ML_param[3]), alb.alphaIG(np.absolute(ML_param[4]), ML_param[5]) ] #b_0=[1 1];c_0=b_0; sumgam = resp.sum(0) b_0 = sumgam[1:3] c_0 = b_0 loga_0 = -np.multiply(b_0, sp.polygamma(0, init_shapes)) + ( np.multiply(c_0, np.log(Escales))) #MAP_shapes=invpsi((-loga_0+ (c_0 .* log(Escales))) ./ b_0) # == init_shapes post.append({ 'lambda': lambdap, 'm_0': m0, 'tau_0': tau0, 'c0': c0, 'b0': b0, 'd_0': d_0, 'e_0': e_0, 'loga_0': loga_0, 'b_0': b_0, 'c_0': c_0 }) post = post[0] #Save posterior expectations for initialization of VB mixModel mix1 = [] if MM == 'GGM': shapes = [0, 0] rates = [0, 0] #shapes=alphaGm(ML_param[2:3], ML_param(2:3,2))'; #rates= 1./ betaGm(ML_param(2:3,1), ML_param(2:3,2))' ; shapes[0] = alb.alphaGm(abs(ML_param[2]), ML_param[3]) shapes[1] = alb.alphaGm(abs(ML_param[4]), ML_param[5]) rates[0] = np.divide(1, betaGm(abs(ML_param[2]), ML_param[3])) rates[1] = np.divide(1, betaGm(abs(ML_param[4]), ML_param[5])) mix1.append({ 'gammas': resp, 'lambda': lambdap, 'pi': pipi, 'mu1': ML_param[0], 'tau1': np.true_divide(1, ML_param[1]), 'shapes': shapes, 'rates': rates, 'q': q, 'prior': prior, 'post': post, 'ML': ML, 'opts': opts }) elif MM == 'GIM': shapes = [0, 0] scales = [0, 0] shapes[0] = [alphaIG(abs(ML_param[2]), ML_param[3])] shapes[1] = [alphaIG(abs(ML_param[4]), ML_param[5])] scales[0] = [betaIG(abs(ML_param[2]), ML_param[3])] # betaIG(ML_param(3,1), ML_param(3,2)) ]; scales[1] = [betaIG(abs(ML_param[4]), ML_param[5])] mix1.append({ 'gammas': resp, 'lambda': lambdap, 'pi': pipi, 'mu1': ML_param[0], 'tau1': np.true_divide(1, ML_param[1]), 'shapes': shapes, 'scales': scales, 'q': q, 'prior': prior, 'post': post, 'ML': ML, 'opts': opts }) mix1 = mix1[0] return mix1
def mmfit2(x, maxiters, tol, MM): print MM all_params = [0, 1, 2, 1] init_mu1 = all_params[0] init_v1 = all_params[1] init_mu2 = all_params[2] init_v2 = all_params[3] init_PI = np.zeros(2) init_PI[0] = 0.5 init_PI[1] = 0.5 #First estimation initial parameters for inv gammas: alphas y betas #if MM==1: # 1#fix for gmm #el if MM == 2: init_a1 = alb.alphaGm(init_mu2, init_v2) init_b1 = alb.betaGm(init_mu2, init_v2) elif MM == 3: init_a1 = alb.alphaIG(init_mu2, init_v2) init_b1 = alb.betaIG(init_mu2, init_v2) #rename parameters for iteration tmp_mu1 = init_mu1 tmp_v1 = init_v1 tmp_mu2 = init_mu2 tmp_v2 = init_v2 tmp_a1 = init_a1 tmp_b1 = init_b1 tmp_PI = init_PI #make structures to save the parameters estimates at each iteration mu1 = np.zeros(maxiters + 2) v1 = np.zeros(maxiters + 2) mu2 = np.zeros(maxiters + 2) v2 = np.zeros(maxiters + 2) a1 = np.zeros(maxiters + 2) b1 = np.zeros(maxiters + 2) tmp_lik = np.zeros(maxiters + 2) real_lik = np.zeros(maxiters + 2) PI = np.zeros(2 * (maxiters + 2)) #3 because we fit 2 components PI = np.reshape(PI, [maxiters + 2, 2]) #save first values of this structures as the initialized values mu1[0] = tmp_mu1 v1[0] = tmp_v1 mu2[0] = tmp_mu2 v2[0] = tmp_v2 a1[0] = tmp_a1 b1[0] = tmp_b1 PI[0, 0] = tmp_PI[0] PI[0, 1] = tmp_PI[1] flag = 0 it = -1 #indexes of samples to assign 0 prob wrt non-gauss components xneg = find(x < pow(10, -14)) while flag == 0: it = it + 1 #print it #for it in range (0,maxiters): #print 'it1',it Nobj = sc.stats.norm(tmp_mu1, pow(tmp_v1, (1 / 2))) pGa = Nobj.pdf(x) pGa[pGa == 0] = pow(10, -14) if MM == 1: 1 elif MM == 2: dum2 = alb.gam(x, tmp_a1, tmp_b1) elif MM == 3: dum2 = alb.invgam(x, tmp_a1, tmp_b1) dum2[xneg] = 0 dum2[np.isnan(dum2)] = 0 dum2[np.isinf(dum2)] = 0 dum2[dum2 == 0] = pow(10, -14) D1 = np.multiply(np.ones(size(x)) * tmp_PI[0], pGa) D2 = np.multiply(np.ones(size(x)) * tmp_PI[1], dum2) D2[xneg] = 0 D = D1 + D2 R1 = np.divide(D1, np.ones(size(x)) * D) R2 = np.divide(D2, np.ones(size(x)) * D) resp = sc.column_stack([R1, R2]) #tmp_lik[it]=sum( np.multiply(resp[:,0],(log(tmp_PI[0])+log(pGa))) + np.multiply(resp[:,1],(log(tmp_PI[1])+log(dum2))));#bishop real_lik[it] = sum( log(np.multiply(tmp_PI[0], pGa) + np.multiply(tmp_PI[1], dum2))) #N=np.ones(2) #N[0]=sum(R1) #N[1]=sum(R2) #PI[0,0]=N[0]/sum(N) #PI[0,1]=N[1]/sum(N) #if it < maxiters-1: #M step N = np.ones(2) N[0] = sum(R1) N[1] = sum(R2) tmp_PI[0] = N[0] / sum(N) tmp_PI[1] = N[1] / sum(N) #print tmp_PI #print tmp_lik[it] #update gaussian mean and variance tmp_mu1 = [] tmp_mu1 = sum(np.multiply(resp[:, 0], x)) / N[0] tmp_v1 = [] tmp_v1 = sum(np.multiply(resp[:, 0], pow(x - tmp_mu1, 2))) / N[0] #if tmp_v <= 0.5: # tmp_v=0.5 #UPDATE EACH INVERSE GAMMA. tmp_mu2 = [] tmp_mu2 = sum(np.multiply(resp[:, 1], x)) / N[1] tmp_v2 = [] tmp_v2 = sum(np.multiply(resp[:, 1], pow(x - tmp_mu2, 2))) / N[1] if tmp_v2 < 0.2: tmp_v2 = 0.2 if MM == 2: tmp_a1 = alb.alphaGm(tmp_mu2, tmp_v2) tmp_b1 = alb.betaGm(tmp_mu2, tmp_v2) elif MM == 3: tmp_a1 = alb.alphaIG(tmp_mu2, tmp_v2) tmp_b1 = alb.betaIG(tmp_mu2, tmp_v2) if it > 20: if abs(real_lik[it] - real_lik[it - 1]) < tol: flag = 1 print it if it > (maxiters - 1): flag = 1 #print it if flag == 0: mu1[it + 1] = tmp_mu1 v1[it + 1] = tmp_v1 mu2[it + 1] = tmp_mu2 v2[it + 1] = tmp_v2 a1[it + 1] = alphaIG(tmp_mu2, tmp_v2) b1[it + 1] = betaIG(tmp_mu2, tmp_v2) PI[it + 1, :] = tmp_PI stmu1 = mu1[it] stv1 = v1[it] stmu2 = mu2[it] stv2 = v2[it] stPI = PI[it, :] lik = tmp_lik[it] stPI = PI[it, :] return stmu1, stv1, stmu2, stv2, stPI, lik, it, resp
def mmfit3(x, maxiters, tol, MM): import copy import numpy as np all_params = [0, 1, 3, 1, -3, 1] init_mu1 = all_params[0] init_v1 = all_params[1] init_mu2 = all_params[2] init_v2 = all_params[3] init_mu3 = all_params[4] init_v3 = all_params[5] init_PI = np.zeros(3) init_PI[0] = np.true_divide(1, 3) init_PI[1] = np.true_divide(1, 3) init_PI[2] = np.true_divide(1, 3) #First estimation initial parameters for inv gammas: alphas y betas #if MM==1: # 1#fix for gmm #el if MM == 2: init_a1 = alb.alphaGm(init_mu2, init_v2) init_b1 = alb.betaGm(init_mu2, init_v2) init_a2 = alb.alphaGm(-1 * init_mu3, init_v3) init_b2 = alb.betaGm(-1 * init_mu3, init_v3) elif MM == 3: init_a1 = alb.alphaIG(init_mu2, init_v2) init_b1 = alb.betaIG(init_mu2, init_v2) init_a2 = alb.alphaIG(-1 * init_mu3, init_v3) init_b2 = alb.betaIG(-1 * init_mu3, init_v3) #rename parameters for iteration tmp_mu1 = copy.deepcopy(init_mu1) tmp_v1 = copy.deepcopy(init_v1) tmp_mu2 = copy.deepcopy(init_mu2) tmp_v2 = copy.deepcopy(init_v2) tmp_mu3 = copy.deepcopy(init_mu3) tmp_v3 = copy.deepcopy(init_v3) tmp_a1 = copy.deepcopy(init_a1) tmp_b1 = copy.deepcopy(init_b1) tmp_a2 = copy.deepcopy(init_a2) tmp_b2 = copy.deepcopy(init_b2) tmp_PI = copy.deepcopy(init_PI) #make structures to save the parameters estimates at each iteration mu1 = np.zeros(maxiters + 2) v1 = np.zeros(maxiters + 2) mu2 = np.zeros(maxiters + 2) v2 = np.zeros(maxiters + 2) mu3 = np.zeros(maxiters + 2) v3 = np.zeros(maxiters + 2) a1 = np.zeros(maxiters + 2) b1 = np.zeros(maxiters + 2) a2 = np.zeros(maxiters + 2) b2 = np.zeros(maxiters + 2) tmp_lik = np.zeros(maxiters + 2) real_lik = np.zeros(maxiters + 2) PI = np.zeros(3 * (maxiters + 2)) #3 because we fit 3 components PI = np.reshape(PI, [maxiters + 2, 3]) #save first values of this structures as the initialized values mu1[0] = tmp_mu1 v1[0] = tmp_v1 mu2[0] = tmp_mu2 v2[0] = tmp_v2 mu3[0] = tmp_mu2 v3[0] = tmp_v2 a1[0] = tmp_a1 b1[0] = tmp_b1 a2[0] = tmp_a2 b2[0] = tmp_b2 #indexes of samples to assign 0 prob wrt each inv gammas xneg = find(x < pow(10, -14)) xpos = find(x > -pow(10, -14)) eps = np.finfo(float).eps #First Expectation step to evaluate initilization it=0 it = 0 Nobj = sc.stats.norm(tmp_mu1, np.power(tmp_v1, 0.5)) pGa = Nobj.pdf(x) pGa[pGa == 0] = 10**-14 if MM == 1: 1 elif MM == 2: dum2 = alb.gam(x, tmp_a1, tmp_b1) dum3 = alb.gam(-1 * x, tmp_a2, tmp_b2) elif MM == 3: dum2 = alb.invgam(x, tmp_a1, tmp_b1) dum3 = alb.invgam(-1 * x, tmp_a2, tmp_b2) dum2[xneg] = 0 dum3[xpos] = 0 D1 = np.multiply(np.ones(size(x)) * tmp_PI[0], pGa) D1[np.where(D1 < 10**-14)] = eps D2 = np.multiply(np.ones(size(x)) * tmp_PI[1], dum2) D2[np.where(D2 < 10**-14)] = eps D3 = np.multiply(np.ones(size(x)) * tmp_PI[2], dum3) D3[np.where(D3 < 10**-14)] = eps D = D1 + D2 + D3 R1 = np.divide(D1, np.ones(size(x)) * D) R2 = np.divide(D2, np.ones(size(x)) * D) R3 = np.divide(D3, np.ones(size(x)) * D) resp = sc.column_stack([R1, R2, R3]) #M step N = np.ones(3) N[0] = sum(R1) N[1] = sum(R2) N[2] = sum(R3) tmp_PI[0] = N[0] / sum(N) tmp_PI[1] = N[1] / sum(N) tmp_PI[2] = N[2] / sum(N) #tmp_lik[it]=sum( np.multiply(resp[:,0],(log(tmp_PI[0])+log(pGa))) + np.multiply(resp[:,1],(log(tmp_PI[1])+log(dum2))) + np.multiply(resp[:,2],(log(tmp_PI[2])+log(dum3))) );#bishop real_lik[it] = sum( log( np.multiply(tmp_PI[0], pGa) + np.multiply(tmp_PI[1], dum2) + np.multiply(tmp_PI[2], dum3))) trol = np.zeros([3, x.shape[0]]) trol[0, :] = np.multiply(D1, tmp_PI[0]) trol[1, :] = np.multiply(D2, tmp_PI[1]) trol[2, :] = np.multiply(D3, tmp_PI[2]) real_lik[it] = np.sum(np.log(trol.sum(0))) #ITERATE flag = 0 while flag == 0: it = it + 1 #update gaussian mean and variance tmp_mu1 = [] tmp_mu1 = sum(np.multiply(resp[:, 0], x)) / N[0] tmp_v1 = [] tmp_v1 = sum(np.multiply(resp[:, 0], pow(x - tmp_mu1, 2))) / N[0] #if tmp_v <= 0.5: # tmp_v=0.5 #UPDATE EACH INVERSE GAMMA. tmp_mu2 = [] tmp_mu2 = sum(np.multiply(resp[:, 1], x)) / N[1] tmp_v2 = [] tmp_v2 = sum(np.multiply(resp[:, 1], pow(x - tmp_mu2, 2))) / N[1] #if tmp_v2< 0.1:#pow(10,-1): #tmp_v2=0.1 tmp_mu3 = [] tmp_mu3 = sum(np.multiply(resp[:, 2], x)) / N[2] tmp_v3 = [] tmp_v3 = sum(np.multiply(resp[:, 2], pow(x - tmp_mu3, 2))) / N[2] #if tmp_v3< 0.1: #tmp_v3=0.1 if MM == 2: tmp_a1 = alb.alphaGm(tmp_mu2, tmp_v2) tmp_b1 = alb.betaGm(tmp_mu2, tmp_v2) tmp_a2 = alb.alphaGm(-1 * tmp_mu3, tmp_v3) tmp_b2 = alb.betaGm(-1 * tmp_mu3, tmp_v3) elif MM == 3: tmp_a1 = alb.alphaIG(tmp_mu2, tmp_v2) tmp_b1 = alb.betaIG(tmp_mu2, tmp_v2) tmp_a2 = alb.alphaIG(-1 * tmp_mu3, tmp_v3) tmp_b2 = alb.betaIG(-1 * tmp_mu3, tmp_v3) #print 'it_num',it Nobj = sc.stats.norm(tmp_mu1, np.power(tmp_v1, 0.5)) pGa = Nobj.pdf(x) pGa[pGa == 0] = 10**-14 if MM == 1: 1 elif MM == 2: dum2 = alb.gam(x, tmp_a1, tmp_b1) dum3 = alb.gam(-1 * x, tmp_a2, tmp_b2) elif MM == 3: dum2 = alb.invgam(x, tmp_a1, tmp_b1) dum3 = alb.invgam(-1 * x, tmp_a2, tmp_b2) dum2[xneg] = 0 dum3[xpos] = 0 dum2[np.isnan(dum2)] = 0 dum2[np.isinf(dum2)] = 0 dum2[dum2 == 0] = 10**-14 dum3[np.isnan(dum3)] = 0 dum3[np.isinf(dum3)] = 0 dum3[dum3 == 0] = 10**-14 D1 = np.multiply(np.ones(size(x)) * tmp_PI[0], pGa) D1[np.where(D1 < 10**-14)] = eps D2 = np.multiply(np.ones(size(x)) * tmp_PI[1], dum2) D2[np.where(D2 < 10**-14)] = eps D3 = np.multiply(np.ones(size(x)) * tmp_PI[2], dum3) D3[np.where(D3 < 10**-14)] = eps #D3[xpos]=0; #D2[xneg]=0; D = D1 + D2 + D3 R1 = np.divide(D1, np.ones(size(x)) * D) R2 = np.divide(D2, np.ones(size(x)) * D) R3 = np.divide(D3, np.ones(size(x)) * D) resp = sc.column_stack([R1, R2, R3]) #M step N = np.ones(3) N[0] = sum(R1) N[1] = sum(R2) N[2] = sum(R3) tmp_PI[0] = N[0] / sum(N) tmp_PI[1] = N[1] / sum(N) tmp_PI[2] = N[2] / sum(N) tmp_PI[np.where(tmp_PI < 10**-14)] = eps #tmp_lik[it]=sum( np.multiply(resp[:,0],(log(tmp_PI[0])+log(pGa))) + np.multiply(resp[:,1],(log(tmp_PI[1])+log(dum2))) + np.multiply(resp[:,2],(log(tmp_PI[2])+log(dum3))) );#bishop real_lik[it] = sum( log( np.multiply(tmp_PI[0], pGa) + np.multiply(tmp_PI[1], dum2) + np.multiply(tmp_PI[2], dum3))) trol = np.zeros([3, x.shape[0]]) trol[0, :] = np.multiply(D1, tmp_PI[0]) trol[1, :] = np.multiply(D2, tmp_PI[1]) trol[2, :] = np.multiply(D3, tmp_PI[2]) real_lik[it] = np.sum(np.log(trol.sum(0))) if (abs((real_lik[it] - real_lik[it - 1]) / real_lik[it - 1]) < tol) | (it > maxiters): flag = 1 stmu1 = tmp_mu1 #mu1[it]; stv1 = tmp_v1 #v1[it]; stmu2 = tmp_mu2 #mu2[it]; stv2 = tmp_v2 #v2[it]; stmu3 = tmp_mu3 #mu3[it]; stv3 = tmp_v3 #v3[it]; stPI = tmp_PI #PI[it,:]; lik = real_lik[0:it] #tmp_lik[it]; return stmu1, stv1, stmu2, stv2, stmu3, stv3, stPI, lik, it, resp