Reference / binference.Density

binference.Density

class binference.Density(*, flat=None, values=None, per_volume=False, func=None, dist=None)[source]

A fixed probability density over the bin coordinate.

Build one with flat(), from_masses(), from_densities(), from_function() or from_distribution(). Wherever a density is expected, the string "flat", a callable, a distribution object and an array are also accepted (see coerce()).

__init__(*, flat=None, values=None, per_volume=False, func=None, dist=None)[source]

Methods

__init__(*[, flat, values, per_volume, ...])

coerce(value[, representation, name])

A Density from a density, "flat", a callable, a distribution or an array.

flat([coordinate])

Uniform in the measure coordinate u of each axis (default) or in z.

from_densities(densities)

Density per unit z-volume in each bin; masses are densities times volumes.

from_distribution(dist)

A distribution object with cdf, logpdf or pdf (e.g. frozen scipy.stats).

from_function(func)

A callable density of z, averaged over each bin by quadrature.

from_masses(masses)

Probability mass per bin (normalised on use).

masses(bins, *[, tol, max_depth, n_sub])

Normalised probability mass of each bin, shape (K,).

classmethod coerce(value, representation=None, name='interim')[source]

A Density from a density, "flat", a callable, a distribution or an array.

An array is read as masses or densities by representation (which applies to arrays only). Without it the array is accepted only on bins where both readings agree (categories and bins of equal volume); masses() raises otherwise.

Parameters:
valueDensity, “flat”, callable, distribution or array_like (K,)

What to read as a density.

representation{“mass”, “density”}, optional

Whether an array holds masses or densities.

namestr, optional

Name of the argument, used in error messages.

classmethod flat(coordinate='measure')[source]

Uniform in the measure coordinate u of each axis (default) or in z.

On linear bins the two agree. On log bins, flat in u gives equal mass per decade. On categories, every category has equal mass.

Parameters:
coordinate{“measure”, “z”}, optional

Flat in the measure coordinate u or in z.

classmethod from_densities(densities)[source]

Density per unit z-volume in each bin; masses are densities times volumes.

Parameters:
densitiesarray_like (K,)

Non-negative density per bin.

classmethod from_distribution(dist)[source]

A distribution object with cdf, logpdf or pdf (e.g. frozen scipy.stats).

On 1-D grids a cdf gives exact bin masses; otherwise the density is averaged over each bin by quadrature.

Parameters:
distobject

A distribution with cdf, logpdf or pdf.

classmethod from_function(func)[source]

A callable density of z, averaged over each bin by quadrature.

On grids func receives points of shape (M,) (1-D) or (M, ndim); on categories it receives the category labels.

Parameters:
funccallable

Non-negative density of z.

classmethod from_masses(masses)[source]

Probability mass per bin (normalised on use).

Parameters:
massesarray_like (K,)

Non-negative mass per bin.

masses(bins, *, tol=0.001, max_depth=12, n_sub=None)[source]

Normalised probability mass of each bin, shape (K,).

Callables and distributions without a usable cdf are integrated by adaptive quadrature to tol (at most max_depth refinements), or with n_sub fixed Gauss-Legendre points per bin and axis (see Likelihoods.from_density).

Parameters:
binsBins

The bins.

tolfloat, optional

Relative tolerance of the adaptive quadrature over each bin.

max_depthint, optional

Most refinements of the adaptive quadrature.

n_subint, optional

Fixed Gauss-Legendre points per bin and axis in place of adaptive quadrature.