Source-linked AI summary

Cobaya: Code for Bayesian Analysis of hierarchical physical models

Jesus Torrado, Antony Lewis

arXiv:2005.05290v2astro-ph.IMastro-ph.CO

TL;DR

Bayesian sampling becomes difficult when hierarchical models contain costly, interdependent calculations with different parameter dependencies. Cobaya automatically analyzes and exploits those dependencies through parameter blocking, optimal ordering, and caching, while supporting modular analysis and parallel computation. The framework reports sub-millisecond posterior-evaluation overhead and convergence comparable to CosmoMC in the cited cosmology test.

  • Problem

    Hierarchical Bayesian posteriors can contain costly intermediate calculations and parameter subsets with different computational costs, which standard sampling applications often fail to exploit.

  • Method

    Cobaya automatically analyzes pipeline dependencies, groups and optimally sorts parameters by computational costs, caches intermediate results, and supports modular samplers and parallel execution.

  • Results

    Cobaya’s posterior-evaluation overhead is ≲0.2 ms for d < 100, and it reaches R −1 ≲0.02 in 8 hours on four cores per chain and four chains, consistently with CosmoMC.

  • Takeaways & Limitations

    Cobaya provides a general modular framework for efficiently exploring complex Bayesian pipelines without requiring user-specific code for dependency handling, caching, or parameter blocking.

Abstract

from arXiv · show

We present Cobaya, a general-purpose Bayesian analysis code aimed at models with complex internal interdependencies. Without the need for specific code by the user, interdependencies between different stages of a model pipeline are exploited for sampling efficiency: intermediate results are automatically cached, and parameters are grouped in blocks according to their dependencies and optimally sorted, taking into account their individual computational costs, so as to minimize the cost of their variation during sampling, thanks to a novel algorithm. Cobaya allows exploration of posteriors using a range of Monte Carlo samplers, and also has functions for maximization and importance-reweighting of Monte Carlo samples with new priors and likelihoods. Cobaya is written in Python in a modular way that allows for extendability, use of calculations provided by external packages, and dynamical reparameterization without modifying its source. It can exploit hybrid OpenMP/MPI parallelization, and has sub-millisecond overhead per posterior evaluation. Though Cobaya is a general purpose statistical framework, it includes interfaces to a set of cosmological Boltzmann codes and likelihoods (the latter being agnostic with respect to the choice of the former), and automatic installers for external dependencies.

I. INTRODUCTION

Cobaya addresses inefficient Bayesian sampling for costly hierarchical models by exploiting dependencies and computational speed differences across pipeline components. It provides a modular framework with sampling, optimization, reweighting, cosmology interfaces, and cluster support.

  • Motivation: Complex hierarchical posteriors contain intermediate computations and parameter subsets with different dependencies and computational costs.These differences create speed hierarchies that standard sampling applications often fail to exploit.
  • Core approach: Cobaya automatically analyzes component dependencies, blocks parameters accordingly, sorts blocks to reduce computation, and caches intermediate results.The framework also supports oversampling fast blocks and fast-dragging algorithms.
  • Analysis capabilities: Cobaya supports its own MCMC algorithms, PolyChord, importance-weighting, posterior and likelihood maximization, and MPI-based cluster computation.Additional samplers can be integrated through compatible sampler-class implementations.
  • Modularity: Theory and likelihood calculations can be supplied as separate external Python packages with their own parameter dependencies.This separates theory modeling and experimental likelihood development instead of requiring one monolithic package.
  • Cosmology support: The framework includes cosmological theory-code and likelihood interfaces, installers for stock external dependencies, and standalone cosmological likelihood wrappers.Supported theory codes include CAMB and CLASS, while likelihoods remain usable independently of the selected theory code.

III. GOALS AND DESIGN CHOICES

