Reference / binference.infer

binference.infer

binference.infer(likelihoods, prior=None, *, bins=None, selection=None, selection_mode=None, method='auto', n_samples=None, rng=None, progress=False, check=True, **engine_kwargs)[source]

Infer a population distribution from per-object likelihoods in one call.

Parameters:
likelihoodsLikelihoods, PopulationModel, or array_like (N, K)

Per-object likelihoods over bins (see Likelihoods for converting posteriors, samples, Gaussian measurements, response matrices or classifier outputs). A raw array is treated as likelihoods over categorical bins (or bins).

priorPopulationPrior, optional

Population prior (default Dirichlet(1.0)). Use GammaRates to infer rates.

binsBins, optional

Bins for a raw array input.

selectionarray_like (K,), callable or Selection, optional

Detection probability (fractions) or exposure (rates) per bin.

selection_mode{“on_measurement”, “on_truth”}, optional

Required with an array or callable selection (see Selection).

methodstr or Engine subclass

Inference engine: "auto", "nuts", "gibbs", "map", "em", "laplace", "vb" or "bootstrap", or a class of your own. "auto" (default) picks Gibbs, Laplace or NUTS from rules set by a benchmark of the engines (Auto); its options (Auto) go in engine_kwargs, and the choice and its reason are in result.info["auto_method"] and result.info["auto_reason"]. "nuts" samples the exact posterior with any prior; "em" is MAP by EM on the simplex (algorithm="em") rather than mix-SQP; it needs a conjugate prior, and a jointly inferred selection or response runs L-BFGS.

n_samplesint, optional

Draws per chain (NUTS, Gibbs, and "auto") or in total (Laplace, VB, bootstrap). Under "auto" it is a minimum: a Gibbs run may be extended past it until it mixes, and Gibbs still gives way when it would need more than max_gibbs_draws (see Auto).

rngint, numpy.random.Generator, or None

Random number generator or seed (SPEC 7).

progressbool

Show progress bars.

checkbool

Warn about problems after the fit: convergence for NUTS and Gibbs, and the inputs for every engine: the Monte Carlo noise of sample likelihoods and, for results with draws, binned counts the response does not describe.

**engine_kwargs

Passed to the engine (e.g. n_chains, n_warmup, target_accept for NUTS; see binference.engines).

Returns:
PopulationResult or PointEstimate

Posterior draws, or the MAP estimate for method="map" and "em".

Examples

>>> import numpy as np, binference
>>> rng = np.random.default_rng(1)
>>> bins = binference.Bins.linear(0.0, 1.0, 5)
>>> z = rng.uniform(0, 1, 300)
>>> lk = binference.Likelihoods.from_gaussian(z + rng.normal(0, 0.05, 300), bins, 0.05)
>>> res = binference.infer(lk, rng=0)
>>> res.mean.shape
(5,)