Samplers

Synopsis:Base class for samplers and other parameter-space explorers
Author:Jesus Torrado

cobaya includes by default a Monte Carlo Markov Chain (MCMC) sampler (a direct translation from CosmoMC) and a dummy evaluate sampler that simply evaluates the posterior at a given (or sampled) reference point. It also includes an interface to the PolyChord sampler (needs to be installed separately).

The sampler to use is specified by a sampler block in the input file, whose only member is the sampler used, containing some options, if necessary.

sampler:
  mcmc:
    max_samples: 1000

or

sampler:
  polychord:
    path: /path/to/cosmo/PolyChord

Samplers can in general be swapped in the input file without needing to modify any other block of the input.

In the cobaya code tree, each sampler is placed in its own folder, containing a file defining the sampler’s class, which inherits from the cobaya.Sampler, and a [sampler_name].yaml file, containing all possible user-specified options for the sampler and their default values. Whatever option is defined in this file automatically becomes an attribute of the sampler’s instance.

To implement your own sampler, or an interface to an external one, simply create a folder under the cobaya/cobaya/samplers/ folder and include the two files described above. Your class needs to inherit from the cobaya.Sampler class below, and needs to implement only the methods initialize, _run, and products.

Sampler class

class sampler.Sampler(info_sampler: Dict[str, Any], model: cobaya.model.Model, output=typing.Union[cobaya.output.Output, NoneType], packages_path: Optional[str] = None, name: Optional[str] = None)

Base class for samplers.

initialize()

Initializes the sampler: prepares the samples’ collection, prepares the output, deals with MPI scheduling, imports an external sampler, etc.

Options defined in the defaults.yaml file in the sampler’s folder are automatically recognized as attributes, with the value given in the input file, if redefined there.

The prior and likelihood are also accessible through the attributes with the same names.

run()

Runs the main part of the algorithm of the sampler. Normally, it looks somewhat like

while not [convergence criterion]:
    [do one more step]
    [update the collection of samples]
products() → Dict[str, Any]

Returns the products expected in a scripted call of cobaya, (e.g. a collection of samples or a list of them).

info()

Returns a copy of the information used to initialise the sampler, including defaults and some new values that are only available after initialisation.

classmethod output_files_regexps(output, info=None, minimal=False)

Returns a list of tuples (regexp, root) of output files potentially produced. If root in the tuple is None, output.folder is used.

If minimal=True, returns regexp’s for the files that should really not be there when we are not resuming.

classmethod check_force_resume(output, info=None)

Performs the necessary checks on existing files if resuming or forcing (including deleting some output files when forcing).

Other functions in the module

sampler.get_sampler(info_sampler: Dict[str, Optional[Dict[str, Any]]], model: cobaya.model.Model, output: Optional[cobaya.output.Output] = None, packages_path: Optional[str] = None) → sampler.Sampler