def run_MCMC_ex(which_par,niter,save_title, renorm_var,firstiter=0,seed="none",guess="random"): """ Functions that runs the MCMC for a given set of parameters Keyword Arguments: which_par -- a list of indices, corresponding to the order defined above, exemple [0,2] means ombh2,tau if order is [ombh2,omch2,tau,As,ns,H0] niter -- number of iterations in MCMC renorm_var -- factor multipling the variance, to play around for better acceptance rate. """ cov_new_temp = cov_new[which_par,:][:,which_par] * renorm_var string_temp = strings[which_par] titles_temp = titles[which_par] x_mean_temp = x_mean[which_par] priors_central_temp = priors_central[which_par] priors_invvar_temp = priors_invvar[which_par] print titles_temp sys.stdout.flush() dd2 = cb.update_dic(dd,x_mean_temp,string_temp) cl = cb.generate_spectrum(dd2)[:lmax+1,1] cl[:2] = 1.e-35 if guess=="random": guess_param = PS2P.prop_dist_form_params(x_mean_temp,cov_new_temp) else : guess_param = guess tt1 = time.time() save_string = "outputs/chain_ex_%s_%s_%d_%d"%(save_title,str(which_par).replace(',','').replace('[','').replace(']','').replace(' ',''),np.random.randint(0,100000),niter) print save_string sys.stdout.flush() testss = np.array(MH.MCMC_log(guess_param, JJi.functional_form_params_n,PS2P.prop_dist_form_params, PS2P.prop_func_form_params,niter,PS2P.Gaussian_priors_func,firstiter,seed,save_string,[dlm,string_temp,dd,nl,bl,cl],[x_mean_temp*0,np.matrix(cov_new_temp)],[priors_central_temp,priors_invvar_temp])) print time.time() - tt1 np.save(save_string+".npy",testss) return testss
def run_MCMC(which_par,niter,save_title, renorm_var): """ Functions that runs the MCMC for a given set of parameters Keyword Arguments: which_par -- a list of indices, corresponding to the order defined above, exemple [0,2] means ombh2,tau if order is [ombh2,omch2,tau,As,ns,H0] niter -- number of iterations in MCMC renorm_var -- factor multipling the variance, to play around for better acceptance rate. """ cov_new_temp = cov_new[which_par,:][:,which_par] * renorm_var string_temp = strings[which_par] titles_temp = titles[which_par] x_mean_temp = x_mean[which_par] priors_central_temp = priors_central[which_par] priors_invvar_temp = priors_invvar[which_par] print titles_temp # generate first guess parameters guess_param = PS2P.prop_dist_form_params(x_mean_temp,cov_new_temp) # generate first fluctuation map dd2 = cb.update_dic(dd,guess_param,string_temp) cl = cb.generate_spectrum(dd2)[:,1] cl[:2] = 1.e-35 renorm = CG.renorm_term(cl,bl,nl) fluc = hp.almxfl(CG.generate_w1term(cl[:lmax+1],bl[:lmax+1],nl[:lmax+1]) + CG.generate_w0term(cl[:lmax+1]),renorm) # the core of the MCMC testss = np.array(MH.MCMC_log_Jeff_test(guess_param, JJi.target_distrib_newrescale,PS2P.prop_dist_form_params, PS2P.prop_func_form_params,niter,PS2P.Gaussian_priors_func,[[dlm,string_temp,dd,nl[:lmax+1],bl[:lmax+1]],[cl[:lmax+1],fluc]],[x_mean_temp*0,np.matrix(cov_new_temp)],[priors_central_temp,priors_invvar_temp])) np.save("chain_%s_%s_%d_%d.npy"%(save_title,str(which_par).replace(',','').replace('[','').replace(']','').replace(' ',''),np.random.randint(0,100000),niter),testss) return testss
def test_3rd_Term(list_guess): #initial guess guess_param = PS2P.prop_dist_form_params(x_mean,cov_new) # generate first fluctuation map dd2 = cb.update_dic(dd,guess_param,strings) cl = cb.generate_spectrum(dd2)[:,1] cl[:2] = 1.e-35 renorm = CG.renorm_term(cl,bl,nl) fluc = hp.almxfl(CG.generate_w1term(cl[:lmax+1],bl[:lmax+1],nl[:lmax+1]) + CG.generate_w0term(cl[:lmax+1]),renorm) Cl_i,fluc_i = [cl,fluc] list_save=[] for i in range(len(list_guess)): print i test_param = list_guess[i] Like_3,Cl_ip1,fluc_ip1,fluc_i_rescaled = test_fluctuation_term(test_param,[dlm,strings,dd2,nl[:lmax+1],bl[:lmax+1]],[Cl_i, fluc_i]) Cl_i,fluc_i = [Cl_ip1,fluc_ip1] list_save.append([Like_3,Cl_ip1,fluc_ip1,fluc_i_rescaled]) return list_save
def run_MCMC(which_par,niter,save_title, renorm_var): """ Functions that runs the MCMC for a given set of parameters Keyword Arguments: which_par -- a list of indices, corresponding to the order defined above, exemple [0,2] means ombh2,tau if order is [ombh2,omch2,tau,As,ns,H0] niter -- number of iterations in MCMC renorm_var -- factor multipling the variance, to play around for better acceptance rate. """ cov_new_temp = cov_new[which_par,:][:,which_par] * renorm_var string_temp = strings[which_par] titles_temp = titles[which_par] x_mean_temp = x_mean[which_par] priors_central_temp = priors_central[which_par] priors_invvar_temp = priors_invvar[which_par] print titles_temp guess_param = PS2P.prop_dist_form_params(x_mean_temp,cov_new_temp) testss = np.array(MH.MCMC_log(guess_param, PS2P.functional_form_params_n,PS2P.prop_dist_form_params, PS2P.prop_func_form_params,niter,PS2P.Gaussian_priors_func,[dlm,string_temp,dd,nl,bl],[x_mean_temp*0,np.matrix(cov_new_temp)],[priors_central_temp,priors_invvar_temp])) #print "%.2f rejected; %.2f accepted; %.2f Lucky accepted"%((flag==0).mean(),(flag==1).mean(),(flag==2).mean()) np.save("chain_%s_%s_%d_%d.npy"%(save_title,str(which_par).replace(',','').replace('[','').replace(']','').replace(' ',''),np.random.randint(0,100000),niter),testss) return testss