Skip to content

BAO Likelihood

The EuclidLikelihood_BAO class implements a Gaussian likelihood for Baryon Acoustic Oscillation (BAO) measurements.

cloelike.EuclidLikelihood_BAO

Classes

EuclidLikelihood_BAO

Source code in cloelike/EuclidLikelihood_BAO.py
class EuclidLikelihood_BAO:
    def __init__(self, data: dict, Background: type, LinearPerturbations: type):
        r"""Class constructor

        Parameters
        ----------
        data: dict
            Data dictionary
        Background: type
            Protocol-consistent background class
        LinearPerturbations: type
            Protocol-consistent linear perturbation class
        """
        self.data = data

        self.redshifts = np.array(list(data["BAO"].keys()))

        self.Background = Background

        # All data files have a fiducial_cosmology entry, we take the
        # first one as they are supposed to be the same
        params_fid = deepcopy(data["fiducial_cosmology"])
        params_fid["gamma_MG"] = 0.545

        # BAO fiducial cosmology stores Omega_m, we need Omega_cdm
        params_fid["Omega_cdm0"] = params_fid["Omega_m0"] - params_fid["Omega_b0"]
        del params_fid["Omega_m0"]

        # BAO fiducial cosmology stores sigma_8, we need As
        params_fid["As"] = 2.1e-9
        sigma_8 = params_fid.pop("sigma_8")
        tmp_background = Background(**params_fid)
        tmp_lin_pert = LinearPerturbations(tmp_background, np.array([0.0]))
        As_fid = params_fid["As"] * (sigma_8 / tmp_lin_pert.sigma8_0()) ** 2
        params_fid["As"] = As_fid

        self.background_fiducial = Background(**params_fid)

        self._prepare()

    def _prepare(self):
        r"""Arrange data vectors and covariance matrices in format required
        for :math:`\chi^2` calculation
        """
        self._flatten_data_vector()
        self._flatten_covariance_matrix()
        self._invert_covariance_matrix()

    def _flatten_data_vector(self):
        r"""Arranges the BAO data into a flattened data vector"""
        self.data_vector = np.squeeze(
            np.concatenate(
                [
                    [
                        self.data["BAO"][z]["data"][param]
                        for param in self.data["BAO"][z]["params"]
                    ]
                    for z in self.redshifts
                ]
            )
        )

    def _flatten_covariance_matrix(self):
        r"""Arranges the BAO covariance into a matrix form"""
        cov_blocks = [self.data["BAO"][z]["covariance"] for z in self.redshifts]
        self.flattened_covariance_matrix = np.block(
            [
                [
                    block if i == j else np.zeros_like(block)
                    for j, block in enumerate(cov_blocks)
                ]
                for i, _ in enumerate(cov_blocks)
            ]
        )

    def _invert_covariance_matrix(self):
        r"""Invert BAO covariance matrix"""
        self.inverse_covariance_matrix = np.linalg.inv(self.flattened_covariance_matrix)

    def get_theory_vector(self, parameters: dict) -> np.ndarray:
        r"""Generate theory vectors based on specified parameters
        Parameters
        ----------
        parameters: dict
            Input parameters
        Return
        ------
        theory_vec: np.ndarray
            Stacked theory vector
        """
        background = self.Background(
            H0=parameters["H0"],
            Omega_cdm0=parameters["Omega_cdm0"],
            Omega_b0=parameters["Omega_b0"],
            Omega_k0=parameters["Omega_k0"],
            w0=parameters["w0"],
            wa=parameters["wa"],
            ns=parameters["ns"],
            As=parameters["As"],
            gamma_MG=parameters["gamma_MG"],
            mnu=parameters["mnu"],
            N_mnu=parameters["N_mnu"],
        )

        alphas_dict = BaryonAcousticOscillations(
            background=background,
            background_fiducial=self.background_fiducial,
            redshifts=self.redshifts,
        ).alphas_dict

        theory_vec = np.concatenate(
            [
                [alphas_dict[z][params] for params in self.data["BAO"][z]["params"]]
                for z in self.redshifts
            ]
        )
        return theory_vec

    def loglike(self, parameters: dict):
        r"""Log-likelihood of BAO probe
        Parameters
        ----------
        parameters: dict
            Ensemble of cosmological parameters
        """
        self.theory_vector = self.get_theory_vector(parameters)
        diff = self.theory_vector - self.data_vector
        chi2 = np.dot(np.dot(diff, self.inverse_covariance_matrix), diff)
        return -0.5 * chi2
