def test_tophat_multivariate(self): min = [1, 2] max = [5, 6] with pytest.raises(AssertionError): abcpmc.TophatPrior(max, min) prior = abcpmc.TophatPrior(min, max) vals = prior() assert np.all(vals > min) assert np.all(vals <= max) assert prior(theta=[0, 0]) == 0 assert prior(theta=[1, 2]) == 1 assert prior(theta=[2, 3]) == 1 assert prior(theta=[5, 5]) == 0 assert prior(theta=[6, 6]) == 0
def test_sample(self): N = 10 T = 2 postfn = lambda theta: None dist = 1.0 distfn = lambda X, Y: dist prior = abcpmc.TophatPrior([0], [100]) sampler = abcpmc.Sampler(N, 0, postfn, distfn) eps = 10 eps_proposal = abcpmc.ConstEps(T, eps) for i, pool in enumerate(sampler.sample(prior, eps_proposal)): assert pool is not None assert pool.t == i assert pool.ratio == 1.0 assert pool.eps == eps assert len(pool.thetas) == N assert np.all(pool.thetas != 0.0) assert len(pool.dists) == N assert np.all(pool.dists == dist) assert len(pool.ws) == N assert np.allclose(np.sum(pool.ws), 1.0) assert i + 1 == T
def test_tophat_univariate(self): min = 1 max = 5 with pytest.raises(AssertionError): abcpmc.TophatPrior(max, min) prior = abcpmc.TophatPrior(min, max) vals = prior() assert vals > min assert vals < max assert prior(theta=0) == 0 assert prior(theta=min) == 1 assert prior(theta=2) == 1 assert prior(theta=max) == 0 assert prior(theta=6) == 0
def abc(pewl, name=None, niter=None, npart=None, restart=None): if restart is not None: # read pool theta_init = np.loadtxt( os.path.join(abc_dir, 'theta.t%i.dat' % restart)) rho_init = np.loadtxt( os.path.join(abc_dir, 'rho.t%i.dat' % restart)) w_init = np.loadtxt( os.path.join(abc_dir, 'w.t%i.dat' % restart)) init_pool = abcpmc.PoolSpec(restart, None, None, theta_init, rho_init, w_init) npart = len(theta_init) print('%i particles' % npart) else: init_pool = None #--- inference with ABC-PMC below --- # prior prior = abcpmc.TophatPrior(prior_min, prior_max) # sampler abcpmc_sampler = abcpmc.Sampler( N=npart, # N_particles Y=x_obs, # data postfn=_sumstat_model_wrap, # simulator dist=_distance_metric_wrap, # distance metric pool=pewl, postfn_kwargs={'dem': dem}#, dist_kwargs={'method': 'L2', 'phi_err': phi_err} ) # threshold eps = abcpmc.ConstEps(niter, eps0) print('eps0', eps.eps) for pool in abcpmc_sampler.sample(prior, eps, pool=init_pool): eps_str = ", ".join(["{0:>.4f}".format(e) for e in pool.eps]) print("T: {0}, eps: [{1}], ratio: {2:>.4f}".format(pool.t, eps_str, pool.ratio)) for i, (mean, std) in enumerate(zip(*abcpmc.weighted_avg_and_std(pool.thetas, pool.ws, axis=0))): print(u" theta[{0}]: {1:>.4f} \u00B1 {2:>.4f}".format(i, mean,std)) print('dist', pool.dists) # write out theta, weights, and distances to file dustInfer.writeABC('eps', pool, abc_dir=abc_dir) dustInfer.writeABC('theta', pool, abc_dir=abc_dir) dustInfer.writeABC('w', pool, abc_dir=abc_dir) dustInfer.writeABC('rho', pool, abc_dir=abc_dir) # update epsilon based on median thresholding eps.eps = np.median(pool.dists, axis=0) print('eps%i' % pool.t, eps.eps) print('----------------------------------------') #if pool.ratio <0.2: break abcpmc_sampler.close() return None
def test_sample(self): N = 10 T = 2 postfn = lambda theta: None dist = lambda X, Y: 0 prior = abcpmc.TophatPrior([0], [100]) sampler = abcpmc.Sampler(N, 0, postfn, dist) eps_proposal = abcpmc.ConstEps(T, 10) for i, pool in enumerate(sampler.sample(prior, eps_proposal)): assert pool is not None assert len(pool.thetas) == N assert i + 1 == T
def sample(T, eps_val, eps_min): prior = abcpmc.TophatPrior([10., np.log(.1), 11.02, .8, 13.], [13., np.log(.7), 13.02, 1.3, 14.]) abcpmc_sampler = abcpmc.Sampler(N=1000, Y=data, postfn=simz, dist=distance, pool=mpi_pool) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal #abcpmc.Sampler.particle_proposal_kwargs = {'k': 50} #abcpmc_sampler.particle_proposal_cls = abcpmc.KNNParticleProposal eps = abcpmc.ConstEps(T, [1.e13, 1.e13]) pools = [] for pool in abcpmc_sampler.sample(prior, eps): print("T:{0},ratio: {1:>.4f}".format(pool.t, pool.ratio)) print eps(pool.t) plot_thetas(pool.thetas, pool.ws, pool.t) np.savetxt( "/home/mj/public_html/nbar_gmf5_Mr20_theta_t" + str(t) + ".dat", theta) np.savetxt("/home/mj/public_html/nbar_gmf5_Mr20_w_t" + str(t) + ".dat", w) if pool.t < 3: eps.eps = np.percentile(np.atleast_2d(pool.dists), 50, axis=0) elif (pool.t > 2) and (pool.t < 20): eps.eps = np.percentile(np.atleast_2d(pool.dists), 75, axis=0) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal else: eps.eps = np.percentile(np.atleast_2d(pool.dists), 90, axis=0) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal #if eps.eps < eps_min: # eps.eps = eps_min pools.append(pool) #abcpmc_sampler.close() return pools
def config(self): try: import abcpmc except ImportError: raise ValueError("To use ABC PMC you need to install it with pip install abcpmc") global abc_pipeline abc_pipeline = self.pipeline self.threshold = self.read_ini("threshold",str, 'LinearEps') self.metric_kw = self.read_ini("metric",str, 'chi2') #mean, chi2 or other if self.metric_kw =='other': self.distance_func = self.read_ini("distance_func",str, None) #only for other metric, self.metric = self.distance_func[1:-1] self.epimax = self.read_ini('epimax', float,5.0) self.epimin = self.read_ini('epimin',float, 1.0) self.part_prop = self.read_ini("particle_prop",str,'weighted_cov') self.set_prior = self.read_ini("set_prior",str,'uniform') self.param_cov = self.read_ini("param_cov_file",str,'None') self.knn = self.read_ini("num_nn",int, 10) self.npart = self.read_ini("npart",int,100) self.niter = self.read_ini("niter",int,2) self.ngauss = self.read_ini("ngauss",int,4) self.run_multigauss = self.read_ini("run_multigauss",bool,False) self.diag_cov = self.read_ini("diag_cov",bool,False) self.ndim = len(self.pipeline.varied_params) #options for decreasing threshold if self.threshold == 'ConstEps': self.eps = abcpmc.ConstEps(self.niter, self.epimax) elif self.threshold == 'ExpEps': self.eps = abcpmc.ExponentialEps(self.niter, self.epimax,self.epimin) else: self.eps = abcpmc.LinearEps(self.niter, self.epimax, self.epimin) print("\nRunning ABC PMC") print("with %d particles, %s prior, %s threshold, %d iterations over (%f,%f), %s kernal \n" % (self.npart,self.set_prior,self.threshold,self.niter,self.epimax,self.epimin,self.part_prop)) #Initial positions for all of the parameters self.p0 = np.array([param.start for param in self.pipeline.varied_params]) #Data file is read for use in dist() for each step #parameter covariance used in the prior self.data, self.cov, self.invcov = self.load_data() #At the moment the same prior (with variable hyperparameters) is # used for all parameters - would be nice to change this to be more flexible self.pmin = np.zeros(self.ndim) self.pmax = np.zeros(self.ndim) for i,pi in enumerate(self.pipeline.varied_params): self.pmin[i] = pi.limits[0] self.pmax[i] = pi.limits[1] if self.set_prior.lower() == 'uniform': self.prior = abcpmc.TophatPrior(self.pmin,self.pmax) elif self.set_prior.lower() == 'gaussian': sigma2 = np.loadtxt(self.param_cov) if len(np.atleast_2d(sigma2)[0][:]) != self.ndim: raise ValueError("Cov matrix for Gaussian prior has %d columns for %d params" % len(np.atleast_2d(sigma2)[0][:]), self.ndim) else: self.prior = abcpmc.GaussianPrior(self.p0, np.atleast_2d(sigma2)) else: raise ValueError("Please set the ABC option 'set_prior' to either 'uniform' or 'gaussian'. At the moment only 'uniform' works in the general case.") #create sampler self.sampler = abcpmc.Sampler(N=self.npart, Y=self.data, postfn=abc_model, dist=self.dist) #set particle proposal kernal abcpmc.Sampler.particle_proposal_kwargs = {} if self.part_prop == 'KNN': abcpmc.Sampler.particle_proposal_kwargs = {'k':self.knn} self.sampler.particle_proposal_cls = abcpmc.KNNParticleProposal elif self.part_prop == 'OLCM': self.sampler.particle_proposal_cls = abcpmc.OLCMParticleProposal self.converged = False
def ABCpmc_HOD(T, eps_val, N_part=1000, prior_name='first_try', observables=['nbar', 'xi'], data_dict={'Mr': 21}, output_dir=None): ''' ABC-PMC implementation. Parameters ---------- - T : Number of iterations - eps_val : - N_part : Number of particles - observables : list of observables. Options are 'nbar', 'gmf', 'xi' - data_dict : dictionary that specifies the observation keywords ''' if output_dir is None: output_dir = util.dat_dir() else: pass #Initializing the vector of observables and inverse covariance matrix if observables == ['xi']: fake_obs = Data.data_xi(**data_dict) fake_obs_cov = Data.data_cov(**data_dict)[1:16, 1:16] xi_Cii = np.diag(fake_obs_cov) elif observables == ['nbar', 'xi']: fake_obs = np.hstack( [Data.data_nbar(**data_dict), Data.data_xi(**data_dict)]) fake_obs_cov = Data.data_cov(**data_dict)[:16, :16] Cii = np.diag(fake_obs_cov) xi_Cii = Cii[1:] nbar_Cii = Cii[0] elif observables == ['nbar', 'gmf']: fake_obs = np.hstack( [Data.data_nbar(**data_dict), Data.data_gmf(**data_dict)]) fake_obs_cov = Data.data_cov('nbar_gmf', **data_dict) Cii = np.diag(fake_obs_cov) gmf_Cii = Cii[1:] nbar_Cii = Cii[0] # True HOD parameters data_hod_dict = Data.data_hod_param(Mr=data_dict['Mr']) data_hod = np.array([ data_hod_dict['logM0'], # log M0 np.log(data_hod_dict['sigma_logM']), # log(sigma) data_hod_dict['logMmin'], # log Mmin data_hod_dict['alpha'], # alpha data_hod_dict['logM1'] # log M1 ]) # Priors prior_min, prior_max = PriorRange(prior_name) prior = abcpmc.TophatPrior(prior_min, prior_max) prior_range = np.zeros((len(prior_min), 2)) prior_range[:, 0] = prior_min prior_range[:, 1] = prior_max # simulator our_model = HODsim(Mr=data_dict['Mr']) # initialize model kwargs = {'prior_range': prior_range, 'observables': observables} def simz(tt): sim = our_model.sum_stat(tt, **kwargs) if sim is None: pickle.dump(tt, open("simz_crash_theta.p", 'wb')) pickle.dump(kwargs, open('simz_crash_kwargs.p', 'wb')) raise ValueError('Simulator is giving NonetType') return sim def multivariate_rho(datum, model): #print datum , model dists = [] if observables == ['nbar', 'xi']: dist_nbar = (datum[0] - model[0])**2. / nbar_Cii dist_xi = np.sum((datum[1:] - model[1:])**2. / xi_Cii) dists = [dist_nbar, dist_xi] elif observables == ['nbar', 'gmf']: dist_nbar = (datum[0] - model[0])**2. / nbar_Cii dist_gmf = np.sum((datum[1:] - model[1:])**2. / gmf_Cii) dists = [dist_nbar, dist_gmf] elif observables == ['xi']: dist_xi = np.sum((datum - model)**2. / xi_Cii) dists = [dist_xi] #print np.array(dists) return np.array(dists) mpi_pool = mpi_util.MpiPool() abcpmc_sampler = abcpmc.Sampler( N=N_part, #N_particles Y=fake_obs, #data postfn=simz, #simulator dist=multivariate_rho, #distance function pool=mpi_pool) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal eps = abcpmc.MultiConstEps(T, eps_val) pools = [] f = open("abc_tolerance.dat", "w") f.close() eps_str = '' for pool in abcpmc_sampler.sample(prior, eps): #while pool.ratio > 0.01: new_eps_str = '\t'.join(eps(pool.t).astype('str')) + '\n' if eps_str != new_eps_str: # if eps is different, open fiel and append f = open("abc_tolerance.dat", "a") eps_str = new_eps_str f.write(eps_str) f.close() print("T:{0},ratio: {1:>.4f}".format(pool.t, pool.ratio)) print eps(pool.t) # plot theta plot_thetas(pool.thetas, pool.ws, pool.t, Mr=data_dict["Mr"], truths=data_hod, plot_range=prior_range, observables=observables, output_dir=output_dir) if (pool.t < 4) and (pool.t > 2): pool.thetas = np.loadtxt( "/home/mj/abc/halo/dat/gold/nbar_xi_Mr21_theta_t3.mercer.dat") pool.ws = np.loadtxt( "/home/mj/abc/halo/dat/gold/nbar_xi_Mr21_w_t3.mercer.dat") eps.eps = [1.12132735353, 127.215586776] # write theta and w to file theta_file = ''.join([ output_dir, util.observable_id_flag(observables), '_Mr', str(data_dict["Mr"]), '_theta_t', str(pool.t), '.mercer.dat' ]) w_file = ''.join([ output_dir, util.observable_id_flag(observables), '_Mr', str(data_dict["Mr"]), '_w_t', str(pool.t), '.mercer.dat' ]) np.savetxt(theta_file, pool.thetas) np.savetxt(w_file, pool.ws) if pool.t < 3: eps.eps = np.percentile(np.atleast_2d(pool.dists), 50, axis=0) elif (pool.t > 2) and (pool.t < 20): eps.eps = np.percentile(np.atleast_2d(pool.dists), 75, axis=0) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal else: eps.eps = np.percentile(np.atleast_2d(pool.dists), 90, axis=0) abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal #if eps.eps < eps_min: # eps.eps = eps_min pools.append(pool) #abcpmc_sampler.close() return pools
mock_nbar = np.loadtxt("mock_nbar.dat") data_nbar = np.mean(mock_nbar) mocks_wp = np.loadtxt("wps.dat") data_wp = np.mean(mocks_wp, axis=0) data = [data_nbar, data_wp] covariance = np.loadtxt("wp_covariance.dat") cii = np.diag(covariance) covar_nz = np.var(mock_nbar) """list of true parameters""" data_hod = np.array([11.92, 0.39, 12.79, 1.15, 13.94]) """Prior""" prior = abcpmc.TophatPrior([9., .1, 12.5, .9, 13.6], [15., 1., 13.09, 1.45, 14.25]) prior_dict = { 'logM0': { 'shape': 'uniform', 'min': 9., 'max': 15. }, 'sigma_logM': { 'shape': 'uniform', 'min': 0., 'max': 1. }, 'logMmin': { 'shape': 'uniform', 'min': 12.5, 'max': 13.09
params['sim'] = 'simba' params['dem'] = 'slab_noll_simple' params['prior_min'] = np.array([0., -4]) params['prior_max'] = np.array([10., 4.]) else: raise NotImplementedError return params if __name__=="__main__": ####################### inputs ####################### name = sys.argv[1] # name of ABC run niter = int(sys.argv[2]) # number of iterations print('plot %s ABC iteration %i' % (name, niter)) ###################################################### dat_dir = os.environ['GALPOPFM_DIR'] abc_dir = os.path.join(dat_dir, 'abc', name) params = run_params(name) sim = params['sim'] dem = params['dem'] prior_min = params['prior_min'] prior_max = params['prior_max'] prior = abcpmc.TophatPrior(prior_min, prior_max) # plot the pools plot_pool(niter, prior=prior, dem=dem, abc_dir=abc_dir) # plot ABCC summary statistics abc_sumstat(niter, sim=sim, dem=dem, abc_dir=abc_dir) #_examine_distance(niter, sim=sim, dem=dem)
def ABC(T, eps_input, Npart=1000, cen_tf=None, cen_prior_name=None, cen_abcrun=None): ''' ABC-PMC implementation. Parameters ---------- T : (int) Number of iterations eps_input : (float) Starting epsilon threshold value N_part : (int) Number of particles prior_name : (string) String that specifies what prior to use. abcrun : (string) String that specifies abc run information ''' abcinh = ABCInherit(cen_tf, abcrun=cen_abcrun, prior_name=cen_prior_name) # Data (Group Catalog Satellite fQ) grpcat = GroupCat(Mrcut=18, position='satellite') grpcat.Read() qfrac = Fq() m_bin = np.array([9.7, 10.1, 10.5, 10.9, 11.3]) M_mid = 0.5 * (m_bin[:-1] + m_bin[1:]) sfq = qfrac.Classify(grpcat.mass, grpcat.sfr, np.median(grpcat.z), sfms_prop=abcinh.sim_kwargs['sfr_prop']['sfms']) ngal, dum = np.histogram(grpcat.mass, bins=m_bin) ngal_q, dum = np.histogram(grpcat.mass[sfq == 'quiescent'], bins=m_bin) data_sum = [M_mid, ngal_q.astype('float') / ngal.astype('float')] # Simulator cen_assigned_sat_file = ''.join([ '/data1/hahn/pmc_abc/pickle/', 'satellite', '.cenassign', '.', cen_abcrun, '_ABC', '.', cen_prior_name, '_prior', '.p' ]) if not os.path.isfile(cen_assigned_sat_file): sat_cen = AssignCenSFR(cen_tf, abcrun=cen_abcrun, prior_name=cen_prior_name) pickle.dump(sat_cen, open(cen_assigned_sat_file, 'wb')) else: sat_cen = pickle.load(open(cen_assigned_sat_file, 'rb')) def Simz(tt): # Simulator (forward model) tqdel_dict = {'name': 'explin', 'm': tt[0], 'b': tt[1]} sat_evol = EvolveSatSFR(sat_cen, tqdelay_dict=tqdel_dict) sfq_sim = qfrac.Classify(sat_evol.mass, sat_evol.sfr, sat_evol.zsnap, sfms_prop=sat_evol.sfms_prop) ngal_sim, dum = np.histogram(sat_evol.mass, bins=m_bin) ngal_q_sim, dum = np.histogram(sat_evol.mass[sfq_sim == 'quiescent'], bins=m_bin) sim_sum = [ M_mid, ngal_q_sim.astype('float') / ngal_sim.astype('float') ] return sim_sum # Priors prior_min = [-11.75, 2.] prior_max = [-10.25, 4.] prior = abcpmc.TophatPrior(prior_min, prior_max) # ABCPMC prior object def rho(simum, datum): datum_dist = datum[1] simum_dist = simum[1] drho = np.sum((datum_dist - simum_dist)**2) return drho abcrun_flag = cen_abcrun + '_central' theta_file = lambda pewl: ''.join([ code_dir(), 'dat/pmc_abc/', 'Satellite.tQdelay.theta_t', str(pewl), '_', abcrun_flag, '.dat' ]) w_file = lambda pewl: ''.join([ code_dir(), 'dat/pmc_abc/', 'Satellite.tQdelay.w_t', str(pewl), '_', abcrun_flag, '.dat' ]) dist_file = lambda pewl: ''.join([ code_dir(), 'dat/pmc_abc/', 'Satellite.tQdelay.dist_t', str(pewl), '_', abcrun_flag, '.dat' ]) eps_file = ''.join([ code_dir(), 'dat/pmc_abc/Satellite.tQdelay.epsilon_', abcrun_flag, '.dat' ]) eps = abcpmc.ConstEps(T, eps_input) try: mpi_pool = mpi_util.MpiPool() abcpmc_sampler = abcpmc.Sampler( N=Npart, # N_particles Y=data_sum, # data postfn=Simz, # simulator dist=rho, # distance function pool=mpi_pool) except AttributeError: abcpmc_sampler = abcpmc.Sampler( N=Npart, # N_particles Y=data_sum, # data postfn=Simz, # simulator dist=rho) # distance function abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal pools = [] f = open(eps_file, "w") f.close() eps_str = '' for pool in abcpmc_sampler.sample(prior, eps, pool=None): print '----------------------------------------' print 'eps ', pool.eps new_eps_str = '\t' + str(pool.eps) + '\n' if eps_str != new_eps_str: # if eps is different, open fiel and append f = open(eps_file, "a") eps_str = new_eps_str f.write(eps_str) f.close() print("T:{0},ratio: {1:>.4f}".format(pool.t, pool.ratio)) print eps(pool.t) # write theta, weights, and distances to file np.savetxt(theta_file(pool.t), pool.thetas, header='tQdelay_slope, tQdelay_offset') np.savetxt(w_file(pool.t), pool.ws) np.savetxt(dist_file(pool.t), pool.dists) # update epsilon based on median thresholding eps.eps = np.median(pool.dists) pools.append(pool) return pools
"""Sum of the abs difference of the mean of the simulated and obs data""" return np.sum(np.abs(np.mean(x, axis=0) - np.mean(y, axis=0))) def MSE(x, y): """ Mean squared error distance measure""" return np.mean(np.power(x - y, 2)) def std(x, y): return abs(np.std(x) - np.std(y)) ''' Setup ''' # 'Best' guess about the distribution, uniform distribution prior = abcpmc.TophatPrior([0.0, 1.0], [2.0, 3.0]) # As threshold for accepting draws from the prior we use the alpha-th percentile # of the sorted distances of the particles of the current iteration alpha = 75 T = 2 # sample for T iterations eps_start = 20.0 # sufficiently high starting threshold (like 5x the variability or more) eps = abcpmc.ConstEps(T, eps_start) ''' Sampling function ''' def launch(threads): eps = abcpmc.ConstEps(T, eps_start) pools = [] # pool is a namedtuple representing the values of one iteration
def euclidian(x, y): return np.linalg.norm(x - y) def std(x, y): return abs(np.std(x) - np.std(y)) ''' Setup ''' # 'Best' guess about the distribution prior = abcpmc.GaussianPrior(mu=[1.0, 1.0], sigma=np.eye(2) * 0.5) # 'Best' guess about the distribution, uniform distribution prior = abcpmc.TophatPrior([0.0, 0.0], [5.0, 5.0]) # As threshold for accepting draws from the prior we use the alpha-th percentile # of the sorted distances of the particles of the current iteration alpha = 75 T = 10 # sample for T iterations eps_start = 1.0 # sufficiently high starting threshold eps = abcpmc.ConstEps(T, eps_start) ''' Sampling function ''' def launch(threads): eps = abcpmc.ConstEps(T, eps_start) pools = [] # pool is a namedtuple representing the values of one iteration
model.populate_mock() data_nbar = model.mock.number_density data_xir = model.mock.compute_galaxy_clustering()[1] np.savetxt("xir_Mr20.dat", data_xir) data = [data_nbar, data_xir] covariance = np.loadtxt("clustering_covariance_Mr20.dat") cii = np.diag(covariance) covar_nbar = np.var(mock_nbar) """list of true parameters""" data_hod = np.array([11.38, 0.26, 12.02, 1.06, 13.31]) """Prior""" prior = abcpmc.TophatPrior([10., .1, 11.02, .8, 13.], [13., .5, 13.02, 1.3, 14.]) prior_dict = { 'logM0': { 'shape': 'uniform', 'min': 10., 'max': 13. }, 'sigma_logM': { 'shape': 'uniform', 'min': .1, 'max': .5 }, 'logMmin': { 'shape': 'uniform', 'min': 11.02, 'max': 13.02
def FixedTauABC(T, eps_input, fixtau='satellite', Npart=1000, prior_name='try0', observables=['fqz_multi'], abcrun=None, restart=False, t_restart=None, eps_restart=None, **sim_kwargs): ''' Run ABC-PMC analysis for central galaxy SFH model with *FIXED* quenching timescale Parameters ---------- T : (int) Number of iterations eps_input : (float) Starting epsilon threshold value N_part : (int) Number of particles prior_name : (string) String that specifies what prior to use. abcrun : (string) String that specifies abc run information ''' if isinstance(eps_input, list): if len(eps_input) != len(observables): raise ValueError if len(observables) > 1 and isinstance(eps_input, float): raise ValueError # output abc run details sfinherit_kwargs, abcrun_flag = MakeABCrun( abcrun=abcrun, Niter=T, Npart=Npart, prior_name=prior_name, eps_val=eps_input, restart=restart, **sim_kwargs) # Data data_sum = DataSummary(observables=observables) # Priors prior_min, prior_max = PriorRange(prior_name) prior = abcpmc.TophatPrior(prior_min, prior_max) # ABCPMC prior object def Simz(tt): # Simulator (forward model) gv_slope = tt[0] gv_offset = tt[1] fudge_slope = tt[2] fudge_offset = tt[3] sim_kwargs = sfinherit_kwargs.copy() sim_kwargs['sfr_prop']['gv'] = {'slope': gv_slope, 'fidmass': 10.5, 'offset': gv_offset} sim_kwargs['evol_prop']['fudge'] = {'slope': fudge_slope, 'fidmass': 10.5, 'offset': fudge_offset} sim_kwargs['evol_prop']['tau'] = {'name': fixtau} sim_output = SimSummary(observables=observables, **sim_kwargs) return sim_output theta_file = lambda pewl: ''.join([code_dir(), 'dat/pmc_abc/', 'CenQue_theta_t', str(pewl), '_', abcrun_flag, '.fixedtau.', fixtau, '.dat']) w_file = lambda pewl: ''.join([code_dir(), 'dat/pmc_abc/', 'CenQue_w_t', str(pewl), '_', abcrun_flag, '.fixedtau.', fixtau, '.dat']) dist_file = lambda pewl: ''.join([code_dir(), 'dat/pmc_abc/', 'CenQue_dist_t', str(pewl), '_', abcrun_flag, '.fixedtau.', fixtau, '.dat']) eps_file = ''.join([code_dir(), 'dat/pmc_abc/epsilon_', abcrun_flag, '.fixedtau.', fixtau, '.dat']) distfn = RhoFq if restart: if t_restart is None: raise ValueError last_thetas = np.loadtxt(theta_file(t_restart)) last_ws = np.loadtxt(w_file(t_restart)) last_dist = np.loadtxt(dist_file(t_restart)) init_pool = abcpmc.PoolSpec(t_restart, None, None, last_thetas, last_dist, last_ws) else: init_pool = None eps = abcpmc.ConstEps(T, eps_input) try: mpi_pool = mpi_util.MpiPool() abcpmc_sampler = abcpmc.Sampler( N=Npart, # N_particles Y=data_sum, # data postfn=Simz, # simulator dist=distfn, # distance function pool=mpi_pool) except AttributeError: abcpmc_sampler = abcpmc.Sampler( N=Npart, # N_particles Y=data_sum, # data postfn=Simz, # simulator dist=distfn) # distance function abcpmc_sampler.particle_proposal_cls = abcpmc.ParticleProposal pools = [] if init_pool is None: f = open(eps_file, "w") f.close() eps_str = '' for pool in abcpmc_sampler.sample(prior, eps, pool=init_pool): print '----------------------------------------' print 'eps ', pool.eps new_eps_str = str(pool.eps)+'\t'+str(pool.ratio)+'\n' if eps_str != new_eps_str: # if eps is different, open fiel and append f = open(eps_file, "a") eps_str = new_eps_str f.write(eps_str) f.close() print("T:{0},ratio: {1:>.4f}".format(pool.t, pool.ratio)) print eps(pool.t) # write theta, weights, and distances to file np.savetxt(theta_file(pool.t), pool.thetas, header='gv_slope, gv_offset, fudge_slope, fudge_offset') np.savetxt(w_file(pool.t), pool.ws) np.savetxt(dist_file(pool.t), pool.dists) # update epsilon based on median thresholding if len(observables) == 1: eps.eps = np.median(pool.dists) else: #print pool.dists print np.median(np.atleast_2d(pool.dists), axis = 0) eps.eps = np.median(np.atleast_2d(pool.dists), axis = 0) print '----------------------------------------' pools.append(pool) return pools
def main_abcpmc_MUSIC2(conf, test=False): """ config should contain [][]: a list etc. eps_start is actually important as the next iteration will only start if the number of computed trials within these boundaries will be Nw. So in one case I had to draw and compute twice as many particles than Nw. About the treads: 14-16 treads are fine, as more treats wont be fully used and just sit in the taskqueue """ # Loads the real data to compare with (and if neccessary also test data) data = iom.unpickleObject(conf['paths']['surveyreal']) if test: dataMUSIC2 = iom.unpickleObject(conf['paths']['surveysim']) print(type(dataMUSIC2.Rmodel), conf['paths']['surveysim']) surmet.abcpmc_dist_severalMetrices(dataMUSIC2, data, metrics=json.loads(conf['metrics']['used']), delal=False, stochdrop=conf['flavor']['stochdrop'], phoenixdrop = conf['flavor']['phoenixdrop'], outpath='/data/') return 0 """ The abcpmc part starts: Define thetas i.e. parameter values to be inferred and priors""" if conf['prior']['type'] == 'tophat': bounds = json.loads(conf['prior']['bounds']) prior = abcpmc.TophatPrior(bounds[0], bounds[1]) elif conf['prior']['type'] == 'gaussian': means = json.loads(conf['prior']['means']) COV = json.loads(conf['prior']['covariance']) prior = abcpmc.GaussianPrior(mu=means, sigma=COV) else: print('inference_abcpmc::main_abcpmc_MUSIC2: prior %s is unknown!' % (conf['prior']['type'])) return 0 eps = abcpmc.ConstEps(conf.getint('pmc', 'T'), json.loads(conf['metrics']['eps_startlimits'])) if test: sampler = abcpmc.Sampler(N=conf.getint('pmc', 'Nw'), Y=data, postfn=testrand, dist=testmetric, threads=conf.getint('mp', 'Nthreads'), maxtasksperchild=conf.getint('mp', 'maxtasksperchild')) else: sampler = abcpmc.Sampler(N=conf.getint('pmc', 'Nw'), Y=data, postfn=partial(music2run.main_ABC, parfile=conf['simulation']['parfile']), dist=partial(surmet.abcpmc_dist_severalMetrices, metrics=json.loads(conf['metrics']['used']), outpath=conf['paths']['abcpmc'], stochdrop=conf['flavor']['stochdrop'], phoenixdrop = conf['flavor']['phoenixdrop']), threads=conf.getint('mp', 'Nthreads'), maxtasksperchild=conf.getint('mp', 'maxtasksperchild')) # Prepares the file for counting with open(conf['paths']['abcpmc'] + 'count.txt', 'w+') as f: f.write('0') sampler.particle_proposal_cls = abcpmc.OLCMParticleProposal """ compare with AstroABC sampler = astroabc.ABC_class(Ndim,walkers,data,tlevels,niter,priors,**prop) sampler.sample(music2run.main_astroABC) """ # startfrom=iom.unpickleObject('/data/ClusterBuster-Output/MUSIC_NVSS02_Test01/launch_pools') pool = None #startfrom[-1] launch(sampler, prior, conf.getfloat('pmc','alpha'), eps, surveypath=conf['paths']['abcpmc'], pool=pool)
#generate data #model.populate_mock() #group_id = model.mock.compute_fof_group_ids() """Load data and variance""" data = np.loadtxt("data_richness.dat") variance = np.loadtxt("variance_richness.dat") - 1.e-11 + 1.e-19 bins = np.arange(1, 61, 1) #binning choice for the group richness distribution """True HOD parameters""" data_hod = np.array([11.92, 0.39, 12.79, 1.15, 13.94]) """Prior""" prior = abcpmc.TophatPrior([11., .1, 12.5, 1., 13.6], [14., .6, 13.09, 1.3, 14.25]) #prior = abcpmc.TophatPrior([11.91,.38,12.78,1.1,13.9],[11.92,.4,12.8,1.2,14.]) prior_dict = { 'logM0': { 'shape': 'uniform', 'min': 11., 'max': 14. }, 'sigma_logM': { 'shape': 'uniform', 'min': .1, 'max': .6 }, 'logMmin': {