Cobaya’s design separates Bayesian-model components while reducing the code and communication work needed to add or modify pipeline elements. Python enables external components and an API that handles dependencies, caching, and parameter-speed structure.

  • Modularity: Cobaya treats priors, likelihoods, theory models, and samplers as separate objects that share limited information.This design keeps each component focused on its own inputs, outputs, or sampled parameters.
  • Rapid prototyping: The API lowers the effort required to add or modify priors, likelihoods, theory codes, and samplers without changing Cobaya’s source.Users describe each component’s requirements and outputs alongside its calculation.
  • External components: Python allows Cobaya to load theory and likelihood classes directly from separately distributed external packages.External packages can be referenced by fully qualified class names.
  • Architecture: Cobaya’s main structure consists of a Bayesian Model and a Monte Carlo Sampler or broader analysis tool.The Model organizes the prior, likelihood, theory, parameterization, and provider layers.

A. Input

Cobaya uses readable Python-dictionary or YAML input to specify models, parameters, components, and analysis tools. Its model layers manage parameter roles and exchanges between pipeline components.

  • Input: Each run specifies the model, parameter space, and analysis tool in a human-readable input format.The input syntax is intended to mirror the code structure and make components easy to exchange.
  • Input: Cobaya input is a Python dictionary that can be serialized as plain-text YAML when its elements are serializable.The format supports parameter roles, dynamic parameters, and Python-function definitions for simple likelihoods and priors.
  • Input: Class defaults and automated options reduce the amount of configuration required for complex models and pipelines.In the example, only the MCMC stopping criterion is explicitly specified while other settings come from defaults or automation.
  • Model structure: The Bayesian Model contains prior, likelihood, parameterization, and provider layers that manage computation and parameter exchange.A Model can be passed to a sampler or integrated into an external user pipeline.
  • Parameter roles: Parameters are classified as sampled, fixed, or derived according to how they enter the model and analysis.Derived parameters can be defined dynamically or inside theory and likelihood code.

2. Prior

Cobaya supports separable one-dimensional priors and additional multidimensional priors, while its theory and likelihood components use dependencies and caching to organize efficient evaluation.

  • Prior specification: The `prior` keyword defines a separable product of one-dimensional priors for each sampled parameter.These priors are interfaced through continuous distributions from scipy.stats.
  • Prior specification: The global `prior` block defines additional priors that may depend on multiple parameters.The example uses a Gaussian prior along x = y.
  • Component dependencies: Theory and likelihood components can form dependency chains, with likelihoods implemented as a special theory subclass that returns likelihood values.Their caching layer avoids recomputing components unaffected by the varied parameter block.
  • Efficient exploration: Cobaya automatically groups parameters by dependencies, sorts the blocks, and caches the intermediate quantity r during the example’s posterior exploration.The sampler varies the [x, y] and [mean radius] blocks separately, placing [mean radius] last.

C. Sampler

Cobaya provides multiple sampling, optimization, and reweighting tools while automatically exploiting parameter speed hierarchies and cached intermediate results.

  • Cobaya implements adaptive fast-slow MCMC, including Metropolis-Hastings with optional fast-parameter oversampling and dragging along the fastest directions.
  • A new automated scheme blocks parameters by likelihood-stage dependencies, measures computational costs, and optimally sorts blocks to reduce sampling time.
  • Cobaya quantifies chain convergence with a modified Gelman-Rubin R−1 statistic computed across parallel chains or recent portions of one chain.
  • Cobaya interfaces with PolyChord for nested sampling, evidence estimation, and exploration of complicated multimodal likelihood surfaces.
  • It also provides minimizer interfaces, fixed-point model evaluation, importance reweighting, and Pandas-based sample management compatible with external analysis tools.
  • GetDist can load Cobaya results and provide confidence intervals, density estimates, convergence diagnostics, plots, and a graphical interface.

V. HIGH PERFORMANCE COMPUTING WITH COBAYA

