Reference / binference.priors.BoundPrior

binference.priors.BoundPrior

class binference.priors.BoundPrior(prior, bins)[source]

Numerical interface of a prior bound to specific bins.

PopulationPrior.bind() returns one, and the engines use it through the model:

  • NUTS, Laplace and L-BFGS: forward(), log_prior_and_grad() and init_from_value(), in unconstrained coordinates x of size dim;

  • MAP on the simplex and the KKT gap, when has_density: log_density(), grad_log_density() and unbounded_density(); for rates with pseudocounts, MAP’s mix-SQP also reads b, the Gamma rate parameters;

  • NUTS’s partial non-centring, when has_centering: gamma, n_active, with_centering, recentre, to_noncentred, log_scales and theta_from_x, which the logistic family’s bound priors share;

  • Gibbs, EM and mean-field VB, when conjugate: posterior_sample(), map_from_counts() and the vb_ methods, given the number of objects in each bin (drawn by Gibbs, expected by EM and VB);

  • prior draws and simulation: sample().

A subclass sets dim and the flags below, and implements the methods its flags promise; the others raise NotImplementedError. Values (fractions or rates) have shape (K,), or (n, K) for n draws.

Parameters:
priorPopulationPrior

The prior.

binsBins

The bins it is bound to.

__init__(prior, bins)[source]

Methods

__init__(prior, bins)

forward(x)

Map unconstrained x to logits / log-rates eta (and a cache).

grad_log_density(value)

Gradient of log_density() with respect to the fractions/rates.

init_from_value(value)

An unconstrained starting point near the given fractions or rates.

log_density(value)

Log-density of fractions (on the simplex) or rates.

log_prior_and_grad(x, cache, value, d_eta)

Prior log-density in x-space and the total gradient.

log_ratio_curvature(rho, on)

Minus the Hessian of the log density in log-ratio coordinates, beyond pseudo-counts.

long_log_tails()

Whether the prior gives bins without data long tails in the unconstrained coordinates.

map_from_counts(counts[, exposure])

The mode on the simplex (or of the rates) of the fractions given counts, shape (K,): EM's M-step.

posterior_sample(counts, rng[, exposure])

A draw of the fractions or rates given counts, shape (K,): the conditional Gibbs draws from at every iteration.

sample(n, rng)

n draws of the fractions or rates from the prior, shape (n, K).

unbounded_density()

Whether the density of the fractions (or rates) is unbounded at the boundary.

vb_expected_log(state)

Expected logarithms of the fractions or rates under state, shape (K,).

vb_mean(state)

Mean of the fractions or rates under state, shape (K,).

vb_sample(state, n, rng)

n draws from the variational posterior state, shape (n, K).

vb_update(counts[, exposure])

Mean-field VB's state given counts: the parameters of the variational posterior, read by the other vb_ methods.

Attributes

conjugate

whether the methods Gibbs, EM and mean-field VB use are implemented

has_centering

whether it has partial non-centring weights (with_centering; the logistic family)

has_density

whether the methods MAP on the simplex and the KKT gap use are implemented

kind

"fractions" (a distribution on the simplex) or "rates"

pseudocounts

counts its mode adds to the data's, alpha - 1 (Dirichlet) or a - 1 (GammaRates), as in EM's M-step; None for other priors

dim

size of the unconstrained coordinates x

conjugate = False

whether the methods Gibbs, EM and mean-field VB use are implemented

dim

size of the unconstrained coordinates x

forward(x)[source]

Map unconstrained x to logits / log-rates eta (and a cache).

Parameters:
xndarray (dim,)

Unconstrained point.

grad_log_density(value)[source]

Gradient of log_density() with respect to the fractions/rates.

Components with zero value are returned as zero (they are handled as active constraints by callers).

Parameters:
valuendarray (K,)

Fractions or rates.

has_centering = False

whether it has partial non-centring weights (with_centering; the logistic family)

has_density = False

whether the methods MAP on the simplex and the KKT gap use are implemented

init_from_value(value)[source]

An unconstrained starting point near the given fractions or rates.

Exact for the Dirichlet family and rates; for the logistic priors a ridge-regularised fit that shrinks towards the prior mean.

