Synopsis:Managing the CAMB cosmological code
Author:Jesus Torrado and Antony Lewis

This module imports and manages the CAMB cosmological code. It requires CAMB 1.0 or higher (for compatibility with older versions, you can temporarily use cobaya 1.0.4, but update asap, since that version is not maintained any more).


If you use this cosmological code, please cite it as:
A. Lewis, A. Challinor, A. Lasenby, Efficient computation of CMB anisotropies in closed FRW (arXiv:astro-ph/9911177)
C. Howlett, A. Lewis, A. Hall, A. Challinor, CMB power spectrum parameter degeneracies in the era of precision cosmology (arXiv:1201.3654)


If you are using a likelihood that requires some observable from CAMB, simply add CAMB to the theory block.

You can specify any parameter that CAMB understands in the params block:

      [any param that CAMB understands, for FIXED and PRECISION]

    [any param that CAMB understands, fixed, sampled or derived]

If you want to use your own version of CAMB, you need to specify its location with a path option inside the classy block. If you do not specify a path, CAMB will be loaded from the automatic-install modules folder, if specified, or otherwise imported as a globally-installed Python package. Cobaya will print at initialisation where it is getting CAMB from.

Modifying CAMB

If you modify CAMB and add new variables, make sure that the variables you create are exposed in the Python interface (instructions here). If you follow those instructions you do not need to make any additional modification in Cobaya.

You can use the model wrapper to test your modification by evaluating observables or getting derived quantities at known points in the parameter space (set debug: True to get more detailed information of what exactly is passed to CLASS).

In your CAMB modification, remember that you can raise a CAMBParamRangeError or a CAMBError whenever the computation of any observable would fail, but you do not expect that observable to be compatible with the data (e.g. at the fringes of the parameter space). Whenever such an error is raised during sampling, the likelihood is assumed to be zero, and the run is not interrupted.



cobaya calls CAMB using its Python interface, which requires that you compile CAMB using intel’s ifort compiler or the GNU gfortran compiler version 6.4 or later. To check if you have the latter, type gfortran --version in the shell, and the first line should look like

GNU Fortran ([your OS version]) [gfortran version] [release date]

Check that [gfortran's version] is at least 6.4. If you get an error instead, you need to install gfortran (contact your local IT service).

CAMB comes with binaries pre-built for Windows, so if you don’t need to modify the CAMB source code, no Fortran compiler is needed.

Automatic installation

If you do not plan to modify CAMB, the easiest way to install it is using the automatic installation script. Just make sure that theory: camb: appears in one of the files passed as arguments to the installation script.

Manual installation (or using your own version)

If you are planning to modify CAMB or use an already modified version, you should not use the automatic installation script. Use the installation method that best adapts to your needs:

  • [Recommended for staying up-to-date] To install CAMB locally and keep it up-to-date, clone the CAMB repository in Github in some folder of your choice, say /path/to/theories/CAMB:

    $ cd /path/to/theories
    $ git clone --recursive
    $ cd CAMB
    $ python build

    To update to the last changes in CAMB (master), run git pull from CAMB’s folder and re-build using the last command.

  • [Recommended for modifying CAMB] First, fork the CAMB repository in Github (follow this instructions) and then follow the same steps as above, substituting the second one with:

    $ git clone --recursive https://[YourGithubUser][YourGithubUser]/CAMB.git
  • To use your own version, assuming it’s placed under /path/to/theories/CAMB, just make sure it is compiled (and that the version on top of which you based your modifications is old enough to have the Python interface implemented.

In the cases above, you must specify the path to your CAMB installation in the input block for CAMB (otherwise a system-wide CAMB may be used instead):

    path: /path/to/theories/CAMB


In any of these methods, you should not install CAMB as python package using python install --user, as the official instructions suggest. It is actually safer not to do so if you intend to switch between different versions or modifications of CAMB.

camb class

class theories.camb.camb(info_theory, modules=None, timing=None)

Importing CAMB from the correct path, if given.


Specifies the quantities that each likelihood needs from the Cosmology code.

Typical requisites in Cosmology (as keywords, case insensitive):

  • Cl={...}: CMB lensed power spectra, as a dictionary {spectrum:l_max}, where the possible spectra are combinations of “t”, “e”, “b” and “p” (lensing potential). Get with get_Cl().
  • [BETA: CAMB only; notation may change!] source_Cl={...}: \(C_\ell\) of given sources with given windows, e.g.: source_name: {"function": "spline"|"gaussian", [source_args]; for now, [source_args] follow the notation of CAMBSources. If can also take lmax: [int], limber: True if Limber approximation desired, and non_linear: True if non-linear contributions requested. Get with get_source_Cl().
  • Pk_interpolator={...}: Matter power spectrum interpolator in \((z, k)\). Takes "z": [list_of_evaluated_redshifts], "k_max": [k_max], "extrap_kmax": [max_k_max_extrapolated], "nonlinear": [True|False], "vars_pairs": [["delta_tot", "delta_tot"], ["Weyl", "Weyl"], [...]]}.
  • H={'z': [z_1, ...], 'units': '1/Mpc' or 'km/s/Mpc'}: Hubble rate at the redshifts requested, in the given units. Get it with get_H().
  • angular_diameter_distance={'z': [z_1, ...]}: Physical angular diameter distance to the redshifts requested. Get it with get_angular_diameter_distance().
  • comoving_radial_distance={'z': [z_1, ...]}: Comoving radial distance from us to the redshifts requested. Get it with get_comoving_radial_distance().
  • fsigma8={'z': [z_1, ...]}: Structure growth rate \(f\sigma_8\) at the redshifts requested. Get it with get_fsigma8().
  • k_max=[...]: Fixes the maximum comoving wavenumber considered.
  • Other derived parameters that are not included in the input but whose value the likelihood may need.
compute(_derived=None, cached=True, **params_values_dict)

Takes a dictionary of parameter values and computes the products needed by the likelihood. If passed a keyword derived with an empty dictionary, it populates it with the value of the derived parameters for the present set of sampled and fixed parameter values.


Interface function for likelihoods to get sampled and derived parameters.

Always use this one; don’t try to access theory code attributes directly!

get_Cl(ell_factor=False, units='muK2')

Returns a dictionary of lensed CMB power spectra and the lensing potential pp power spectrum.

Set the units with the keyword units='1'|'muK2'|'K2' (default: ‘muK2’, except for the lensing potential power spectrum, which is always unitless).

If ell_factor=True (default: False), multiplies the spectra by \(\ell(\ell+1)/(2\pi)\) (or by \(\ell^2(\ell+1)^2/(2\pi)\) in the case of the lensing potential pp spectrum).

get_H(z, units='km/s/Mpc')

Returns the Hubble rate at the given redshifts.

The redshifts must be a subset of those requested when needs() was called.

The available units are km/s/Mpc (i.e. c*H(Mpc^-1)) and 1/Mpc.


Returns the physical angular diameter distance to the given redshifts.

The redshifts must be a subset of those requested when needs() was called.


Structure growth rate \(f\sigma_8\), as defined in eq. 33 of Planck 2015 results. XIII. Cosmological parameters, at the given redshifts.

The redshifts must be a subset of those requested when needs() was called.


Returns a (dict of) power spectrum interpolator(s) PowerSpectrumInterpolator.


Returns a dict of power spectra of for the computed sources, with keys a tuple of sources ([source1], [source2]), and an additional key ell containing the multipoles.