Cobaya targets cluster-based Bayesian computation through low evaluation overhead, hybrid parallelization, and tools for organizing theory–data grids.

  • ≲0.2 ms is Cobaya’s overhead per posterior evaluation, with little dimensionality dependence for d < 100.For computations taking more than a few milliseconds, sampling in Cobaya is not significantly slower than repeated evaluation in a simple loop.
  • GetDist outputs include posterior contours for sampled parameters and one-dimensional posteriors for derived parameters such as r and θ.The example’s contours enclose 68% and 95% of the probability, with posteriors normalized to the same maximum.
  • Cobaya uses hybrid MPI+threading parallelization, with MPI-aware samplers and threaded likelihood, theory, and selected sampler operations.Each MPI process carries an independent Model, while parallel chains communicate to improve sampling efficiency and assess convergence.
  • Cobaya prepares all allowed theory–data combinations, creates nested result folders, and generates cluster-queue submission scripts.
  • Users without clusters can use documented on-demand cloud computing through Amazon EC2.

VI. COSMOLOGY

Cobaya applies its modular, dependency-aware framework to cosmological analysis, integrating external theory and likelihood codes while preserving computational efficiency.

  • Cobaya was developed for cosmology and includes tools for standard cosmological theory codes and publicly available likelihoods.
  • Input complexity: A graphical application generates cosmological inputs and automatically selects sampler configurations, including an MCMC proposal covariance matrix.
  • External dependencies: Cobaya generally interfaces stock external theory and likelihood codes through light wrappers and provides a one-line installer across supported systems.
  • User-modified external code: Users can provide modified external-code installations by specifying their paths, without repackaging those codes.
  • Alternative external codes with the same role: Likelihood interfaces remain agnostic to the cosmological theory code when alternative codes provide the quantities requested by other pipeline components.
  • Computational cost: Caching can avoid recomputing transfer functions when power-spectrum and nonlinear-evolution parameters vary.Cosmological likelihood evaluation may involve Boltzmann calculations plus nonlinear modelling and other derived-observable computations.
  • Cobaya and CosmoMC both reach Gelman-Rubin convergence R −1 ≲0.02 in 8 hours using four cores per chain and four chains for ΛCDM with CAMB and Planck data.The comparison assumes a good initial proposal covariance matrix.
  • Cobaya supports more efficient handling of increasingly modular cosmological pipelines, including primordial power-spectrum reconstruction with a separate Theory class.

A. An example use case in Cosmology

Cobaya demonstrates a modular cosmological inference pipeline for recovering primordial power-spectrum features under lensed and delensed CMB scenarios. The example illustrates configurable theory and likelihood components, parameter redefinitions and priors, dependency-aware sampling, and degeneracies limiting feature recovery.

  • Use case and model: The example injects a four-parameter primordial power-spectrum feature and forecasts its recovery using idealized Planck and Simons Observatory data.The feature parameters are amplitude A, wavelength l, envelope centre kc, and envelope log-width w.
  • Lensing scenarios: The setup supports standard lensed and assumed-delensed CMB spectra, with delensing intended to reduce lensing-induced smoothing of small-scale oscillations.The example also notes that detectability is not very sensitive to delensing modes with L > 200.
  • Pipeline construction: Cobaya builds the pipeline by defining a separate primordial-spectrum Theory class, using CAMB for CMB spectra, and wrapping generated feature-containing data in a generic Likelihood class.The components communicate through Cobaya interfaces without modifying Cobaya or CAMB.
  • Parameterization and priors: Parameter redefinitions enable log-uniform sampling for feature amplitude, wavelength, and envelope centre, while an additional prior excludes combinations producing no significant CMB-range trace.These transformations and priors are specified in the input rather than by modifying the underlying Theory class.
  • Sampling strategy: Cobaya automatically exploits the parameter speed hierarchy by placing the slower CMB transfer-function block before the primordial-spectrum block during MCMC sampling.The feature parameters are sent to FeaturePrimordialPk, while the slower cosmological parameters are sent to CAMB.
  • Results and scope: The delensed analysis exhibits a degeneracy toward features with simultaneously higher-k centres, wider envelopes, and larger amplitudes, while the non-delensed chains mostly follow prior regions with negligible feature imprints.The degeneracy cannot usually be mapped to amplitudes near ΔP(k)/P(k) ∼1 because the feature model assumes a small perturbation.

VII. CONCLUSION

