binny.binning.photoz_fitting module#

Fit Gaussian core and tail photometric-redshift errors from catalogues.

This module fits an unbinned two-component Gaussian mixture to photometric redshift residuals using the expectation-maximization algorithm.

The residual is defined as

epsilon = (z_obs - z_true) / (1 + z_true).

The mixture consists of a zero-mean Gaussian core and a displaced Gaussian tail. Multiple random initializations are used, and the solution with the highest log-likelihood is retained.

The module also evaluates residual PDFs and CDFs and samples observed redshifts from the same Gaussian mixture.

The fitted parameters use Binny’s photometric-redshift naming conventions. The fitted tail displacement can be converted to Binny’s mean scale and offset using to_binny_photoz_params.

binny.calibration.photoz_fitting.fit_photoz_errors(z_true: ndarray, z_obs: ndarray, *, core_min_weight: float = 0.5, n_init: int = 10, max_iter: int = 1000, tol: float = 1e-08, seed: int = 0, return_metadata: bool = False) → dict[str, float] | tuple[dict[str, float], dict[str, float | int | bool]]#

Fit a Gaussian core and displaced tail to photometric-redshift residuals.

Fits an unbinned two-component Gaussian mixture to the normalized photometric-redshift residual:

\[\epsilon = \frac{z_{\mathrm{obs}} - z_{\mathrm{true}}} {1 + z_{\mathrm{true}}}.\]

The probability density is

\[p(\epsilon) = (1 - f_{\mathrm{out}}) \mathcal{N}(\epsilon; 0, \sigma_{\mathrm{core}}^2) + f_{\mathrm{out}} \mathcal{N}(\epsilon; \mu_{\mathrm{out}}, \sigma_{\mathrm{out}}^2),\]

where scatter_scale is the core scatter, outlier_scatter_scale is the tail scatter, outlier_mean_shift is the tail displacement, and outlier_frac is the tail mixture fraction.

The core mean is fixed at zero, while the tail mean, both standard deviations, and the mixture fraction are fitted.

The fit uses expectation-maximization with multiple random initializations. All catalogue residuals must be finite; no histogram binning or residual-range truncation is applied.

Parameters:
  • z_true – One-dimensional array of true galaxy redshifts.

  • z_obs – One-dimensional array of observed photometric redshifts, with the same shape as z_true.

  • core_min_weight – Minimum allowed core mixture fraction. Must lie strictly between zero and one. The default value of 0.5 restricts outlier_frac to at most 0.5.

  • n_init – Number of random expectation-maximization initializations. Must be positive. Defaults to 10.

  • max_iter – Maximum number of expectation-maximization iterations per initialization. Must be positive. Defaults to 1000.

  • tol – Relative log-likelihood convergence tolerance. Must be positive and finite. Defaults to 1e-8.

  • seed – Random seed controlling the expectation-maximization initializations. Defaults to 0.

  • return_metadata – Whether to return fitting diagnostics alongside the fitted parameters. Defaults to False.

Returns:

  • scatter_scale: Standard deviation of the zero-mean core.

  • outlier_scatter_scale: Standard deviation of the Gaussian tail.

  • outlier_mean_shift: Mean displacement of the Gaussian tail.

  • outlier_frac: Fitted tail mixture fraction.

If return_metadata=True, returns a tuple containing the fitted parameter dictionary and a metadata dictionary with:

  • loglike: Total log-likelihood of the selected fit.

  • n: Number of catalogue objects used in the fit.

  • n_outside: Number of excluded residuals, always zero.

  • converged: Whether the selected optimization converged.

  • n_iter: Number of iterations in the selected optimization.

  • aic: Akaike information criterion.

  • bic: Bayesian information criterion.

  • ks_statistic: Kolmogorov-Smirnov statistic.

Return type:

Dictionary containing the fitted photometric-redshift parameters

Raises:
  • ValueError – If the catalogue arrays have incompatible shapes, contain insufficient or non-finite values, or contain true redshifts less than or equal to -1.

  • ValueError – If the residual distribution is degenerate.

  • ValueError – If core_min_weight, n_init, max_iter, or tol violates its allowed range.

Notes

The Akaike and Bayesian information criteria are computed using four free mixture parameters. They are intended for relative model comparison rather than as absolute goodness-of-fit measures.

The Kolmogorov-Smirnov statistic is descriptive because the fitted parameters are estimated from the same catalogue.

The reported outlier_frac is a mixture-model parameter and does not necessarily correspond to a uniquely identifiable population of catastrophic photometric-redshift outliers.

The fitted outlier_mean_shift is converted into Binny’s outlier_mean_scale and outlier_mean_offset by to_binny_photoz_params.

