binny.nz.sampling module#
Utilities for sampling mock redshift catalogs from tabulated distributions.
- binny.nz.sampling.sample_mock_catalog_from_nz(z: Any, nz: Any, n_samples: int, *, observation_model: Callable[[NDArray[float64], Generator], NDArray[float64]] | None = None, weights: Any | None = None, seed: int | None = None, rng: Generator | None = None) dict[str, NDArray[float64]]#
Generate a mock redshift catalog from a tabulated distribution.
Redshifts are sampled from the supplied distribution without assuming a particular measurement technique. An optional callable can generate observed redshifts from the sampled values.
The observation model must accept
(z_samples, rng)and return one finite observed redshift per input sample. This permits photo-z, spec-z, grism, narrow-band, or user-defined measurement models.- Parameters:
z – Strictly increasing one-dimensional redshift grid.
nz – Nonnegative distribution evaluated on
z.n_samples – Number of catalog objects to generate.
observation_model – Optional callable accepting sampled redshifts and a NumPy random generator, returning observed redshifts. The input distribution must represent the underlying redshift population appropriate for this model.
weights – Optional scalar or one-dimensional array of object weights. Defaults to unit weights.
seed – Optional random seed for reproducibility.
rng – Optional NumPy random generator. Cannot be combined with
seed.
- Returns:
Dictionary containing
zandweight. When an observation model is provided, also containsz_obs. Thezvalues represent the distribution supplied by the caller; they are interpreted as true redshifts only when the input distribution is a true-redshift distribution.- Raises:
TypeError – If the sample count, random generator, or observation model has an invalid type.
ValueError – If the distribution, weights, or observed redshifts are invalid.
- binny.nz.sampling.sample_redshifts_from_nz(z: Any, nz: Any, n_samples: int, *, seed: int | None = None, rng: Generator | None = None) NDArray[float64]#
Sample continuous redshifts from a tabulated redshift distribution.
The distribution is interpreted as piecewise linear between grid points. Sampling uses inverse-CDF sampling with exact inversion within each interval, including on nonuniform grids. The input is normalized internally and may represent any redshift distribution.
- Parameters:
z – Strictly increasing one-dimensional redshift grid.
nz – Nonnegative distribution evaluated on
z.n_samples – Number of redshifts to generate.
seed – Optional random seed for reproducibility.
rng – Optional NumPy random generator. Cannot be combined with
seed.
- Returns:
One-dimensional array of sampled redshifts within the input grid.
- Raises:
TypeError – If
n_samplesorrnghas an invalid type.ValueError – If the distribution is invalid or has zero integral.