def __init__(self, fname_params): self.p = ParamRun(fname_params) self.cosmo = self.p.get_cosmo() self.cosmo_vary = COSMO_VARY(self.p) self.kwargs = self.p.get_cosmo_pars() self.hmc = get_hmcalc(**self.kwargs) self._PP = norm.sf(-1) - norm.sf(1)
class chan(object): """ Given a parameter file, looks up corresponding sampler and calculates best-fit parameters, and min & max values. Outputs a dictionary of best-fit parameters, chi-squared and dof. Args: fname_params (str): Name of parameter file. diff (bool): If True, return differences `vv-vmin`, `vmax-vv`. If False, return min and max values. pars (str): Which parameters to output. **specargs: Special keyword-arguments used in conjunction with user-defined parameters to output. Returns: params (dict): Dictionary of values. "name" : [50, -sigma, +sigma] probability-wise percentiles. chi2, dof (float): Chi-squared of best-fit and degrees of freedom. chains: The chains of the fitted parameters. """ def __init__(self, fname_params): self.p = ParamRun(fname_params) self.cosmo = self.p.get_cosmo() self.sel = self._selfunc(self.p) self.hm_correction = self._halmodcor(self.p, self.cosmo) def _halmodcor(self, p, cosmo): # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None return hm_correction def _selfunc(self, p): # Include selection function if needed sel = p.get('mcmc').get('selection_function') if sel is not None: if sel == 'erf': sel = selection_planck_erf elif sel == 'tophat': sel = selection_planck_tophat elif sel == 'none': sel = None return sel def _get_dndz(self, fname, width): """Get the modified galaxy number counts.""" zd, Nd = np.loadtxt(fname, unpack=True) Nd /= simps(Nd, x=zd) zavg = np.average(zd, weights=Nd) nzf = interp1d(zd, Nd, kind="cubic", bounds_error=False, fill_value=0) Nd_new = nzf(zavg + (1 / width) * (zd - zavg)) return zd, Nd_new def _th(self, pars): return get_theory(self.p, self.d, self.cosmo, hm_correction=self.hm_correction, selection=self.sel, **pars) def get_chains(self, pars, **specargs): """Returns a dictionary containing the chains of `pars`. """ def bias_one(p0, num): """Calculates the halo model bias for a set of parameters.""" bb = hm_bias(self.cosmo, 1 / (1 + zarr), d.tracers[num][1].profile, **lik.build_kwargs(p0)) return bb def bias_avg(num, skip): """Calculates the halo model bias of a profile, from a chain.""" #from pathos.multiprocessing import ProcessingPool as Pool #with Pool() as pool: # bb = pool.map(lambda p0: bias_one(p0, num), # sam.chain[::skip]) bb = list(map(lambda p0: bias_one(p0, num), sam.chain[::skip])) bb = np.mean(np.array(bb), axis=1) return bb # path to chain fname = lambda s: self.p.get("global")["output_dir"] + "/sampler_" + \ self.p.get("mcmc")["run_name"] + "_" + s + "_chain" if type(pars) == str: pars = [pars] import os print(os.getcwd()) preCHAINS = {} fid_pars = pars.copy() for par in pars: try: print(fname(par)) preCHAINS[par] = np.load(fname(par) + ".npy") fid_pars.remove(par) print("Found saved chains for %s." % par) except FileNotFoundError: continue if "bg" or "by" in fid_pars: # skip every (for computationally expensive hm_bias) b_skip = specargs.get("reduce_by_factor") if b_skip is None: print("'reduce_by_factor' not given. Defaulting to 100.") b_skip = 100 for s, v in enumerate(self.p.get("data_vectors")): d = DataManager(self.p, v, self.cosmo, all_data=False) self.d = d lik = Likelihood(self.p.get('params'), d.data_vector, d.covar, self._th, template=d.templates) sam = Sampler(lik.lnprob, lik.p0, lik.p_free_names, self.p.get_sampler_prefix(v['name']), self.p.get('mcmc')) sam.get_chain() chains = lik.build_kwargs(sam.chain.T) sam.update_p0(sam.chain[np.argmax(sam.probs)]) kwargs = lik.build_kwargs(sam.p0) w = kwargs["width"] zz, NN = self._get_dndz(d.tracers[0][0].dndz, w) zmean = np.average(zz, weights=NN) chains["z"] = zmean if "probs" in pars: chains["probs"] = sam.probs if ("by" or "bg") in fid_pars: sigz = np.sqrt(np.sum(NN * (zz - zmean)**2) / np.sum(NN)) zarr = np.linspace(zmean - sigz, zmean + sigz, 10) if "bg" in pars: chains["bg"] = bias_avg(num=0, skip=b_skip) if "by" in pars: chains["by"] = bias_avg(num=1, skip=b_skip) # Construct tomographic dictionary if s == 0: keys = ["z"] + fid_pars CHAINS = {k: chains[k] for k in keys} else: for k in keys: CHAINS[k] = np.vstack((CHAINS[k], chains[k])) # save bias chains to save time if not already saved if "bg" in fid_pars: np.save(fname("bg"), CHAINS["bg"]) if "by" in fid_pars: np.save(fname("by"), CHAINS["by"]) return {**preCHAINS, **CHAINS} def get_best_fit(self, pars, diff=True, chains=None, **specargs): """Returns a dictionary containing the best-fit values & errors.""" if type(pars) == str: pars = [pars] # pass chains to save time if chains is None: CHAINS = self.get_chains(pars, **specargs) else: CHAINS = chains for s, _ in enumerate(CHAINS["z"]): # loop over all bins print("Calculating best-fit for z-bin %d/%d..." % (s + 1, len(CHAINS["z"]))) chains = {k: CHAINS[k][s] for k in CHAINS.keys() if k != "z"} bf = get_summary_numbers(pars, chains, diff=diff) if s == 0: BEST_FIT = bf BEST_FIT["z"] = CHAINS["z"] else: for k in pars: BEST_FIT[k] = np.vstack((BEST_FIT[k], bf[k])) return BEST_FIT def get_overall_best_fit(self, pars, **specargs): """Returns the overall best-fit, the chi-square and the N.d.o.f.""" if type(pars) == str: pars = [pars] for s, v in enumerate(self.p.get("data_vectors")): d = DataManager(self.p, v, self.cosmo, all_data=False) self.d = d lik = Likelihood(self.p.get('params'), d.data_vector, d.covar, self._th, template=d.templates) sam = Sampler(lik.lnprob, lik.p0, lik.p_free_names, self.p.get_sampler_prefix(v['name']), self.p.get('mcmc')) sam.get_chain() sam.update_p0(sam.chain[np.argmax(sam.probs)]) kwargs = lik.build_kwargs(sam.p0) w = kwargs["width"] zz, NN = self._get_dndz(d.tracers[0][0].dndz, w) zmean = np.average(zz, weights=NN) kwargs["z"] = zmean kwargs["chi2"] = lik.chi2(sam.p0) all_pars = self.p.p.get("params") dof = np.sum( [param["vary"] for param in all_pars if "vary" in param]) kwargs["dof"] = len(lik.dv) - dof kwargs["PTE"] = 1 - chi2.cdf(kwargs["chi2"], kwargs["dof"]) if s == 0: keys = ["z", "chi2", "dof", "PTE"] + pars OV_BF = {k: kwargs[k] for k in keys} else: for k in keys: OV_BF[k] = np.vstack((OV_BF[k], kwargs[k])) return OV_BF
from model.cosmo_utils import COSMO_VARY, COSMO_ARGS from scipy.interpolate import interp1d from matplotlib import rc rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) rc('text', usetex=True) sample='wisc3' dgg_wdpj=np.load("output_default/cls_"+sample+"_"+sample+".npz") dgg_wodpj=np.load("output_default_nodpj/cls_"+sample+"_"+sample+".npz") cov_gg=np.load("output_default/cov_comb_m_"+sample+"_"+sample+ "_"+sample+"_"+sample+".npz")['cov'] ls=dgg_wdpj['ls'] fname_params = "params_wnarrow.yml" p = ParamRun(fname_params) kwargs = p.get_cosmo_pars() cosmo = p.get_cosmo() hmc = get_hmcalc(cosmo, **kwargs) cosmo_vary = COSMO_VARY(p) hm_correction = HalomodCorrection(cosmo) v=None for s,vv in enumerate(p.get("data_vectors")): if vv['name']==sample: v=vv dat = DataManager(p, v, cosmo, all_data=False) gat = DataManager(p, v, cosmo, all_data=True) def th(pars,d):
def get_bpe(z, n_r, delta, nmass=256): a = 1. / (1 + z) lmarr = np.linspace(8., 16., nmass) marr = 10.**lmarr Dm = delta / ccl.omega_x(cosmo, a, "matter") # CCL uses Delta_m mfunc = ccl.massfunc(cosmo, marr, a, Dm) bh = ccl.halo_bias(cosmo, marr, a, Dm) et = np.array( [integrated_profile(get_battaglia(m, z, delta), n_r) for m in marr]) return itg.simps(et * bh * mfunc, x=lmarr) fname_params = "params_wnarrow.yml" p = ParamRun(fname_params) cosmo = p.get_cosmo() q = chan(fname_params) red = {"reduce_by_factor": 10} CHAINS = q.get_chains("by", **red) # dictionary of chains and redshifts chains = 1e3 * CHAINS["by"] # values only bf = q.get_best_fit("by", chains=CHAINS, **red) z, by = np.hstack((bf["z"])), 1e3 * bf["by"].T # DES data DESx = np.array([0.15, 0.24, 0.2495, 0.383, 0.393, 0.526, 0.536, 0.678, 0.688]) DESy = 1e-1 * np.array([1.5, 1.51, 0.91, 2.46, 2.55, 3.85, 3.08, 2.61, 2.25]) DESsy_min = 1e-1 * np.array( [1.275, 0.940, 0.2587, 1.88, 2.092, 2.961, 2.377, 1.442, 1.284]) DESsy_max = 1e-1 * np.array(
from likelihood.chanal import chan from model.data import DataManager from model.theory import get_theory import matplotlib.pyplot as plt from model.hmcorr import HM_Gauss from model.power_spectrum import hm_bias from model.utils import get_hmcalc from model.cosmo_utils import COSMO_VARY, COSMO_ARGS try: fname_params = sys.argv[1] except IndexError: fname_params = "params_lensing_gyk.yml" # default for testing raise ValueError("Must provide param file name as command-line argument") p = ParamRun(fname_params) kwargs = p.get_cosmo_pars() run_name = p.get('mcmc')['run_name'] # Cosmology (Planck 2018) cosmo = p.get_cosmo() hmc = get_hmcalc(cosmo, **kwargs) cosmo_vary = COSMO_VARY(p) kwargs = p.get_cosmo_pars() hm_correction = HM_Gauss(cosmo, **kwargs).hm_correction # chain handler - get autocorrelation times to remove burn-in q = chan(fname_params) print("Loading chains...") chains = q.get_chains() chains.pop("z") print("Computing chain autocorrelation times...")
import os from argparse import ArgumentParser from analysis.params import ParamRun from analysis import pipeline_utils as pu parser = ArgumentParser() parser.add_argument("fname_params", help="yaml target parameter file") parser.add_argument("--jk-id", type=int) parser.add_argument("--joint-cov", action="store_true") args = parser.parse_args() fname_params = args.fname_params p = ParamRun(fname_params) os.system('mkdir -p ' + p.get_outdir()) # mkdir if needed if args.joint_cov: assert args.jk_id is None, "No joint covs after completing a single JK!" print("Computing joint covariances...") pu.get_joint_cov(p) import sys sys.exit(0) fields = pu.read_fields(p) if args.jk_id is None: print("Computing power spectra...", end="") xcorr = pu.get_xcorr(p, fields) print("OK") print("Generating theory power spectra") mcorr = pu.model_xcorr(p, fields, xcorr) print("Computing covariances...") pu.get_cov(p, fields, xcorr, mcorr)
from analysis.covariance import Covariance from analysis.jackknife import JackKnife from analysis.params import ParamRun from model.profile2D import Arnaud, HOD from model.power_spectrum import hm_ang_power_spectrum, \ HalomodCorrection from model.trispectrum import hm_ang_1h_covariance from model.utils import beam_gaussian, beam_hpix, \ selection_planck_erf, selection_planck_tophat try: fname_params = sys.argv[1] except: raise ValueError("Must provide param file name as command-line argument") p = ParamRun(fname_params) # Cosmology (Planck 2018) cosmo = p.get_cosmo() # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None # Include selection function if needed sel = p.get('mcmc').get('selection_function') if sel is not None: if sel == 'erf': sel = selection_planck_erf
import numpy as np from analysis.params import ParamRun from likelihood.like import Likelihood from likelihood.sampler import Sampler from model.data import DataManager from model.theory import get_theory import matplotlib.pyplot as plt from model.power_spectrum import HalomodCorrection, hm_bias from model.utils import selection_planck_erf, selection_planck_tophat try: fname_params = sys.argv[1] except IndexError: raise ValueError("Must provide param file name as command-line argument") p = ParamRun(fname_params) run_name = p.get('mcmc')['run_name'] # Cosmology (Planck 2018) cosmo = p.get_cosmo() # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None # Include selection function if needed sel = p.get('mcmc').get('selection_function') if sel is not None: if sel == 'erf':
from scipy.interpolate import interp1d from matplotlib import rc rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']}) rc('text', usetex=True) sample = 'wisc3' dgg_wdpj = np.load("output_default/cls_" + sample + "_" + sample + ".npz") dgg_wodpj = np.load("output_default_nodpj/cls_" + sample + "_" + sample + ".npz") cov_gg = np.load("output_default/cov_comb_m_" + sample + "_" + sample + "_" + sample + "_" + sample + ".npz")['cov'] ls = dgg_wdpj['ls'] fname_params = "params_wnarrow.yml" p = ParamRun(fname_params) cosmo = p.get_cosmo() hm_correction = HalomodCorrection(cosmo) v = None for s, vv in enumerate(p.get("data_vectors")): if vv['name'] == sample: v = vv dat = DataManager(p, v, cosmo, all_data=False) gat = DataManager(p, v, cosmo, all_data=True) def th(pars, d): return get_theory(p, d,
help='Use MPI (default: False)') parser.add_argument('--jk-region', type=int, default=-1, help='Jackknife region to use (default: no jackknives)') parser.add_argument('--data-name', type=str, default='none', help='Name of the data vector to analyze') o = parser.parse_args() if o.param_file == 'none': raise ValueError("Must provide param file name") p = ParamRun(o.param_file) # Jackknives if o.jk_region < 0: jk_region = None else: jk_region = o.jk_region def extract_map_p0(p, v, parnames): """Extract the proposal p0 from a specific map.""" for m in p.get("maps"): if m["name"] == v["name"]: break p0 = [m["model"][k] for k in parnames]
else: cosmo_fid = COSMO_ARGS(kwargs) hmc_fid = get_hmcalc(cosmo_fid, **kwargs) return get_theory(p, self.d, cosmo_fid, hmc_fid, hm_correction=hm_correction, selection=sel, include_2h=True, include_1h=False, **pars) fname_params = "params_wnarrow.yml" p = ParamRun(fname_params) kwargs = p.get_cosmo_pars() cosmo = p.get_cosmo() hmc = get_hmcalc(cosmo, **kwargs) cosmo_vary = COSMO_VARY(p) # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None # Include selection function if needed sel = p.get('mcmc').get('selection_function') if sel is not None: if sel == 'erf': sel = selection_planck_erf
class chan(object): """ Given a parameter file, looks up corresponding sampler and calculates best-fit parameters, and min & max values. Outputs a dictionary of best-fit parameters, chi-squared and dof. Args: fname_params (str): Name of parameter file. diff (bool): If True, return differences `vv-vmin`, `vmax-vv`. If False, return min and max values. pars (str): Which parameters to output. **specargs: Special keyword-arguments used in conjunction with user-defined parameters to output. Returns: params (dict): Dictionary of values. "name" : [50, -sigma, +sigma] probability-wise percentiles. chi2, dof (float): Chi-squared of best-fit and degrees of freedom. chains: The chains of the fitted parameters. """ def __init__(self, fname_params): self.p = ParamRun(fname_params) self.cosmo = self.p.get_cosmo() self.cosmo_vary = COSMO_VARY(self.p) self.kwargs = self.p.get_cosmo_pars() self.hmc = get_hmcalc(**self.kwargs) self._PP = norm.sf(-1) - norm.sf(1) def _get_dndz(self, fname, width): """Get the modified galaxy number counts.""" zd, Nd = np.loadtxt(fname, unpack=True) Nd /= simps(Nd, x=zd) zavg = np.average(zd, weights=Nd) nzf = interp1d(zd, Nd, kind="cubic", bounds_error=False, fill_value=0) Nd_new = nzf(zavg + (1 / width) * (zd - zavg)) return zd, Nd_new def _th(self, pars): if self.cosmo_vary: cosmo = COSMO_ARGS(pars) else: cosmo = self.cosmo return get_theory(self.p, self.d, cosmo, self.hmc, **pars) def get_chains(self, pars=None, **specargs): """Returns a dictionary containing the chains of `pars`. """ # if `pars` is not set, collect chains for all free parameters if pars is None: pars = [ par["name"] for par in self.p.get("params") if par.get("vary") ] def bias_one(p0, num): """Calculates the halo model bias for a set of parameters.""" if self.cosmo_vary: cosmo = COSMO_ARGS(pars) hmc = get_hmcalc( cosmo, **{ "mass_function": self.p.get_massfunc(), "halo_bias": self.p.get_halobias() }) else: cosmo = self.cosmo hmc = self.hmc bb = hm_bias(cosmo, hmc, 1 / (1 + zarr), d.tracers[num][1], **lik.build_kwargs(p0)) return bb def bias_avg(num, skip): """Calculates the halo model bias of a profile, from a chain.""" from pathos.multiprocessing import ProcessingPool as Pool with Pool() as pool: bb = pool.map(lambda p0: bias_one(p0, num), sam.chain[::skip]) # bb = list(map(lambda p0: bias_one(p0, num), sam.chain[::skip])) bb = np.mean(np.array(bb), axis=1) return bb # path to chain fname = lambda s: self.p.get("global")["output_dir"] + "/sampler_" + \ self.p.get("mcmc")["run_name"] + "_" + s + "_chain" if type(pars) == str: pars = [pars] preCHAINS = {} fid_pars = pars.copy() for par in pars: try: preCHAINS[par] = np.load(fname(par) + ".npy") fid_pars.remove(par) print("Found saved chains for %s." % par) except FileNotFoundError: continue if ("bg" in fid_pars) or ("by" in fid_pars) or ("bk" in fid_pars): # thin sample (for computationally expensive hm_bias) b_skip = specargs.get("thin") if b_skip is None: print("Chain 'thin' factor not given. Defaulting to 100.") b_skip = 100 for s, v in enumerate(self.p.get("data_vectors")): print(v["name"]) d = DataManager(self.p, v, all_data=False) self.d = d lik = Likelihood(self.p.get('params'), d.data_vector, d.covar, self._th, template=d.templates) sam = Sampler(lik.lnprob, lik.p0, lik.p_free_names, self.p.get_sampler_prefix(v['name']), self.p.get('mcmc')) sam.get_chain() chains = lik.build_kwargs(sam.chain.T) sam.update_p0(sam.chain[np.argmax(sam.probs)]) # print(sam.p0) kwargs = lik.build_kwargs(sam.p0) w = kwargs["width"] zz, NN = self._get_dndz(d.tracers[0][0].dndz, w) zmean = np.average(zz, weights=NN) chains["z"] = zmean if "probs" in pars: chains["probs"] = sam.probs if ("bg" in fid_pars) or ("by" in fid_pars) or ("bk" in fid_pars): sigz = np.sqrt(np.sum(NN * (zz - zmean)**2) / np.sum(NN)) zarr = np.linspace(zmean - sigz, zmean + sigz, 10) if "bg" in pars: chains["bg"] = bias_avg(num=0, skip=b_skip) if "by" in pars: chains["by"] = bias_avg(num=1, skip=b_skip) if "bk" in pars: chains["bk"] = bias_avg(num=2, skip=b_skip) # Construct tomographic dictionary if s == 0: keys = ["z"] + fid_pars CHAINS = {k: [chains[k]] for k in keys} else: for k in keys: CHAINS[k].append(chains[k]) # save bias chains to save time if not already saved if "bg" in fid_pars: np.save(fname("bg"), CHAINS["bg"]) if "by" in fid_pars: np.save(fname("by"), CHAINS["by"]) if "bk" in fid_pars: np.save(fname("bk"), CHAINS["bk"]) return {**preCHAINS, **CHAINS} def get_tau(self, chains): from emcee.autocorr import integrated_time nsteps = self.p.get("mcmc")["n_steps"] nwalkers = self.p.get("mcmc")["n_walkers"] pars = list(chains.keys()) npars = len(pars) nzbins = len(self.p.get("data_vectors")) taus = np.zeros((npars, nzbins)) for i, par in enumerate(pars): for j, chain in enumerate(chains[par]): # first dim should be time chain = chain.reshape((nsteps, nwalkers)) taus[i, j] = integrated_time(chain, tol=20, quiet=True) return taus def remove_burn_in(self, chain): from emcee.autocorr import integrated_time nsteps = self.p.get("mcmc")["n_steps"] nwalkers = self.p.get("mcmc")["n_walkers"] # first dim should be time chain = chain.reshape((nsteps, nwalkers)) tau = integrated_time(chain, tol=20, quiet=True) # remove burn-in elements from chain chain = chain[int(np.ceil(tau)):].flatten() return chain def vpercentile(self, chain): """Best fit and errors using manual watershed.""" from scipy.signal import savgol_filter percentile = 100 * self._PP pdf, x = np.histogram(chain, bins=100, density=True) x = (x[:-1] + x[1:]) / 2 # smooth posterior window = int(np.ceil(np.sqrt(pdf.size)) // 2 * 2 + 1) pdf = savgol_filter(pdf, window, 3) par_bf = x[np.argmax(pdf)] eps = 0.005 cut = pdf.max() * np.arange(1 - eps, 0, -eps) for cc in cut: bb = np.where(pdf - cc > 0)[0] if bb.size < 2: continue par_min, par_max = x[bb[0]], x[bb[-1]] N_enclosed = (par_min < chain) & (chain < par_max) perc = 100 * N_enclosed.sum() / chain.size if perc > percentile: break return par_bf, par_min, par_max def gauss_kde(self, chain, parname=None): """Best fit and erros using Gaussian-KDE watershed.""" def get_prob(a, b, f): xr = np.linspace(a, b, 128) return simps(f(xr), x=xr) def cutfunc(pthr, f, x_lim=None): if x_lim is None: x1, x2 = x_min, x_max else: x1, x2 = x_lim r_lo = root_scalar(limfunc, args=(pthr, f), bracket=(x1, x_bf)).root r_hi = root_scalar(limfunc, args=(pthr, f), bracket=(x_bf, x2)).root pr = get_prob(r_lo, r_hi, f) return pr - self._PP def extend_kde(f): """Extend kde boundaries in case of boundary inconsistency.""" import warnings warnings.warn( ("Posterior st.dev. hits prior bound for %s." % parname), RuntimeWarning) from scipy.interpolate import interp1d # retrieve prior boundaries for this parameter for par in self.p.get("params"): if par["name"] == parname: if par["prior"]["type"] == "TopHat": val = par["prior"]["values"] else: print("Prior type not `TopHat`!") break xx = np.linspace(val[0], val[1], 256) yy = f(xx) yy[0] = yy[-1] = 0 f_new = interp1d(xx, yy, kind="cubic", bounds_error=False, fill_value=0) import matplotlib.pyplot as plt fig, ax = plt.subplots() plt.ion() xold = np.linspace(x_min, x_max, 256) ax.plot(xx, yy, "r:", lw=2, label="new dist") ax.plot(xold, f(xold), "k-", lw=2, label="original dist") ax.legend(loc="best") ax.set_title(parname) plt.show() plt.pause(0.001) return f_new minfunc = lambda x, f: -f(x) limfunc = lambda x, thr, f: np.atleast_1d(f(x))[0] - thr x_min = np.amin(chain) x_max = np.amax(chain) F = gaussian_kde(chain) x_bf = minimize_scalar(minfunc, args=(F), bracket=[x_min, x_max]).x[0] p_bf = F(x_bf)[0] try: p_thr = root_scalar(cutfunc, args=(F), x0=p_bf / 2, x1=p_bf / 3).root except ValueError: F = extend_kde(F) p_thr = root_scalar(cutfunc, args=(F), x0=p_bf / 2, x1=p_bf / 3).root x_lo = root_scalar(limfunc, args=(p_thr, F), bracket=(x_min, x_bf)).root x_hi = root_scalar(limfunc, args=(p_thr, F), bracket=(x_bf, x_max)).root return x_bf, x_lo, x_hi def get_summary_numbers(self, pars, chains, diff=True): """Builds a best-fit dictionary, given a chain dictionary.""" def diff_func(Q): # (-/+) instead of (vmin, vmax) Q[1] = Q[0] - Q[1] Q[2] = Q[2] - Q[0] return Q try: Q = np.vstack( [self.gauss_kde(chains[par], parname=par) for par in pars]).T except ValueError as err: print(err, "\nApproximating chain elements as delta-functions.") Q = np.vstack([self.vpercentile(chains[par]) for par in pars]).T # force data point to error boundary if outside Q[1] = np.min([Q[0], Q[1]], axis=0) Q[2] = np.max([Q[0], Q[2]], axis=0) Q = Q if not diff else diff_func(Q) Q = {par: Qi for par, Qi in zip(pars, Q.T)} return Q def get_best_fit(self, pars, diff=True, chains=None, **specargs): """Returns a dictionary containing the best-fit values & errors.""" if type(pars) == str: pars = [pars] # pass chains to save time if chains is None: CHAINS = self.get_chains(pars, **specargs) else: CHAINS = chains for s, _ in enumerate(CHAINS["z"]): # loop over all bins print("Calculating best-fit for z-bin %d/%d..." % (s + 1, len(CHAINS["z"]))) # remove burn-in elements from chains while assmebling them chains = { k: self.remove_burn_in(CHAINS[k][s]) for k in CHAINS.keys() if k != "z" } bf = self.get_summary_numbers(pars, chains, diff=diff) if s == 0: BEST_FIT = bf BEST_FIT["z"] = CHAINS["z"] else: for k in pars: BEST_FIT[k] = np.vstack((BEST_FIT[k], bf[k])) return BEST_FIT def get_overall_best_fit(self, pars, **specargs): """Returns the overall best-fit, the chi-square and the N.d.o.f.""" if type(pars) == str: pars = [pars] for s, v in enumerate(self.p.get("data_vectors")): d = DataManager(self.p, v, all_data=False) self.d = d lik = Likelihood(self.p.get('params'), d.data_vector, d.covar, self._th, template=d.templates) sam = Sampler(lik.lnprob, lik.p0, lik.p_free_names, self.p.get_sampler_prefix(v['name']), self.p.get('mcmc')) sam.get_chain() sam.update_p0(sam.chain[np.argmax(sam.probs)]) kwargs = lik.build_kwargs(sam.p0) w = kwargs["width"] zz, NN = self._get_dndz(d.tracers[0][0].dndz, w) zmean = np.average(zz, weights=NN) kwargs["z"] = zmean kwargs["chi2"] = lik.chi2(sam.p0) all_pars = self.p.p.get("params") dof = np.sum( [param["vary"] for param in all_pars if "vary" in param]) kwargs["dof"] = len(lik.dv) - dof kwargs["PTE"] = 1 - chi2.cdf(kwargs["chi2"], kwargs["dof"]) if s == 0: keys = ["z", "chi2", "dof", "PTE"] + pars OV_BF = {k: kwargs[k] for k in keys} else: for k in keys: OV_BF[k] = np.vstack((OV_BF[k], kwargs[k])) return OV_BF
from model.data import DataManager from model.theory import get_theory from model.power_spectrum import HalomodCorrection, hm_bias from model.utils import selection_planck_erf, selection_planck_tophat try: fname_params = sys.argv[1] except IndexError: raise ValueError("Must provide param file name as command-line argument") try: jk_region = int(sys.argv[2]) except IndexError: raise ValueError("Must provide jackknife region") p = ParamRun(fname_params) # Cosmology (Planck 2018) cosmo = p.get_cosmo() # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None # Include selection function if needed sel = p.get('mcmc').get('selection_function') if sel is not None: if sel == 'erf': sel = selection_planck_erf
def __init__(self, fname_params): self.p = ParamRun(fname_params) self.cosmo = self.p.get_cosmo() self.sel = self._selfunc(self.p) self.hm_correction = self._halmodcor(self.p, self.cosmo)
from model.hmcorr import HM_halofit import pyccl as ccl parser = ArgumentParser() parser.add_argument("fname_params", help="yaml target parameter file") parser.add_argument("--data-vector", "-dv", help="target data vector") # attention: `args.no_mpi == True` by default, which may be misleading # when sampling and calling `use_mpi=args.no_mpi` parser.add_argument("--no-mpi", help="specify for no MPI", action="store_false") parser.add_argument("--jk-id", type=int, help="JK region") args = parser.parse_args() fname_params = args.fname_params p = ParamRun(fname_params) jk_region = args.jk_id # JK id hmc = p.get_hmc() # halo model calculator hm_correction = p.get_hm_correction() # halo model correction v = p.get_data_vector(args.data_vector) # data vector print(v['name']) # Construct data vector and covariance d = DataManager(p, v, jk_region=jk_region) # Theory predictor wrapper def th(kwargs): """Theory for free cosmology.""" cosmo_use = p.get_cosmo(pars=kwargs) # optimized internally
# "params_wnarrow_ns.yml", # "params_masked.yml" ] sci = [r"$\mathrm{2MPZ}$"] + \ [r"$\mathrm{WI \times SC}$ - $\mathrm{%d}$" % i for i in range(1, 6)] lbls = [ "Fiducial", "NILC", "Fixed $w_z$", "Tinker 2010", r"$\langle N_s \rangle$ independent" "tSZ-masked" ] colours = ["k", "grey", "r", "brown", "orange"] col = [copper(i) for i in np.linspace(0, 1, len(sci))] fmts = ["o", "o", "v", "s", "*", "d"] p = ParamRun(param_yml[0]) #temp = [chan(paryml, diff=True, error_type="hpercentile", chains=False, b_hydro=0.5*np.ones([1,6])) # for paryml in param_yml] #pars = [t[0] for t in temp] #data = np.array([[p["b_hydro"] for p in par] for par in pars]) #data = [d.T for d in data] print("HI") BF = [chan(fname).get_best_fit("b_hydro") for fname in param_yml] print("Best fits OK") widths = chan(param_yml[0]).get_best_fit("width") widths = np.hstack((widths["width"][:, 0])) dz, dN = [[] for i in range(2)] i = 0 for g in p.get("maps"): if g["type"] == "g":
from model.power_spectrum import HalomodCorrection, hm_bias import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerBase from matplotlib.cm import copper from matplotlib import rc rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']}) rc('text', usetex=True) try: fname_params = sys.argv[1] bin_name = sys.argv[2] except IndexError: fname_params = "params_dam_wnarrow.yml" bin_name = "wisc3" p = ParamRun("params_dam_wnarrow.yml") run_name = p.get('mcmc')['run_name'] cosmo = p.get_cosmo() # Include halo model correction if needed if p.get('mcmc').get('hm_correct'): hm_correction = HalomodCorrection(cosmo) else: hm_correction = None for v in p.get('data_vectors'): if v['name'] == bin_name: d = DataManager(p, v, cosmo) z, nz = np.loadtxt(d.tracers[0][0].dndz, unpack=True) zmean = np.average(z, weights=nz) sigz = np.sqrt(np.sum(nz * (z - zmean)**2) / np.sum(nz)) # Theory predictor wrapper