Samplers

Synopsis:Prototype sampler class and sampler loader
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, close, and products.

Sampler class

class sampler.Sampler(info_sampler, model, output, resume=False, modules=None)

Prototype of the sampler class.

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 accesible 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]
close(exception_type, exception_value, traceback)

Finalizes the sampler, if something needs to be done (e.g. generating additional output).

products()

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

Other functions in the module

sampler.get_sampler(info_sampler, posterior, output_file, resume=False, modules=None)

Auxiliary function to retrieve and initialize the requested sampler.