Parameters:
valuendarray (K,)

Fractions or rates.

kind = 'fractions'

"fractions" (a distribution on the simplex) or "rates"

log_density(value)[source]

Log-density of fractions (on the simplex) or rates.

Parameters:
valuendarray (…, K)

Fractions or rates.

log_prior_and_grad(x, cache, value, d_eta)[source]

Prior log-density in x-space and the total gradient.

d_eta is the gradient of the log-likelihood with respect to eta; the returned gradient is d/dx [log prior(x) + loglike].

Parameters:
xndarray (dim,)

Unconstrained point.

cacheobject

The cache forward() returned for x.

valuendarray (K,)

Fractions or rates at x.

d_etandarray (K,)

Gradient of the log-likelihood with respect to eta.

log_ratio_curvature(rho, on)[source]

Minus the Hessian of the log density in log-ratio coordinates, beyond pseudo-counts.

A matrix A such that V.T @ A @ V is that Hessian for any basis V of the log-ratios, on the bins on. Priors with pseudocounts, or without conjugate updates, add nothing here.

Parameters:
rhondarray (n,)

Fractions of the bins on, summing to one.

onndarray (K,) of bool

The bins in the support.

long_log_tails()[source]

Whether the prior gives bins without data long tails in the unconstrained coordinates.

As a group of bins empties, its total fraction (or rate) \(s\) goes to zero and the density of x, the coordinates NUTS samples, falls like \(s^{a} = e^{a \log s}\): a tail of scale \(1/a\) in \(\log s\), which NUTS needs long trajectories to cross. The tails count as long when some \(a < 1\), the threshold of a Dirichlet’s \(\alpha_k < 1\). The exponent \(a\) is the density’s exponent at the boundary plus one for each bin in the group, from the Jacobian of x: for one bin of a Dirichlet, \(\alpha_k - 1\) becomes \(\alpha_k\). Where single bins have the longest tails, as for a Dirichlet or Gamma rates, the tails are long exactly when the density is unbounded, so this default returns unbounded_density(); a prior whose groups of several bins have their own exponents, such as a tree, overrides it.

map_from_counts(counts, exposure=None)[source]

The mode on the simplex (or of the rates) of the fractions given counts, shape (K,): EM’s M-step.

Parameters:
countsndarray (K,)

Number of objects in each bin.

exposurendarray (K,), optional

Exposure of each bin, for rates (None: one); fractions ignore it.

posterior_sample(counts, rng, exposure=None)[source]

A draw of the fractions or rates given counts, shape (K,): the conditional Gibbs draws from at every iteration.

Parameters:
countsndarray (K,)

Number of objects in each bin.

rngnumpy.random.Generator

Random number generator.

exposurendarray (K,), optional

Exposure of each bin, for rates (None: one); fractions ignore it.

pseudocounts = None

counts its mode adds to the data’s, alpha - 1 (Dirichlet) or a - 1 (GammaRates), as in EM’s M-step; None for other priors

sample(n, rng)[source]

n draws of the fractions or rates from the prior, shape (n, K).

Parameters:
nint

Number of draws.

rngnumpy.random.Generator

Random number generator.

unbounded_density()[source]

Whether the density of the fractions (or rates) is unbounded at the boundary.

Then its mode can lie on the boundary (components exactly zero).

vb_expected_log(state)[source]

Expected logarithms of the fractions or rates under state, shape (K,).

Parameters:
stateobject

What vb_update() returned.

vb_mean(state)[source]

Mean of the fractions or rates under state, shape (K,).

Parameters:
stateobject

What vb_update() returned.

vb_sample(state, n, rng)[source]

n draws from the variational posterior state, shape (n, K).

Parameters:
stateobject

What vb_update() returned.

nint

Number of draws.

rngnumpy.random.Generator

Random number generator.

vb_update(counts, exposure=None)[source]

Mean-field VB’s state given counts: the parameters of the variational posterior, read by the other vb_ methods.

Parameters:
countsndarray (K,)

Expected number of objects in each bin.

exposurendarray (K,), optional

Exposure of each bin, for rates (None: one); fractions ignore it.