firecrown.likelihood_base

Base classes for likelihood framework.

This module contains all abstract base classes and core types used throughout the likelihood framework. It is designed to be dependency-free with respect to the firecrown.likelihood package to avoid circular import issues.

Classes moved from: - likelihood/_base.py: Likelihood, NamedParameters, Statistic, Source, Tracer, etc.

Attributes

SOURCE_GALAXY_SYSTEMATIC_DEFAULT_DELTA_Z

SOURCE_GALAXY_SYSTEMATIC_DEFAULT_SIGMA_Z

Exceptions

StatisticUnreadError

Error raised when accessing an un-read statistic.

Classes

Likelihood

Likelihood is an abstract class.

NamedParameters

Provides access to a set of parameters of a given set of types.

Statistic

The abstract base class for all physics-related statistics.

GuardedStatistic

An internal class used to maintain state on statistics.

TrivialStatistic

A minimal statistic only to be used for testing Gaussian likelihoods.

SourceSystematic

An abstract systematic class (e.g., shear biases, photo-z shifts, etc.).

Source

The abstract base class for all sources.

Tracer

Extending the pyccl.Tracer object with additional information.

SourceGalaxyArgs

Class for galaxy based sources arguments.

SourceGalaxySystematic

Abstract base class for all galaxy-based source systematics.

SourceGalaxyPhotoZShift

A photo-z shift bias.

PhotoZShift

Photo-z shift systematic.

PhotoZShiftFactory

Factory class for PhotoZShift objects.

SourceGalaxyPhotoZShiftandStretch

A photo-z shift & stretch bias.

PhotoZShiftandStretch

Photo-z shift and stretch systematic.

PhotoZShiftandStretchFactory

Factory class for PhotoZShiftandStretch objects.

SourceGalaxySelectField

The source galaxy select field systematic.

SourceGalaxy

Source class for galaxy based sources.

Functions

dndz_shift_and_stretch_active(z, dndz, delta_z, sigma_z)

Shift and stretch the photo-z distribution using an active transformation.

dndz_shift_and_stretch_passive(z, dndz, delta_z, sigma_z)

Shift and stretch the photo-z distribution using a passive transformation.

Module Contents

class firecrown.likelihood_base.Likelihood(*, parameter_prefix=None, raise_on_unused_parameter=True)[source]

Bases: firecrown.updatable.Updatable

Inheritance diagram of firecrown.likelihood_base.Likelihood

Likelihood is an abstract class.

Concrete subclasses represent specific likelihood forms (e.g. gaussian with constant covariance matrix, or Student’s t, etc.).

Concrete subclasses must have an implementation of both read() and compute_loglike(). Note that abstract subclasses of Likelihood might implement these methods, and provide other abstract methods for their subclasses to implement.

Parameters:
  • parameter_prefix (None | str)

  • raise_on_unused_parameter (bool)

raise_on_unused_parameter = True
abstractmethod read(sacc_data)[source]

Read the covariance matrix for this likelihood from the SACC file.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None

abstractmethod make_realization_vector()[source]

Create a new realization of the model.

This new realization uses the previously computed theory vector and covariance matrix.

Returns:

the new realization of the theory vector

Return type:

numpy.typing.NDArray[numpy.float64]

make_realization(sacc_data, add_noise=True, strict=True)[source]

Create a new realization of the model.

This realization uses the previously computed theory vector and covariance matrix.

Parameters:
  • sacc_data (sacc.Sacc) – The SACC data object containing the covariance matrix

  • add_noise (bool) – If True, add noise to the realization. If False, return only the theory vector.

  • strict (bool) – If True, check that the indices of the realization cover all the indices of the SACC data object.

Returns:

the new SACC object containing the new realization

Return type:

sacc.Sacc

compute_loglike_for_sampling(tools)[source]

Compute the log-likelihood of generic CCL data, swallowing some CCL errors.

If CCL raises an error indicating an integration error, this function returns -np.inf.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – the ModelingTools to be used in calculating the likelihood

Returns:

the log-likelihood

Return type:

float

