MassVariance

class hmf.core.mass_variance.MassVariance(*, power: PowerSource, filter: object = NOTHING, mass_accuracy: MassAccuracy = NOTHING, k_accuracy: KAccuracy = NOTHING, truncation_rtol=0.001)[source]

The mass variance sigma(m) of the unnormalised linear power at z = 0, and its slope.

See the module documentation for the mass lattice behind it. The results do not depend on sigma8 (the power is unnormalised) or on redshift: those enter later.

Examples

>>> import numpy as np
>>> from hmf.core.power_source import TabulatedPower
>>> from hmf.core.units import Msun_h, h_Mpc, power_unit, rho_unit
>>> k = np.logspace(-5, 3, 400)
>>> source = TabulatedPower(
...     k=k * h_Mpc, pk=1e4 * k / (1 + (k / 0.02) ** 2.5) ** 1.6 * power_unit,
...     mean_density=8.5e10 * rho_unit,
... )
>>> mv = MassVariance(power=source, filter="TopHat")
>>> s = mv.sigma([1e10, 1e12, 1e14] * Msun_h)
>>> bool(np.all(np.diff(s) < 0))
True
Parameters:
  • power (hmf.core.power_source.PowerSource) – The source of the unnormalised linear power at z = 0 (a PowerSource).

  • filter (hmf.core.filters.Filter, default computed) – The smoothing filter: a Filter instance, or the alias of one (e.g. ‘SharpK’).

  • mass_accuracy (hmf.core.accuracy.MassAccuracy, default computed) – The settings of the mass lattice.

  • k_accuracy (hmf.core.accuracy.KAccuracy, default computed) – The settings of the k grid. Its upper end is set from mass_accuracy.log10_m_min.

  • truncation_rtol (float, default 0.001) – The largest bound on the relative error in sigma or dln(sigma)/dln(m), from truncating the k integrals at the ends of the grid, accepted for a mass. Masses beyond it raise a DomainError.

Attributes

power: PowerSource
filter: Filter
mass_accuracy: MassAccuracy
k_accuracy: KAccuracy
truncation_rtol: float
valid_domain
computed_defaults_inputs: ClassVar[tuple[str, ...]] = ()

The dotted paths, below the stage, of the parameters its computed_defaults() are computed from (e.g. the cosmology, for sigma_8 and n_s). evolve() computes the defaults again when one of them changes. A parameter the hook only checks for (whether it is given) is not one of them. A stage that defines computed_defaults() must declare at least one: its routing table raises a TypeError otherwise.

qualified_parameters: ClassVar[frozenset[str]] = frozenset({})

in a tree holding this stage, parameter_names() lists them with the name of the stage field holding it ("transfer.model"). Any unique name still routes.

Type:

Fields whose names are too generic to stand alone (e.g. model)

shared_parameters: ClassVar[frozenset[str]] = frozenset({})

every field of that name below this stage (e.g. a cosmology held by two stages it composes) is set together by the flat name, so that they stay equal. The stage that composes the others declares it, whatever the stages it holds declare.

Type:

Names that are one parameter in this stage’s tree

Methods

classmethod computed_defaults(given: GivenParameters) → Mapping[str, Any]

Defaults of from_flat() that are computed from the given parameters.

A hook for stages whose defaults depend on other parameters (e.g. sigma_8 from the cosmology); the base stage has none. Defaults for parameters (or below stages) that are given are ignored.

A stage that defines it must list the parameters its defaults are computed from in computed_defaults_inputs (a TypeError is raised when its routing table is first built if it lists none). evolve() calls the hook again when one of them changes, with the parameters changed in that call and the new values of the inputs, and every parameter it gives a default for, and that the call does not give, takes that default. So the hook must compute its defaults from its inputs alone, and give a default for each parameter it computes whenever that parameter is not given. undeclared_inputs() checks, in a test, that it reads no other parameter.

Parameters:

given – The parameters given, by dotted path.

Returns:

Values by name or dotted path.

Return type:

Mapping