Functions
__init__
__init__(data: dict, Background: type, LinearPerturbations: type)

Class constructor

Parameters:

Name Type Description Default
data dict

Data dictionary

required
Background type

Protocol-consistent background class

required
LinearPerturbations type

Protocol-consistent linear perturbation class

required
Source code in cloelike/EuclidLikelihood_BAO.py
def __init__(self, data: dict, Background: type, LinearPerturbations: type):
    r"""Class constructor

    Parameters
    ----------
    data: dict
        Data dictionary
    Background: type
        Protocol-consistent background class
    LinearPerturbations: type
        Protocol-consistent linear perturbation class
    """
    self.data = data

    self.redshifts = np.array(list(data["BAO"].keys()))

    self.Background = Background

    # All data files have a fiducial_cosmology entry, we take the
    # first one as they are supposed to be the same
    params_fid = deepcopy(data["fiducial_cosmology"])
    params_fid["gamma_MG"] = 0.545

    # BAO fiducial cosmology stores Omega_m, we need Omega_cdm
    params_fid["Omega_cdm0"] = params_fid["Omega_m0"] - params_fid["Omega_b0"]
    del params_fid["Omega_m0"]

    # BAO fiducial cosmology stores sigma_8, we need As
    params_fid["As"] = 2.1e-9
    sigma_8 = params_fid.pop("sigma_8")
    tmp_background = Background(**params_fid)
    tmp_lin_pert = LinearPerturbations(tmp_background, np.array([0.0]))
    As_fid = params_fid["As"] * (sigma_8 / tmp_lin_pert.sigma8_0()) ** 2
    params_fid["As"] = As_fid

    self.background_fiducial = Background(**params_fid)

    self._prepare()
get_theory_vector
get_theory_vector(parameters: dict) -> np.ndarray

Generate theory vectors based on specified parameters

Parameters:

Name Type Description Default
parameters dict

Input parameters

required
Return

theory_vec: np.ndarray Stacked theory vector

Source code in cloelike/EuclidLikelihood_BAO.py
def get_theory_vector(self, parameters: dict) -> np.ndarray:
    r"""Generate theory vectors based on specified parameters
    Parameters
    ----------
    parameters: dict
        Input parameters
    Return
    ------
    theory_vec: np.ndarray
        Stacked theory vector
    """
    background = self.Background(
        H0=parameters["H0"],
        Omega_cdm0=parameters["Omega_cdm0"],
        Omega_b0=parameters["Omega_b0"],
        Omega_k0=parameters["Omega_k0"],
        w0=parameters["w0"],
        wa=parameters["wa"],
        ns=parameters["ns"],
        As=parameters["As"],
        gamma_MG=parameters["gamma_MG"],
        mnu=parameters["mnu"],
        N_mnu=parameters["N_mnu"],
    )

    alphas_dict = BaryonAcousticOscillations(
        background=background,
        background_fiducial=self.background_fiducial,
        redshifts=self.redshifts,
    ).alphas_dict

    theory_vec = np.concatenate(
        [
            [alphas_dict[z][params] for params in self.data["BAO"][z]["params"]]
            for z in self.redshifts
        ]
    )
    return theory_vec
loglike
loglike(parameters: dict)

Log-likelihood of BAO probe

Parameters:

Name Type Description Default
parameters dict

Ensemble of cosmological parameters

required
Source code in cloelike/EuclidLikelihood_BAO.py
def loglike(self, parameters: dict):
    r"""Log-likelihood of BAO probe
    Parameters
    ----------
    parameters: dict
        Ensemble of cosmological parameters
    """
    self.theory_vector = self.get_theory_vector(parameters)
    diff = self.theory_vector - self.data_vector
    chi2 = np.dot(np.dot(diff, self.inverse_covariance_matrix), diff)
    return -0.5 * chi2

options: show_source: true