CosmoFit.Fitter¶
- class CosmoFit.Fitter(model, datasets, free_params, initial, fixed=None, bounds=None, dataset_kwargs=None, compute_rd=False, derive_sigma8=False)[source]¶
Bases:
objectTies together a cosmological model, one or more datasets, a prior, and (optionally) an MCMC run and best-fit search.
- Parameters:
model (type) – A
Cosmologysubclass, e.g.LCDMorCPL– or a custom model built withcosmology.custom.define_model()(or a hand-writtenCosmologysubclass declaringEXTRA_PARAMS), for testing a new model not built into CosmoFit.datasets (list[str]) – Which likelihoods to combine. Keys of
DATASET_REGISTRY(currently"cc","desi","sdss_bao","pantheon","des_sn5yr","planck","fsigma8","s8"). Do not combine"desi"/"sdss_bao"or"pantheon"/"des_sn5yr"in the same fit – see the corresponding likelihood classes’ docstrings for why.free_params (list[str]) – Names of the
CosmologyParametersfields that are allowed to vary (e.g.["H0", "Omega_m", "w0", "wa", "rd"]).initial (dict[str, float]) – Starting values for every parameter in
CosmologyParametersthat you want to override (both free and fixed ones may be given here; anything not given falls back to the dataclass default).fixed (dict[str, float], optional) – Explicit values for parameters that are not being fit. Equivalent to putting them in
initialand simply not listing them infree_params– provided separately mainly for readability.bounds (dict[str, tuple[float, float]], optional) – Overrides for the default uniform-prior bounds (
model.PARAMS_CLASS.default_bounds(), which iscosmology.core.parameters.DEFAULT_BOUNDSfor every built-in model).dataset_kwargs (dict[str, dict], optional) – Extra keyword arguments passed to the constructor of each likelihood, keyed by dataset name. E.g.
{"pantheon": {"marginalize_MB": True}}.compute_rd (bool, optional) –
If True, the BAO sound horizon
rdis computed from the physical densities (omega_b,omega_cb,N_eff,m_nu) rather than fitted as a free nuisance parameter – seecosmology.calculators.sound_horizon. Default False.This changes what a BAO fit can measure. With
rdfree, BAO constrains only the productH0 rd, soH0is unconstrained by BAO alone. Withrdcomputed,H0becomes measurable – but throughOmega_b, which BAO cannot pin down on its own. That is what the"omega_b"(BBN) dataset is for, and the pair is how every published “BAO + BBN gives H0” constraint is produced:Fitter(model=LCDM, datasets=["desi", "omega_b"], free_params=["H0", "Omega_m", "Omega_b"], compute_rd=True)
Passing
compute_rd=Truewith"rd"infree_paramsraises: the likelihood would ignore the sampled value, and its “posterior” would be its prior.
- __init__(model, datasets, free_params, initial, fixed=None, bounds=None, dataset_kwargs=None, compute_rd=False, derive_sigma8=False)[source]¶
Methods
__init__(model, datasets, free_params, initial)best_fit([x0, bounds, eps, method, ...])Maximum-likelihood point via
scipy.optimize.minimize(L-BFGS-B), starting either fromx0, from the highest-posterior MCMC sample if available, or frominitial.chain_id(**extra)A filename-safe name identifying this exact fit, e.g.
"CPL_3f9a1c04"-- for saving chains without inventing a name for each one.chi2([theta])Total chi2 at
theta(defaults to the initial point).chi2_breakdown([theta])Per-dataset chi2 at
theta(defaults to the initial point).convergence([burnin, tol])MCMC convergence diagnostics, based on the integrated autocorrelation time tau of each free parameter's chain (
emcee's own recommended diagnostic -- see https://emcee.readthedocs.io/en/stable/tutorials/autocorr/).fisher([steps, theta])Fisher matrix: the curvature of
chi2at the best fit,F_ij = (1/2) d2 chi2 / d theta_i d theta_j.flat_samples([burnin])from_chain(path[, name, model, burnin])Rebuild a fitter from a saved chain and attach that chain: the "open last week's results and keep working" entry point.
load_chain(path[, name, burnin])Attach a chain saved by an earlier
run_mcmc(save=...)to this fitter, without sampling anything.profile(name, values[, restarts, seed, ...])Profile likelihood:
chi2minimized over every other free parameter, at each fixed value ofname.run_mcmc([nwalkers, nsteps, burnin, ...])Run an
emceeensemble MCMC, viaEnsembleSampler.run_nested([n_live, dlogz, seed, progress])Integrate the posterior by nested sampling, giving the Bayesian evidence
ln Zas well as samples.samples_dict([burnin])Flat posterior samples as a dict of 1D arrays, keyed by parameter name.
summary([burnin])Posterior median +/- 68% interval for every free parameter.
Attributes
best_fit_chi2best_fit_paramsn_datandimresultA single
FitResultsnapshot of whatever this fitter currently has: the best-fit point (ifbest_fit()has been called), the MCMC posterior (ifrun_mcmc()has been called), or both.theta0chainThe
ChainFilethis fit's chain is stored in, once run_mcmc(save=...), load_chain or from_chain has pointed it at one -- so "where did this posterior come from" stays answerable from the fitter itself.