GPry Interface

This module provides the bridge interface connecting Discovery models to GPry via the Cobaya framework. GPry uses Gaussian Process emulation with active learning to efficiently explore the parameter space.

Hint

GPry is particularly useful for expensive likelihoods where each evaluation is costly. The GP surrogate model learns the likelihood surface with fewer evaluations than traditional samplers.

Discovery ↔︎ GPry bridge (via Cobaya)

This version is tailored to GPry and Cobaya’s model wrapper. It builds a Cobaya model (with your likelihood + priors) and passes that model directly to gpry.Runner.

Key references: - GPry Runner accepts a Cobaya model as its first argument; in that case it

reads priors and names from the model (no bounds needed).

  • In Cobaya, an external likelihood can be a plain Python function placed under the likelihood block, and priors are defined in the params block using scipy.stats names (e.g. uniform, norm, loguniform) or min/max.

What you get

  • build_cobaya_info(…) → a ready-to-use Cobaya info dict (likelihood + params)

  • get_cobaya_model(info) → a cobaya.model.Model

  • DiscoveryGPryCobayaBridge → convenience class that: 1) adapts your Discovery-style likelihood 2) builds the Cobaya model with proper priors 3) launches gpry.Runner(model, …) and exposes results

Assumptions

  • Your Discovery model is either a callable loglike(params_dict) -> float or an object with .log_prob(params_dict) / .log_likelihood(params_dict).

  • You provide priors in a compact mapping per parameter. Supported entries here map to Cobaya priors:

    • (“uniform”, a, b) → prior: {min: a, max: b}

    • (“loguniform”, a, b) → prior: {dist: loguniform, a: a, b: b}

    • (“normal”, mean, sigma[, …]) → prior: {dist: norm, loc: mean, scale: sigma}

    • (“fixed”, value) → value: value (non-sampled)

    (These match Cobaya’s documented syntax and SciPy parameterization. citeturn6search0)

If you need custom/callable priors, you can add them under Cobaya’s top-level prior: block—see the TODO note in build_cobaya_info.

Notes on parameter naming

Cobaya requires parameter names to be valid Python identifiers (no special chars like +, -, etc.). This module automatically sanitizes parameter names by replacing invalid characters with underscores, and maintains a mapping to convert back to the original names when calling the Discovery likelihood.

discoverysamplers.gpry_interface.build_cobaya_info(*, discovery_model, priors, latex_labels=None, like_name='discovery_like')[source]

Create a Cobaya info dict with one external likelihood and a params block.

Parameters:
  • discovery_model (Any) – Discovery model (callable or object with logL method)

  • priors (dict) – Prior specifications keyed by original parameter names

  • latex_labels (dict, optional) – LaTeX labels keyed by original parameter names

  • like_name (str) – Name for the likelihood in the Cobaya info

Return type:

Tuple[Dict[str, Any], Dict[str, str], Dict[str, str], Dict[str, float]]

Returns:

  • info (dict) – Cobaya info dict ready for get_model()

  • orig_to_sanitized (dict) – Mapping from original parameter names to sanitized names

  • sanitized_to_orig (dict) – Mapping from sanitized names back to original names

  • fixed_params (dict) – Fixed parameter values (original names)

Notes

  • Parameter names are automatically sanitized to be valid Python identifiers

  • Fixed parameters are baked into the likelihood function and NOT included in the Cobaya params block (to avoid “unused parameter” errors)

  • If you need external/callable global priors (beyond 1D priors in params), add them to info[‘prior’] after this returns (see Cobaya docs). citeturn5view0

discoverysamplers.gpry_interface.get_cobaya_model(info)[source]
class discoverysamplers.gpry_interface.DiscoveryGPryCobayaBridge(discovery_model, priors, *, latex_labels=None, like_name='discovery_like', runner_kwargs=None)[source]

Bases: object

Bridge that: - builds a Cobaya model from a Discovery-style likelihood + priors - runs GPry by passing the Cobaya model to gpry.Runner

Parameter names are automatically sanitized to be valid Python identifiers (required by Cobaya). The original names are stored and can be used for accessing results.

orig_param_names

Original parameter names from the priors dict

Type:

list

sanitized_param_names

Sanitized parameter names used internally by Cobaya

Type:

list

orig_to_sanitized

Mapping from original to sanitized names

Type:

dict

sanitized_to_orig

Mapping from sanitized to original names

Type:

dict

__init__(discovery_model, priors, *, latex_labels=None, like_name='discovery_like', runner_kwargs=None)[source]
run_sampler(*, checkpoint=None, **run_kwargs)[source]

Create and run gpry.Runner with the Cobaya model.

Per GPry docs, when passing a Cobaya model as first argument, GPry uses the model’s prior and parameter names automatically. citeturn2view0

Returns:

  • info (dict) – The Cobaya info dict used to create the model

  • sampler (gpry.Runner) – The GPry Runner object after running

posterior_samples()[source]
Return type:

Optional[ndarray]

return_logZ(*, results=None)[source]

Return the log evidence estimate.

Note: GPry uses Gaussian Process surrogate modeling and does not directly compute the Bayesian evidence in the same way nested samplers do. This method is provided for API consistency but raises NotImplementedError.

Raises:

NotImplementedError – Always raised - GPry does not compute evidence in the standard nested sampling sense

Return type:

Dict[str, float]

return_sampled_samples(*, results=None)[source]

Return the sampled parameter chains.

Returns:

Dictionary containing: - ‘names’: list of original parameter names - ‘labels’: list of parameter labels - ‘chain’: ndarray of shape (nsamples, n_sampled_params)

Return type:

dict

Raises:

RuntimeError – If no results are available

return_all_samples(*, results=None)[source]

Return all samples including fixed parameters.

Returns:

Dictionary containing: - ‘names’: list of all parameter names - ‘labels’: list of parameter labels - ‘chain’: ndarray of shape (nsamples, n_all_params)

Return type:

dict

Raises:

RuntimeError – If no results are available

plot_trace(*, burn=0, plot_fixed=False, **kwargs)[source]

Plot trace of samples vs sample index.

Parameters:
  • burn (int, optional) – Number of initial samples to discard, by default 0.

  • plot_fixed (bool, optional) – If True, includes fixed parameters in the plot, by default False.

  • **kwargs – Additional keyword arguments passed to plots.plot_trace().

Returns:

Figure containing the trace plots.

Return type:

matplotlib.figure.Figure

plot_corner(*, burn=0, **kwargs)[source]

Corner plot of sampled parameters.

Parameters:
  • burn (int, optional) – Number of initial samples to discard, by default 0.

  • **kwargs – Additional keyword arguments passed to corner.corner().

Returns:

Corner plot figure.

Return type:

matplotlib.figure.Figure

See Also