Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made various functions available publicly #48

Closed
wants to merge 15 commits into from
Closed
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ examples/__pycache__/
examples/chains/
dist/
lilit.egg-info/
build/
build/
.eggs/
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# LiLit: Likelihood for LiteBIRD

Authors: Giacomo Galloni

Contributors: Giulia Piccirilli ("Cross-Correlation Science" analysis)
Author: Giacomo Galloni

## Overview

Expand Down Expand Up @@ -90,9 +88,8 @@ The number of processess translates on the number of chains that will be runned
* numpy
* healpy
* pyyaml
* pickle
* matplotlib
* camb, or classy
* camb

## Documentation

Expand Down
3 changes: 1 addition & 2 deletions examples/samplingBB.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from cobaya.log import LoggedError
from lilit import LiLit

debug = False
debug = True
name = "BB"
lmax = 500

Expand Down Expand Up @@ -94,7 +94,6 @@
"pivot_tensor": 0.01,
"num_massive_neutrinos": 1,
"theta_H0_range": [20, 100],
# "Accuracy.AccuracyBoost": 2, # This helps getting an extra squeeze on the accordance of Cobaya and Fiducial spectra
},
},
},
Expand Down
21 changes: 16 additions & 5 deletions examples/samplingTTTEEE.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from cobaya.run import run
from cobaya.log import LoggedError
from lilit import LiLit
from lilit import CAMBres2dict

debug = False
debug = True
name = "TTTEEE"

# Note that the order of these list has to be the same of the fields keyword
lmin = [20, 2]
lmin = [2, 20]
lmax = [1500, 1200]
fsky = [0.8, 0.5]

Expand Down Expand Up @@ -42,6 +43,7 @@
"params": {
"As": {"latex": "A_\\mathrm{s}", "value": "lambda logA: 1e-10*np.exp(logA)"},
"H0": {"latex": "H_0", "max": 100, "min": 20},
# "H0": 67.32117,
"cosmomc_theta": {
"derived": False,
"value": "lambda theta_MC_100: 1.e-2*theta_MC_100",
Expand All @@ -51,39 +53,45 @@
"latex": "\\log(10^{10} A_\\mathrm{s})",
"prior": {"max": 3.91, "min": 1.61},
"proposal": 0.001,
"ref": {"dist": "norm", "loc": 3.044, "scale": 0.0001},
"ref": {"dist": "norm", "loc": 3.04478383213, "scale": 0.0001},
# "ref": 3.04478383213,
},
"mnu": 0.06,
"ns": {
"latex": "n_\\mathrm{s}",
"prior": {"max": 1.2, "min": 0.8},
"proposal": 0.002,
"ref": {"dist": "norm", "loc": 0.9660499, "scale": 0.0004},
# "ref": 0.9660499,
},
"ombh2": {
"latex": "\\Omega_\\mathrm{b} h^2",
"prior": {"max": 0.1, "min": 0.005},
"proposal": 0.0001,
"ref": {"dist": "norm", "loc": 0.0223828, "scale": 0.00001},
# "ref": 0.0223828,
},
"omch2": {
"latex": "\\Omega_\\mathrm{c} h^2",
"prior": {"max": 0.99, "min": 0.001},
"proposal": 0.0005,
"ref": {"dist": "norm", "loc": 0.1201075, "scale": 0.0001},
# "ref": 0.1201075,
},
"tau": {
"latex": "\\tau_\\mathrm{reio}",
"prior": {"max": 0.8, "min": 0.01},
"proposal": 0.003,
"ref": {"dist": "norm", "loc": 0.05430842, "scale": 0.0006},
# "ref": 0.05430842,
},
"theta_MC_100": {
"drop": True,
"latex": "100\\theta_\\mathrm{MC}",
"prior": {"max": 10, "min": 0.5},
"proposal": 0.0002,
"ref": {"dist": "norm", "loc": 1.04109, "scale": 0.00004},
# "ref": 1.041456798,
"renames": "theta",
},
},
Expand All @@ -108,14 +116,17 @@
"max_l": 2700, # This is necessary to get accurate lensing B-modes
"WantTransfer": True, # This is necessary to be concordant with Planck2018 fiducial spectra
"Transfer.high_precision": True, # This is necessary to be concordant with Planck2018 fiducial spectra (this will impact negatively on the performance, so you might want to switch it off. However, remember to chanfe the fiducial accordingly.)
"Reion.helium_delta_redshift": 0.4,
"Reion.helium_redshiftstart": 6.0,
"Reion.fraction": -1,
"parameterization": 2,
"num_nu_massless": 2.046,
"share_delta_neff": True,
"YHe": 0.2454006,
"pivot_tensor": 0.01,
"nu_mass_degeneracies": [0],
"pivot_tensor": 0.05,
"num_massive_neutrinos": 1,
"theta_H0_range": [20, 100],
# "Accuracy.AccuracyBoost": 2, # This helps getting an extra squeeze on the accordance of Cobaya and Fiducial spectra
},
},
},
Expand Down
103 changes: 3 additions & 100 deletions lilit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,12 @@

