Using the model wrapper

In the last section we have seen how to run basic samples. Before we get to creating cosmological likelihoods, let us take a look at the model wrapper. It creates a python object that lets you interact with the different parts of your model: theory code, prior and likelihood. It can do the same as the evaluate sampler, and much more, in an interactive way.

You can use it to test your modifications, to evaluate the cosmological observables and likelihoods at particular points, and also to interface cosmological codes and likelihoods with external tools, such as a different sampler, a machine-learning framework…

Models are created from the same input as the run() function: a dictionary containing the same blocks, except for the sampler block (it can be included, but will be ignored).

So let us create a simple one using the input generator: Planck 2018 polarized CMB and lensing with CLASS as a theory code. Let us copy the python version (you can also copy the yaml version and load it with yaml_load()).

info_txt = r"""
likelihood:
  planck_2018_lowl.TT:
  planck_2018_lowl.EE:
  planck_2018_highl_plik.TTTEEE:
  planck_2018_lensing.clik:
theory:
  classy:
    extra_args: {N_ur: 2.0328, N_ncdm: 1}
params:
  logA:
    prior: {min: 2, max: 4}
    ref: {dist: norm, loc: 3.05, scale: 0.001}
    proposal: 0.001
    latex: \log(10^{10} A_\mathrm{s})
    drop: true
  A_s: {value: 'lambda logA: 1e-10*np.exp(logA)', latex: 'A_\mathrm{s}'}
  n_s:
    prior: {min: 0.8, max: 1.2}
    ref: {dist: norm, loc: 0.96, scale: 0.004}
    proposal: 0.002
    latex: n_\mathrm{s}
  H0:
    prior: {min: 40, max: 100}
    ref: {dist: norm, loc: 70, scale: 2}
    proposal: 2
    latex: H_0
  omega_b:
    prior: {min: 0.005, max: 0.1}
    ref: {dist: norm, loc: 0.0221, scale: 0.0001}
    proposal: 0.0001
    latex: \Omega_\mathrm{b} h^2
  omega_cdm:
    prior: {min: 0.001, max: 0.99}
    ref: {dist: norm, loc: 0.12, scale: 0.001}
    proposal: 0.0005
    latex: \Omega_\mathrm{c} h^2
  m_ncdm: {renames: mnu, value: 0.06}
  Omega_Lambda: {latex: \Omega_\Lambda}
  YHe: {latex: 'Y_\mathrm{P}'}
  tau_reio:
    prior: {min: 0.01, max: 0.8}
    ref: {dist: norm, loc: 0.06, scale: 0.01}
    proposal: 0.005
    latex: \tau_\mathrm{reio}
"""

from cobaya.yaml import yaml_load

info = yaml_load(info_txt)

# Add your modules installation folder
info['modules'] = '/path/to/modules'

Now let’s build a model (we will need the path to your modules’ installation):

from cobaya.model import get_model

model = get_model(info)

To get (log) probabilities and derived parameters for particular parameter values, we can use the different methods of the model.Model (see below), to which we pass a dictionary of sampled parameter values.

Note

Notice that we can only fix sampled parameters: in the example above, the primordial log-amplitude logA is sampled (has a prior) but the amplitude defined from it A_s it not (it just acts as an interface between the sampler and CLASS), so we can not pass it as an argument to the methods of model.Model.

To get a list of the sampled parameters in use:

print(list(model.parameterization.sampled_params()))