dlnsigma_dlnm(m: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The logarithmic slope dln(sigma)/dln(m) (independent of the normalisation of the power).

Parameters:

m (Quantity) – Masses, in Msun/h (or Msun, if the power source has an H0).

Returns:

dln(sigma)/dln(m), dimensionless, of the shape of m (a scalar for a scalar m).

Return type:

numpy.float64 or ndarray

Raises:

DomainError – As for sigma().

evolve(**changes: Any) → Self

Return a copy of this stage tree with some parameters changed.

Parameters are named by their flat name, a shared name, their dotted path or a dotted suffix of it unique in the tree (see hmf.core.routing and parameter_names()); a dotted path into a model field ("fit.A") changes that field of the model, and a stage’s dotted path replaces that stage. Only the stages on the path from each changed field to this one are rebuilt, each by its evolve_own(); every other stage is shared, with its cached results, and so is every derived_property() whose inputs do not change.

A change to a parameter that a stage’s defaults are computed from (its computed_defaults_inputs, e.g. a mass function’s cosmology) computes them again: every parameter that computed_defaults() gives a default for takes the new default, unless it is given in the same call. So mf.evolve(cosmology=Planck15) takes sigma_8 and n_s from Planck15, and mf.evolve(cosmology=Planck15, sigma_8=0.8) keeps sigma_8 = 0.8. Other changes compute no default again.

Every name is checked before anything is built. The new stages are built, and so validated, by their constructors; if that fails, the error propagates and this stage, which is immutable, is unchanged.

Parameters:

**changes – New values of parameters, by name or dotted path.

Returns:

A new stage of the same class (this one, if nothing changes).

Return type:

Stage

Raises:
  • TypeError – If a name is not a parameter (with suggestions), is ambiguous (with the dotted paths to choose from), is a derived property, or is given twice.

  • ValueError – If a default computed again cannot be computed from the new parameters (e.g. the new cosmology does not give sigma_8, and sigma_8 is not given), or for an invalid value.

evolve_own(**changes: Any) → Self

Return a copy of this stage with some of its own fields changed.

No routing: each name is a field of this stage, and nothing else changes. It is the step evolve() takes at each stage it rebuilds. The value of each derived_property() already computed is carried to the new stage if none of its inputs changes; every other cached result is computed again.

Parameters:

**changes – New values of fields, by name.

Returns:

A new stage of the same class.

Return type:

Stage

Raises:

TypeError – If a name is not a field of this stage (with suggestions).

classmethod fields_info() → tuple[FieldInfo, ...]

Describe this class’s fields, without creating an instance.

Returns:

The name, type, default and documentation of each field.

Return type:

tuple of FieldInfo

classmethod from_flat(mapping: Mapping[str, Any]) → Self

Build the stage tree from parameters by name.

Names are those of evolve(); a mapping value of a stage’s dotted path ({"linear_power": {"sigma_8": 0.8}}) gives the fields below that stage. Every stage of the tree that is not given is built from its parameters, and every derived field is computed. Parameters not given take their field’s default, or the one computed_defaults() gives.

Parameters:

mapping – Values of parameters, by name or dotted path, or nested mappings.

Return type:

Stage

Raises:

TypeError – For an unknown, ambiguous, derived or repeated name, or a required parameter that is not given.

classmethod invalidated_by(name: str) → tuple[str, ...]

The stages of the tree that a change of a parameter rebuilds.

The stage holding it and every stage above it, and the stages holding a derived field that is computed from it (and the stages above those), if the derived links hold. Every other stage is shared with the original tree. It is an upper bound: evolve() keeps a stage whose derived field does not change (e.g. after a disk_cache change, which is not part of any stage’s value).

Parameters:

name – A parameter, by any name evolve() accepts.

Returns:

The dotted paths of the stages ("" for this one), from the top.

Return type:

tuple of str

Raises:

TypeError – If the name is not a parameter, or is ambiguous.

ln_sigma_and_slope_kernel(m: ArrayLike) → tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]][source]

ln(sigma) and dln(sigma)/dln(m) at masses in Msun/h, at kernel level.

The values of sigma() (as its ln) and dlnsigma_dlnm(), from one lookup of the lattice, on plain arrays in canonical units, for library code (see hmf.core._kernels). Results do not depend on the batch size, or on the order of requests. The results for the latest few arrays of masses are remembered (not as part of the stage’s value), and returned again, read-only, for the same masses.

Parameters:

m – Masses in Msun/h: a plain array, of any shape.

Returns:

ln_sigma, dlnsigma_dlnm – Dimensionless, with the shape of m; read-only.

Return type:

numpy.ndarray

Raises:

DomainError – As for sigma() (a mass outside valid_domain is found from the extent of m).

