mixle.stats.latent.indian_buffet_process module¶
Indian buffet process finite truncation with variational Bayes estimation.
This module implements the finite Beta-Bernoulli truncation of the Indian buffet process (IBP). With K features and concentration alpha,
pi_k ~ Beta(alpha / K, 1) z_nk | pi_k ~ Bernoulli(pi_k)
Rows z_n may be supplied either as dense binary vectors of length K or as sparse lists/sets of active feature indices. Estimation is variational Bayes with a factorized posterior q(pi_k) = Beta(a_k, b_k). The local variational factor for an observed feature row is deterministic, so the accumulator gathers weighted feature-use counts and the estimator performs the conjugate Beta update.
- class IndianBuffetProcessDistribution(num_features, alpha=1.0, beta_params=None, feature_probs=None, min_prob=1.0e-128, name=None, keys=None, data_format='auto')[source]
Bases:
SequenceEncodableProbabilityDistributionFinite-truncated Indian buffet process over binary feature rows.
- Parameters:
num_features (int) – Truncation level K.
alpha (float) – IBP concentration parameter.
beta_params (Sequence[Sequence[float]] | ndarray | None) – Optional (K, 2) variational Beta parameters for q(pi_k). If omitted, the prior Beta(alpha / K, 1) is used.
feature_probs (Sequence[float] | ndarray | None) – Optional plug-in feature probabilities. When supplied without beta_params, a lightweight Beta posterior with matching mean is created for expected-log-density calls.
min_prob (float) – Minimum plug-in probability used when feature_probs are given.
name (str | None) – Optional distribution name.
keys (str | None) – Optional key for tying sufficient statistics.
data_format (str) – ‘dense’, ‘sparse’, or ‘auto’ input interpretation.
- classmethod compute_capabilities()[source]
- classmethod compute_declaration()[source]
- density(x)[source]
Return the probability density or mass at a single observation.
- density_semantics()[source]
What
log_densityreturns relative to the true log-density (default: exact).Override to declare that this distribution’s
log_densityis a variational lower bound (ELBO), an upper bound, or an approximation rather than the exactlog p(x). This is surfaced as theExactDensitycapability and noted inmixle.describe(), so code that needs an exact likelihood canrequire(x, ExactDensity)instead of silently trusting a bound.
- log_density(x)[source]
Plug-in log-density of one feature row using E_q[pi_k].
- expected_log_density(x)[source]
VB expected log-density E_q[log p(z | pi)] for one observed row.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized plug-in log-density for encoded feature rows.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked finite-IBP parameters for equal-feature mixtures.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of finite-IBP component log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return component-stacked legacy
(feature_counts, total_count, alpha)statistics.
- seq_expected_log_density(x)[source]
Return vectorized expected log-density values for encoded observations.
- seq_local_elbo(x)[source]
Per-row VB contribution; rows are observed, so there is no local entropy.
- enumerator()[source]
Enumerate feature rows in descending probability order.
The plug-in row density factorizes over features –
log p(z) = sum_k [z_k log pi_k + (1-z_k) log(1-pi_k)]withpi_k = E_q[feature_probs]– so the truncated IBP row is a product of independent Bernoulli features and enumerates by best-first over the per-feature supports (the same structure as the Erdos-Renyi graph). Rows are emitted in the configureddata_format(a dense 0/1 list, or a sorted list of active feature indices when sparse), each carrying its exactlog_density.- Return type:
DistributionEnumerator
- to_fisher(**kwargs)[source]
Return this distribution’s own Fisher view.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
IndianBuffetProcessSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
IndianBuffetProcessEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
IndianBuffetProcessDataEncoder
- class IndianBuffetProcessEnumerator(dist)[source]
Bases:
DistributionEnumerator- Parameters:
dist (IndianBuffetProcessDistribution)
- class IndianBuffetProcessSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for finite-truncated IBP feature rows.
- Parameters:
dist (IndianBuffetProcessDistribution)
seed (int | None)
- sample(size=None)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class IndianBuffetProcessAccumulator(num_features, alpha=1.0, keys=None, data_format='auto')[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulates weighted feature-use counts for the IBP VB update.
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (Any)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident accumulation of weighted feature-use counts (numpy or torch).
The weighted feature counts are reduced via a weight-vector / binary-matrix product on the active engine; the alpha metadata is host bookkeeping. Matches seq_update.
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- from_value(x)[source]
- scale(c)[source]
Scale additive counts while preserving the IBP concentration metadata.
- Parameters:
c (float)
- Return type:
IndianBuffetProcessAccumulator
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
IndianBuffetProcessDataEncoder
- class IndianBuffetProcessAccumulatorFactory(num_features, alpha=1.0, keys=None, data_format='auto')[source]
Bases:
StatisticAccumulatorFactoryFactory for IBP accumulators.
- make()[source]
- Return type:
IndianBuffetProcessAccumulator
- class IndianBuffetProcessEstimator(num_features, alpha=1.0, pseudo_count=None, suff_stat=None, estimate_alpha=True, min_alpha=1.0e-12, max_alpha=1.0e12, min_prob=1.0e-128, name=None, keys=None, data_format='auto')[source]
Bases:
ParameterEstimatorVariational Bayes estimator for the finite-truncated IBP.
pseudo_count follows the convention used by other mixle.stats Bernoulli estimators: if suff_stat is supplied, it is treated as a prior probability vector and re-weighted by pseudo_count; otherwise pseudo_count is centered at the IBP prior mean alpha / (alpha + K).
- Parameters:
- accumulator_factory()[source]
- Return type:
IndianBuffetProcessAccumulatorFactory
- estimate(nobs, suff_stat)[source]
- class IndianBuffetProcessDataEncoder(num_features, data_format='auto')[source]
Bases:
DataSequenceEncoderEncode dense or sparse IBP rows as a dense boolean matrix.