abstractmethod compute_loglike(tools)[source]

Compute the log-likelihood of generic CCL data.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – the ModelingTools to be used in calculating the likelihood

Returns:

the log-likelihood

Return type:

float

class firecrown.likelihood_base.NamedParameters(mapping=None)[source]

Provides access to a set of parameters of a given set of types.

Access to the parameters is provided by a type-safe interface. Each of the access functions assures that the parameter value it returns is of the specified type.

Parameters:

mapping (None | collections.abc.Mapping[str, str | int | bool | float | numpy.typing.NDArray[numpy.int64] | numpy.typing.NDArray[numpy.float64]])

get_bool(name, default_value=None)[source]

Return the named parameter as a bool.

Parameters:
  • name (str) – the name of the parameter to be returned

  • default_value (None | bool) – the default value if the parameter is not found

Returns:

the value of the parameter (or the default value)

Return type:

bool

get_string(name, default_value=None)[source]

Return the named parameter as a string.

Parameters:
  • name (str) – the name of the parameter to be returned

  • default_value (None | str) – the default value if the parameter is not found

Returns:

the value of the parameter (or the default value)

Return type:

str

get_int(name, default_value=None)[source]

Return the named parameter as an int.

Parameters:
  • name (str) – the name of the parameter to be returned

  • default_value (None | int) – the default value if the parameter is not found

Returns:

the value of the parameter (or the default value)

Return type:

int

get_float(name, default_value=None)[source]

Return the named parameter as a float.

Parameters:
  • name (str) – the name of the parameter to be returned

  • default_value (None | float) – the default value if the parameter is not found

Returns:

the value of the parameter (or the default value)

Return type:

float

get_int_array(name)[source]

Return the named parameter as a numpy array of int.

Parameters:

name (str) – the name of the parameter to be returned

Returns:

the value of the parameter

Return type:

numpy.typing.NDArray[numpy.int64]

get_float_array(name)[source]

Return the named parameter as a numpy array of float.

Parameters:

name (str) – the name of the parameter to be returned

Returns:

the value of the parameter

Return type:

numpy.typing.NDArray[numpy.float64]

to_set()[source]

Return the contained data as a set.

Returns:

the value of the parameter as a set

Return type:

set[str | int | bool | float | numpy.typing.NDArray[numpy.int64] | numpy.typing.NDArray[numpy.float64]]

set_from_basic_dict(basic_dict)[source]

Set the contained data from a dictionary of basic types.

Parameters:

basic_dict (dict[str, str | float | int | bool | collections.abc.Sequence[float] | collections.abc.Sequence[int] | collections.abc.Sequence[bool]]) – the mapping from strings to values used for initialization

Return type:

None

convert_to_basic_dict()[source]

Convert a NamedParameters object to a dictionary of built-in types.

Returns:

a dictionary containing the parameters as built-in Python types

Return type:

dict[str, str | float | int | bool | collections.abc.Sequence[float] | collections.abc.Sequence[int] | collections.abc.Sequence[bool]]

__contains__(key)[source]

Check if a key is in the NamedParameters object.

Parameters:

key (str) – the key to check

Returns:

True if the key is in the NamedParameters object, False otherwise

Return type:

bool

exception firecrown.likelihood_base.StatisticUnreadError(stat)[source]

Bases: RuntimeError

Inheritance diagram of firecrown.likelihood_base.StatisticUnreadError

Error raised when accessing an un-read statistic.

Run-time error indicating an attempt has been made to use a statistic that has not had read called in it.

Parameters:

stat (Statistic)

statistic
class firecrown.likelihood_base.Statistic(parameter_prefix=None)[source]

Bases: firecrown.updatable.Updatable

Inheritance diagram of firecrown.likelihood_base.Statistic

The abstract base class for all physics-related statistics.

Statistics read data from a SACC object as part of a multi-phase initialization. They manage a DataVector and, given a ModelingTools object, can compute a TheoryVector.

Statistics represent things like two-point functions and mass functions.

Parameters:

parameter_prefix (None | str)

