def gmf(Mr=20, Nmock=500): ''' Plot Group Multiplicty Function of fake observations ''' prettyplot() pretty_colors = prettycolors() # import fake obs GMF gmf, sig_gmf = Data.data_gmf(Mr=Mr, Nmock=Nmock) # group richness bins gmf_bin = Data.data_gmf_bins() fig = plt.figure(1) sub = fig.add_subplot(111) sub.errorbar( 0.5*(gmf_bin[:-1]+gmf_bin[1:]), gmf, yerr=sig_gmf, fmt="ok", capsize=1.0 ) sub.set_xlim([1, 60]) sub.set_yscale('log') sub.set_ylabel(r"Group Multiplicity Function (h$^{3}$ Mpc$^{-3}$)", fontsize=20) sub.set_xlabel(r"$\mathtt{Group\;\;Richness}$", fontsize=20) # save to file fig_file = ''.join([util.fig_dir(), 'gmf.Mr', str(Mr), '.Nmock', str(Nmock), '.png']) fig.savefig(fig_file, bbox_inches='tight') return None
def TrueObservables(Mr=21, b_normal=0.25): ''' Plot xi and gmf for the data ''' # xi data xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_xi_cov = cov_data[1:16, 1:16] xi_r_bin = Data.data_xi_bin(Mr=Mr) # gmf data data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_gmf_cov = cov_data[16:, 16:] r_binedge = Data.data_gmf_bins() gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) prettyplot() pretty_colors = prettycolors() fig = plt.figure(figsize=(12, 6)) sub_xi = fig.add_subplot(121) sub_gmf = fig.add_subplot(122) #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', # markersize=0, lw=0, capsize=3, elinewidth=1.5) #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0) sub_xi.fill_between(xi_r_bin, xi_data - np.sqrt(np.diag(data_xi_cov)), xi_data + np.sqrt(np.diag(data_xi_cov)), color=pretty_colors[1]) sub_xi.set_yscale('log') sub_xi.set_xscale('log') sub_xi.set_xlim(0.1, 20) sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25) sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25) #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), # fmt="o", color='k', # markersize=0, lw=0, capsize=4, elinewidth=2) #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation') sub_gmf.fill_between(gmf_r_bin, data_gmf - np.sqrt(np.diag(data_gmf_cov)), data_gmf + np.sqrt(np.diag(data_gmf_cov)), color=pretty_colors[1]) sub_gmf.set_xlim(1, 20) sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25) sub_gmf.yaxis.tick_right() sub_gmf.yaxis.set_ticks_position('both') sub_gmf.yaxis.set_label_position('right') sub_gmf.set_ylim([10**-7, 2.0 * 10**-4]) sub_gmf.set_yscale('log') sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25) fig.subplots_adjust(hspace=0.05) fig_name = ''.join([ut.fig_dir(), 'paper.data_observables', '.pdf']) fig.savefig(fig_name, bbox_inches='tight', dpi=150) plt.close() return None
def TrueObservables(Mr=21, b_normal=0.25): ''' Plot xi and gmf for the data ''' # xi data xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_xi_cov = cov_data[1:16, 1:16] xi_r_bin = Data.data_xi_bin(Mr=Mr) # gmf data data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_gmf_cov = cov_data[16:, 16:] r_binedge = Data.data_gmf_bins() gmf_r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) prettyplot() pretty_colors = prettycolors() fig = plt.figure(figsize=(12,6)) sub_xi = fig.add_subplot(121) sub_gmf = fig.add_subplot(122) #sub_xi.errorbar(xi_r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', # markersize=0, lw=0, capsize=3, elinewidth=1.5) #sub_xi.scatter(xi_r_bin, xi_data, c='k', s=10, lw=0) sub_xi.fill_between(xi_r_bin, xi_data-np.sqrt(np.diag(data_xi_cov)), xi_data+np.sqrt(np.diag(data_xi_cov)), color=pretty_colors[1]) sub_xi.set_yscale('log') sub_xi.set_xscale('log') sub_xi.set_xlim(0.1, 20) sub_xi.set_xlabel(r'$\mathtt{r}\; [\mathtt{Mpc}/h]$', fontsize=25) sub_xi.set_ylabel(r'$\xi(r)$', fontsize=25) #sub_gmf.errorbar(gmf_r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), # fmt="o", color='k', # markersize=0, lw=0, capsize=4, elinewidth=2) #sub_gmf.scatter(gmf_r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation') sub_gmf.fill_between(gmf_r_bin, data_gmf-np.sqrt(np.diag(data_gmf_cov)), data_gmf+np.sqrt(np.diag(data_gmf_cov)), color=pretty_colors[1]) sub_gmf.set_xlim(1, 20) sub_gmf.set_xlabel(r'$\mathtt{N}$ (Group Richness)', fontsize=25) sub_gmf.yaxis.tick_right() sub_gmf.yaxis.set_ticks_position('both') sub_gmf.yaxis.set_label_position('right') sub_gmf.set_ylim([10**-7, 2.0*10**-4]) sub_gmf.set_yscale('log') sub_gmf.set_ylabel(r'$\zeta(\mathtt{N})$', fontsize=25) fig.subplots_adjust(hspace=0.05) fig_name = ''.join([ut.fig_dir(), 'paper.data_observables', '.pdf']) fig.savefig(fig_name, bbox_inches='tight', dpi=150) plt.close() return None
def PlotCovariance(obvs, Mr=21, b_normal=0.25, inference='mcmc'): ''' Plot the covariance matrix for a specified obvs ''' # import the covariance matrix covar = Data.data_cov(Mr=Mr, b_normal=b_normal, inference=inference) if obvs == 'xi': obvs_cov = covar[1:16 , 1:16] r_bin = Data.xi_binedges() elif obvs == 'gmf': obvs_cov = covar[17:, 17:] binedges = Data.data_gmf_bins() r_bin = 0.5 * (binedges[:-1] + binedges[1:]) n_bin = int(np.sqrt(obvs_cov.size)) # calculate the reduced covariance for plotting red_covar = np.zeros([n_bin, n_bin]) for ii in range(n_bin): for jj in range(n_bin): red_covar[ii][jj] = obvs_cov[ii][jj]/np.sqrt(obvs_cov[ii][ii] * obvs_cov[jj][jj]) prettyplot() fig = plt.figure() sub = fig.add_subplot(111) cont = sub.pcolormesh(r_bin, r_bin, red_covar, cmap=plt.cm.afmhot_r) plt.colorbar(cont) sub.set_xlim([r_bin[0], r_bin[-1]]) sub.set_ylim([r_bin[0], r_bin[-1]]) sub.set_xscale('log') sub.set_yscale('log') sub.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc/h}$]', fontsize=25) sub.set_ylabel(r'$\mathtt{r}\;[\mathtt{Mpc/h}$]', fontsize=25) fig_file = ''.join([util.fig_dir(), obvs.upper(), 'covariance', '.Mr', str(Mr), '.bnorm', str(round(b_normal,2)), '.', inference, '_inf.png']) fig.savefig(fig_file, bbox_inches='tight') plt.close() return None
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
def PosteriorObservable(Mr=21, b_normal=0.25, clobber=False): ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood ''' prettyplot() pretty_colors = prettycolors() fig = plt.figure(1, figsize=(16, 12)) gs = gridspec.GridSpec(2, 2, height_ratios=[2.5, 1], width_ratios=[1, 1]) for obvs in ['nbargmf', 'nbarxi']: if obvs == 'nbargmf': result_dir = ''.join([ ut.dat_dir(), 'paper/ABC', obvs, '/run1/', ]) theta_file = lambda tt: ''.join( [result_dir, 'nbar_gmf_theta_t', str(tt), '.ABCnbargmf.dat']) tf = 8 obvs_list = ['gmf'] elif obvs == 'nbarxi': result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/']) theta_file = lambda tt: ''.join( [result_dir, 'nbar_xi_theta_t', str(tt), '.abc.dat']) tf = 9 obvs_list = ['xi'] else: raise ValueError theta = np.loadtxt(theta_file(tf)) # import thetas #theta = theta[:10] obvs_file = ''.join( theta_file(tf).rsplit('.dat')[:-1] + ['.', obvs_list[0], '.p']) print obvs_file HODsimulator = ABC_HODsim(Mr=Mr, b_normal=b_normal) if not os.path.isfile(obvs_file) or clobber: model_obv = [] for i in xrange(len(theta)): print i obv_i = HODsimulator(theta[i], prior_range=None, observables=obvs_list) model_obv.append(obv_i[0]) model_obv = np.array(model_obv) pickle.dump(model_obv, open(obvs_file, 'wb')) else: model_obv = pickle.load(open(obvs_file, 'rb')) if 'xi' in obvs: r_bin = Data.data_xi_bin(Mr=Mr) elif 'gmf' in obvs: r_binedge = Data.data_gmf_bins() r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0) # plotting if obvs == 'nbarxi': ax = plt.subplot(gs[0]) elif obvs == 'nbargmf': ax = plt.subplot(gs[1]) if 'xi' in obvs: # 2PCF xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_xi_cov = cov_data[1:16, 1:16] ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, xi_data, yerr=np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.scatter(r_bin, xi_data, c='k', s=10, lw=0) ax.set_ylabel(r'$\xi_\mathtt{gg}(\mathtt{r})$', fontsize=27) ax.set_yscale('log') ax.set_xscale('log') ax.set_xticklabels([]) ax.set_xlim([0.1, 20.]) ax.set_ylim([0.09, 1000.]) ax = plt.subplot(gs[2]) ax.fill_between(r_bin, a / xi_data, e / xi_data, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b / xi_data, d / xi_data, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, np.repeat(1., len(xi_data)), yerr=np.sqrt(np.diag(data_xi_cov)) / xi_data, fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.plot(np.arange(0.1, 20., 0.1), np.repeat(1., len(np.arange(0.1, 20, 0.1))), c='k', ls='--', lw=2) ax.set_xlim([0.1, 20.]) ax.set_xscale('log') ax.set_ylim([0.5, 1.5]) ax.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc}/h]$', fontsize=25) ax.set_ylabel(r'$\xi_\mathtt{gg}/\xi_\mathtt{gg}^\mathtt{obvs}$', fontsize=25) elif 'gmf' in obvs: # GMF data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_gmf_cov = cov_data[16:, 16:] ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none", label='ABC Posterior') ax.errorbar(r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), fmt="o", color='k', markersize=0, lw=0, capsize=4, elinewidth=2) ax.scatter(r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation') ax.legend(loc='upper right', scatterpoints=1, prop={'size': 25}, borderpad=1.0) ax.yaxis.tick_right() ax.yaxis.set_ticks_position('both') ax.yaxis.set_label_position('right') ax.set_ylabel(r'$\zeta$ $[(\mathrm{h}/\mathtt{Mpc})^{3}]$', fontsize=25) ax.set_yscale('log') ax.set_xlim([1., 20.]) ax.set_xticklabels([]) ax.set_ylim([10.**-7.2, 2 * 10**-4.]) ax = plt.subplot(gs[3]) ax.fill_between(r_bin, a / data_gmf, e / data_gmf, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b / data_gmf, d / data_gmf, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, np.repeat(1., len(data_gmf)), yerr=np.sqrt(np.diag(data_gmf_cov)) / data_gmf, fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.plot(np.arange(1., 20., 1), np.repeat(1., len(np.arange(1., 20, 1))), c='k', ls='--', lw=1.75) ax.yaxis.tick_right() ax.yaxis.set_label_position('right') ax.set_ylim([-0.1, 2.1]) ax.set_ylabel(r'$\zeta/\zeta^\mathtt{obvs}$', fontsize=25) ax.set_xlim([1., 20.]) ax.set_xlabel(r'$\mathtt{N}$ [Group Richness]', fontsize=25) fig.subplots_adjust(wspace=0.05, hspace=0.0) fig_name = ''.join([ut.fig_dir(), 'paper', '.ABCposterior', '.pdf']) fig.savefig(fig_name, bbox_inches='tight') plt.close() return None
def plot_posterior_model(observable, abc_theta_file=None, data_dict={'Mr':20, 'b_normal': 0.25, 'Nmock':500}, clobber=False): ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood Parameters ---------- observable : string One of the following strings ['xi', 'scaledxi', 'gmf'] ''' # load the particles if abc_theta_file is None: raise ValueError("Please specify the theta output file from ABC-PMC run") theta = np.loadtxt(abc_theta_file) if observable == 'scaledxi': obvs_str = 'xi' else: obvs_str = observable obvs_file = ''.join(abc_theta_file.rsplit('.dat')[:-1] + ['.', observable, '.dat']) print obvs_file if not os.path.isfile(obvs_file) or clobber: for i in xrange(len(theta)): obv_i = HODsimulator( theta[i], prior_range=None, observables=[obvs_str], Mr=data_dict['Mr']) try: model_obv.append(obv_i[0]) except UnboundLocalError: model_obv = [obv_i[0]] model_obv = np.array(model_obv) np.savetxt(obvs_file, model_obv) else: model_obv = np.loadtxt(obvs_file) if 'xi' in observable: r_bin = Data.data_xi_bins(Mr=data_dict['Mr']) elif observable == 'gmf': r_bin = Data.data_gmf_bins() a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0) # plotting fig = plt.figure(1) ax = fig.add_subplot(111) if observable == 'xi': # 2PCF data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none") #ax.plot(r_bin, c, "k", lw=1) ax.errorbar(r_bin, data_xi, yerr = np.sqrt(np.diag(data_xi_cov)), fmt=".k", capsize=0) ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20) ax.set_ylabel(r'$\xi_{\rm gg}$', fontsize=25) ax.set_xscale('log') ax.set_xlim([0.1, 20.]) elif observable == 'scaledxi': # Scaled 2PFC (r * xi) data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data ax.fill_between(r_bin, r_bin*a, r_bin*e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, r_bin*b, r_bin*d, color="k", alpha=0.3, edgecolor="none") ax.errorbar(r_bin, r_bin*data_xi, yerr=r_bin*np.sqrt(np.diag(data_xi_cov)), fmt=".k", capsize=0) ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20) ax.set_ylabel(r'$r \xi_{\rm gg}$', fontsize=25) ax.set_xscale('log') ax.set_xlim([0.1, 20.]) elif observable == 'gmf': # GMF data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict) ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none") ax.errorbar(r_bin, data_gmf, yerr = data_gmf_sigma, fmt=".k", capsize=0) ax.set_xlabel(r'Group Richness', fontsize=25) ax.set_ylabel(r'GMF $[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25) ax.set_yscale('log') ax.set_xlim([1., 50.]) fig.savefig( ''.join([util.fig_dir(), observable, '.posterior_prediction', '.Mr', str(data_dict['Mr']), '_Nmock', str(data_dict['Nmock']), '.pdf']), bbox_inches='tight')
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 _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
def plot_posterior_model(observable, abc_theta_file=None, data_dict={ 'Mr': 20, 'b_normal': 0.25, 'Nmock': 500 }, clobber=False): ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood Parameters ---------- observable : string One of the following strings ['xi', 'scaledxi', 'gmf'] ''' # load the particles if abc_theta_file is None: raise ValueError( "Please specify the theta output file from ABC-PMC run") theta = np.loadtxt(abc_theta_file) if observable == 'scaledxi': obvs_str = 'xi' else: obvs_str = observable obvs_file = ''.join( abc_theta_file.rsplit('.dat')[:-1] + ['.', observable, '.dat']) print obvs_file if not os.path.isfile(obvs_file) or clobber: for i in xrange(len(theta)): obv_i = HODsimulator(theta[i], prior_range=None, observables=[obvs_str], Mr=data_dict['Mr']) try: model_obv.append(obv_i[0]) except UnboundLocalError: model_obv = [obv_i[0]] model_obv = np.array(model_obv) np.savetxt(obvs_file, model_obv) else: model_obv = np.loadtxt(obvs_file) if 'xi' in observable: r_bin = Data.data_xi_bins(Mr=data_dict['Mr']) elif observable == 'gmf': r_bin = Data.data_gmf_bins() a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0) # plotting fig = plt.figure(1) ax = fig.add_subplot(111) if observable == 'xi': # 2PCF data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none") #ax.plot(r_bin, c, "k", lw=1) ax.errorbar(r_bin, data_xi, yerr=np.sqrt(np.diag(data_xi_cov)), fmt=".k", capsize=0) ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20) ax.set_ylabel(r'$\xi_{\rm gg}$', fontsize=25) ax.set_xscale('log') ax.set_xlim([0.1, 20.]) elif observable == 'scaledxi': # Scaled 2PFC (r * xi) data_xi, data_xi_cov = Data.data_xi_full_cov(**data_dict) # data ax.fill_between(r_bin, r_bin * a, r_bin * e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, r_bin * b, r_bin * d, color="k", alpha=0.3, edgecolor="none") ax.errorbar(r_bin, r_bin * data_xi, yerr=r_bin * np.sqrt(np.diag(data_xi_cov)), fmt=".k", capsize=0) ax.set_xlabel(r'$r\;[\mathrm{Mpc}/h]$', fontsize=20) ax.set_ylabel(r'$r \xi_{\rm gg}$', fontsize=25) ax.set_xscale('log') ax.set_xlim([0.1, 20.]) elif observable == 'gmf': # GMF data_gmf, data_gmf_sigma = Data.data_gmf(**data_dict) ax.fill_between(r_bin, a, e, color="k", alpha=0.1, edgecolor="none") ax.fill_between(r_bin, b, d, color="k", alpha=0.3, edgecolor="none") ax.errorbar(r_bin, data_gmf, yerr=data_gmf_sigma, fmt=".k", capsize=0) ax.set_xlabel(r'Group Richness', fontsize=25) ax.set_ylabel(r'GMF $[\mathrm{h}^3\mathrm{Mpc}^{-3}]$', fontsize=25) ax.set_yscale('log') ax.set_xlim([1., 50.]) fig.savefig(''.join([ util.fig_dir(), observable, '.posterior_prediction', '.Mr', str(data_dict['Mr']), '_Nmock', str(data_dict['Nmock']), '.pdf' ]), bbox_inches='tight')
def PosteriorObservable(Mr=21, b_normal=0.25, clobber=False): ''' Plot 1\sigma and 2\sigma model predictions from ABC-PMC posterior likelihood ''' prettyplot() pretty_colors=prettycolors() fig = plt.figure(1, figsize=(16,12)) gs = gridspec.GridSpec(2, 2, height_ratios=[2.5, 1], width_ratios=[1,1]) for obvs in ['nbargmf', 'nbarxi']: if obvs == 'nbargmf': result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/run1/',]) theta_file = lambda tt: ''.join([result_dir, 'nbar_gmf_theta_t', str(tt), '.ABCnbargmf.dat']) tf = 8 obvs_list = ['gmf'] elif obvs == 'nbarxi': result_dir = ''.join([ut.dat_dir(), 'paper/ABC', obvs, '/']) theta_file = lambda tt: ''.join([result_dir, 'nbar_xi_theta_t', str(tt), '.abc.dat']) tf = 9 obvs_list = ['xi'] else: raise ValueError theta = np.loadtxt(theta_file(tf)) # import thetas #theta = theta[:10] obvs_file = ''.join(theta_file(tf).rsplit('.dat')[:-1] + ['.', obvs_list[0], '.p']) print obvs_file HODsimulator = ABC_HODsim(Mr=Mr, b_normal=b_normal) if not os.path.isfile(obvs_file) or clobber: model_obv = [] for i in xrange(len(theta)): print i obv_i = HODsimulator( theta[i], prior_range=None, observables=obvs_list) model_obv.append(obv_i[0]) model_obv = np.array(model_obv) pickle.dump(model_obv, open(obvs_file, 'wb')) else: model_obv = pickle.load(open(obvs_file, 'rb')) if 'xi' in obvs: r_bin = Data.data_xi_bin(Mr=Mr) elif 'gmf' in obvs: r_binedge = Data.data_gmf_bins() r_bin = 0.5 * (r_binedge[:-1] + r_binedge[1:]) a, b, c, d, e = np.percentile(model_obv, [2.5, 16, 50, 84, 97.5], axis=0) # plotting if obvs == 'nbarxi': ax = plt.subplot(gs[0]) elif obvs == 'nbargmf': ax = plt.subplot(gs[1]) if 'xi' in obvs: # 2PCF xi_data = Data.data_xi(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_xi_cov = cov_data[1:16, 1:16] ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, xi_data, yerr = np.sqrt(np.diag(data_xi_cov)), fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.scatter(r_bin, xi_data, c='k', s=10, lw=0) ax.set_ylabel(r'$\xi_\mathtt{gg}(\mathtt{r})$', fontsize=27) ax.set_yscale('log') ax.set_xscale('log') ax.set_xticklabels([]) ax.set_xlim([0.1, 20.]) ax.set_ylim([0.09, 1000.]) ax = plt.subplot(gs[2]) ax.fill_between(r_bin, a/xi_data, e/xi_data, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b/xi_data, d/xi_data, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, np.repeat(1., len(xi_data)), yerr=np.sqrt(np.diag(data_xi_cov))/xi_data, fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.plot(np.arange(0.1, 20., 0.1), np.repeat(1., len(np.arange(0.1, 20, 0.1))), c='k', ls='--', lw=2) ax.set_xlim([0.1, 20.]) ax.set_xscale('log') ax.set_ylim([0.5, 1.5]) ax.set_xlabel(r'$\mathtt{r}\;[\mathtt{Mpc}/h]$', fontsize=25) ax.set_ylabel(r'$\xi_\mathtt{gg}/\xi_\mathtt{gg}^\mathtt{obvs}$', fontsize=25) elif 'gmf' in obvs: # GMF data_gmf = Data.data_gmf(Mr=Mr, b_normal=b_normal) cov_data = Data.data_cov(Mr=Mr, b_normal=b_normal, inference='mcmc') data_gmf_cov = cov_data[16:, 16:] ax.fill_between(r_bin, a, e, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b, d, color=pretty_colors[3], alpha=0.5, edgecolor="none", label='ABC Posterior') ax.errorbar(r_bin, data_gmf, yerr=np.sqrt(np.diag(data_gmf_cov)), fmt="o", color='k', markersize=0, lw=0, capsize=4, elinewidth=2) ax.scatter(r_bin, data_gmf, s=15, lw=0, c='k', label='Mock Observation') ax.legend(loc='upper right', scatterpoints=1, prop={'size': 25}, borderpad=1.0) ax.yaxis.tick_right() ax.yaxis.set_ticks_position('both') ax.yaxis.set_label_position('right') ax.set_ylabel(r'$\zeta$ $[(\mathrm{h}/\mathtt{Mpc})^{3}]$', fontsize=25) ax.set_yscale('log') ax.set_xlim([1., 20.]) ax.set_xticklabels([]) ax.set_ylim([10.**-7.2, 2*10**-4.]) ax = plt.subplot(gs[3]) ax.fill_between(r_bin, a/data_gmf, e/data_gmf, color=pretty_colors[3], alpha=0.3, edgecolor="none") ax.fill_between(r_bin, b/data_gmf, d/data_gmf, color=pretty_colors[3], alpha=0.5, edgecolor="none") ax.errorbar(r_bin, np.repeat(1., len(data_gmf)), yerr=np.sqrt(np.diag(data_gmf_cov))/data_gmf, fmt="o", color='k', markersize=0, lw=0, capsize=3, elinewidth=1.5) ax.plot(np.arange(1., 20., 1), np.repeat(1., len(np.arange(1., 20, 1))), c='k', ls='--', lw=1.75) ax.yaxis.tick_right() ax.yaxis.set_label_position('right') ax.set_ylim([-0.1, 2.1]) ax.set_ylabel(r'$\zeta/\zeta^\mathtt{obvs}$', fontsize=25) ax.set_xlim([1., 20.]) ax.set_xlabel(r'$\mathtt{N}$ [Group Richness]', fontsize=25) fig.subplots_adjust(wspace=0.05, hspace=0.0) fig_name = ''.join([ut.fig_dir(), 'paper', '.ABCposterior', '.pdf']) fig.savefig(fig_name, bbox_inches='tight') plt.close() return None