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: object

Ties together a cosmological model, one or more datasets, a prior, and (optionally) an MCMC run and best-fit search.

Parameters:
  • model (type) – A Cosmology subclass, e.g. LCDM or CPL – or a custom model built with cosmology.custom.define_model() (or a hand-written Cosmology subclass declaring EXTRA_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 CosmologyParameters fields that are allowed to vary (e.g. ["H0", "Omega_m", "w0", "wa", "rd"]).

  • initial (dict[str, float]) – Starting values for every parameter in CosmologyParameters that 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 initial and simply not listing them in free_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 is cosmology.core.parameters.DEFAULT_BOUNDS for 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 rd is computed from the physical densities (omega_b, omega_cb, N_eff, m_nu) rather than fitted as a free nuisance parameter – see cosmology.calculators.sound_horizon. Default False.

    This changes what a BAO fit can measure. With rd free, BAO constrains only the product H0 rd, so H0 is unconstrained by BAO alone. With rd computed, H0 becomes measurable – but through Omega_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=True with "rd" in free_params raises: 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 from x0, from the highest-posterior MCMC sample if available, or from initial.

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 chi2 at 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: chi2 minimized over every other free parameter, at each fixed value of name.

run_mcmc([nwalkers, nsteps, burnin, ...])

Run an emcee ensemble MCMC, via EnsembleSampler.

run_nested([n_live, dlogz, seed, progress])

Integrate the posterior by nested sampling, giving the Bayesian evidence ln Z as 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_chi2

best_fit_params

n_data

ndim

result

A single FitResult snapshot of whatever this fitter currently has: the best-fit point (if best_fit() has been called), the MCMC posterior (if run_mcmc() has been called), or both.

theta0

chain

The ChainFile this 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.