sacc_indices: None | numpy.typing.NDArray[numpy.int64]
ready = False
computed_theory_vector = False
theory_vector: None | firecrown.data_types.TheoryVector = None
read(_)[source]

Read the data for this statistic and mark it as ready for use.

Derived classes that override this function should make sure to call the base class method using:

super().read(sacc_data)

as the last thing they do.

Parameters:

_ (sacc.Sacc) – currently unused, but required by the interface.

Return type:

None

abstractmethod get_data_vector()[source]

Gets the statistic data vector.

Returns:

The data vector.

Return type:

firecrown.data_types.DataVector

compute_theory_vector(tools)[source]

Compute a statistic from sources, applying any systematics.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – the modeling tools used to compute the theory vector.

Returns:

The computed theory vector.

Return type:

firecrown.data_types.TheoryVector

get_theory_vector()[source]

Returns the last computed theory vector.

Raises a RuntimeError if the vector has not been computed.

Returns:

The already-computed theory vector.

Return type:

firecrown.data_types.TheoryVector

class firecrown.likelihood_base.GuardedStatistic(stat)[source]

Bases: firecrown.updatable.Updatable

Inheritance diagram of firecrown.likelihood_base.GuardedStatistic

An internal class used to maintain state on statistics.

GuardedStatistic is used by the framework to maintain and validate the state of instances of classes derived from Statistic.

Parameters:

stat (Statistic)

statistic
read(sacc_data)[source]

Read whatever data is needed from the given sacc.Sacc object.

After this function is called, the object should be prepared for the calling of the methods get_data_vector() and compute_theory_vector().

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to read from.

Return type:

None

get_data_vector()[source]

Return the contained Statistic’s data vector.

GuardedStatistic ensures that read() has been called. first.

Returns:

The most recently calculated data vector.

Return type:

firecrown.data_types.DataVector

compute_theory_vector(tools)[source]

Return the contained Statistic’s computed theory vector.

GuardedStatistic ensures that read() has been called. first.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – the modeling tools used to compute the theory vector.

Returns:

The computed theory vector.

Return type:

firecrown.data_types.TheoryVector

class firecrown.likelihood_base.TrivialStatistic[source]

Bases: Statistic

Inheritance diagram of firecrown.likelihood_base.TrivialStatistic

A minimal statistic only to be used for testing Gaussian likelihoods.

It returns a DataVector and TheoryVector each of which is three elements long. The SACC data provided to TrivialStatistic.read() must supply the necessary values.

count = 3
data_vector: None | firecrown.data_types.DataVector = None
mean
computed_theory_vector = False
read(sacc_data)[source]

Read the necessary items from the sacc data.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None

get_data_vector()[source]

Return the data vector; raise exception if there is none.

Returns:

The data vector.

Return type:

firecrown.data_types.DataVector

class firecrown.likelihood_base.SourceSystematic(parameter_prefix=None)[source]

Bases: firecrown.updatable.Updatable

Inheritance diagram of firecrown.likelihood_base.SourceSystematic

An abstract systematic class (e.g., shear biases, photo-z shifts, etc.).

This class currently has no methods at all, because the argument types for the apply method of different subclasses are different.

Parameters:

parameter_prefix (None | str)

read(sacc_data)[source]

Call to allow this object to read from the appropriate sacc data.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None

class firecrown.likelihood_base.Source(sacc_tracer)[source]

Bases: firecrown.updatable.Updatable

Inheritance diagram of firecrown.likelihood_base.Source

The abstract base class for all sources.

Parameters:

sacc_tracer (str)

cosmo_hash: None | int
tracers: collections.abc.Sequence[Tracer]
sacc_tracer
abstractmethod read_systematics(sacc_data)[source]

Abstract method to read the systematics for this source from the SACC file.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None

read(sacc_data)[source]

Read the data for this source from the SACC file.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None

abstractmethod get_scale()[source]

Abstract method to return the scale for this Source.

Returns:

the scale

Return type:

float

abstractmethod create_tracers(tools)[source]

Abstract method to create tracers for this Source.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – The modeling tools used for creating the tracers

