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()andinit_from_value(), in unconstrained coordinatesxof sizedim;MAP on the simplex and the KKT gap, when
has_density:log_density(),grad_log_density()andunbounded_density(); for rates withpseudocounts, MAP’s mix-SQP also readsb, the Gamma rate parameters;NUTS’s partial non-centring, when
has_centering:gamma,n_active,with_centering,recentre,to_noncentred,log_scalesandtheta_from_x, which the logistic family’s bound priors share;Gibbs, EM and mean-field VB, when
conjugate:posterior_sample(),map_from_counts()and thevb_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
dimand the flags below, and implements the methods its flags promise; the others raiseNotImplementedError. Values (fractions or rates) have shape(K,), or(n, K)forndraws.- Parameters:
- priorPopulationPrior
The prior.
- binsBins
The bins it is bound to.
Methods
__init__(prior, bins)forward(x)Map unconstrained
xto logits / log-rateseta(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.
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)ndraws of the fractions or rates from the prior, shape(n, K).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)ndraws from the variational posteriorstate, shape(n, K).vb_update(counts[, exposure])Mean-field VB's state given
counts: the parameters of the variational posterior, read by the othervb_methods.Attributes
whether the methods Gibbs, EM and mean-field VB use are implemented
whether it has partial non-centring weights (
with_centering; the logistic family)whether the methods MAP on the simplex and the KKT gap use are implemented
"fractions"(a distribution on the simplex) or"rates"counts its mode adds to the data's,
alpha - 1(Dirichlet) ora - 1(GammaRates), as in EM's M-step;Nonefor other priorssize 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
xto logits / log-rateseta(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_etais the gradient of the log-likelihood with respect toeta; the returned gradient isd/dx [log prior(x) + loglike].- Parameters:
- xndarray (dim,)
Unconstrained point.
- cacheobject
The cache
forward()returned forx.- 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
Asuch thatV.T @ A @ Vis that Hessian for any basisVof the log-ratios, on the binson. Priors withpseudocounts, 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 ofx: 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 returnsunbounded_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) ora - 1(GammaRates), as in EM’s M-step;Nonefor other priors
- sample(n, rng)[source]¶
ndraws 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]¶
ndraws from the variational posteriorstate, 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 othervb_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.