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
runfunction.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
priorandlikelihoodblocks.- Return type:
- 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
modelreference 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
runfunction.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
samplerblock for Cobaya’s run function.- Return type:
- mc.polychord_info_from_run()[source]#
Creates a PolyChord sampler with standard parameters.
- Returns:
sampler – a dict with the
samplerblock for Cobaya’s run function.- Return type:
- 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
runfunction. 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 therunfunction).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).
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)
acquisition (GPAcquisition, optional) – The acquisition engine instance that has been used to fit the GP. This is used to extract the covariance matrix if it is available from the GPAcquisition class.
convergence (Convergence_criterion, optional) – The convergence criterion that has been used to fit the GP. This is used to extract the covariance matrix if it is available from the ConvergenceCriterion class.
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.
- mc.mc_sample_from_gp_ns(gpr, bounds=None, params=None, sampler='mcmc', 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
runfunction. 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 therunfunction).bounds (List of boundaries (lower,upper), optional) – By default it reads them from the GP regressor.
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.
- Return type:
(X_MC, y_MC, w_MC)
- 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_dictwith keysw,X,logpost,logprior(optional),loglike(optional).paramsshould be a list of strings, or of tuples(name, latex_label)where thelatex_labelshould not include the$delimiters.boundsshould be a list of boundaries for the parameters.)sampler_typeshould benestedormcmc.