get_tracers(tools)[source]

Return the tracer for the given cosmology.

This method caches its result, so if called a second time with the same cosmology, no calculation needs to be done.

Parameters:

tools (firecrown.modeling_tools.ModelingTools) – The modeling tools used for creating the tracers

Returns:

the list of tracers

Return type:

collections.abc.Sequence[Tracer]

class firecrown.likelihood_base.Tracer(tracer, tracer_name=None, field=None, pt_tracer=None, halo_profile=None, halo_2pt=None)[source]

Extending the pyccl.Tracer object with additional information.

Bundles together a pyccl.Tracer object with optional information about the underlying 3D field, or a pyccl.nl_pt.PTTracer and halo profiles.

Parameters:
  • tracer (pyccl.Tracer)

  • tracer_name (None | str)

  • field (None | str)

  • pt_tracer (None | pyccl.nl_pt.PTTracer)

  • halo_profile (None | pyccl.halos.HaloProfile)

  • halo_2pt (None | pyccl.halos.Profile2pt)

static determine_field_name(field, tracer)[source]

Gets a field name for a tracer.

This function encapsulates the policy for determining the value to be assigned to the field attribute of a Tracer.

It is a static method only to keep it grouped with the class for which it is defining the initialization policy.

Parameters:
  • field (None | str) – the (stub) name of the field

  • tracer (None | str) – the name of the tracer

Returns:

the full name of the field

Return type:

str

ccl_tracer
tracer_name: str
field
pt_tracer = None
halo_profile = None
halo_2pt = None
property has_pt: bool[source]

Answer whether we have a perturbation theory tracer.

Returns:

True if we have a pt_tracer, and False if not.

Return type:

bool

property has_hm: bool[source]

Answer whether we have a halo model profile.

Return True if we have a halo_profile, and False if not.

Return type:

bool

class firecrown.likelihood_base.SourceGalaxyArgs[source]

Class for galaxy based sources arguments.

z: numpy.typing.NDArray[numpy.float64]
dndz: numpy.typing.NDArray[numpy.float64]
scale: float = 1.0
field: str = 'delta_matter'
class firecrown.likelihood_base.SourceGalaxySystematic(parameter_prefix=None)[source]

Bases: SourceSystematic, Generic[_SourceGalaxyArgsT]

Inheritance diagram of firecrown.likelihood_base.SourceGalaxySystematic

Abstract base class for all galaxy-based source systematics.

Parameters:

parameter_prefix (None | str)

abstractmethod apply(tools, tracer_arg)[source]

Apply method to include systematics in the tracer_arg.

Parameters:
  • tools (firecrown.modeling_tools.ModelingTools) – the modeling tools use to update the tracer arg

  • tracer_arg (_SourceGalaxyArgsT) – the original source galaxy tracer arg to which we apply the systematic.

Returns:

a new source galaxy tracer arg with the systematic applied

Return type:

_SourceGalaxyArgsT

firecrown.likelihood_base.SOURCE_GALAXY_SYSTEMATIC_DEFAULT_DELTA_Z = 0.0
firecrown.likelihood_base.SOURCE_GALAXY_SYSTEMATIC_DEFAULT_SIGMA_Z = 1.0
firecrown.likelihood_base.dndz_shift_and_stretch_active(z, dndz, delta_z, sigma_z)[source]

Shift and stretch the photo-z distribution using an active transformation.

We use “makima” interpolation, a cubic spline method based on the modified Akima algorithm. This approach prevents overshooting when the data remains constant for more than two consecutive nodes. Additionally, we set extrapolate=False and we set extrapolated values to zero.

The active transformation preserves the redshift array and modifies the dndz array. This transformation introduces an interpolation error on dndz.

Sign convention: For the pure-shift case (\(\sigma_z = 1\)), the transformed distribution satisfies:

\[n'(z) = n(z + \delta_z)\]

A positive \(\delta_z\) therefore shifts the distribution toward lower redshifts (i.e. the peak moves to \(z_{\rm peak} - \delta_z\)).

Note

This sign convention is opposite to the one used in the Cosmosis Standard Library photoz_bias module (additive mode), which implements \(n'(z) = n(z - \Delta z_{\rm CSL})\), so that a positive shift moves the distribution toward higher redshifts. The relationship between the two conventions is \(\delta_z = -\Delta z_{\rm CSL}\).

Parameters:
  • z (numpy.typing.NDArray[numpy.float64]) – the redshifts

  • dndz (numpy.typing.NDArray[numpy.float64]) – the dndz

  • delta_z (float) – the photo-z shift (positive values shift the distribution toward lower redshifts)

  • sigma_z (float) – the photo-z stretch

Returns:

the shifted and stretched dndz

Return type:

tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]]

firecrown.likelihood_base.dndz_shift_and_stretch_passive(z, dndz, delta_z, sigma_z)[source]

Shift and stretch the photo-z distribution using a passive transformation.

The passive transformation modifies the redshift array and preserves the dndz values. For the pure-shift case (\(\sigma_z = 1\)), each tabulated redshift \(z_i\) is replaced by \(z_i - \delta_z\), so the distribution is shifted toward lower redshifts when \(\delta_z > 0\). This is equivalent to evaluating the original distribution at \(z + \delta_z\):

\[n'(z) = n(z + \delta_z)\]

Note

See dndz_shift_and_stretch_active() for details on the sign convention.

Parameters:
  • z (numpy.typing.NDArray[numpy.float64]) – the redshifts

  • dndz (numpy.typing.NDArray[numpy.float64]) – the dndz

  • delta_z (float) – the photo-z shift (positive values shift the distribution toward lower redshifts)

  • sigma_z (float) – the photo-z stretch

Returns:

the shifted and stretched dndz

Return type:

tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]]

class firecrown.likelihood_base.SourceGalaxyPhotoZShift(sacc_tracer, active=True)[source]

Bases: SourceGalaxySystematic[_SourceGalaxyArgsT], Generic[_SourceGalaxyArgsT]

Inheritance diagram of firecrown.likelihood_base.SourceGalaxyPhotoZShift

A photo-z shift bias.