Cobaya is a modular Bayesian framework for complex pipelines that automatically exploits component interdependencies and computational costs to improve posterior exploration. It is designed for high-performance computing and supports multiple posterior-analysis operations.

  • Cobaya automatically accounts for pipeline interdependencies and computational costs to optimally sort parameter blocks without user-written optimization code.
  • Cobaya provides a modular framework with structured inputs and a minimal API for external theories and likelihoods.
  • Cobaya supports posterior sampling, maximization, and importance reweighting.
  • Cobaya is suited to high-performance computing through low overhead, MPI parallelization, and batch-running grids of jobs.
  • Future development plans include HPC-enabled containerization and improved batch-running tools.

Appendix A: Automatic parameter blocking for speed-hierarchy exploitation

The blocking algorithm exploits speed hierarchies induced by interdependent theory and likelihood calculations. It groups parameters by shared recomputation patterns and chooses an ordering that minimizes the total cost of parameter cycling.

  • Interdependent theory and likelihood components create a speed hierarchy because cached intermediate quantities avoid recomputing unaffected pipeline stages.
  • Blocks improve sampling robustness by proposing randomized directions among parameters with approximately similar speeds.
  • The current release focuses on likelihoods without readily available derivatives, for which derivative-based samplers may be more efficient when derivatives can be calculated.
  • Parameters are grouped when varying them requires recomputation of the same likelihood components, represented by binary footprint vectors.
  • The algorithm accounts for Cholesky-induced downward mixing by propagating recomputation requirements through the footprint matrix.
  • For N blocks, the optimal ordering minimizes the total cost T(σ) over permutations using cumulative per-block costs.
  • A test compares optimal sorting with random sorting and two naive orderings using likelihoods with different speeds and parameter counts.

Oversampling of fast parameters

Cobaya can oversample fast parameter blocks to exploit speed differences while accounting for correlations that may reduce sampling efficiency. The oversampling scheme is configurable and depends on the sampled posterior.

  • Oversampling fast parameters can improve exploration of fast directions at relatively low computational cost in pipelines with large speed hierarchies.
  • Fixed oversampling factors modify each block’s effective parameter count when computing the optimal ordering.
  • The optimal oversampling factors generally depend on the posterior, so no universally optimal oversampling scheme exists.
  • Cobaya parameterizes oversampling factors from relative block speeds using a constant power o and rounds down to the nearest integer.
  • The sampler accepts oversampling power o from zero upward, trading additional computation per slow step for better exploration of fast parameter space.

Implementation details in Cobaya

Cobaya automatically derives parameter blocking from component dependencies and can obtain evaluation times either empirically or manually. It then computes an ordering and optional oversampling configuration for the sampler.

  • Cobaya determines blocks automatically from the components required by each input parameter and the dependencies among theory and likelihood components.
  • Component evaluation times can be measured using a small prior sample or specified manually for each theory or likelihood.
  • Cobaya minimizes the ordering cost by brute force when the number of parameter blocks is ≲10.
  • The MCMC sampler can automatically compute the optimal ordering for a specified oversampling power.
  • Cobaya uses a default oversampling power of o = 0.4 as a balance between better exploration and limiting computational cost, and thins output by the total oversampling factor.

Use in dragging

Cobaya supports dragging fast parameter directions alongside slower ones, improving sampling when fast and slow parameters are strongly dependent. It selects the fast–slow split automatically when more than two parameter blocks are present.

  • Use in dragging: Dragging fast directions lets slow steps explore the marginalized slow posterior while fast parameters follow the required parameter-space location.The method can be dramatically more efficient when fast and slow parameters have strong dependencies.
  • Use in dragging: For more than two blocks, Cobaya chooses the fast–slow split that maximizes the log-difference in computation time across possible splits.The specified oversampling factor is then applied to the slower block.
  • Use in dragging: The appendix reproduces the source code and input files needed to run the paper’s cosmological use case, including the Theory and Likelihood classes.It also identifies inputs for delensed and non-delensed scenarios and supporting fiducial data and covariance files.
Loading 2005.05290v2…