def test_subvol_gmf(Mr): """ """ thr = -1.0 * np.float(Mr) model = PrebuiltHodModelFactory("zheng07", threshold=thr, halocat="multidark", redshift=0.0) model.new_haloprop_func_dict = {"sim_subvol": util.mk_id_column} datsubvol = lambda x: util.mask_func(x, 0) model.populate_mock(simname="multidark", masking_function=datsubvol, enforce_PBC=False) # compute group richness # These are wrong because they assume periodicity # rich = richness(model.mock.compute_fof_group_ids()) # gmf = GMF(rich) #GMF # print gmf # print GMF(rich , counts = True) galaxy_sample = model.mock.galaxy_table x = galaxy_sample["x"] y = galaxy_sample["y"] z = galaxy_sample["z"] vz = galaxy_sample["vz"] pos = three_dim_pos_bundle(model.mock.galaxy_table, "x", "y", "z", velocity=vz, velocity_distortion_dimension="z") b_para, b_perp = 0.2, 0.2 groups = FoFGroups(pos, b_perp, b_para, period=None, Lbox=200, num_threads="max") gids = groups.group_ids rich = richness(gids) gmf = GMF(rich) print gmf print GMF(rich, counts=True) return None
def build_xi_bins(Mr=21): ''' hardcoded r bins for xi. ''' model = PrebuiltHodModelFactory('zheng07', threshold = -1.0*np.float(Mr)) model.populate_mock() # population mock realization r_bin = model.mock.compute_galaxy_clustering(rbins=hardcoded_xi_bins())[0] output_file = ''.join([util.dat_dir(), 'xir_rbin.Mr', str(Mr), '.dat']) np.savetxt(output_file, r_bin) return None
def test_zheng07_mean_occupation_centrals(): """ Compares the central probability with halotools """ zheng07_model = PrebuiltHodModelFactory('zheng07') test_mass = np.logspace(10, 15).astype('float32') p = zheng07_model.mean_occupation_centrals(prim_haloprop=test_mass) p_tf = Zheng07Cens(test_mass, **(zheng07_model.param_dict)).distribution.probs assert_allclose(p, p_tf.numpy(), atol=1.e-3)
def xi_binning_tests(Mr=20): model = PrebuiltHodModelFactory('zheng07', threshold = -1.0*np.float(Mr)) rbins = np.concatenate([np.array([0.1]), np.logspace(np.log10(0.5), np.log10(20.), 15)]) print 'R bins = ', rbins for ii in xrange(10): model.populate_mock() # population mock realization #rbins = np.logspace(-1, np.log10(20.), 16) r_bin, xi_r = model.mock.compute_galaxy_clustering(rbins=rbins) print xi_r
def __init__(self, Mr=21, b_normal=0.25): ''' Class object that describes our forward model used in MCMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) self.halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar')
def test_GMFbinning(Mr): """ Tests for the GMF binning scheme in order to make it sensible. """ gids_saved = "gids_saved.p" if not os.path.isfile(gids_saved): thr = -1.0 * np.float(Mr) model = PrebuiltHodModelFactory("zheng07", threshold=thr, halocat="multidark", redshift=0.0) model.new_haloprop_func_dict = {"sim_subvol": util.mk_id_column} datsubvol = lambda x: util.mask_func(x, 0) model.populate_mock(simname="multidark", masking_function=datsubvol, enforce_PBC=False) galaxy_sample = model.mock.galaxy_table x = galaxy_sample["x"] y = galaxy_sample["y"] z = galaxy_sample["z"] vz = galaxy_sample["vz"] pos = three_dim_pos_bundle( model.mock.galaxy_table, "x", "y", "z", velocity=vz, velocity_distortion_dimension="z" ) b_para, b_perp = 0.2, 0.2 groups = FoFGroups(pos, b_perp, b_para, period=None, Lbox=200, num_threads="max") pickle.dump(groups, open(gids_saved, "wb")) else: groups = pickle.load(open(gids_saved, "rb")) gids = groups.group_ids rich = richness(gids) # print "rich=" , rich rbins = np.logspace(np.log10(3.0), np.log10(20), 10) rbins = np.array([1, 2.0, 3.0, 4.0, 5.0, 6.0, 7, 9, 11, 14, 17, 20]) gmf = GMF(rich, counts=False, bins=rbins) gmf_counts = GMF(rich, counts=True, bins=rbins) print rbins print gmf print gmf_counts fig = plt.figure(1) sub = fig.add_subplot(111) sub.plot(0.5 * (rbins[:-1] + rbins[1:]), gmf) sub.set_xscale("log") sub.set_yscale("log") plt.show() # print gmf # print GMF(rich , counts = True) return None
def Add_Hod(self, param_dict=PARAM_DICT, **kwargs): ''' Initializes a Zheng07 HOD model and populates the mock ''' print('Adding and populating Zheng07 HOD model...') start = time() self.hod = PrebuiltHodModelFactory('zheng07', redshift=REDSHIFT, modulate_with_cenocc=True) self.hod.param_dict.update(param_dict) self.hod.populate_mock(self.halos, **kwargs) # Compute centrals and satellites numbers self.hod.Ncens = (self.hod.mock.galaxy_table['gal_type'] == 'centrals').sum() self.hod.Nsats = (self.hod.mock.galaxy_table['gal_type'] == 'satellites').sum() print(f'\t Done. Elapsed time: {time()-start:.2f} s')
def test_subvol_gmf(Mr): ''' ''' thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr, halocat='multidark', redshift=0.) model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} datsubvol = lambda x: util.mask_func(x, 0) model.populate_mock(simname='multidark', masking_function=datsubvol, enforce_PBC=False) #compute group richness # These are wrong because they assume periodicity #rich = richness(model.mock.compute_fof_group_ids()) #gmf = GMF(rich) #GMF #print gmf #print GMF(rich , counts = True) galaxy_sample = model.mock.galaxy_table x = galaxy_sample['x'] y = galaxy_sample['y'] z = galaxy_sample['z'] vz = galaxy_sample['vz'] pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z', velocity=vz, velocity_distortion_dimension="z") b_para, b_perp = 0.2, 0.2 groups = FoFGroups(pos, b_perp, b_para, period=None, Lbox=200, num_threads='max') gids = groups.group_ids rich = richness(gids) gmf = GMF(rich) print gmf print GMF(rich, counts=True) return None
def test_zheng07_mean_occupation_satellites(): """ Compare the poisson rate with halotools """ zheng07_model = PrebuiltHodModelFactory('zheng07', modulate_with_cenocc=True) test_mass = np.logspace(10, 15).astype('float32') p = zheng07_model.mean_occupation_satellites(prim_haloprop=test_mass) n_cen = Zheng07Cens(test_mass, **(zheng07_model.param_dict)) n_sat = Zheng07SatsPoisson(test_mass, n_cen, **(zheng07_model.param_dict)) assert_allclose(p, n_sat.distribution.rate.numpy(), atol=1.e-2)
def main(): # Entire MultiDark Volume (Analytic xi) cov = np.loadtxt("../data/wpxicov_dr72_bright0_mr21.0_z0.159_nj400") print(cov.shape) model = PrebuiltHodModelFactory('zheng07', threshold=-21) print(model.param_dict) model.param_dict['logM0'] = 12.59 model.param_dict['sigma_logM'] = 0.49 model.param_dict['logMmin'] = 12.78 model.param_dict['alpha'] = 1.14 model.param_dict['logM1'] = 13.99 #, 'sigma_logM': 0.39, 'logMmin': 12.79, 'alpha': 1.15, 'logM1': 13.94} halocat = CachedHaloCatalog(simname = 'bolplanck', redshift = 0, halo_finder = 'rockstar') model.populate_mock(halocat, enforce_PBC = True) pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') tstart = time.time() t0 = tstart pos = pos.astype(np.float32) x, y, z = pos[:,0] , pos[:,1] , pos[:,2] t1 = time.time() print("Done reading the data - time taken = {0:10.1f} seconds" .format(t1 - t0)) print("Beginning Correlation functions calculations") boxsize = 250 nthreads = 4 pimax = 40.0 binfile = path.join(path.dirname(path.abspath(__file__)), "../", "bin") autocorr = 1 numbins_to_print = 12 print("\nRunning 2-D projected correlation function wp(rp)") results_wp = _countpairs.countpairs_wp(boxsize, pimax, nthreads, binfile, x, y, z) print("\n# ****** wp: first {0} bins ******* " .format(numbins_to_print)) print("# rmin rmax rpavg wp npairs") print("##########################################################") for ibin in range(numbins_to_print): items = results_wp[ibin] print("{0:12.4f} {1:12.4f} {2:10.4f} {3:10.1f} {4:10d}" .format(items[0], items[1], items[2], items[3], items[4])) print("-----------------------------------------------------------") data_wp = np.loadtxt("../data/wpxi_dr72_bright0_mr21.0_z0.159_nj400")[:,1] print(data_wp.shape) data_wp_error = np.sqrt(np.diag(cov)[:12]) print(data_wp_error.shape) rbins = np.loadtxt(binfile) rs = np.mean(rbins , axis = 1) plt.figure(figsize=(10,10)) plt.errorbar(rs , data_wp , data_wp_error , fmt=".k" , capsize = 2) plt.plot(rs , np.array(results_wp)[:,3]) plt.loglog() plt.savefig("wp.pdf")
def __init__(self, Mr=-21, b_normal=0.25): ''' Class object that describes our forward model used in AMC-PMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) #self.model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} self.halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') self.RR = data_RR(box='md_sub') self.randoms = data_random(box='md_sub') self.NR = len(self.randoms)
class Mock: def __init__(self, path): ''' Links the object to input catalog ''' self.path = path self.model = self.path.split('/')[-2] def Read_Halo_Table(self): ''' Using the path of the Rockstar catalogue, reads the it and stores the halo table in self.table ''' # Reads the file print('Reading Rockstar file', self.path,'...' ) start = time() self.table = CSVCatalog(self.path, NAMES, usecols=USE_COLS) # Creates Position and Velocity columns self.table['Position'] = Table_Pos(self.table) self.table['Velocity'] = Table_Vel(self.table) print(f'\t Done. Elapsed time: {time()-start:.2f} s') def Create_Halo_Catalog(self): ''' Using table, cosmology and redshift builds the Halo Catalog in self.halos ''' print('Creating Halo catalog...') start = time() halos = HaloCatalog(self.table, cosmo=cosmology.Planck15, redshift=REDSHIFT, mdef='vir', position='Position', velocity='Velocity', mass='m200c') self.halos = halos.to_halotools(BoxSize=BOXSIZE) print(f'\t Done. Elapsed time: {time()-start:.2f} s') def Add_Hod(self, param_dict=PARAM_DICT, **kwargs): ''' Initializes a Zheng07 HOD model and populates the mock ''' print('Adding and populating Zheng07 HOD model...') start = time() self.hod = PrebuiltHodModelFactory('zheng07', redshift=REDSHIFT, modulate_with_cenocc=True) self.hod.param_dict.update(param_dict) self.hod.populate_mock(self.halos, **kwargs) # Compute centrals and satellites numbers self.hod.Ncens = (self.hod.mock.galaxy_table['gal_type'] == 'centrals').sum() self.hod.Nsats = (self.hod.mock.galaxy_table['gal_type'] == 'satellites').sum() print(f'\t Done. Elapsed time: {time()-start:.2f} s')
def xigm_tabulated_generator(hod = 'leauthaud11' , tr = 9.8 , \ rmin = 0.001 , rmax = 80 , nbins = 10 , \ scale = 'log' , sim = 'bolshoi', z = 0.0): """ returns galaxy_matter_cross_correlation <delta_g delta_m > arguments: hod : HOD model, default = Leauthaud threshold : log10 of stellar mass threshold in units of Msun/h, default = 10.6 rmin : minimum radius of the r-bins in Mpc/h, default = .001 Mpc/h rmax : maximum radius of the r-bins in Mpc/h, default = 50 Mpc/h nbins : number of rbins, default = 100 scale : log or linear, scaling of the rbins, default = log simulation name : bolshoi or multidark, default = bolshoi z = redshift """ if z<0: raise ValueError("redshift cannot be negative") if scale == 'log': rbin = np.logspace(np.log10(rmin) , np.log10(rmax) , nbins) elif scale == 'linear': rbin = np.linspace(rmin , rmax , nbins) else: raise ValueError("invalid scaling for the rbins") model = PrebuiltHodModelFactory(hod ,threshold = tr , redshift = z) model.populate_mock(simname = sim , redshift = z) rr , xigm = model.mock.compute_galaxy_matter_cross_clustering(rbins = rbin) return rr , xigm
def __init__(self, Mr=21, b_normal=0.25): ''' Class object that describes our forward model used in MCMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) self.halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar')
def to_halotools(cosmo, redshift, mdef, concentration_key=None, **kwargs): """ Parameters ---------- cosmo : the nbodykit or astropy Cosmology object to use in the model redshift : float the desired redshift of the model mdef : str, optional string specifying mass definition, used for computing default halo radii and concentration; should be 'vir' or 'XXXc' or 'XXXm' where 'XXX' is an int specifying the overdensity concentration_key : str the name of the column that will specify concentration; if not provided, the analytic formula from `Dutton and Maccio 2014 <https://arxiv.org/abs/1402.7073>`_ is used. **kwargs : additional keywords passed to the model components; see the Halotools documentation for further details Returns ------- :class:`~halotools.empirical_models.HodModelFactory` the halotools object implementing the HOD model """ from halotools.empirical_models import PrebuiltHodModelFactory # modulate sats with cen occ model by default kwargs.setdefault('modulate_with_cenocc', True) # need astropy Cosmology if isinstance(cosmo, Cosmology): cosmo = cosmo.to_astropy() # determine concentration key if concentration_key is None: conc_mass_model = 'dutton_maccio14' else: conc_mass_model = 'direct_from_halo_catalog' # determine mass column mass_key = 'halo_m' + mdef # the configuration kwargs.update({ 'cosmology': cosmo, 'redshift': redshift, 'mdef': mdef, 'prim_haloprop_key': mass_key }) return PrebuiltHodModelFactory(name, **kwargs)
def prebuilt_hodmodel_time_profile(): ''' Profile how long it takes to declare PrebuiltHodModelFactory. It takes roughly 0.000933730602264 seconds. So not very long. ''' ittook = [] for i in xrange(20): start_time = time.time() model = PrebuiltHodModelFactory('zheng07', threshold=-20) ittook.append(time.time() - start_time) print np.mean(np.array(ittook)), ' seconds'
def build_xi_nbar_gmf(Mr=21): ''' Build "data" xi, nbar, GMF values and write to file ''' model = PrebuiltHodModelFactory('zheng07', threshold = -1.0*np.float(Mr)) model.populate_mock(halocat = halocat , enforce_PBC = False) # population mock realization pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # write xi rbins = hardcoded_xi_bins() rmax = rbins.max() approx_cell1_size = [rmax, rmax, rmax] approx_cell2_size = approx_cell1_size approx_cellran_size = [rmax, rmax, rmax] period = np.array([Lbox , Lbox , Lbox]) data_xir = tpcf( sample1, rbins, sample2 = sample2, randoms=randoms, period = period, max_sample_size=int(1e4), estimator='Landy-Szalay', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed = RR, NR_precomputed = NR1) output_file = ''.join([util.dat_dir(), 'xir.Mr', str(Mr), '.dat']) np.savetxt(output_file, data_xir) # write nbar values nbar = model.mock.number_density output_file = ''.join([util.dat_dir(), 'nbar.Mr', str(Mr), '.dat']) np.savetxt(output_file, [nbar]) # write GMF rich = richness(model.mock.compute_fof_group_ids()) gmf = GMF(rich) # GMF output_file = ''.join([util.dat_dir(), 'gmf.Mr', str(Mr), '.dat']) np.savetxt(output_file, gmf) return None
def __init__(self, Mr=21, b_normal=0.25): ''' Class object that describes our forward model used in AMC-PMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) ####self.model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} self.halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') self.RR = data_RR(box='md_sub') self.randoms = data_random(box='md_sub') self.NR = len(self.randoms)
# by default, TabCorr applies redshift-space distortions (RSDs) in the # tabulation of correlation functions. rp_bins = np.logspace(-1, 1, 20) halocat = CachedHaloCatalog(simname='bolplanck') halotab = TabCorr.tabulate(halocat, wp, rp_bins, pi_max=40) # We can save the result for later use. halotab.write('bolplanck.hdf5') # We could read it in like this. Thus, we can skip the previous steps in the # future. halotab = TabCorr.read('bolplanck.hdf5') # Now, we're ready to calculate correlation functions for a specific model. model = PrebuiltHodModelFactory('zheng07', threshold=-18) rp_ave = 0.5 * (rp_bins[1:] + rp_bins[:-1]) ngal, wp = halotab.predict(model) plt.plot(rp_ave, wp, label='total') ngal, wp = halotab.predict(model, separate_gal_type=True) for key in wp.keys(): plt.plot(rp_ave, wp[key], label=key, ls='--') plt.xscale('log') plt.yscale('log') plt.xlabel(r'$r_p \ [h^{-1} \ \mathrm{Mpc}]$') plt.ylabel(r'$w_p \ [h^{-1} \ \mathrm{Mpc}]$') plt.legend(loc='lower left', frameon=False)
def data_hod_param(Mr=21): ''' HOD parameters of 'observations'. Returns dictionary with hod parameters. ''' model = PrebuiltHodModelFactory('zheng07', threshold = -1.0*np.float(Mr)) return model.param_dict
def test_GMFbinning(Mr): ''' Tests for the GMF binning scheme in order to make it sensible. ''' gids_saved = 'gids_saved.p' if not os.path.isfile(gids_saved): thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr, halocat='multidark', redshift=0.) model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} datsubvol = lambda x: util.mask_func(x, 0) model.populate_mock(simname='multidark', masking_function=datsubvol, enforce_PBC=False) galaxy_sample = model.mock.galaxy_table x = galaxy_sample['x'] y = galaxy_sample['y'] z = galaxy_sample['z'] vz = galaxy_sample['vz'] pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z', velocity=vz, velocity_distortion_dimension="z") b_para, b_perp = 0.2, 0.2 groups = FoFGroups(pos, b_perp, b_para, period=None, Lbox=200, num_threads='max') pickle.dump(groups, open(gids_saved, 'wb')) else: groups = pickle.load(open(gids_saved, 'rb')) gids = groups.group_ids rich = richness(gids) #print "rich=" , rich rbins = np.logspace(np.log10(3.), np.log10(20), 10) rbins = np.array([1, 2., 3., 4., 5., 6., 7, 9, 11, 14, 17, 20]) gmf = GMF(rich, counts=False, bins=rbins) gmf_counts = GMF(rich, counts=True, bins=rbins) print rbins print gmf print gmf_counts fig = plt.figure(1) sub = fig.add_subplot(111) sub.plot(0.5 * (rbins[:-1] + rbins[1:]), gmf) sub.set_xscale('log') sub.set_yscale('log') plt.show() #print gmf #print GMF(rich , counts = True) return None
def build_ABC_cov_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build covariance matrix used in ABC for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the multidark simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. Notes ----- * This covariance matrix is the covariance matrix calculated from the *entire* multidark box. So this does _not_ account for the sample variance, which the MCMC covariance does. ''' nbars, xir, gmfs = [], [], [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') rbins = xi_binedges() # some setting for tpcf calculations rmax = rbins.max() approx_cell1_size = [rmax, rmax, rmax] approx_cellran_size = [rmax, rmax, rmax] # load randoms and RRs for the ENTIRE MultiDark volume ###randoms = data_random(box='md_all') ###RR = data_RR(box='md_all') ###NR = len(randoms) for i in xrange(1, 125): print 'mock#', i # populate the mock subvolume model.populate_mock(halocat) # returning the positions of galaxies pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # calculate nbar nbars.append(len(pos) / 1000**3.) # calculate xi(r) for the ENTIRE MultiDark volume # using the natural estimator DD/RR - 1 xi = tpcf(pos, rbins, period=model.mock.Lbox, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size) xir.append(xi) # calculate gmf nbar = len(pos) / 1000**3. b = b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w, gbins)[0] / (1000.**3.) gmfs.append(gmf) # GMF # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join([util.obvs_dir(), 'abc_nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack( (np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([ util.obvs_dir(), 'ABC.nbar_xi_gmf_cov', '.no_poisson', '.Mr', str(Mr), '.bnorm', str(round(b_normal, 2)), '.dat' ]) np.savetxt(nopoisson_file, fullcov) return None
def build_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build data vector [nbar, xi, gmf] and save to file This data vector is built from the zeroth slice of the multidark The other slices will be used for building the covariance matrix. Parameters ---------- Mr : (int) Absolute magnitude cut off M_r. Default M_r = -21. b_normal : (float) FoF Linking length ''' thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') ####model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} ####datsubvol = lambda x: util.mask_func(x, 0) ####model.populate_mock(halocat, masking_function=datsubvol, enforce_PBC=False) model.populate_mock(halocat) #all the things necessary for tpcf calculation pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') #masking the galaxies outside the subvolume 0 pos = util.mask_galaxy_table(pos, 0) rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax, rmax, rmax] approx_cellran_size = [rmax, rmax, rmax] #compute number density nbar = len(pos) / 200**3. # load MD subvolume randoms and RRs randoms = data_random(box='md_sub') RR = data_RR(box='md_sub') NR = len(randoms) #compue tpcf with Natural estimator data_xir = tpcf(pos, rbins, pos, randoms=randoms, period=None, max_sample_size=int(2e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) fullvec = np.append(nbar, data_xir) #compute gmf b = b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w, gbins)[0] / (200.**3.) fullvec = np.append(fullvec, gmf) output_file = data_file(Mr=Mr, b_normal=b_normal) np.savetxt(output_file, fullvec) return None
def Subvolume_Analytic(N_sub, ratio=False): ''' Test the 2PCF estimates from MultiDark subvolume versus the analytic 2PCF for the entire MultiDark volume Parameters ---------- N_sub : (int) Number of subvolumes to sample ''' prettyplot() pretty_colors = prettycolors() pickle_file = ''.join([ '/export/bbq2/hahn/ccppabc/dump/', 'xi_subvolume_test', '.Nsub', str(N_sub), '.p']) fig = plt.figure(1) sub = fig.add_subplot(111) xi_bin = xi_binedges() if os.path.isfile(pickle_file): data_dump = pickle.load(open(pickle_file, 'rb')) full_xi = data_dump['full_xi'] else: # Entire MultiDark Volume (Analytic xi) model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') model.populate_mock(halocat) pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # while the estimator claims to be Landy-Szalay, I highly suspect it # actually uses Landy-Szalay since DR pairs cannot be calculated from # analytic randoms full_xi = tpcf(pos, xi_bin, period=model.mock.Lbox, max_sample_size=int(2e5), estimator='Landy-Szalay', num_threads=1) data_dump = {} data_dump['full_xi'] = full_xi if not ratio: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), full_xi, lw=2, ls='-', c='k', label=r'Analytic $\xi$ Entire Volume') if not os.path.isfile(pickle_file): # MultiDark SubVolume (precomputed RR pairs) sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21) sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} sub_halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') RR = data_RR() randoms = data_random() NR = len(randoms) for method in ['Landy-Szalay', 'Natural']: if method == 'Landy-Szalay': iii = 3 elif method == 'Natural': iii = 5 if not os.path.isfile(pickle_file): sub_xis_list = [] sub_xis = np.zeros(len(full_xi)) for ii in range(1,N_sub+1): # randomly sample one of the subvolumes rint = ii #np.random.randint(1, 125) simsubvol = lambda x: util.mask_func(x, rint) sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False) pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z') xi, yi , zi = util.random_shifter(rint) temp_randoms = randoms.copy() temp_randoms[:,0] += xi temp_randoms[:,1] += yi temp_randoms[:,2] += zi rmax = xi_bin.max() approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] sub_xi = tpcf( pos, xi_bin, pos, randoms=temp_randoms, period = None, max_sample_size=int(1e5), estimator=method, approx_cell1_size = approx_cell1_size, approx_cellran_size = approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) label = None if ii == N_sub - 1: label = 'Subvolumes' #if not ratio: # sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi, lw=0.5, ls='--', c=pretty_colors[iii]) sub_xis += sub_xi sub_xis_list.append(sub_xi) sub_xi_avg = sub_xis/np.float(N_sub) data_dump[method] = {} data_dump[method]['sub_xi_avg'] = sub_xi_avg data_dump[method]['sub_xis_list'] = sub_xis_list else: sub_xis_list = data_dump[method]['sub_xis_list'] sub_xi_avg = data_dump[method]['sub_xi_avg'] if not ratio: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg, lw=2, ls='--', c=pretty_colors[iii], label='Subvolume '+method) else: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg/full_xi, lw=2, ls='--', c=pretty_colors[iii], label='Subvolume '+method) if not os.path.isfile(pickle_file): pickle.dump(data_dump, open(pickle_file, 'wb')) sub.set_xlim([0.1, 50.]) sub.set_xlabel('r', fontsize=30) sub.set_xscale('log') if not ratio: sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25) sub.set_yscale('log') else: sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25) sub.legend(loc='lower left') if ratio: fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.ratio.png']) else: fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.png']) fig.savefig(fig_file, bbox_inches='tight', dpi=100) plt.close() return None
def initialise_model(redshift, model_name, halo_m_prop='halo_m'): ''' create an instance of halotools model class logMcut : The cut-off mass for the halo to host in a central galaxy. Given in solar mass. sigma_lnM : Parameter that modulates the shape of the number of central galaxies. logM1 : The scale mass for the number of satellite galaxies. kappa : Parameter that affects the cut-off mass for satellite galaxies. A detailed discussion and best-fit values of these parameters can be found in Zheng+2009. alpha : The power law index of the number of satellite galaxies. alpha_c : float. The central velocity bias parameter. Modulates the peculiar velocity of the central galaxy. The larger the absolute value of the parameter, the larger the peculiar velocity of the central. The sign of the value does not matter. s : The satellite profile modulation parameter. Modulates how the radial distribution of satellite galaxies within halos deviate from the radial profile of the halo. Positive value favors satellite galaxies to populate the outskirts of the halo whereas negative value favors satellite galaxy to live near the center of the halo. |s| must be < 1. s_v : float. The satellite velocity bias parameter. Modulates how the satellite galaxy peculiar velocity deviates from that of the local dark matter particle. Positive value favors high peculiar velocity satellite galaxies and vice versa. Note that our implementation preserves the Newton's second law of the satellite galaxies. |s| must be < 1. s_p : float. The perihelion distance modulation parameter. A positive value favors satellite galaxies to have larger distances to the halo center upon their closest approach to the center and vice versa. This can be regarded as a "fancier" satellite profile modulation parameter. |s| must be < 1. A : float. The assembly bias parameter. Introduces the effect of assembly bias. A positive value favors higher concentration halos to host galaxies whereas a negative value favors lower concentration halos to host galaxies. If you are invoking assembly bias decoration, i.e. a non-zero A parameter, you need to run gen_medianc.py first. A detailed discussion of these parameters can be found in Yuan et al. in prep. To turn off any of the five decorations, just set the corresponding parameter to 0. ''' # print('Initialising HOD model: {}...'.format(model_name)) # halotools prebiult models if model_name == 'matter': model_type = 'matter' model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-18, prim_haloprop_key=halo_m_prop) model.param_dict['logMmin'] = 13.3 model.param_dict['sigma_logM'] = 0.8 model.param_dict['alpha'] = 1 model.param_dict['logM0'] = 13.3 model.param_dict['logM1'] = 13.8 elif model_name in PrebuiltHodModelFactory.prebuilt_model_nickname_list: model_type = 'prebuilt' if model_name == 'zheng07': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-18, prim_haloprop_key=halo_m_prop) model.param_dict['logMmin'] = 13.3 model.param_dict['sigma_logM'] = 0.8 model.param_dict['alpha'] = 1 model.param_dict['logM0'] = 13.3 model.param_dict['logM1'] = 13.8 elif model_name == 'cacciato09': model = PrebuiltHodModelFactory('cacciato09', redshift=redshift, threshold=10, prim_haloprop_key=halo_m_prop) model.param_dict['log_L_0'] = 9.935 model.param_dict['log_M_1'] = 12.9 # 11.07 model.param_dict['gamma_1'] = 0.3 # 3.273 model.param_dict['gamma_2'] = 0.255 model.param_dict['sigma'] = 0.143 model.param_dict['a_1'] = 0.501 model.param_dict['a_2'] = 2.106 model.param_dict['log_M_2'] = 14.28 model.param_dict['b_0'] = -0.5 # -0.766 model.param_dict['b_1'] = 1.008 model.param_dict['b_2'] = -0.094 model.param_dict['delta_1'] = 0 model.param_dict['delta_2'] = 0 elif model_name == 'leauthaud11': model = PrebuiltHodModelFactory('leauthaud11', redshift=redshift, threshold=11, prim_haloprop_key=halo_m_prop) model.param_dict['smhm_m0_0'] = 11.5 # 10.72 model.param_dict['smhm_m0_a'] = 0.59 model.param_dict['smhm_m1_0'] = 13.4 # 12.35 model.param_dict['smhm_m1_a'] = 0.3 model.param_dict['smhm_beta_0'] = 2 # 0.43 model.param_dict['smhm_beta_a'] = 0.18 model.param_dict['smhm_delta_0'] = 0.1 # 0.56 model.param_dict['smhm_delta_a'] = 0.18 model.param_dict['smhm_gamma_0'] = 1 # 1.54 model.param_dict['smhm_gamma_a'] = 2.52 model.param_dict['scatter_model_param1'] = 0.2 model.param_dict['alphasat'] = 1 model.param_dict['betasat'] = 1.1 # 0.859 model.param_dict['bsat'] = 11 # 10.62 model.param_dict['betacut'] = 6 # -0.13 model.param_dict['bcut'] = 0.01 # 1.47 elif model_name == 'tinker13': model = PrebuiltHodModelFactory( 'tinker13', redshift=redshift, threshold=11, prim_haloprop_key=halo_m_prop, quiescent_fraction_abscissa=[1e12, 1e13, 1e14, 1e15], quiescent_fraction_ordinates=[0.25, 0.5, 0.75, 0.9]) model.param_dict['smhm_m0_0_active'] = 11 model.param_dict['smhm_m0_0_quiescent'] = 10.8 model.param_dict['smhm_m1_0_active'] = 12.2 model.param_dict['smhm_m1_0_quiescent'] = 11.8 model.param_dict['smhm_beta_0_active'] = 0.44 model.param_dict['smhm_beta_0_quiescent'] = 0.32 # model.param_dict['alphasat_active'] = 1 # model.param_dict['alphasat_quiescent'] = 1 # model.param_dict['betacut_active'] = 0.77 # model.param_dict['betacut_quiescent'] = -0.12 # model.param_dict['bcut_active'] = 0.2 # model.param_dict['bcut_quiescent'] = 0.2 # model.param_dict['betasat_active'] = 1.5 # model.param_dict['betasat_quiescent'] = 0.62 model.param_dict['bsat_active'] = 13 model.param_dict['bsat_quiescent'] = 8 elif model_name == 'hearin15': model = PrebuiltHodModelFactory('hearin15', redshift=redshift, threshold=11) elif model_name == 'zu_mandelbaum15': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-1) elif model_name == 'zu_mandelbaum16': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-1) # generalised HOD models with 5-parameter zheng07 as base model else: # 'gen_base1' model_type = 'general' model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-18, prim_haloprop_key=halo_m_prop) # five baseline parameters model.param_dict['logMcut'] = 13.35 model.param_dict['sigma_lnM'] = 0.85 model.param_dict['kappa'] = 1 model.param_dict['logM1'] = 13.8 model.param_dict['alpha'] = 1 # decoration parameters model.param_dict['s'] = 0 # sat ranking by halo centric distance model.param_dict['s_v'] = 0 # sat ranking by relative speed model.param_dict['s_p'] = 0 # sat ranking perihelion distance model.param_dict['alpha_c'] = 0 # centrals velocity bias model.param_dict['A_cen'] = 0 # centrals assembly bias, pseudomass model.param_dict['A_sat'] = 0 # satellites assembly bias, pseudomass ''' BOOKKEEPING: DO NOT MODIFY EXISTING MODELS, CREATE NEW ONES ''' if model_name == 'gen_base2': # params are tweaked to produce same ND model.param_dict['logM1'] = 13.85 model.param_dict['alpha'] = 1.377 elif model_name == 'gen_base3': model.param_dict['logM1'] = 13.9 model.param_dict['alpha'] = 1.663 elif model_name == 'gen_base4': model.param_dict['logM1'] = 13.796 model.param_dict['alpha'] = 0.95 elif model_name == 'gen_base5': model.param_dict['logM1'] = 13.805 model.param_dict['alpha'] = 1.05 elif model_name == 'gen_base6': model.param_dict['logM1'] = 13.770 model.param_dict['alpha'] = 0.75 elif model_name == 'gen_base7': model.param_dict['logM1'] = 13.848 model.param_dict['alpha'] = 1.25 elif model_name == 'gen_ass1': model.param_dict['A_cen'] = 1 model.param_dict['A_sat'] = 0 elif model_name == 'gen_ass2': model.param_dict['A_cen'] = 0 model.param_dict['A_sat'] = 1 elif model_name == 'gen_ass3': model.param_dict['A_cen'] = 1 model.param_dict['A_sat'] = 1 elif model_name == 'gen_ass1_n': model.param_dict['A_cen'] = -1 model.param_dict['A_sat'] = 0 elif model_name == 'gen_ass2_n': model.param_dict['A_cen'] = 0 model.param_dict['A_sat'] = -1 elif model_name == 'gen_ass3_n': model.param_dict['A_cen'] = -1 model.param_dict['A_sat'] = -1 elif model_name == 'gen_vel1': model.param_dict['alpha_c'] = 1 elif model_name == 'gen_vel2': model.param_dict['alpha_c'] = 0.2 elif model_name == 'gen_s1': model.param_dict['s'] = 0.9 elif model_name == 'gen_sv1': model.param_dict['s_v'] = 0.9 elif model_name == 'gen_sp1': model.param_dict['s_p'] = 0.9 elif model_name == 'gen_s1_n': model.param_dict['s'] = -0.9 elif model_name == 'gen_sv1_n': model.param_dict['s_v'] = -0.9 elif model_name == 'gen_sp1_n': model.param_dict['s_p'] = -0.9 elif model_name == 'gen_allbiases': model.param_dict['A_cen'] = 1 model.param_dict['A_sat'] = 1 model.param_dict['alpha_c'] = 1 model.param_dict['s'] = 0.9 model.param_dict['s_v'] = 0.9 model.param_dict['s_p'] = 0.9 elif model_name == 'gen_allbiases_n': model.param_dict['A_cen'] = -1 model.param_dict['A_sat'] = -1 model.param_dict['alpha_c'] = 1 model.param_dict['s'] = -0.9 model.param_dict['s_v'] = -0.9 model.param_dict['s_p'] = -0.9 # add useful model properties here model.model_name = model_name model.model_type = model_type model.halo_m_prop = halo_m_prop return model
def main(): ###############################Multi-Dark########################### model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') t0 = time.time() model.populate_mock(halocat, enforce_PBC = False) print(time.time() - t0) t0 = time.time() model.populate_mock(halocat, enforce_PBC = False) print(time.time() - t0) pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') nthreads = 1 binfile = path.join(path.dirname(path.abspath(__file__)), "/home/mj/Corrfunc/xi_theory/tests/", "bins") autocorr = 1 pos = pos.astype(np.float32) x , y , z = pos[:,0] , pos[:,1] , pos[:,2] DD = _countpairs.countpairs(autocorr, nthreads, binfile, x, y, z, x , y , z) ND = len(pos) NR = 50*800000 DD = np.array(DD)[:,3] num_randoms = 50 * 800000 xran = np.random.uniform(0, 1000, num_randoms) yran = np.random.uniform(0, 1000, num_randoms) zran = np.random.uniform(0, 1000, num_randoms) randoms = np.vstack((xran, yran, zran)).T randoms = randoms.astype(np.float32) xran = randoms[:,0] yran = randoms[:,1] zran = randoms[:,2] results_RR = _countpairs.countpairs(autocorr, nthreads, binfile, xran, yran, zran, xran, yran, zran) RR = np.array(results_RR)[:,3] factor1 = 1.*ND*ND/(NR*NR) mult = lambda x,y: x*y xi_MD = mult(1.0/factor1 , DD/RR) - 1. print(xi_MD) ###############################Subvolume of Multi-Dark########################### num_srandoms = 50 * 8000 xran = np.random.uniform(0, 200, num_srandoms) yran = np.random.uniform(0, 200, num_srandoms) zran = np.random.uniform(0, 200, num_srandoms) randoms = np.vstack((xran, yran, zran)).T randoms = randoms.astype(np.float32) xran = randoms[:,0] yran = randoms[:,1] zran = randoms[:,2] results_RR = _countpairs.countpairs(autocorr, nthreads, binfile, xran, yran, zran, xran, yran, zran) RR_sub = np.array(results_RR)[:,3] import util sub_model = PrebuiltHodModelFactory('zheng07' , threshold = -21) sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} sub_halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') xi_subs = [] for i in range(10): simsubvol = lambda x: util.mask_func(x, i) sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False) sub_pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z') nthreads = 1 binfile = path.join(path.dirname(path.abspath(__file__)), "/home/mj/Corrfunc/xi_theory/tests/", "bins") autocorr = 1 sub_pos = sub_pos.astype(np.float32) x , y , z = sub_pos[:,0] , sub_pos[:,1] , sub_pos[:,2] DD_sub = _countpairs.countpairs(autocorr, nthreads, binfile, x, y, z, x, y, z) ND_sub = len(sub_pos) NR_sub = 50 * 8000 DD_sub = np.array(DD_sub)[:,3] factor1 = 1.*ND_sub*ND_sub/(NR_sub*NR_sub) mult = lambda x,y: x*y xi_n = mult(1.0/factor1 , DD_sub/RR_sub) - 1. xi_subs.append(xi_n) xi_subs = np.array(xi_subs) np.savetxt("xi_subs.dat" , xi_subs) import matplotlib.pyplot as plt from ChangTools.plotting import prettyplot from ChangTools.plotting import prettycolors binfile = path.join(path.dirname(path.abspath(__file__)), "/home/mj/Corrfunc/xi_theory/tests/", "bins") rbins = np.loadtxt(binfile) rbins_centers = np.mean(rbins , axis = 1) xi_subs = np.loadtxt("xi_subs.dat") fig = plt.figure(figsize=(10,10)) ax = fig.add_subplot(111) for i in range(10): ax.semilogx(rbins_centers , xi_subs[i,:] / xi_MD , alpha = 0.2) plt.xlabel(r"$r$" , fontsize = 20) plt.ylabel(r"$\xi_{\rm subvolume}(r) / \xi_{\rm MD}(r)$" , fontsize = 20) plt.savefig("xi_ratios.pdf")
def build_ABC_cov_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build covariance matrix used in ABC for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the multidark simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. Notes ----- * This covariance matrix is the covariance matrix calculated from the *entire* multidark box. So this does _not_ account for the sample variance, which the MCMC covariance does. ''' nbars, xir, gmfs = [], [], [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') rbins = xi_binedges() # some setting for tpcf calculations rmax = rbins.max() approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] # load randoms and RRs for the ENTIRE MultiDark volume ###randoms = data_random(box='md_all') ###RR = data_RR(box='md_all') ###NR = len(randoms) for i in xrange(1,125): print 'mock#', i # populate the mock subvolume model.populate_mock(halocat) # returning the positions of galaxies pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # calculate nbar nbars.append(len(pos) / 1000**3.) # calculate xi(r) for the ENTIRE MultiDark volume # using the natural estimator DD/RR - 1 xi = tpcf( pos, rbins, period=model.mock.Lbox, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size) xir.append(xi) # calculate gmf nbar = len(pos) / 1000**3. b = b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w , gbins)[0] / (1000.**3.) gmfs.append(gmf) # GMF # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join([util.obvs_dir(), 'abc_nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack((np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([util.obvs_dir(), 'ABC.nbar_xi_gmf_cov', '.no_poisson', '.Mr', str(Mr), '.bnorm', str(round(b_normal, 2)), '.dat']) np.savetxt(nopoisson_file, fullcov) return None
def build_MCMC_cov_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build covariance matrix used in MCMC for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the other subvolumes (subvolume 1 to subvolume 125) of the simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. ''' nbars = [] xir = [] gmfs = [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') ###model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} #some settings for tpcf calculations rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] #load randoms and RRs randoms = data_random(box='md_sub') RR = data_RR(box='md_sub') NR = len(randoms) for i in xrange(1,125): print 'mock#', i # populate the mock subvolume ###mocksubvol = lambda x: util.mask_func(x, i) ###model.populate_mock(halocat, ### masking_function=mocksubvol, ### enforce_PBC=False) model.populate_mock(halocat) # returning the positions of galaxies in the entire volume pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # masking out the galaxies outside the subvolume i pos = util.mask_galaxy_table(pos , i) # calculate nbar print "shape of pos" , pos.shape nbars.append(len(pos) / 200**3.) # translate the positions of randoms to the new subbox xi0 , yi0 , zi0 = util.random_shifter(i) temp_randoms = randoms.copy() temp_randoms[:,0] += xi0 temp_randoms[:,1] += yi0 temp_randoms[:,2] += zi0 #calculate xi(r) xi=tpcf( pos, rbins, pos, randoms=temp_randoms, period=None, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed = RR, NR_precomputed = NR) xir.append(xi) # calculate gmf nbar = len(pos) / 200**3. b = b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w , gbins)[0] / 200.**3. gmfs.append(gmf) # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join([util.obvs_dir(), 'nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack((np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([util.obvs_dir(), 'MCMC.nbar_xi_gmf_cov', '.no_poisson', '.Mr', str(Mr), '.bnorm', str(round(b_normal,2)), '.dat']) np.savetxt(nopoisson_file, fullcov) return None
def build_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build data vector [nbar, xi, gmf] and save to file This data vector is built from the zeroth slice of the multidark The other slices will be used for building the covariance matrix. Parameters ---------- Mr : (int) Absolute magnitude cut off M_r. Default M_r = -21. b_normal : (float) FoF Linking length ''' thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') ####model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} ####datsubvol = lambda x: util.mask_func(x, 0) ####model.populate_mock(halocat, masking_function=datsubvol, enforce_PBC=False) model.populate_mock(halocat) #all the things necessary for tpcf calculation pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') #masking the galaxies outside the subvolume 0 pos = util.mask_galaxy_table(pos , 0) rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] #compute number density nbar = len(pos) / 200**3. # load MD subvolume randoms and RRs randoms = data_random(box='md_sub') RR = data_RR(box='md_sub') NR = len(randoms) #compue tpcf with Natural estimator data_xir = tpcf( pos, rbins, pos, randoms=randoms, period=None, max_sample_size=int(2e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) fullvec = np.append(nbar, data_xir) #compute gmf b = b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w , gbins)[0] / (200.**3.) fullvec = np.append(fullvec, gmf) output_file = data_file(Mr=Mr, b_normal=b_normal) np.savetxt(output_file, fullvec) return None
def model(theta, Mstar, prior_range = None): ''' Given theta (HOD parameters) and stellar mass threshold Mstar, compute wp not sure prior_range is necessary here ''' model = PrebuiltHodModelFactory('hearin15', threshold = Mstar, redshift = 0.0 , central_assembias_strength = 0., satellite_assembias_strength = 0.) model.param_dict['alphasat'] = theta[0]#1.0 model.param_dict['bcut'] = theta[1]#1.47, model.param_dict['betacut'] = theta[2]#0.13 model.param_dict['betasat'] = theta[3]#0.859 model.param_dict['bsat'] = theta[4]#0.62 model.param_dict['mean_occupation_centrals_assembias_param1'] = theta[5] # 0 model.param_dict['mean_occupation_satellites_assembias_param1'] = theta[6] #.75 model.param_dict['scatter_model_param1'] = theta[7]#0.2 model.param_dict['smhm_beta_0'] = theta[8]# 0.43 model.param_dict['smhm_beta_a'] = theta[9]#0.18 model.param_dict['smhm_delta_0'] = theta[10]#0.56 model.param_dict['smhm_delta_a'] = theta[11]#0.18, model.param_dict['smhm_gamma_0']= theta[12]#1.54 model.param_dict['smhm_gamma_a'] = theta[13]#2.52, model.param_dict['smhm_m0_0'] = theta[14]# 10.72, model.param_dict['smhm_m0_a'] = theta[15]# 0.59 model.param_dict['smhm_m1_0'] = theta[16]# 12.35, model.param_dict['smhm_m1_a'] = theta[17]#0.3 model.populate_mock(simname = 'bolshoi', redshift = 0, halo_finder = 'rockstar') #x = model.mock.galaxy_table['x'] #y = model.mock.galaxy_table['y'] #z = model.mock.galaxy_table['z'] #all_positions = return_xyz_formatted_array(x, y, z) all_positions = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') wp_all = wp(all_positions, rp_bins, pi_bins, period=model.mock.Lbox, estimator = 'Landy-Szalay', num_threads = 1) #wp = proj_clustering(pos , rbins , pbins , cellsize = [rmax, rmax, rmax]) print 1 return rp_bin_centers, wp_all
def populate_halocat(halocat, model_name): print('Initialising HOD model...') if model_name == 'zheng07': model = PrebuiltHodModelFactory('zheng07', redshift=halocat.redshift, threshold=-18) # model_instance.param_dict['logMmin'] = 12.1 # model_instance.mock.populate() if model_name == 'leauthaud11': model = PrebuiltHodModelFactory('leauthaud11', redshift=halocat.redshift, threshold=10) if model_name == 'tinker13': model = PrebuiltHodModelFactory( 'tinker13', redshift=halocat.redshift, threshold=10, quiescent_fraction_abscissa=[1e12, 1e13, 1e14, 1e15], quiescent_fraction_ordinates=[0.25, 0.5, 0.75, 0.9]) if model_name == 'hearin15': model = PrebuiltHodModelFactory('zheng07', redshift=halocat.redshift, threshold=-1) if model_name == 'zu_mandelbaum15': model = PrebuiltHodModelFactory('zheng07', redshift=halocat.redshift, threshold=-1) if model_name == 'zu_mandelbaum16': model = PrebuiltHodModelFactory('zheng07', redshift=halocat.redshift, threshold=-1) if model_name == 'cacciato09': model = PrebuiltHodModelFactory('zheng07', redshift=halocat.redshift, threshold=-1) print('HOD model initialised. Populating {} halos with {}...'.format( len(halocat.halo_table), model_name)) model.populate_mock(halocat, Num_ptcl_requirement=100) print('Mock catalogue populated with {} galaxies'.format( len(model.mock.galaxy_table))) return model
def Subvolume_FullvolumeCut(N_sub, ratio=False): ''' Test the 2PCF estimates from MultiDark subvolume versus the 2PCF for the entire MultiDark volume WITHOUT periodic boundary conditions and actual pair counts, CUT into subvolumes of the same size *AFTER* populate mock Parameters ---------- N_sub : (int) Number of subvolumes to sample ''' prettyplot() pretty_colors = prettycolors() pickle_file = ''.join([ '/export/bbq2/hahn/ccppabc/dump/', 'xi_subvolume_fullvolume_cut_test', '.Nsub', str(N_sub), '.p' ]) fig = plt.figure(1) sub = fig.add_subplot(111) xi_bin = xi_binedges() # Entire MultiDark Volume (No Periodic Boundary Conditions) model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') sub_RR = data_RR(box='md_sub') sub_randoms = data_random(box='md_sub') sub_NR = len(sub_randoms) rmax = xi_bin.max() full_approx_cell1_size = [rmax, rmax, rmax] full_approx_cellran_size = [rmax, rmax, rmax] model.populate_mock(halocat, enforce_PBC=False) subvol_id = util.mk_id_column(table=model.mock.galaxy_table) full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # Full Volume if os.path.isfile(pickle_file): data_dump = pickle.load(open(pickle_file, 'rb')) full_xi = data_dump['full_xi'] else: model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') full_randoms = data_random(box='md_all') full_RR = data_RR(box='md_all') full_NR = len(full_randoms) rmax = xi_bin.max() full_approx_cell1_size = [rmax, rmax, rmax] full_approx_cellran_size = [rmax, rmax, rmax] model.populate_mock(halocat, enforce_PBC=False) full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') full_xi = tpcf(full_pos, xi_bin, randoms=full_randoms, period=None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(full_pos.shape[0]), estimator='Natural', approx_cell1_size=full_approx_cell1_size, approx_cellran_size=full_approx_cellran_size, RR_precomputed=full_RR, NR_precomputed=full_NR) data_dump = {} data_dump['full_xi'] = full_xi if not ratio: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), full_xi, lw=2, ls='-', c='k', label=r'Full Volume') if os.path.isfile(pickle_file): fullcut_xi_list = data_dump['fullcut_xi']['fullcut_xi_list'] fullcut_xi_avg = data_dump['fullcut_xi']['fullcut_xi_avg'] else: data_dump['fullcut_xi'] = {} fullcut_xi_list = [] fullcut_xi_tot = np.zeros(len(xi_bin) - 1) for id in np.unique(subvol_id)[:N_sub]: print 'Subvolume ', id in_cut = np.where(subvol_id == id) fullcut_pos = full_pos[in_cut] fullcut_xi = tpcf(fullcut_pos, xi_bin, randoms=sub_randoms, period=None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(fullcut_pos.shape[0]), estimator='Natural', approx_cell1_size=full_approx_cell1_size, approx_cellran_size=full_approx_cellran_size, RR_precomputed=sub_RR, NR_precomputed=sub_NR) fullcut_xi_list.append(fullcut_xi) fullcut_xi_tot += fullcut_xi fullcut_xi_avg = fullcut_xi_tot / np.float(N_sub) data_dump['fullcut_xi']['fullcut_xi_list'] = fullcut_xi_list data_dump['fullcut_xi']['fullcut_xi_avg'] = fullcut_xi_avg if not ratio: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), fullcut_xi_avg, lw=2, ls='-', c='k', label=r'Full Volume Cut Average') else: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), fullcut_xi_avg / full_xi, lw=2, ls='-', c='k', label=r'Full Volume Cut Average') if not os.path.isfile(pickle_file): # MultiDark SubVolume (precomputed RR pairs) sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21) sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} sub_halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') sub_RR = data_RR(box='md_sub') sub_randoms = data_random(box='md_sub') sub_NR = len(sub_randoms) sub_xis_list = [] sub_xis = np.zeros(len(full_xi)) for ii in range(1, N_sub): print 'Subvolume ', ii # randomly sample one of the subvolumes rint = ii #np.random.randint(1, 125) simsubvol = lambda x: util.mask_func(x, rint) sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False) pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z') xi, yi, zi = util.random_shifter(rint) temp_randoms = sub_randoms.copy() temp_randoms[:, 0] += xi temp_randoms[:, 1] += yi temp_randoms[:, 2] += zi rmax = xi_bin.max() sub_approx_cell1_size = [rmax, rmax, rmax] sub_approx_cellran_size = [rmax, rmax, rmax] sub_xi = tpcf(pos, xi_bin, randoms=temp_randoms, period=None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(pos.shape[0]), estimator='Natural', approx_cell1_size=sub_approx_cell1_size, approx_cellran_size=sub_approx_cellran_size, RR_precomputed=sub_RR, NR_precomputed=sub_NR) label = None if ii == N_sub - 1: label = 'Subvolumes' sub_xis += sub_xi sub_xis_list.append(sub_xi) sub_xi_avg = sub_xis / np.float(N_sub) data_dump['Natural'] = {} data_dump['Natural']['sub_xi_avg'] = sub_xi_avg data_dump['Natural']['sub_xis_list'] = sub_xis_list else: sub_xis_list = data_dump['Natural']['sub_xis_list'] sub_xi_avg = data_dump['Natural']['sub_xi_avg'] if not os.path.isfile(pickle_file): pickle.dump(data_dump, open(pickle_file, 'wb')) if not ratio: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), sub_xi_avg, lw=2, ls='--', c=pretty_colors[3], label='Subvolume') else: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), sub_xi_avg / full_xi, lw=2, ls='--', c=pretty_colors[3], label='Subvolume') sub.set_xlim([0.1, 50.]) sub.set_xlabel('r', fontsize=30) sub.set_xscale('log') if not ratio: sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25) sub.set_yscale('log') else: sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25) sub.legend(loc='lower left') if ratio: fig_file = ''.join([ util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.ratio.png' ]) else: fig_file = ''.join([ util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.png' ]) fig.savefig(fig_file, bbox_inches='tight', dpi=100) plt.close() return None
def build_nbar_xi_gmf_cov(Mr=21): ''' Build covariance matrix for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the multidark simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. ''' nbars = [] xir = [] gmfs = [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') #some settings for tpcf calculations rbins = hardcoded_xi_bins() for i in xrange(1, 125): print 'mock#', i # populate the mock subvolume model.populate_mock(halocat) # returning the positions of galaxies pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # calculate nbar nbars.append(len(pos) / 1000**3.) # translate the positions of randoms to the new subbox #calculate xi(r) xi = tpcf(pos, rbins, period=model.mock.Lbox, max_sample_size=int(2e5), estimator='Landy-Szalay') xir.append(xi) # calculate gmf nbar = len(pos) / 1000**3. b_normal = 0.75 b = b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w, gbins)[0] / (1000.**3.) gmfs.append(gmf) # GMF # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join( [util.multidat_dir(), 'abc_nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack( (np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([ util.multidat_dir(), 'abc_nbar_xi_gmf_cov.no_poisson.Mr', str(Mr), '.dat' ]) np.savetxt(nopoisson_file, fullcov) # and a correlation matrix full_corr_file = ''.join( [util.multidat_dir(), 'abc_nbar_xi_gmf_corr.Mr', str(Mr), '.dat']) np.savetxt(full_corr_file, fullcorr) return None
def standard_hod_model(): return PrebuiltHodModelFactory('zheng07', threshold=-20)
class MCMC_HODsim(object): def __init__(self, Mr=-21, b_normal=0.25): ''' Class object that describes our forward model used in AMC-PMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) self.halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') self.RR = data_RR(box='md_all') self.randoms = data_random('md_all') self.NR = len(self.randoms) def __call__(self, theta, prior_range=None, observables=['nbar', 'gmf']): return self._sum_stat(theta, prior_range=prior_range, observables=observables) def _sum_stat(self, theta, prior_range=None, observables=['nbar', 'gmf']): ''' Given theta, sum_stat calculates the observables from our forward model Parameters ---------- theta : (self explanatory) prior_range : If specified, checks to make sure that theta is within the prior range. ''' self.model.param_dict['logM0'] = theta[0] self.model.param_dict['sigma_logM'] = np.exp(theta[1]) self.model.param_dict['logMmin'] = theta[2] self.model.param_dict['alpha'] = theta[3] self.model.param_dict['logM1'] = theta[4] rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax, rmax, rmax] approx_cellran_size = [rmax, rmax, rmax] if prior_range is None: self.model.populate_mock(self.halocat, enforce_PBC=False) pos = three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 1000.**3.) # nbar of the galaxy catalog elif obv == 'gmf': nbar = len(pos) / 1000**3. b = self.b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = data_gmf_bins() gmf = np.histogram(w, gbins)[0] / (1000.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf(pos, rbins, pos, randoms=randoms, period=None, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=self.RR, NR_precomputed=self.NR) obvs.append(greek_xi) else: raise NotImplementedError( 'Only nbar 2pcf, gmf implemented so far') return obvs else: if np.all((prior_range[:, 0] < theta) & (theta < prior_range[:, 1])): # if all theta_i is within prior range ... try: self.model.populate_mock(self.halocat) pos = three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 1000**3.) # nbar of the galaxy catalog elif obv == 'gmf': nbar = len(pos) / 1000**3. b = self.b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = data_gmf_bins() gmf = np.histogram(w, gbins)[0] / (1000.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf( pos, rbins, pos, randoms=randoms, period=None, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=self.RR, NR_precomputed=self.NR) obvs.append(greek_xi) else: raise NotImplementedError( 'Only nbar, tpcf, and gmf are implemented so far' ) return obvs except ValueError: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1] * 1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs else: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1] * 1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs
xi_bin = np.concatenate([np.array([0.15]), np.logspace(np.log10(0.5), np.log10(20.), 15)]) rmax = xi_bin.max() # Get those randoms num_randoms = 50 * 800000 xran = np.random.uniform(0, 1000, num_randoms) yran = np.random.uniform(0, 1000, num_randoms) zran = np.random.uniform(0, 1000, num_randoms) full_randoms = np.vstack((xran, yran, zran)).T # Get the full box mock model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') model.populate_mock(halocat, enforce_PBC=False) pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # Get full tpcf print "getting full vol tpcf..." xi_full_pc = tpcf(pos, xi_bin, randoms=full_randoms, do_auto=True, do_cross=False, max_sample_size=int(pos.shape[0]), estimator='Natural', approx_cell1_size=[rmax, rmax, rmax], approx_cellran_size=[rmax, rmax, rmax]) print "done"
def Subvolume_FullvolumeCut(N_sub, ratio=False): ''' Test the 2PCF estimates from MultiDark subvolume versus the 2PCF for the entire MultiDark volume WITHOUT periodic boundary conditions and actual pair counts, CUT into subvolumes of the same size *AFTER* populate mock Parameters ---------- N_sub : (int) Number of subvolumes to sample ''' prettyplot() pretty_colors = prettycolors() pickle_file = ''.join([ '/export/bbq2/hahn/ccppabc/dump/', 'xi_subvolume_fullvolume_cut_test', '.Nsub', str(N_sub), '.p']) fig = plt.figure(1) sub = fig.add_subplot(111) xi_bin = xi_binedges() # Entire MultiDark Volume (No Periodic Boundary Conditions) model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') sub_RR = data_RR(box='md_sub') sub_randoms = data_random(box='md_sub') sub_NR = len(sub_randoms) rmax = xi_bin.max() full_approx_cell1_size = [rmax , rmax , rmax] full_approx_cellran_size = [rmax , rmax , rmax] model.populate_mock(halocat, enforce_PBC=False) subvol_id = util.mk_id_column(table=model.mock.galaxy_table) full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # Full Volume if os.path.isfile(pickle_file): data_dump = pickle.load(open(pickle_file, 'rb')) full_xi = data_dump['full_xi'] else: model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') full_randoms = data_random(box='md_all') full_RR = data_RR(box='md_all') full_NR = len(full_randoms) rmax = xi_bin.max() full_approx_cell1_size = [rmax , rmax , rmax] full_approx_cellran_size = [rmax , rmax , rmax] model.populate_mock(halocat, enforce_PBC=False) full_pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') full_xi = tpcf( full_pos, xi_bin, randoms=full_randoms, period=None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(full_pos.shape[0]), estimator='Natural', approx_cell1_size=full_approx_cell1_size, approx_cellran_size=full_approx_cellran_size, RR_precomputed = full_RR, NR_precomputed = full_NR) data_dump = {} data_dump['full_xi'] = full_xi if not ratio: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), full_xi, lw=2, ls='-', c='k', label=r'Full Volume') if os.path.isfile(pickle_file): fullcut_xi_list = data_dump['fullcut_xi']['fullcut_xi_list'] fullcut_xi_avg = data_dump['fullcut_xi']['fullcut_xi_avg'] else: data_dump['fullcut_xi'] = {} fullcut_xi_list = [] fullcut_xi_tot = np.zeros(len(xi_bin)-1) for id in np.unique(subvol_id)[:N_sub]: print 'Subvolume ', id in_cut = np.where(subvol_id == id) fullcut_pos = full_pos[in_cut] fullcut_xi = tpcf( fullcut_pos, xi_bin, randoms=sub_randoms, period=None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(fullcut_pos.shape[0]), estimator='Natural', approx_cell1_size=full_approx_cell1_size, approx_cellran_size=full_approx_cellran_size, RR_precomputed=sub_RR, NR_precomputed=sub_NR) fullcut_xi_list.append(fullcut_xi) fullcut_xi_tot += fullcut_xi fullcut_xi_avg = fullcut_xi_tot / np.float(N_sub) data_dump['fullcut_xi']['fullcut_xi_list']= fullcut_xi_list data_dump['fullcut_xi']['fullcut_xi_avg']= fullcut_xi_avg if not ratio: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), fullcut_xi_avg, lw=2, ls='-', c='k', label=r'Full Volume Cut Average') else: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), fullcut_xi_avg/full_xi, lw=2, ls='-', c='k', label=r'Full Volume Cut Average') if not os.path.isfile(pickle_file): # MultiDark SubVolume (precomputed RR pairs) sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21) sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} sub_halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') sub_RR = data_RR(box='md_sub') sub_randoms = data_random(box='md_sub') sub_NR = len(sub_randoms) sub_xis_list = [] sub_xis = np.zeros(len(full_xi)) for ii in range(1,N_sub): print 'Subvolume ', ii # randomly sample one of the subvolumes rint = ii #np.random.randint(1, 125) simsubvol = lambda x: util.mask_func(x, rint) sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False) pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z') xi, yi , zi = util.random_shifter(rint) temp_randoms = sub_randoms.copy() temp_randoms[:,0] += xi temp_randoms[:,1] += yi temp_randoms[:,2] += zi rmax = xi_bin.max() sub_approx_cell1_size = [rmax , rmax , rmax] sub_approx_cellran_size = [rmax , rmax , rmax] sub_xi = tpcf( pos, xi_bin, randoms=temp_randoms, period = None, do_auto=True, do_cross=False, num_threads=5, max_sample_size=int(pos.shape[0]), estimator='Natural', approx_cell1_size = sub_approx_cell1_size, approx_cellran_size = sub_approx_cellran_size, RR_precomputed=sub_RR, NR_precomputed=sub_NR) label = None if ii == N_sub - 1: label = 'Subvolumes' sub_xis += sub_xi sub_xis_list.append(sub_xi) sub_xi_avg = sub_xis/np.float(N_sub) data_dump['Natural'] = {} data_dump['Natural']['sub_xi_avg'] = sub_xi_avg data_dump['Natural']['sub_xis_list'] = sub_xis_list else: sub_xis_list = data_dump['Natural']['sub_xis_list'] sub_xi_avg = data_dump['Natural']['sub_xi_avg'] if not os.path.isfile(pickle_file): pickle.dump(data_dump, open(pickle_file, 'wb')) if not ratio: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg, lw=2, ls='--', c=pretty_colors[3], label='Subvolume') else: sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi_avg/full_xi, lw=2, ls='--', c=pretty_colors[3], label='Subvolume') sub.set_xlim([0.1, 50.]) sub.set_xlabel('r', fontsize=30) sub.set_xscale('log') if not ratio: sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25) sub.set_yscale('log') else: sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25) sub.legend(loc='lower left') if ratio: fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.ratio.png']) else: fig_file = ''.join([util.fig_dir(), 'test_xi_subvolume_fullvolume_cut.Nsub', str(N_sub), '.png']) fig.savefig(fig_file, bbox_inches='tight', dpi=100) plt.close() return None
d['mean_occupation_centrals_assembias_param1'] = chain['Acen'][idx_random] d['mean_occupation_satellites_assembias_param1'] = chain['Asat'][idx_random] except KeyError: pass return d def update_param_dict(model, chain): new_param_dict = retrieve_random_param_dict(chain) for key, value in new_param_dict.iteritems(): model.param_dict[key] = value ################################################################################ # build the two models std_model = PrebuiltHodModelFactory('zheng07') centrals_occupation = AssembiasZheng07Cens() centrals_profile = TrivialPhaseSpace() satellites_occupation = AssembiasZheng07Sats() satellites_profile = NFWPhaseSpace() satellites_occupation._suppress_repeated_param_warning = True model_dict = ({'centrals_occupation': centrals_occupation, 'centrals_profile': centrals_profile, 'satellites_occupation': satellites_occupation, 'satellites_profile': satellites_profile}) ab_model = HodModelFactory(**model_dict) ################################################################################
def build_MCMC_cov_nbar_xi_gmf(Mr=21, b_normal=0.25): ''' Build covariance matrix used in MCMC for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the other subvolumes (subvolume 1 to subvolume 125) of the simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. ''' nbars = [] xir = [] gmfs = [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') ###model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} #some settings for tpcf calculations rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax, rmax, rmax] approx_cellran_size = [rmax, rmax, rmax] #load randoms and RRs randoms = data_random(box='md_sub') RR = data_RR(box='md_sub') NR = len(randoms) for i in xrange(1, 125): print 'mock#', i # populate the mock subvolume ###mocksubvol = lambda x: util.mask_func(x, i) ###model.populate_mock(halocat, ### masking_function=mocksubvol, ### enforce_PBC=False) model.populate_mock(halocat) # returning the positions of galaxies in the entire volume pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # masking out the galaxies outside the subvolume i pos = util.mask_galaxy_table(pos, i) # calculate nbar print "shape of pos", pos.shape nbars.append(len(pos) / 200**3.) # translate the positions of randoms to the new subbox xi0, yi0, zi0 = util.random_shifter(i) temp_randoms = randoms.copy() temp_randoms[:, 0] += xi0 temp_randoms[:, 1] += yi0 temp_randoms[:, 2] += zi0 #calculate xi(r) xi = tpcf(pos, rbins, pos, randoms=temp_randoms, period=None, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) xir.append(xi) # calculate gmf nbar = len(pos) / 200**3. b = b_normal * (nbar)**(-1. / 3) groups = pyfof.friends_of_friends(pos, b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w, gbins)[0] / 200.**3. gmfs.append(gmf) # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join([util.obvs_dir(), 'nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack( (np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([ util.obvs_dir(), 'MCMC.nbar_xi_gmf_cov', '.no_poisson', '.Mr', str(Mr), '.bnorm', str(round(b_normal, 2)), '.dat' ]) np.savetxt(nopoisson_file, fullcov) return None
class ABC_HODsim(object): def __init__(self, Mr=21, b_normal=0.25): ''' Class object that describes our forward model used in AMC-PMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) ####self.model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} self.halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') self.RR = data_RR(box='md_sub') self.randoms = data_random(box='md_sub') self.NR = len(self.randoms) def __call__(self, theta, prior_range=None, observables=['nbar', 'gmf']): return self._sum_stat(theta, prior_range=prior_range, observables=observables) def _sum_stat(self, theta, prior_range=None, observables=['nbar', 'gmf']): ''' Given theta, sum_stat calculates the observables from our forward model Parameters ---------- theta : (self explanatory) prior_range : If specified, checks to make sure that theta is within the prior range. ''' self.model.param_dict['logM0'] = theta[0] self.model.param_dict['sigma_logM'] = np.exp(theta[1]) self.model.param_dict['logMmin'] = theta[2] self.model.param_dict['alpha'] = theta[3] self.model.param_dict['logM1'] = theta[4] rbins = xi_binedges() rmax = rbins.max() period = None approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] if prior_range is None: rint = np.random.randint(1, 125) ####simsubvol = lambda x: util.mask_func(x, rint) ####self.model.populate_mock(self.halocat, #### masking_function=simsubvol, #### enforce_PBC=False) self.model.populate_mock(self.halocat) pos =three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') pos = util.mask_galaxy_table(pos , rint) xi , yi , zi = util.random_shifter(rint) temp_randoms = self.randoms.copy() temp_randoms[:,0] += xi temp_randoms[:,1] += yi temp_randoms[:,2] += zi obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 200**3.) # nbar of the galaxy catalog elif obv == 'gmf': #compute group richness nbar = len(pos) / 200**3. b = self.b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins = data_gmf_bins() gmf = np.histogram(w , gbins)[0] / (200.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf( pos, rbins, pos, randoms=temp_randoms, period = period, max_sample_size=int(1e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed = self.RR, NR_precomputed = self.NR) obvs.append(greek_xi) else: raise NotImplementedError('Only nbar 2pcf, gmf implemented so far') return obvs else: if np.all((prior_range[:,0] < theta) & (theta < prior_range[:,1])): # if all theta_i is within prior range ... try: rint = np.random.randint(1, 125) simsubvol = lambda x: util.mask_func(x, rint) self.model.populate_mock(self.halocat, masking_function=simsubvol, enforce_PBC=False) pos =three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') #imposing mask on the galaxy table pos = util.mask_galaxy_table(pos , rint) xi , yi , zi = util.random_shifter(rint) temp_randoms = self.randoms.copy() temp_randoms[:,0] += xi temp_randoms[:,1] += yi temp_randoms[:,2] += zi obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 200**3.) # nbar of the galaxy catalog elif obv == 'gmf': nbar = len(pos) / 200**3. b = self.b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins =data_gmf_bins() gmf = np.histogram(w , gbins)[0] / (200.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf( pos, rbins, pos, randoms=temp_randoms, period = period, max_sample_size=int(1e5), estimator='Natural', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed = self.RR, NR_precomputed = self.NR) obvs.append(greek_xi) else: raise NotImplementedError('Only nbar, tpcf, and gmf are implemented so far') return obvs except ValueError: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1]*1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs else: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1]*1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs
pbins = np.logspace(-1, 1.25, 15) def proj_clustering(pos , rbins, pbins , cellsize): return wp(sample1 = pos, rp_bins = rbins, pi_bins = pbins, sample2 = None, period = np.array([250,250,250]), approx_cell1_size = cellsize) from halotools.empirical_models import PrebuiltHodModelFactory # The "fiducial" stellar mass threshold is 10**10.5 model = PrebuiltHodModelFactory('hearin15', threshold = 10.5, redshift = 0., central_assembias_strength = 1, satellite_assembias_strength = 1) model.param_dict['scatter_model_param1'] = 0.4 # This is the "fiducial" scatter used throughout the paper baseline_model = PrebuiltHodModelFactory('leauthaud11', threshold = 10.5, redshift = 0.) baseline_model.param_dict['scatter_model_param1'] = 0.4 #base_model baseline_model.populate_mock() pos = three_dim_pos_bundle(baseline_model.mock.galaxy_table, 'x', 'y', 'z') wp_base = proj_clustering(pos , rbins , pbins , cellsize = [rmax, rmax, rmax]) #central assembly bias only
def initialise_model(redshift, model_name='zheng07'): print('Initialising HOD model: {}...'.format(model_name)) # Default is ‘halo_mvir’ if model_name == 'zheng07': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-18, prim_haloprop_key=halo_m_prop) model.param_dict['logMmin'] = 13.3 model.param_dict['sigma_logM'] = 0.8 model.param_dict['alpha'] = 1 model.param_dict['logM0'] = 13.3 model.param_dict['logM1'] = 13.8 elif model_name == 'leauthaud11': model = PrebuiltHodModelFactory('leauthaud11', redshift=redshift, threshold=11, prim_haloprop_key=halo_m_prop) model.param_dict['smhm_m0_0'] = 11.5 # 10.72 model.param_dict['smhm_m0_a'] = 0.59 model.param_dict['smhm_m1_0'] = 13.4 # 12.35 model.param_dict['smhm_m1_a'] = 0.3 model.param_dict['smhm_beta_0'] = 2 # 0.43 model.param_dict['smhm_beta_a'] = 0.18 model.param_dict['smhm_delta_0'] = 0.1 # 0.56 model.param_dict['smhm_delta_a'] = 0.18 model.param_dict['smhm_gamma_0'] = 1 # 1.54 model.param_dict['smhm_gamma_a'] = 2.52 model.param_dict['scatter_model_param1'] = 0.2 model.param_dict['alphasat'] = 1 model.param_dict['betasat'] = 1.1 # 0.859 model.param_dict['bsat'] = 11 # 10.62 model.param_dict['betacut'] = 6 # -0.13 model.param_dict['bcut'] = 0.01 # 1.47 elif model_name == 'tinker13': model = PrebuiltHodModelFactory( 'tinker13', redshift=redshift, threshold=11, prim_haloprop_key=halo_m_prop, quiescent_fraction_abscissa=[1e12, 1e13, 1e14, 1e15], quiescent_fraction_ordinates=[0.25, 0.5, 0.75, 0.9]) model.param_dict['smhm_m0_0_active'] = 11 model.param_dict['smhm_m0_0_quiescent'] = 10.8 model.param_dict['smhm_m1_0_active'] = 12.2 model.param_dict['smhm_m1_0_quiescent'] = 11.8 model.param_dict['smhm_beta_0_active'] = 0.44 model.param_dict['smhm_beta_0_quiescent'] = 0.32 # model.param_dict['alphasat_active'] = 1 # model.param_dict['alphasat_quiescent'] = 1 # model.param_dict['betacut_active'] = 0.77 # model.param_dict['betacut_quiescent'] = -0.12 # model.param_dict['bcut_active'] = 0.2 # model.param_dict['bcut_quiescent'] = 0.2 # model.param_dict['betasat_active'] = 1.5 # model.param_dict['betasat_quiescent'] = 0.62 model.param_dict['bsat_active'] = 13 model.param_dict['bsat_quiescent'] = 8 elif model_name == 'hearin15': model = PrebuiltHodModelFactory('hearin15', redshift=redshift, threshold=11) elif model_name == 'zu_mandelbaum15': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-1) elif model_name == 'zu_mandelbaum16': model = PrebuiltHodModelFactory('zheng07', redshift=redshift, threshold=-1) elif model_name == 'cacciato09': model = PrebuiltHodModelFactory('cacciato09', redshift=redshift, threshold=10, prim_haloprop_key=halo_m_prop) model.param_dict['log_L_0'] = 9.935 model.param_dict['log_M_1'] = 12.9 # 11.07 model.param_dict['gamma_1'] = 0.3 # 3.273 model.param_dict['gamma_2'] = 0.255 model.param_dict['sigma'] = 0.143 model.param_dict['a_1'] = 0.501 model.param_dict['a_2'] = 2.106 model.param_dict['log_M_2'] = 14.28 model.param_dict['b_0'] = -0.5 # -0.766 model.param_dict['b_1'] = 1.008 model.param_dict['b_2'] = -0.094 model.param_dict['delta_1'] = 0 model.param_dict['delta_2'] = 0 # add useful model properties here model.model_name = model_name # add model_name field return model
upper_assembias_bound = np.inf, **kwargs) cens_occ_model = AssembiasZheng07Cens(threshold = -21) cens_prof_model = TrivialPhaseSpace() sats_occ_model = AssembiasZheng07Sats(threshold = -21) sats_prof_model = NFWPhaseSpace() model= HodModelFactory( centrals_occupation = cens_occ_model, centrals_profile = cens_prof_model, satellites_occupation = sats_occ_model, satellites_profile = sats_prof_model) print model.param_dict baseline_model = PrebuiltHodModelFactory("Zheng07" , threshold = -21) ############### Setting the model parameters to those of Guo 15####### model.param_dict['logM0'] = 12.59 model.param_dict['sigma_logM'] = 0.49 model.param_dict['logMmin'] = 12.78 model.param_dict['alpha'] = 1.14 model.param_dict['logM1'] = 13.99 model.param_dict['mean_occupation_satellites_assembias_param1'] = 1.0 baseline_model.param_dict['logM0'] = 12.59 baseline_model.param_dict['sigma_logM'] = 0.49 baseline_model.param_dict['logMmin'] = 12.78 baseline_model.param_dict['alpha'] = 1.14 baseline_model.param_dict['logM1'] = 13.99
import numpy as np from astropy.table import Table import pyfof def richness(group_id): ''' Calculate the richness of a group given group_ids of galaxies. Uses astropy.table module ''' gals = Table() gals['groupid'] = group_id gals['dummy'] = 1 grouped_table = gals.group_by('groupid') grp_richness = grouped_table['dummy'].groups.aggregate(np.sum) return grp_richness model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') model.populate_mock(halocat) pos =three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') #groups = FoFGroups(pos, 0.75, 0.75, Lbox = model.mock.Lbox, num_threads=1) #gids = groups.group_ids #print richness(gids) import time a = time.time() groups = pyfof.friends_of_friends(pos, 0.75*(len(pos)/1000**3.)**(-1./3))
def build_nbar_xi_gmf_cov(Mr=21): ''' Build covariance matrix for the full nbar, xi, gmf data vector using realisations of galaxy mocks for "data" HOD parameters in the halos from the multidark simulation. Covariance matrices for different sets of observables can be extracted from the full covariance matrix by slicing through the indices. ''' nbars = [] xir = [] gmfs = [] thr = -1. * np.float(Mr) model = PrebuiltHodModelFactory('zheng07', threshold=thr) halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') #some settings for tpcf calculations rbins = hardcoded_xi_bins() for i in xrange(1,125): print 'mock#', i # populate the mock subvolume model.populate_mock(halocat) # returning the positions of galaxies pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # calculate nbar nbars.append(len(pos) / 1000**3.) # translate the positions of randoms to the new subbox #calculate xi(r) xi = tpcf(pos, rbins, period = model.mock.Lbox, max_sample_size=int(2e5), estimator='Landy-Szalay') xir.append(xi) # calculate gmf nbar = len(pos) / 1000**3. b_normal = 0.75 b = b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins = gmf_bins() gmf = np.histogram(w , gbins)[0] / (1000.**3.) gmfs.append(gmf) # GMF # save nbar variance nbar_var = np.var(nbars, axis=0, ddof=1) nbar_file = ''.join([util.multidat_dir(), 'abc_nbar_var.Mr', str(Mr), '.dat']) np.savetxt(nbar_file, [nbar_var]) # write full covariance matrix of various combinations of the data # and invert for the likelihood evaluations # --- covariance for all three --- fulldatarr = np.hstack((np.array(nbars).reshape(len(nbars), 1), np.array(xir), np.array(gmfs))) fullcov = np.cov(fulldatarr.T) fullcorr = np.corrcoef(fulldatarr.T) # and save the covariance matrix nopoisson_file = ''.join([util.multidat_dir(), 'abc_nbar_xi_gmf_cov.no_poisson.Mr', str(Mr), '.dat']) np.savetxt(nopoisson_file, fullcov) # and a correlation matrix full_corr_file = ''.join([util.multidat_dir(), 'abc_nbar_xi_gmf_corr.Mr', str(Mr), '.dat']) np.savetxt(full_corr_file, fullcorr) return None
class MCMC_HODsim(object): def __init__(self, Mr=21, b_normal=0.25): ''' Class object that describes our forward model used in MCMC inference. Our model forward models the galaxy catalog using HOD parameters using HaloTools. ''' self.Mr = Mr self.b_normal = b_normal thr = -1. * np.float(Mr) self.model = PrebuiltHodModelFactory('zheng07', threshold=thr) self.halocat = CachedHaloCatalog(simname = 'multidark', redshift = 0, halo_finder = 'rockstar') ##self.RR = data_RR(box='md_all') ##self.randoms = data_random('md_all') ##self.NR = len(self.randoms) def __call__(self, theta, prior_range=None, observables=['nbar', 'gmf']): return self._sum_stat(theta, prior_range=prior_range, observables=observables) def _sum_stat(self, theta, prior_range=None, observables=['nbar', 'gmf']): ''' Given theta, sum_stat calculates the observables from our forward model Parameters ---------- theta : (self explanatory) prior_range : If specified, checks to make sure that theta is within the prior range. ''' self.model.param_dict['logM0'] = theta[0] self.model.param_dict['sigma_logM'] = np.exp(theta[1]) self.model.param_dict['logMmin'] = theta[2] self.model.param_dict['alpha'] = theta[3] self.model.param_dict['logM1'] = theta[4] rbins = xi_binedges() rmax = rbins.max() approx_cell1_size = [rmax , rmax , rmax] approx_cellran_size = [rmax , rmax , rmax] if prior_range is None: self.model.populate_mock(self.halocat) pos =three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 1000.**3.) # nbar of the galaxy catalog elif obv == 'gmf': nbar = len(pos) / 1000**3. b = self.b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins =data_gmf_bins() gmf = np.histogram(w , gbins)[0] / (1000.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf( pos, rbins, period=self.model.mock.Lbox, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size) obvs.append(greek_xi) else: raise NotImplementedError('Only nbar 2pcf, gmf implemented so far') return obvs else: if np.all((prior_range[:,0] < theta) & (theta < prior_range[:,1])): # if all theta_i is within prior range ... try: self.model.populate_mock(self.halocat) pos=three_dim_pos_bundle(self.model.mock.galaxy_table, 'x', 'y', 'z') obvs = [] for obv in observables: if obv == 'nbar': obvs.append(len(pos) / 1000**3.) # nbar of the galaxy catalog elif obv == 'gmf': nbar = len(pos) / 1000**3. b = self.b_normal * (nbar)**(-1./3) groups = pyfof.friends_of_friends(pos , b) w = np.array([len(x) for x in groups]) gbins =data_gmf_bins() gmf = np.histogram(w , gbins)[0] / (1000.**3.) obvs.append(gmf) elif obv == 'xi': greek_xi = tpcf( pos, rbins, period=self.model.mock.Lbox, max_sample_size=int(3e5), estimator='Natural', approx_cell1_size=approx_cell1_size) obvs.append(greek_xi) else: raise NotImplementedError('Only nbar, tpcf, and gmf are implemented so far') return obvs except ValueError: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1]*1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs else: obvs = [] for obv in observables: if obv == 'nbar': obvs.append(10.) elif obv == 'gmf': bins = data_gmf_bins() obvs.append(np.ones_like(bins)[:-1]*1000.) elif obv == 'xi': obvs.append(np.zeros(len(xi_binedges()[:-1]))) return obvs
def build_xi_nbar_gmf_cov(Mr=21, Nmock=500): ''' Build covariance matrix for xi, variance for nbar, and a bunch of stuff for gmf ... using Nmock realizations of halotool mocks ''' xir = [] model = PrebuiltHodModelFactory('zheng07', threshold = -1.0*np.float(Mr)) #nbars = [] #gmfs = [] #gmf_counts = [] for i in xrange(Nmock): print 'mock#', i model.populate_mock() # xi(r) xir.append(model.mock.compute_galaxy_clustering(rbins=hardcoded_xi_bins())[1]) # nbar #nbars.append(model.mock.number_density) # gmf #rich = richness(model.mock.compute_fof_group_ids()) #gmfs.append(GMF(rich)) # GMF #gmf_counts.append(GMF(rich, counts=True)) # Group counts # save xi covariance xi_covar = np.cov(np.array(xir).T) output_file = ''.join([util.dat_dir(), 'xir_covariance.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) np.savetxt(output_file, xi_covar) # save nbar values #nbar_cov = np.var(nbars, axis=0) #output_file = ''.join([util.dat_dir(), 'nbar_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(output_file, [nbar_cov]) # write GMF covariance #gmf_cov = np.cov(np.array(gmfs).T) #output_file = ''.join([util.dat_dir(), 'gmf_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(output_file, gmf_cov) # write GMF Poisson #gmf_counts_mean = np.mean(gmf_counts, axis=0) #poisson_gmf = np.sqrt(gmf_counts_mean) / 250.**3 # poisson errors #output_file = ''.join([util.dat_dir(), 'gmf_sigma_poisson.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(output_file, poisson_gmf) # write GMF standard dev #sigma_gmf = np.std(gmfs, axis=0) # sample variance #output_file = ''.join([util.dat_dir(), 'gmf_sigma_stddev.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(output_file, sigma_gmf) # write GMF total noise #sigma_tot = (sigma_gmf**2 + poisson_gmf**2)**0.5 # total sigma #output_file = ''.join([util.dat_dir(), 'gmf_sigma.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(output_file, sigma_tot) # write full covariance matrix of various combinations of the data # covariance for all three #fulldatarr = np.hstack(np.array(nbars).reshape(nbars.shape[0], 1), # np.array(gmfs), np.array(xir)) #fullcov = np.cov(fulldatarr.T) #outfn = ''.join([util.dat_dir(), 'nbar_gmf_xir_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(outfn, fullcov) # covariance for nbar and gmf ##nbgmf_arr = np.hstack(np.array(nbars).reshape(nbars.shape[0], 1), # np.array(gmfs)) #nbgmf_cov = np.cov(nbgmf_arr.T) #outfn = ''.join([util.dat_dir(), 'nbar_gmf_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(outfn, nbgmf_cov) # covariance for nbar and xi #nbxi_arr = np.hstack(np.array(nbars).reshape(nbars.shape[0], 1), # np.array(xir)) #nbxi_cov = np.cov(nbxi_arr.T) #outfn = ''.join([util.dat_dir(), 'nbar_xi_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(outfn, nbxi_cov) # covariance for gmf and xi #gmfxi_arr = np.hstack(np.array(gmfs), np.array(xir)) #gmfxi_cov = np.cov(gmfxi_arr.T) #outfn = ''.join([util.dat_dir(), 'gmf_xi_cov.Mr', str(Mr), '.Nmock', str(Nmock), '.dat']) #np.savetxt(outfn, gmfxi_cov) return None
def test_precomputed_rr(Nr, Mr = 21): ''' Mr = Luminositty threshold Nr = Number of randoms ''' rbins = np.logspace(-1, 1.25, 15) rmax = rbins.max() rbin_centers = (rbins[1:] + rbins[0:-1])/2. halocat = CachedHaloCatalog(simname = 'bolshoi', redshift = 0) model = PrebuiltHodModelFactory("zheng07") model.populate_mock(halocat = halocat, enforce_PBC = False) data = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') print data.shape L = halocat.Lbox xmin , ymin , zmin = 0., 0., 0. xmax , ymax , zmax = L, L, L num_randoms = Nr xran = np.random.uniform(xmin, xmax, num_randoms) yran = np.random.uniform(ymin, ymax, num_randoms) zran = np.random.uniform(zmin, zmax, num_randoms) randoms = np.vstack((xran, yran, zran)).T verbose = False num_threads = cpu_count() period = None approx_cell1_size = [rmax, rmax, rmax] approx_cell2_size = approx_cell1_size approx_cellran_size = [rmax, rmax, rmax] normal_result = tpcf( data, rbins, data, randoms=randoms, period = period, max_sample_size=int(1e4), estimator='Landy-Szalay', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size) #count data pairs DD = npairs( data, data, rbins, period, verbose, num_threads, approx_cell1_size, approx_cell2_size) DD = np.diff(DD) #count random pairs RR = npairs( randoms, randoms, rbins, period, verbose, num_threads, approx_cellran_size, approx_cellran_size) RR = np.diff(RR) #count data random pairs DR = npairs( data, randoms, rbins, period, verbose, num_threads, approx_cell1_size, approx_cell2_size) DR = np.diff(DR) print "DD=", DD print "DR=", DR print "RR=", RR ND = len(data) NR = len(randoms) factor1 = ND*ND/(NR*NR) factor2 = ND*NR/(NR*NR) mult = lambda x,y: x*y xi_LS = mult(1.0/factor1,DD/RR) - mult(1.0/factor2,2.0*DR/RR) + 1.0 print "xi=" , xi_LS print "normal=" , normal_result result_with_RR_precomputed = tpcf( data, rbins, data, randoms=randoms, period = period, max_sample_size=int(1e5), estimator='Landy-Szalay', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed = RR, NR_precomputed = NR) print "xi_pre=" , result_with_RR_precomputed
def test_precomputed_rr(Nr, Mr=21): ''' Mr = Luminositty threshold Nr = Number of randoms ''' rbins = np.logspace(-1, 1.25, 15) rmax = rbins.max() rbin_centers = (rbins[1:] + rbins[0:-1]) / 2. halocat = CachedHaloCatalog(simname='bolshoi', redshift=0) model = PrebuiltHodModelFactory("zheng07") model.populate_mock(halocat=halocat, enforce_PBC=False) data = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') print data.shape L = halocat.Lbox xmin, ymin, zmin = 0., 0., 0. xmax, ymax, zmax = L, L, L num_randoms = Nr xran = np.random.uniform(xmin, xmax, num_randoms) yran = np.random.uniform(ymin, ymax, num_randoms) zran = np.random.uniform(zmin, zmax, num_randoms) randoms = np.vstack((xran, yran, zran)).T verbose = False num_threads = cpu_count() period = None approx_cell1_size = [rmax, rmax, rmax] approx_cell2_size = approx_cell1_size approx_cellran_size = [rmax, rmax, rmax] normal_result = tpcf(data, rbins, data, randoms=randoms, period=period, max_sample_size=int(1e4), estimator='Landy-Szalay', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size) #count data pairs DD = npairs(data, data, rbins, period, verbose, num_threads, approx_cell1_size, approx_cell2_size) DD = np.diff(DD) #count random pairs RR = npairs(randoms, randoms, rbins, period, verbose, num_threads, approx_cellran_size, approx_cellran_size) RR = np.diff(RR) #count data random pairs DR = npairs(data, randoms, rbins, period, verbose, num_threads, approx_cell1_size, approx_cell2_size) DR = np.diff(DR) print "DD=", DD print "DR=", DR print "RR=", RR ND = len(data) NR = len(randoms) factor1 = ND * ND / (NR * NR) factor2 = ND * NR / (NR * NR) mult = lambda x, y: x * y xi_LS = mult(1.0 / factor1, DD / RR) - mult(1.0 / factor2, 2.0 * DR / RR) + 1.0 print "xi=", xi_LS print "normal=", normal_result result_with_RR_precomputed = tpcf(data, rbins, data, randoms=randoms, period=period, max_sample_size=int(1e5), estimator='Landy-Szalay', approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) print "xi_pre=", result_with_RR_precomputed
def Subvolume_Analytic(N_sub, ratio=False): ''' Test the 2PCF estimates from MultiDark subvolume versus the analytic 2PCF for the entire MultiDark volume Parameters ---------- N_sub : (int) Number of subvolumes to sample ''' prettyplot() pretty_colors = prettycolors() pickle_file = ''.join([ '/export/bbq2/hahn/ccppabc/dump/', 'xi_subvolume_test', '.Nsub', str(N_sub), '.p' ]) fig = plt.figure(1) sub = fig.add_subplot(111) xi_bin = xi_binedges() if os.path.isfile(pickle_file): data_dump = pickle.load(open(pickle_file, 'rb')) full_xi = data_dump['full_xi'] else: # Entire MultiDark Volume (Analytic xi) model = PrebuiltHodModelFactory('zheng07', threshold=-21) halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') model.populate_mock(halocat) pos = three_dim_pos_bundle(model.mock.galaxy_table, 'x', 'y', 'z') # while the estimator claims to be Landy-Szalay, I highly suspect it # actually uses Landy-Szalay since DR pairs cannot be calculated from # analytic randoms full_xi = tpcf(pos, xi_bin, period=model.mock.Lbox, max_sample_size=int(2e5), estimator='Landy-Szalay', num_threads=1) data_dump = {} data_dump['full_xi'] = full_xi if not ratio: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), full_xi, lw=2, ls='-', c='k', label=r'Analytic $\xi$ Entire Volume') if not os.path.isfile(pickle_file): # MultiDark SubVolume (precomputed RR pairs) sub_model = PrebuiltHodModelFactory('zheng07', threshold=-21) sub_model.new_haloprop_func_dict = {'sim_subvol': util.mk_id_column} sub_halocat = CachedHaloCatalog(simname='multidark', redshift=0, halo_finder='rockstar') RR = data_RR() randoms = data_random() NR = len(randoms) for method in ['Landy-Szalay', 'Natural']: if method == 'Landy-Szalay': iii = 3 elif method == 'Natural': iii = 5 if not os.path.isfile(pickle_file): sub_xis_list = [] sub_xis = np.zeros(len(full_xi)) for ii in range(1, N_sub + 1): # randomly sample one of the subvolumes rint = ii #np.random.randint(1, 125) simsubvol = lambda x: util.mask_func(x, rint) sub_model.populate_mock(sub_halocat, masking_function=simsubvol, enforce_PBC=False) pos = three_dim_pos_bundle(sub_model.mock.galaxy_table, 'x', 'y', 'z') xi, yi, zi = util.random_shifter(rint) temp_randoms = randoms.copy() temp_randoms[:, 0] += xi temp_randoms[:, 1] += yi temp_randoms[:, 2] += zi rmax = xi_bin.max() approx_cell1_size = [rmax, rmax, rmax] approx_cellran_size = [rmax, rmax, rmax] sub_xi = tpcf(pos, xi_bin, pos, randoms=temp_randoms, period=None, max_sample_size=int(1e5), estimator=method, approx_cell1_size=approx_cell1_size, approx_cellran_size=approx_cellran_size, RR_precomputed=RR, NR_precomputed=NR) label = None if ii == N_sub - 1: label = 'Subvolumes' #if not ratio: # sub.plot(0.5*(xi_bin[:-1]+xi_bin[1:]), sub_xi, lw=0.5, ls='--', c=pretty_colors[iii]) sub_xis += sub_xi sub_xis_list.append(sub_xi) sub_xi_avg = sub_xis / np.float(N_sub) data_dump[method] = {} data_dump[method]['sub_xi_avg'] = sub_xi_avg data_dump[method]['sub_xis_list'] = sub_xis_list else: sub_xis_list = data_dump[method]['sub_xis_list'] sub_xi_avg = data_dump[method]['sub_xi_avg'] if not ratio: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), sub_xi_avg, lw=2, ls='--', c=pretty_colors[iii], label='Subvolume ' + method) else: sub.plot(0.5 * (xi_bin[:-1] + xi_bin[1:]), sub_xi_avg / full_xi, lw=2, ls='--', c=pretty_colors[iii], label='Subvolume ' + method) if not os.path.isfile(pickle_file): pickle.dump(data_dump, open(pickle_file, 'wb')) sub.set_xlim([0.1, 50.]) sub.set_xlabel('r', fontsize=30) sub.set_xscale('log') if not ratio: sub.set_ylabel(r"$\xi \mathtt{(r)}$", fontsize=25) sub.set_yscale('log') else: sub.set_ylabel(r"$\overline{\xi^\mathtt{sub}}/\xi^\mathtt{all}$", fontsize=25) sub.legend(loc='lower left') if ratio: fig_file = ''.join([ util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.ratio.png' ]) else: fig_file = ''.join([ util.fig_dir(), 'test_xi_subvolume_analytic.Nsub', str(N_sub), '.png' ]) fig.savefig(fig_file, bbox_inches='tight', dpi=100) plt.close() return None