Since there are lots of nuisance parameters (most of them coming from planck_2018_highl_plik.TTTEEE), let us be a little lazy and give them random values, which will help us illustrate how to use the model to sample from the prior. To do that, we will use the method sample(), where the class prior.Prior is a member of the Model. If we check out its documentation, we’ll notice that:

  • It returns an array of samples (hence the [0] after the call below)
  • Samples are returned as arrays, not a dictionaries, so we need to add the parameter names, whose order corresponds to the output of model.parameterization.sampled_params().
  • There is an external prior: a gaussian on a linear combination of SZ parameters inherited from the likelihood planck_2018_highl_plik.TTTEEE; thus, since we don’t know how to sample from it, we need to add the ignore_external=True keyword (mind that the returned samples are not samples from the full prior, but from the separable 1-dimensional prior described in the params block.

We overwrite our prior sample with some cosmological parameter values of interest, and compute the logposterior (check out the documentation of logposterior() below):

point = dict(zip(model.parameterization.sampled_params(),
                 model.prior.sample(ignore_external=True)[0]))

point.update({'omega_b': 0.0223, 'omega_cdm': 0.120, 'H0': 67.01712,
              'logA': 3.06, 'n_s': 0.966, 'tau_reio': 0.065})

logposterior = model.logposterior(point)
print('Full log-posterior:')
print('   logposterior: %g' % logposterior.logpost)
print('   logpriors: %r' % dict(zip(list(model.prior), logposterior.logpriors)))
print('   loglikelihoods: %r' % dict(zip(list(model.likelihood), logposterior.loglikes)))
print('   derived params: %r' % dict(zip(list(model.parameterization.derived_params()), logposterior.derived)))

And this will print something like

Full log-posterior:
   logposterior: -16826.4
   logpriors: {'0': -21.068028542859743, 'SZ': -4.4079090809077135}
   loglikelihoods: {'planck_2018_lowl.TT': -11.659342468489626, 'planck_2018_lowl.EE': -199.91123947242667, 'planck_2018_highl_plik.TTTEEE': -16584.946305616675, 'planck_2018_lensing.clik': -4.390708962961917}
   derived params: {'A_s': 2.1327557162026904e-09, 'Omega_Lambda': 0.68165001855872, 'YHe': 0.2453671312631958}

Note

0 is the name of the 1-dimensional prior specified in the params block.

Note

Notice that the log-probability methods of Model can take, as well as a dictionary, an array of parameter values in the correct order. This may be useful when using these methods to interact with external codes.

Note

If we try to evaluate the posterior outside the prior bounds, logposterior() will return an empty list for the likelihood values: likelihoods and derived parameters are only computed if the prior is non-null, for the sake of efficiency.

If you would like to evaluate the likelihood for such a point, call loglikes() instead.

Note

If you want to use any of the wrapper log-probability methods with an external code, especially with C or Fortran, consider setting the keyword make_finite=True in those methods, which will return the largest (or smallest) machine-representable floating point numbers, instead of numpy’s infinities.

We can also use the Model to get the cosmological observables that were computed for the likelihood. To see what has been requested from the theory code, do

print(model.likelihood.theory.requested())

Which will print something like

{'cl': {'pp': 2048, 'bb': 29, 'ee': 2508, 'tt': 2508, 'eb': 0, 'te': 2508, 'tb': 0}}

If we take a look at the documentation of needs() (clickable!), we will see that to request the power spectrum we would use the method get_Cl:

Cls = model.likelihood.theory.get_Cl(ell_factor=True)
import matplotlib.pyplot as plt

plt.figure(figsize=(8, 6))
plt.plot(Cls["ell"][2:], Cls["tt"][2:])
plt.ylabel(r"$\ell(\ell+1)/(2\pi)\,C_\ell\;(\mu \mathrm{K}^2)$")
plt.xlabel(r"$\ell$")
plt.savefig("cltt.png")
# plt.show()
_images/cosmo_model_cltt.png

Warning

Cosmological observables requested this way always correspond to the last set of parameters with which the likelihood was evaluated.

If we want to request additional observables not already requested by the likelihoods, we can use the method needs() (clickable!) of the theory code (check out its documentation for the syntax).

As a final example, let us request the Hubble parameter for a number of redshifts and plot both it and the power spectrum for a range of values of \(\Omega_\mathrm{CDM}h^2\):

# Request H(z)
import numpy as np

redshifts = np.linspace(0, 2.5, 40)
model.likelihood.theory.needs(H={"z": redshifts})

omega_cdm = [0.10, 0.11, 0.12, 0.13, 0.14]

f, (ax_cl, ax_h) = plt.subplots(1, 2, figsize=(14, 6))
for o in omega_cdm:
    point["omega_cdm"] = o
    model.logposterior(point)  # to force computation of theory
    Cls = model.likelihood.theory.get_Cl(ell_factor=True)
    ax_cl.plot(Cls["ell"][2:], Cls["tt"][2:], label=r"$\Omega_\mathrm{CDM}h^2=%g$" % o)
    H = model.likelihood.theory.get_H(redshifts)
    ax_h.plot(redshifts, H / (1 + redshifts), label=r"$\Omega_\mathrm{CDM}h^2=%g$" % o)
ax_cl.set_ylabel(r"$\ell(\ell+1)/(2\pi)\,C_\ell\;(\mu \mathrm{K}^2)$")
ax_cl.set_xlabel(r"$\ell$")
ax_cl.legend()
ax_h.set_ylabel(r"$H/(1+z)\;(\mathrm{km}/\mathrm{s}/\mathrm{Mpc}^{-1})$")
ax_h.set_xlabel(r"$z$")
ax_h.legend()
plt.savefig("omegacdm.png")
# plt.show()
_images/cosmo_model_omegacdm.png

If you are creating several models in a single session, call close() after you are finished with each one, to release the memory it has used.

If you had set timing=True in the input info, dump_timing() would print the average computation time of your likelihoods and theory code (see the bottom of section Creating your own cosmological likelihood for a use example).

Note

If you are not really interested in any likelihood value and just want to get some cosmological observables (possibly using your modified version of a cosmological code), use the mock ‘one’ likelihood as the only likelihood, and add the requests for cosmological quantities by hand, as we did above with \(H(z)\).

NB: you will not be able to request some of the derived parameters unless you have requested some cosmological product to compute.

Warning

Unfortunately, not all likelihoods and cosmological codes are instance-safe, e.g. you can’t define two models using each the unbinned TT and TTTEEE likelihoods at the same time.

Low-level access to the theory code

You can access the imported CAMB or CLASS module as, respectively, Model.likelihood.theory.camb and Model.likelihood.theory.classy. But be careful about manually changing their settings: it may unexpectedly influence subsequent cosmological observable computations for the present model instance.

Model wrapper class

class model.Model(info_params, info_likelihood, info_prior=None, info_theory=None, modules=None, timing=None, allow_renames=True)

Class containing all the information necessary to compute the unnormalized posterior.

Allows for low-level interaction with the theory code, prior and likelihood.

NB: do not initialize this class directly; use get_model() instead, with some info as input.

info()

Returns a copy of the information used to create the model, including defaults.

logpriors(params_values, make_finite=False)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns the log-values of the priors, in the same order as it is returned by list([your_model].prior). The first one, named 0, corresponds to the product of the 1-dimensional priors specified in the params block, and it’s normalized (in general, the external prior densities aren’t).

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

logprior(params_values, make_finite=False)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns the log-value of the prior (in general, unnormalized, unless the only priors specified are the 1-dimensional ones in the params block).

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

loglikes(params_values, return_derived=True, make_finite=False, cached=True, _no_check=False)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns a tuple (loglikes, derived_params), where loglikes are the log-values of the likelihoods (unnormalized, in general) in the same order as it is returned by list([your_model].likelihood), and derived_params are the values of the derived parameters in the order given by list([your_model].parameterization.derived_params()).

To return just the list of log-likelihood values, make return_derived=False.

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

If cached=False (default: True), it ignores previously computed results that could be reused.

loglike(params_values, return_derived=True, make_finite=False, cached=True)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns a tuple (loglike, derived_params), where loglike is the log-value of the likelihood (unnormalized, in general), and derived_params are the values of the derived parameters in the order given by list([your_model].parameterization.derived_params()). If the model contains multiple likelihoods, the sum of the loglikes is returned.

To return just the list of log-likelihood values, make return_derived=False.

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

If cached=False (default: True), it ignores previously computed results that could be reused.

logposterior(params_values, return_derived=True, make_finite=False, cached=True)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns the a logposterior NamedTuple, with the following fields:

  • logpost: log-value of the posterior.
  • logpriors: log-values of the priors, in the same order as in list([your_model].prior). The first one, corresponds to the product of the 1-dimensional priors specified in the params block. Except for the first one, the priors are unnormalized.
  • loglikes: log-values of the likelihoods (unnormalized, in general), in the same order as in list([your_model].likelihood).
  • derived: values of the derived parameters in the order given by list([your_model].parameterization.derived_params()).

Only computes the log-likelihood and the derived parameters if the prior is non-null (otherwise the fields loglikes and derived are empty lists).

To ignore the derived parameters, make return_derived=False.

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

If cached=False (default: True), it ignores previously computed results that could be reused.

logpost(params_values, make_finite=False, cached=True)

Takes an array or dictionary of sampled parameter values. If the argument is an array, parameters must have the same order as in the input. When in doubt, you can get the correct order as list([your_model].parameterization.sampled_params()).

Returns the log-value of the posterior.

If make_finite=True, it will try to represent infinities as the largest real numbers allowed by machine precision.

If cached=False (default: True), it ignores previously computed results that could be reused.

dump_timing()

Prints the average computation time of the theory code and likelihoods.

It’s more reliable the more times the likelihood has been evaluated.