ln_sigma_at_radius_kernel(r: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

ln(sigma) at filter radii in Mpc/h, evaluated directly, at kernel level.

The integral for sigma at each radius, with this stage’s filter, power and k grid, without the mass lattice: so it is not limited to the lattice’s range (mass_accuracy.extension does not apply), and does not interpolate. It agrees with ln_sigma_and_slope_kernel() at the radius’s mass to INTERPOLATION_RTOL at the default settings. For sigma_8, use a TopHat stage at R = 8 Mpc/h. Nothing is memoised.

Parameters:

r – Filter radii in Mpc/h: a plain array, of any shape.

Returns:

ln(sigma), dimensionless, with the shape of r.

Return type:

numpy.ndarray

Raises:

DomainError – If a radius is not finite and > 0, the k grid can’t resolve the integrals there, or sigma is not finite there (see the module documentation).

m_from_radius(r: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass of a filter radius: m = (4π/3) rho_cb (cR)³, with the filter’s c.

rho_cb is the mean density of CDM + baryons today (the power source’s rho_mean0).

Parameters:

r (Quantity) – Radii, in Mpc/h (or Mpc, if the power source has an H0).

Returns:

Masses, in Msun/h.

Return type:

Quantity

Raises:

DomainError – If a radius is not finite and > 0.

m_from_radius_kernel(r: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass of a filter radius, at kernel level: see m_from_radius().

Parameters:

r – Filter radii in Mpc/h: a plain array, of any shape.

Returns:

Masses in Msun/h, with the shape of r.

Return type:

numpy.ndarray

Raises:

DomainError – If a radius is not finite and > 0.

m_from_sigma(sigma: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass at which sigma(m) takes the given values: the inverse of sigma().

sigma(m) is inverted on the lattice nodes of the default range [mass_accuracy.log10_m_min, mass_accuracy.log10_m_max], then within the bracketing interval by bisection on the sigma interpolant, so m_from_sigma(sigma(m)) returns m to about 1e-12.

Parameters:

sigma (float or array_like) – Values of sigma (dimensionless, unnormalised as in sigma()).

Returns:

Masses, in Msun/h.

Return type:

Quantity

Raises:

DomainError – If a value is outside valid_domain (not finite and > 0), is outside the range of sigma on the default lattice range, or sigma(m) is not monotonically decreasing where the value is crossed.

m_from_sigma_kernel(sigma: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass at which sigma(m) takes the given values, at kernel level.

The inverse of ln_sigma_and_slope_kernel()’s sigma: see m_from_sigma().

Parameters:

sigma – Values of sigma (dimensionless, unnormalised as in sigma()): a plain array, of any shape.

Returns:

Masses in Msun/h, with the shape of sigma.

Return type:

numpy.ndarray

Raises:

DomainError – As for m_from_sigma() (a value that is not finite and > 0 is found from the extent of sigma).

classmethod parameter_defaults() → dict[str, Any]

The default of each parameter that has one, without creating a stage.

A default computed by a factory that does not depend on the stage is called (it gives a model or a setting, e.g. KAccuracy()), and the computed_defaults() of the stages of the tree, from no given parameters, are included. from_flat(parameter_defaults()) equals from_flat({}).

Return type:

dict

classmethod parameter_info() → dict[str, ParameterInfo]

Describe every parameter of the stage tree, without creating a stage.

Returns:

A ParameterInfo (dotted paths, type, default, doc, whether it is shared) by the name the parameter is routed by.

Return type:

dict

classmethod parameter_names() → tuple[str, ...]

The names of the parameters of the stage tree (see parameter_info()).

Return type:

tuple of str

classmethod quantities_available() → dict[str, tuple[str, ...]]

The public outputs of each stage of the tree.

From the classes alone: each stage’s unit_boundary() methods (e.g. dndm) and its public properties and cached properties. Other methods (e.g. MassFunction.at, which gives a view, and the plain-array ..._kernel entry points for library code), fields, private names and constants are left out.

Returns:

The names, by the dotted path of the stage ("" for this one).

Return type:

dict

radius_from_m(m: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The filter radius of a mass: the inverse of m_from_radius().

Parameters:

m (Quantity) – Masses, in Msun/h (or Msun, if the power source has an H0).

Returns:

Radii, in Mpc/h.

Return type:

Quantity

Raises:

DomainError – If a mass is not finite and > 0.

radius_from_m_kernel(m: ArrayLike) → ndarray[tuple[Any, ...], dtype[float64]][source]

The filter radius of a mass, at kernel level: see radius_from_m().

Any mass > 0 has a radius: the lattice’s range does not apply.

Parameters:

m – Masses in Msun/h: a plain array, of any shape.

Returns:

Radii in Mpc/h, with the shape of m.

Return type:

numpy.ndarray

Raises:

DomainError – If a mass is not finite and > 0.

sigma(m: Any) → ndarray[tuple[Any, ...], dtype[float64]][source]

The mass variance sigma(m) of the unnormalised linear power at z = 0.

Parameters:

m (Quantity) – Masses, in Msun/h (or Msun, if the power source has an H0).

Returns:

sigma, dimensionless, of the shape of m (a scalar for a scalar m).

Return type:

numpy.float64 or ndarray

Raises:

DomainError – If a mass is outside valid_domain (not finite and > 0, or outside the lattice with extension='raise'), can’t be resolved by the k grid, or sigma is not finite there.

to_flat() → dict[str, Any]

The value of every parameter of the stage tree, by the name it is routed by.

type(stage).from_flat(stage.to_flat()) equals the stage, and the keys are parameter_names().

Return type:

dict