This systematic shifts the photo-z distribution by some amount delta_z. The transformation applied is \(n'(z) = n(z + \delta_z)\), so a positive delta_z shifts the distribution toward lower redshifts.

Note

See dndz_shift_and_stretch_active() for details on the sign convention.

The following parameters are special Updatable parameters, which means that they can be updated by the sampler, sacc_tracer is going to be used as a prefix for the parameters:

Variables:

delta_z – the photo-z shift (positive values shift the distribution toward lower redshifts).

Parameters:
  • sacc_tracer (str)

  • active (bool)

delta_z
apply(tools, tracer_arg)[source]

Apply a shift to the photo-z distribution of a source.

Parameters:
  • tools (firecrown.modeling_tools.ModelingTools) – the modeling tools use to update the tracer arg

  • tracer_arg (_SourceGalaxyArgsT) – the original source galaxy tracer arg to which we apply the systematic.

Returns:

a new source galaxy tracer arg with the systematic applied

Return type:

_SourceGalaxyArgsT

class firecrown.likelihood_base.PhotoZShift(sacc_tracer, active=True)[source]

Bases: SourceGalaxyPhotoZShift

Inheritance diagram of firecrown.likelihood_base.PhotoZShift

Photo-z shift systematic.

Parameters:
  • sacc_tracer (str)

  • active (bool)

class firecrown.likelihood_base.PhotoZShiftFactory(/, **data)[source]

Bases: pydantic.BaseModel

Inheritance diagram of firecrown.likelihood_base.PhotoZShiftFactory

Factory class for PhotoZShift objects.

Parameters:

data (Any)

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: Annotated[Literal['PhotoZShiftFactory'], Field(description='The type of the systematic.')] = 'PhotoZShiftFactory'
create(bin_name)[source]

Create a PhotoZShift object with the given tracer name.

Parameters:

bin_name (str)

Return type:

PhotoZShift

create_global()[source]

Create a PhotoZShift object with the given tracer name.

Return type:

PhotoZShift

class firecrown.likelihood_base.SourceGalaxyPhotoZShiftandStretch(sacc_tracer, active=True)[source]

Bases: SourceGalaxyPhotoZShift[_SourceGalaxyArgsT]

Inheritance diagram of firecrown.likelihood_base.SourceGalaxyPhotoZShiftandStretch

A photo-z shift & stretch bias.

This systematic shifts and stretches the photo-z distribution by delta_z and sigma_z, respectively. The shift follows the same sign convention as SourceGalaxyPhotoZShift: a positive delta_z shifts the distribution toward lower redshifts (i.e. \(n'(z) = n(z + \delta_z)\) for the pure-shift case \(\sigma_z = 1\)).

Note

See dndz_shift_and_stretch_active() for details on the sign convention.

The following parameters are special Updatable parameters, which means that they can be updated by the sampler, sacc_tracer is going to be used as a prefix for the parameters:

Variables:
  • delta_z – the photo-z shift (positive values shift the distribution toward lower redshifts).

  • sigma_z – the photo-z stretch.

Parameters:
  • sacc_tracer (str)

  • active (bool)

sigma_z
apply(_, tracer_arg)[source]

Apply a shift & stretch to the photo-z distribution of a source.

Parameters:
class firecrown.likelihood_base.PhotoZShiftandStretch(sacc_tracer, active=True)[source]

Bases: SourceGalaxyPhotoZShiftandStretch

Inheritance diagram of firecrown.likelihood_base.PhotoZShiftandStretch

Photo-z shift and stretch systematic.

Parameters:
  • sacc_tracer (str)

  • active (bool)

class firecrown.likelihood_base.PhotoZShiftandStretchFactory(/, **data)[source]

Bases: pydantic.BaseModel

Inheritance diagram of firecrown.likelihood_base.PhotoZShiftandStretchFactory

Factory class for PhotoZShiftandStretch objects.

Parameters:

data (Any)

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: Annotated[Literal['PhotoZShiftandStretchFactory'], Field(description='The type of the systematic.')] = 'PhotoZShiftandStretchFactory'
create(bin_name)[source]

Create a PhotoZShiftandStretch object with the given tracer name.

Parameters:

bin_name (str)

Return type:

PhotoZShiftandStretch

create_global()[source]

Create a PhotoZShiftandStretch object with the given tracer name.

Return type:

PhotoZShiftandStretch

class firecrown.likelihood_base.SourceGalaxySelectField(field='delta_matter')[source]

Bases: SourceGalaxySystematic[_SourceGalaxyArgsT], Generic[_SourceGalaxyArgsT]

Inheritance diagram of firecrown.likelihood_base.SourceGalaxySelectField

The source galaxy select field systematic.

A systematic that allows specifying the 3D field that will be used to select the 3D power spectrum when computing the angular power spectrum.

Parameters:

field (str)

field = 'delta_matter'
apply(tools, tracer_arg)[source]

Apply method to include systematics in the tracer_arg.

Parameters:
  • tools (firecrown.modeling_tools.ModelingTools) – the modeling tools used to update the tracer_arg

  • tracer_arg (_SourceGalaxyArgsT) – the original source galaxy tracer arg to which we apply the systematics.

Returns:

a new source galaxy tracer arg with the systematic applied

Return type:

_SourceGalaxyArgsT

class firecrown.likelihood_base.SourceGalaxy(*, sacc_tracer, systematics=None)[source]

Bases: Source, Generic[_SourceGalaxyArgsT]

Inheritance diagram of firecrown.likelihood_base.SourceGalaxy

Source class for galaxy based sources.

Parameters:
sacc_tracer
current_tracer_args: None | _SourceGalaxyArgsT = None
systematics: firecrown.updatable.UpdatableCollection[SourceGalaxySystematic]
tracer_args: _SourceGalaxyArgsT
read_systematics(sacc_data)[source]

Read the systematics for this source from the SACC file.

Parameters:

sacc_data (sacc.Sacc) – The SACC data object to be read

Return type:

None