minimize
sampler¶
Synopsis:  Posterior/likelihood maximization (i.e. log(post) and chi^2 minimization). 

Author:  Jesus Torrado 
This is a maximizer for posteriors or likelihoods, based on scipy.optimize.Minimize and PyBOBYQA (added in 2.0).
Note
BOBYQA tends to work better on Cosmological problems with the default settings.
Note
If you use BOBYQA, please cite it as:
C. Cartis, J. Fiala, B. Marteau, L. Roberts,
“Improving the Flexibility and Robustness of ModelBased DerivativeFree Optimization Solvers”
(arXiv:1804.00154)
C. Cartis, L. Roberts, O. SheridanMethven,
“Escaping local minima with derivativefree methods: a numerical investigation”
(arXiv:1812.11343)
M.J.D. Powell,
“The BOBYQA Algorithm for Bound Constrained Optimization without Derivatives”,
(Technical Report 2009/NA06, DAMTP, University of Cambridge)
If you use scipy, you can find the appropriate references here.
It works more effectively when run on top of a Monte Carlo sample: just change the sampler
for Minimize
with the desired options, and it will use as a starting point the
maximum a posteriori (MAP) or best fit (maximum likelihood, or minimal \(\chi^2\))
found so far, as well as the covariance matrix of the sample for rescaling of the
parameter jumps.
As text output, it produces two different files:
[output prefix].minimum.txt
, in the same format as Cobaya samples, but containing a single line.[output prefix].minimum
, the equivalent GetDistformatted file.
If ignore_prior: True
, those files are named .bestfit[.txt]
instead of minimum
,
and contain the bestfit (maximum of the likelihood) instead of the MAP
(maximum of the posterior).
Warning
For historical reasons, in the first two lines of the GetDistformatted output file
log(Like)
indicates the negative logposterior, and similarly chisq
is
\(2\) times the logposterior. The actual loglikelihood 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.
When called from a Python script, Cobaya’s run
function returns the updated info
and the products described below in the method
products
.
It is recommended to run a couple of parallel MPI processes: it will finally pick the best among the results.
Warning
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).
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: bobyqascipy
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()`
# scipy:
#  https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
#  options for individual methods
override_scipy:
# option: value
# bobyqa:
#  https://numericalalgorithmsgroup.github.io/pybobyqa/build/html/userguide.html
#  https://numericalalgorithmsgroup.github.io/pybobyqa/build/html/advanced.html
override_bobyqa:
# option: value
# Relaxed convergence criterion for numericallynoisy likelihoods
rhoend: 0.05
# 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.
covmat:
covmat_params:
Minimize class¶

class
samplers.minimize.
Minimize
(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)¶ 
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 scipy.Minimize

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

products
()¶ Returns a dictionary containing:
minimum
:OnePoint
that maximizes the posterior or likelihood (depending onignore_prior
).result_object
: instance of results class of scipy or pyBOBYQA.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 nontrivial
M
andX0
are returned, this means that the minimizer has been working on an affinetransformed 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 theresult_object
.

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