Reference / binference.Selection

binference.Selection

class binference.Selection(values, mode, *, scale='probability')[source]

Per-bin detection probability (fractions) or exposure (rates), with its mode.

Parameters:
valuesarray_like (K,)

Strictly positive detection probabilities or exposures per bin.

mode{“on_measurement”, “on_truth”}

How selection enters the likelihood (see the module documentation); combined() builds the third mode.

scale{“probability”, “exposure”, “relative”}

Whether the values are detection probabilities, exposures or relative values. "probability" (default): values are detection probabilities in (0, 1]. "exposure": absolute positive values, such as the exposure of a rate model. "relative": only ratios between bins are known, which suffices for fractions but not for rates or totals.

Attributes:
valuesndarray (K,)

\(S_k\), the probability that an object in bin k is selected.

rowndarray (K,) or None

The factor applied to the likelihood columns (\(S_k\) on the truth, \(S^{\rm truth}_k\) combined, None on the measurement).

basisBasis or None

Set when the values are per component of a Basis (\(\psi_k\)-weighted averages) rather than per bin; None on an identity basis, whose components are the bins.

injectionstuple or None

For a selection measured by injections (from_injections()), a named tuple (injected, recovered, prior, smooth): counts per bin, the Beta prior and the step scale of the smooth model (or None). The selection is then uncertain, and inferred jointly with the population; values are its posterior means given the injections alone (independent Betas) or the mode of \(\mathrm{logit}\,S\) (smooth).

__init__(values, mode, *, scale='probability')[source]

Methods

__init__(values, mode, *[, scale])

combined(on_truth, on_measurement[, bins, ...])

Selection on the truth and on the measurement at once.

fixed()

This selection taken as known at its values, a plug-in that ignores their uncertainty.

from_function(func, bins, mode, *[, scale, ...])

A selection given as a function of the true value, averaged over each bin.

from_injections(z, detected, bins, mode, *)

A selection measured by injecting objects of known true value z.

sample(n, *[, rng])

(n, K) posterior draws of \(S\) given the injections alone (independent Betas), floored at S_FLOOR so that none underflows to zero.

Attributes

basis

injections

row

scale

values

K

mode

"on_measurement", "on_truth" or "combined", from row.

classmethod combined(on_truth, on_measurement, bins=None, *, scale='probability', tol=1e-06, max_depth=20)[source]

Selection on the truth and on the measurement at once.

Parameters:
on_trutharray_like (K,) or callable

\(S^{\rm truth}\), the detection probability given the true value.

on_measurementarray_like (K,) or callable

\(P(d \in A \mid z)\), the probability that the measured data pass the cut given the true value.

binsBins or Basis, optional

Bins over which callable parts are averaged, in the measure coordinate; needed when either part is a callable (when both are, the product is averaged as a whole). With a Basis the result is per component, as for from_function().

scale{“probability”, “exposure”, “relative”}, optional

Whether the values of both parts are detection probabilities, exposures or relative values, as for Selection.

tolfloat, optional

Quadrature tolerance for callable parts (see from_function()).

max_depthint, optional

Most refinements of the adaptive quadrature for callable parts.

fixed()[source]

This selection taken as known at its values, a plug-in that ignores their uncertainty.

classmethod from_function(func, bins, mode, *, scale='probability', tol=1e-06, max_depth=20)[source]

A selection given as a function of the true value, averaged over each bin.

func(z) returns the detection probability (or exposure) at points z (shape (M,) in 1-D, (M, ndim) on grids, labels for categories). Each bin takes its average in the measure coordinate, the within-bin shape of the population, by adaptive quadrature to tol relative to the largest bin average. A single jump inside a bin is found wherever it lies (a window narrower than the spacing of the points can still fall between them: put bin edges at its ends), and each refinement halves the cell that holds it, so reaching tol takes about \(\log_2(1/\mathrm{tol})\) refinements, 20 for the default tol, which the default max_depth allows; for a smaller tol raise max_depth, or put a bin edge at the jump. With a Basis for bins, the averages are taken on its quadrature cells and weighted by each component.

Parameters:
funccallable

Detection probability or exposure at points z.

binsBins or Basis

Bins over which func is averaged.

mode{“on_measurement”, “on_truth”}

How selection enters the likelihood, as for Selection.

scale{“probability”, “exposure”, “relative”}, optional

Whether the values are detection probabilities, exposures or relative values, as for Selection.

tolfloat, optional

Quadrature tolerance relative to the largest bin average.

max_depthint, optional

Most refinements of the adaptive quadrature; each halves a cell.

classmethod from_injections(z, detected, bins, mode, *, prior=(0.5, 0.5), smooth=None)[source]

A selection measured by injecting objects of known true value z.

Counts the injections per bin, \(n^{\rm inj}_k\), and those recovered, \(n^{\rm rec}_k\); with a \(\mathrm{Beta}(a, b)\) prior each \(S_k\) has the posterior \(\mathrm{Beta}(a + n^{\rm rec}_k, b + n^{\rm inj}_k - n^{\rm rec}_k)\) given the injections alone. With smooth the \(S_k\) are tied together by a second-order random walk on \(y_k = \mathrm{logit}\,S_k\), the log density gaining \(-\sum_k (y_{k-1} - 2y_k + y_{k+1})^2 / 2\tau_S^2\), which lends sparsely injected bins strength from their neighbours.

A selection on the truth is uncertain: NUTS, MAP (space="unconstrained"), Laplace and, for independent Betas and a conjugate prior, Gibbs infer it jointly with the population, where the catalogue informs the ratios between the \(S_k\) (their level comes from the injections); fixed() gives a fixed selection at values, ignoring the uncertainty. A selection on the measurement cannot be inferred (the likelihoods’ cut already fixes it), and neither can a selection with a rates prior; pass fixed() for both.

Parameters:
zarray_like, shape (n,) or (n, ndim)

True values of the injected objects.

detectedarray_like of bool, shape (n,)

Whether each injected object was recovered.

binsBins

The bins of the population.

mode{“on_truth”, “on_measurement”}

As for Selection.

prior(float, float)

Beta prior \((a, b)\) of every \(S_k\), both positive; Jeffreys’ \((1/2, 1/2)\) by default.

smoothfloat, optional

Step scale \(\tau_S\) of the random walk, in logit per bin (1-D bins only); None (default) keeps the \(S_k\) independent.

property mode

"on_measurement", "on_truth" or "combined", from row.

sample(n, *, rng=None)[source]

(n, K) posterior draws of \(S\) given the injections alone (independent Betas), floored at S_FLOOR so that none underflows to zero.

Parameters:
nint

Number of draws.

rngint, numpy.random.Generator, or None

Random number generator.