minimize sampler


Posterior/likelihood maximization (i.e. -log(post) and chi^2 minimization).


Jesus Torrado

This is a maximizer for posteriors or likelihoods, based on scipy.optimize.Minimize,

The default is BOBYQA, which tends to work better than scipy on Cosmological problems with default settings.

It works more effectively when run on top of a Monte Carlo sample: it will use the maximum a posteriori as a starting point (or the best fit, depending on whether the prior is ignored, see below), and the recovered covariance matrix of the posterior to rescale the variables.

To take advantage of a previous run with a Monte Carlo sampler, either:

  • change the sampler to minimize in the input file,

  • or, if running from the shell, repeat the cobaya-run command used for the original run, adding the --minimize flag.

When called from a Python script, Cobaya’s run function returns the updated info and the products described below in the method samplers.minimize.Minimize.products() (see below).

If text output is requested, it produces two different files:


For historical reasons, in the first two lines of the GetDist-formatted output file -log(Like) indicates the negative log-posterior, and similarly chi-sq is \(-2\) times the log-posterior. The actual log-likelihood can be obtained as \(-2\) times the sum of the individual \(\chi^2\) (chi2__, with double underscore) in the table that follows these first lines.

It is recommended to run a couple of parallel MPI processes: it will finally pick the best among the results.


Since Cobaya is often used on likelihoods featuring numerical noise (e.g. Cosmology), we have reduced the default accuracy criterion for the minimizers, so that they converge in a limited amount of time. If your posterior is fast to evaluate, you may want to refine the convergence parameters (see override options in the yaml below).

Maximizing the likelihood instead of the posterior

To maximize the likelihood, add ignore_prior: True in the minimize input block.

When producing text output, the generated files are named .bestfit[.txt] instead of minimum, and contain the best-fit (maximum of the likelihood) instead of the MAP (maximum of the posterior).

Options and defaults

Simply copy this block in your input yaml file and modify whatever options you want (you can delete the rest).

# Default arguments for the -logposterior/chi^2 minimizer

# Method: bobyqa|scipy
method: bobyqa
# Minimizes the full posterior (False) or just the likelihood (True)
# Likelihood maximization is subject to prior bounds!
ignore_prior: False
# Maximum number of iterations (default: practically infinite)
max_evals: 1e6d
# Number of different starting positions to try minimizing from (may be rounded up if MPI)
best_of: 2
# Treatment of unbounded parameters: confidence level to use
# (Use with care if there are likelihood modes close to the edge of the prior)
confidence_for_unbounded: 0.9999995  # 5 sigmas of the prior
# Seeding runs
seed:  # an initial seed (entropy) for the numpy random generator
# Override keyword arguments for `scipy.optimize.minimize()` or `pybobyqa.solve()` or `iminuit.minimize()`
# scipy:
#  -
#  - options for individual methods
# option: value
# bobyqa:
#  -
#  -
  # option: value
  # Relaxed convergence criterion for numerically-noisy likelihoods
  rhoend: 0.05
# iminuit:
#  -
#  - options for individual methods
# option: value
# File (including path) or matrix defining a covariance matrix for the proposal:
# - null (default): will be generated from params info (prior and proposal)
# - matrix: remember to set `covmat_params` to the parameters in the matrix
# - "auto" (cosmology runs only): will be looked up in a library
# In any case, if an old chain its present, its covmat will be loaded instead.

Minimize class

class samplers.minimize.Minimize(info_sampler, model, output=typing.Optional[cobaya.output.Output], packages_path=None, name=None)

Initializes the minimizer: sets the boundaries of the problem, selects starting points and sets up the affine transformation.


Transforms a point into the search space.


Transforms a point from the search space back into the parameter space.


Runs minimization functions

process_results(results, successes, affine_transform_baselines, transform_matrices)

Determines success (or not), chooses best (if MPI or multiple starts) and produces output (if requested).


Returns a dictionary containing:

  • minimum: OnePoint that maximizes the posterior or likelihood (depending on ignore_prior).

  • result_object: instance of results class of scipy or pyBOBYQA.

  • full_set_of_mins: dictionary of minima obtained from multiple initial points. For each it stores the value of the minimized function and a boolean indicating whether the minimization was successful or not. None if only one initial point was run.

  • M: inverse of the affine transform matrix (see below). None if no transformation applied.

  • X0: offset of the affine transform matrix (see below) None if no transformation applied.

If non-trivial M and X0 are returned, this means that the minimizer has been working on an affine-transformed parameter space \(x^\prime\), from which the real space points can be obtained as \(x = M x^\prime + X_0\). This inverse transformation needs to be applied to the coordinates appearing inside the result_object.

getdist_point_text(params, weight=None, minuslogpost=None)

Creates the multi-line string containing the minimum in GetDist format.


Writes the GetDist format point.

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).