A Python package encoding the likelihood for LiteBIRD.

In this repository I provide some basic examples of forecasting likelihoods for LiteBIRD. These are implemented to be used a [Cobaya](https://github.com/CobayaSampler/cobaya) context ([J. Torrado and A. Lewis, 2020](https://arxiv.org/abs/2005.05290)), which encapsulates [CosmoMC](https://github.com/cmbant/CosmoMC) in a Python framework. The idea of this repository is to ease the creation of common framework among different LiteBIRDers, trying to homogenize the post-PTEP works as we recently discussed among the collaboration. The main product of this repository is the Likelihood for LiteBIRD (LiLit), which you can find [here](lilit/likelihood.py). Within LiLit, the most relevant study cases of LiteBIRD (T, E, B) are already tested and working. So, if you need to work with those, you should not need to look into the actual definition of the likelihood function, since you can proptly start running your MCMCs. Despite this, you should provide to the likelihood some file where to find the proper LiteBIRD noise power spectra, given that LiLit is implementing a simple inverse noise weighting just as a place-holder for something more realistic. As regards lensing, LiLit will need you to pass the reconstruction noise, since its computation is not coded, thus there is no place-holder for lensing.

This repository should also give to new Cobaya-users a good starting point to build upon (see [this](examples/other_simple_likelihood.py)).

The repository can be found at [https://github.com/ggalloni/LiLit](https://github.com/ggalloni/LiLit).

## Installation

To install the LiLit package it is sufficient to do:

```
pip install lilit
```

Then, to access the LiLit class, it is sufficient to import it as:

```
from lilit import LiLit
```

## Some further details on LiLit

If you want to start using LiLit, here are some further details on what you can do with it. Firstly, LiLit is independent of the number of fields considered and can be dynamically modified at declaration. Thus, it makes no difference if you want to use B-modes alone, or if you want to use CMB temperature, E-modes, lensing, etc. The only constraint is that the Boltzmann code you are using to provide the spectra should understand the fields you are asking to LiLit. Each of these fields may have their own $\ell_{\rm min}$, $\ell_{\rm max}$ and $f_{\rm sky}$. So, I implemented the possibility to pass all these quantities as lists to LiLit, which will then take case of the proper multipoles cuts and so on. The only requirement is that you should pass these lists following the order in which you passes the requested fields. For example:

```
fields = ["t", "e", "b"]

# lmax = [lmaxTT, lmaxEE, lmaxBB]
lmax = [1500, 1200, 900]

# lmin = [lminTT, lminEE, lminBB]
lmin = [20, 2, 2]

# fsky = [fskyTT, fskyEE, fskyBB]
fsky = [1.0, 0.8, 0.6]
```

If you do not want to pass custom fiducial power spectra for the fields you requested, you can exploit the fact that LiLit will internally compute every spectra according to _Planck_ 2018 best-fit values of the cosmological parameters. Note that if you requested B-modes, you must provide the value you want to assign to the tensor-to-scalar ratio $r$ (be careful on what pivot scale you are using here). If you do not pass the spectral index $n_t$, it will follow the standard consistency relation.

As regards noise, as mentioned above you should pass realistic power spectra according to what you are working on. Just as a mere place-holder, LiLit will compute the inverse-weighted noise ([P. Campeti et al., 2020](https://arxiv.org/abs/2007.04241)) over each channel of LiteBIRD ([E. Allys et al., 2022](https://arxiv.org/abs/2202.02773)).

Once you have fixed all these quantities, you are ready to define your likelihood. The only remaining things to decide are what approximation you want to use to compute the $\chi^2$ and the actual name of the likelihood. LiLit implements the exact likelihood ([S. Hamimeche and A. Lewis, 2008](https://arxiv.org/abs/0801.0554)) and the Gaussian one. Thus, you can define your likelihood with:

```
name = exampleTEB

# using the exact likelihood approximation
exampleTEB = LiLit(name=name, fields=fields, like="exact", nl_file="/path/to/noise.pkl", lmax=lmax, fsky=fsky, debug=False)

# using the Gaussian likelihood approximation
exampleTEB = LiLit(name=name, fields=fields, like="gaussian", nl_file="/path/to/noise.pkl", lmax=lmax, fsky=fsky, debug=False)
```

Note that you may want to set debug to True in order to check that everything is OK.

I tried to be as modular as possible so that you can plug whatever existing function you have. Also, this should make parallelization easier if you need it.

## Other simple likelihood

[Here](examples/other_simple_likelihood.py), you can find two very simple examples of the skeleton of a single-field likelihood and a two-field one. If you are new to Cobaya, you may want to have a look at these before jumping to LiLit.

As regards the single-field likelihood, what you can find there is a very basic example of how to run Cobaya on a generic field X. Some parts of this may require additional attention before running, however this should give you an idea of how to work with this package. For instance, here I am considering a very simple case in which I do not need to pass anything to the likelihood class but the spectra, and in which the likelihood does not provide any derived parameters.

## Examples

I provide also few working examples of the usage of LiLit. Some particular attention should be given to the dictionaries defined in the sampling scripts. If you are not familiar with their structure, have a look at Cobaya's [documentation](https://cobaya.readthedocs.io/en/latest/). Then, I customized these to maximize accordance with the fiducial _Planck_ 2018 spectra. These also assume that you will be using [CAMB](https://github.com/cmbant/CAMB) ([A. Lewis et al., 2000](https://arxiv.org/abs/astro-ph/9911177)); for the [CLASS](https://github.com/lesgourg/class_public) ([D. Blas et al., 2011](https://arxiv.org/abs/1104.2933)) equivalent of this, refer again to documentiation, since some of the parameters are renamed. Note that Cobaya will understand whatever newly defined parameter you added to the Boltzmann code.

In the parameters block of the sampling dictionary, parameters with a prior will be interpreted as open parameters, while all the others are essentially derived ones. Cobaya will figure out by itself whether it has to ask for some of them to the theory code (CAMB) or to other parameters. Also, it will figure out what parts of the routines need certain parameters. For example, you can pass $A_s$ to the likelihood function and do something with it, but at the same time it will pass it also to CAMB to compute the spectra.

Once you have finished preparing your sampling.py file, you can run it simply by using:

`python sampling.py > log.txt`

Sending the inline output to a text file might help in recovering information on how the run is going. If you want to run it parallely, you may want to use something like:

`mpirun -np 4 --cpus-per-proc 4 python sampling.py > log.txt`

The number of processess translates on the number of chains that will be runned simultaneously.

## Dependencies
* cobaya
* numpy
* healpy
* pyyaml
* pickle
* matplotlib
* camb, or classy

## Documentation

The documentation can be found at [https://ggalloni.github.io/LiLit](https://ggalloni.github.io/LiLit).

## Developing the code

If you want to help developing the code, feel free to send a pull request. Also, feel free to write me so that we can discuss on eventual major developments.

## Support

For additional details on how Cobaya works, I suggest to see the [documentation](https://cobaya.readthedocs.io/en/latest/), which is very good to find whatever is not working in you case. If you cannot find the problem there, feel free to open an issue.
.. include:: ../README.md
"""

from .likelihood import LiLit
from .functions import *

__author__ = "Giacomo Galloni"
__version__ = "1.1.4"
__version__ = "1.2.1"
__docformat__ = "numpy"
Loading