binny.calibration.photoz_fitting.photoz_residual_cdf(x: ndarray, *, scatter_scale: float, outlier_scatter_scale: float, outlier_mean_shift: float, outlier_frac: float) → ndarray#

Evaluate the photometric-redshift residual cumulative distribution.

Parameters:
  • x – One-dimensional array of normalized residual values.

  • scatter_scale – Standard deviation of the core component.

  • outlier_scatter_scale – Standard deviation of the outlier component.

  • outlier_mean_shift – Mean displacement of the outlier component.

  • outlier_frac – Outlier mixture fraction between zero and one.

Returns:

Mixture cumulative probability evaluated at each residual value.

Raises:

ValueError – If the residuals or mixture parameters are invalid.

binny.calibration.photoz_fitting.photoz_residual_pdf(x: ndarray, *, scatter_scale: float, outlier_scatter_scale: float, outlier_mean_shift: float, outlier_frac: float, return_components: bool = False) → ndarray | tuple[ndarray, dict[str, ndarray]]#

Evaluate the photometric-redshift residual probability density.

Evaluates an analytically normalized Gaussian mixture consisting of a zero-mean core and a displaced outlier component.

Parameters:
  • x – One-dimensional array of normalized residual values.

  • scatter_scale – Standard deviation of the core component.

  • outlier_scatter_scale – Standard deviation of the outlier component.

  • outlier_mean_shift – Mean displacement of the outlier component.

  • outlier_frac – Outlier mixture fraction between zero and one.

  • return_components – Whether to return the individual weighted component densities alongside the total density.

Returns:

Mixture probability density evaluated at the supplied residuals. If return_components is True, also returns a dictionary containing the weighted core and outlier densities.

Raises:

ValueError – If the residuals or mixture parameters are invalid.

binny.calibration.photoz_fitting.sample_photoz_errors(z_true: ndarray, *, scatter_scale: float, outlier_scatter_scale: float, outlier_mean_shift: float, outlier_frac: float, seed: int | None = None, rng: Generator | None = None) → ndarray#

Sample observed redshifts from a Gaussian photo-z error model.

Draw normalized residuals from the core and outlier mixture and convert them into observed redshifts.

Parameters:
  • z_true – One-dimensional array of finite true redshifts greater than -1.

  • scatter_scale – Positive, finite core standard deviation.

  • outlier_scatter_scale – Positive, finite tail standard deviation.

  • outlier_mean_shift – Finite tail mean displacement.

  • outlier_frac – Tail mixture fraction between zero and one.

  • seed – Optional random seed.

  • rng – Optional NumPy random number generator. Cannot be supplied together with seed.

Returns:

Sampled observed redshifts with the same shape as z_true.

Raises:

ValueError – If redshifts, mixture parameters, or random number generator arguments are invalid.

Notes

Outlier membership is sampled independently for each galaxy. Observed redshifts are not clipped or truncated.

binny.calibration.photoz_fitting.to_binny_photoz_params(fit: dict) → dict[str, float]#

Convert fitted parameters to Binny’s photometric-redshift assignment model.

Converts the fitted Gaussian-mixture parameters into the complete parameter dictionary expected by Binny’s photometric-redshift assignment model.

The fitted core has zero mean and scatter scatter_scale. The displaced tail has scatter outlier_scatter_scale, displacement outlier_mean_shift, and mixture fraction outlier_frac.

The tail displacement is represented in Binny using

outlier_mean_scale = 1 + outlier_mean_shift outlier_mean_offset = -outlier_mean_shift.

Parameters:

fit – Dictionary containing scatter_scale, outlier_scatter_scale, outlier_mean_shift, and outlier_frac. Additional diagnostic entries are ignored.

Returns:

  • mean_scale: Core mean scale, fixed at 1.

  • mean_offset: Core mean offset, fixed at 0.

  • scatter_scale: Fitted core scatter.

  • outlier_frac: Fitted tail mixture fraction.

  • outlier_mean_scale: Tail mean scale, equal to 1 + outlier_mean_shift.

  • outlier_mean_offset: Tail mean offset, equal to -outlier_mean_shift.

  • outlier_scatter_scale: Fitted tail scatter.

Return type:

Dictionary containing the photometric-redshift assignment parameters

Raises:
  • ValueError – If outlier_mean_shift is less than or equal to -1, making the corresponding mean scale non-positive.

  • KeyError – If a required fitted parameter is missing.

Notes

The conversion preserves the fitted normalized residual model

epsilon = (z_obs - z_true) / (1 + z_true)

when used with Binny’s photometric-redshift assignment probability function.