mc#

This module provides some helper methods to interface between GPry and MC samplers (Cobaya and the NS’ used for NORA).

Under normal circumstances you shouldn’t have to use any of the methods in here if you use the Runner class to run GPry.

gpry.mc.get_cobaya_log_level(verbose)[source]#

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

gpry.mc.cobaya_generate_surr_model_input(surrogate, bounds=None, params=None)[source]#

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

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

Parameters:
  • surrogate (SurrogateModel, 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 the surrogate. 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

gpry.mc.mcmc_info_from_run(model, surrogate, 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.

  • surrogate (SurrogateModel, 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

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

gpry.mc.mc_sample_from_gp_cobaya(surrogate, 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:
  • surrogate (SurrogateModel, 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 (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.

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

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

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.

gpry.mc.mc_sample_from_gp_ns(surrogate, 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:
  • surrogate (SurrogateModel, 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, logZ, logZstd) – and weights (None if equal weights), and log-evidence and its standard deviation.

Return type:

arrays of samples parameters, surrogate posteriors

gpry.mc.process_gdsamples(gdsamples_dict)[source]#

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

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