MC-tools#

This module provides some helper methods to interface between GPry and Cobaya’s inbuilt MC samplers.

Functions to interface Cobaya and GetDist for generation and processing of MC samples.

mc.get_cobaya_log_level(verbose)[source]#

Given GPry’s verbosity level, returns the corresponding Cobaya debug level.

mc.cobaya_generate_gp_model_input(gpr, bounds=None, params=None)[source]#

Returns a Cobaya model input dict corresponding to the GP surrogate model gpr.

If no other argument is passed, it samples within the bounds of the GP model, and uses generic parameter names.

Parameters:
  • gpr (GaussianProcessRegressor, which has been fit to data and returned from) – the run function.

  • bounds (List of boundaries (lower,upper), optional) – If none are provided it tries to extract the bounds from true_model. If that fails it tries extracting them from gpr. If none of those methods succeed an error is raised.

  • paramnames (List of parameter strings, optional) – If none are provided it tries to extract parameter names from true_model. If that fails it uses some dummy strings.

Returns:

info – A dict containing the prior and likelihood blocks.

Return type:

dict

mc.mcmc_info_from_run(model, gpr, cov=None, cov_params=None, verbose=3)[source]#

Creates appropriate MCMC sampler inputs from the results of a run.

Changes model reference point to the best training sample (or the rank-th best if running in parallel).

Parameters:
  • model (Cobaya model object) – Contains all information about the parameters in the likelihood and their priors as well as the likelihood itself.

  • gpr (GaussianProcessRegressor, which has been fit to data and returned from) – the run function.

  • cov (Covariance matrix, optional) – A covariance matrix to speed up convergence of the MCMC. If none is provided the MCMC will run without but it will be slower at converging.

  • cov_params (List of strings, optional) – List of parameters corresponding to the rows and columns of the covariance matrix passed via cov.

  • verbose (int (default: 3)) – Verbosity of the MC sampler.

Returns:

sampler – a dict with the sampler block for Cobaya’s run function.

Return type:

dict

mc.polychord_info_from_run()[source]#

Creates a PolyChord sampler with standard parameters.

Returns:

sampler – a dict with the sampler block for Cobaya’s run function.

Return type:

dict

mc.mc_sample_from_gp_cobaya(gpr, bounds=None, params=None, sampler='mcmc', sampler_options=None, covmat=None, covmat_params=None, output=None, run=True, resume=False, verbose=3)[source]#

Generates a Cobaya Sampler and runs it on the surrogate model.

Parameters:
  • gpr (GaussianProcessRegressor, which has been fit to data and returned from the) – run function. Alternatively a string containing a path with the location of a saved GP run (checkpoint) can be provided (the same path that was used to save the checkpoint in the run function).

  • bounds (List of boundaries (lower,upper), optional) – By default it reads them from the GP regressor.

  • params (List of parameter strings, optional) – By default it uses some dummy strings.

  • true_model (Cobaya Model, optional) – If passed, it uses it to get bounds and parameter names (unless overriden by the corresponding kwargs).

  • sampler (string (default “mcmc”). or dict) – Cobaya sampler to be used.

  • sampler_options (dict, optional) – Dictionary of options to be passed to the sampler (see Cobaya documentation for the interface of that sampler).

  • covmat (array, optional) – Approximate covariance matrix of the posterior to be used e.g. for the proposal distribution of an MCMC run.

  • covmat_params (list of str, optional) – List of parameter names for the rows and columns of the passed covmat, if different from params, or in different order.

  • output (path, optional) – The path where the resulting Monte Carlo sample shall be stored.

  • run (bool, default: True) – Whether to run the sampler. If False, returns just an initialised sampler.

  • verbose (int (default 3)) – Verbosity level, similarly valued to that of the Runner, e.g. 3 indicates cobaya’s ‘info’ level, 4 the ‘debug’ level, and lower-than-three values print only warnings and errors.

  • resume (bool, optional (default=False)) – Whether to resume from existing output files (True) or force overwrite (False)

Returns:

  • surr_info (dict) – The dictionary that was used to run (or initialized) the sampler, corresponding to the surrogate model, and populated with the sampler input specification.

  • sampler (Sampler instance) – The sampler instance that has been run (or just initialised). The sampler products can be retrieved with the Sampler.products() method.

mc.mc_sample_from_gp_ns(gpr, bounds=None, params=None, sampler=None, sampler_options=None, output=None, run=True, verbose=3)[source]#

Generates an MC sample of the surrogate model using one of the NS interfaces.

Parameters:
  • gpr (GaussianProcessRegressor, which has been fit to data and returned from the) – run function. Alternatively a string containing a path with the location of a saved GP run (checkpoint) can be provided (the same path that was used to save the checkpoint in the run function).

  • bounds (List of boundaries (lower,upper), optional) – By default it reads them from the GP regressor.

  • params (List of parameter strings, optional) – By default it uses some dummy strings.

  • sampler (string, optional) – Nested sampler to be used. If undefined, uses PolyChord if available, otherwise UltraNest.

  • sampler_options (dict, optional) – Dictionary of options to be passed to the nested sampler.

  • output (path, optional) – The path where the resulting Monte Carlo sample shall be stored.

  • run (bool, default: True) – Whether to run the sampler. If False, returns just an initialised sampler.

  • verbose (int (default 3)) – Verbosity level, similarly valued to that of the Runner, e.g. 3 indicates normal output, and 4 ‘debug’ level output; lower-than-three values print only warnings and errors.

Returns:

(X_MC, y_MC, w_MC) – (None if equal weights).

Return type:

arrays of samples parameters, surrogate posteriors and weights

mc.process_gdsamples(gdsamples_dict)[source]#

Returns a dict with values as getdist.MCSamples, transforming/loading the original dict values as appropriate.

mc.samples_dict_to_getdist(samples_dict, params=None, bounds=None, sampler_type=None)[source]#

Expects samples_dict with keys w, X, logpost, logprior (optional), loglike (optional).

params should be a list of strings, or of tuples (name, latex_label) where the latex_label should not include the $ delimiters.

bounds should be a list of boundaries for the parameters.)

sampler_type should be nested or mcmc.