mixle.stats.combinator.optional module¶
Create, estimate, and sample from an Optional distribution.
Defines the OptionalDistribution, OptionalSampler, OptionalAccumulatorFactory, OptionalAccumulator, OptionalEstimator, and the OptionalDataEncoder classes for use with mixle.
This distribution assigns a probability (p) to data being missing. With probability (1-p) the data is assumed to come from a base distribution set by the user.
The OptionalDistribution allows for potentially missing data. The value p (the probability of being missing) must be specified to sample from the distribution.
- class OptionalDistribution(dist, p=None, missing_value=None, name=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionMixture-style wrapper that models missing observations explicitly.
- Parameters:
- compute_capabilities()[source]
- set_prior(prior)[source]
Distribute the joint prior
(p_prior, dist_prior)to the missing probability and base dist.prior=Noneis a no-op (point model, existing behavior byte-identical). Otherwise the first element is a conjugate Beta prior onp(caching the digamma expectations used byexpected_log_density) and the second is pushed to the base distribution’sset_prior.
- expected_log_density(x)[source]
Posterior-expected log-density
E_q[log p(x)]atx.With a conjugate Beta prior on
pthe expectation overpis available in closed form via digamma terms (missing =>da - dab; observed =>db - dab + dist.expected_log_density(x)); otherwise this falls back to the plug-inlog_density.- Parameters:
x (T)
- Return type:
- seq_expected_log_density(x)[source]
Vectorized posterior-expected log-density; falls back to
seq_log_densitywithout a prior.
- compute_declaration()[source]
- density(x)[source]
Evaluate the density of the Optional distribution at x.
See log_density() for details.
- Parameters:
x (T) – Observation from base dist or missing value.
- Returns:
Density at x.
- Return type:
- 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]
Evalute the log density of the Optional distribution at x.
If x is a missing value: return log(p) if p is not None, else return 0.0 If x is not the missing_value: if p is not None, return the log_denisty(x) at base dist + log(1-p) else: return
log_density(x).
- Parameters:
x (T) – Observation from base dist or missing value.
- Returns:
Log-density at x.
- Return type:
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for optional encoded data.
- gradient_fit_state(engine, torch, leaves, recurse, tensor_param)[source]
Return distribution-owned state for autograd fitting.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked optional-wrapper parameters for homogeneous mixture kernels.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of optional-wrapper log densities.
- classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]
Return per-component legacy optional-wrapper sufficient statistics.
- to_fisher(**kwargs)[source]
Fisher view for the optional/missing-gate.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
OptionalSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
OptionalEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
OptionalDataEncoder
- enumerator()[source]
Returns an OptionalEnumerator iterating the support (including the missing value) in descending probability order.
- Return type:
OptionalEnumerator
- class OptionalEnumerator(dist)[source]
Bases:
DistributionEnumerator- Parameters:
dist (OptionalDistribution)
- class OptionalSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (OptionalDistribution)
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.- Parameters:
size (int | None)
- class OptionalEstimatorAccumulator(accumulator, missing_value=None, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulator- Parameters:
- update(x, weight, estimate)[source]
- Parameters:
x (T)
weight (float)
estimate (OptionalDistribution)
- Return type:
None
- initialize(x, weight, rng)[source]
- Parameters:
x (T)
weight (float)
rng (RandomState)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident E-step: missing/observed mass is summed on the active engine and the observed child accumulator is routed through the engine. Matches seq_update.
- seq_initialize(x, weights, rng)[source]
- combine(suff_stat)[source]
- from_value(x)[source]
- scale(c)[source]
Scale missing/observed weights and delegate observed statistics.
- Parameters:
c (float)
- Return type:
OptionalEstimatorAccumulator
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- 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.
- acc_to_encoder()[source]
- Return type:
OptionalDataEncoder
- class OptionalEstimatorAccumulatorFactory(estimator, missing_value=None, keys=None, name=None)[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- Return type:
OptionalEstimatorAccumulator
- class OptionalEstimator(estimator, missing_value=None, est_prob=False, pseudo_count=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimator- Parameters:
- accumulator_factory()[source]
- Return type:
OptionalEstimatorAccumulatorFactory
- get_prior()[source]
Return the joint prior as
(p_prior, dist_prior)from this estimator and the base estimator.
- set_prior(prior)[source]
Distribute
(p_prior, dist_prior)to this estimator’spprior and the base estimator.prior=Noneis a no-op (empirical/pseudo-count path stays byte-identical). The first element is a conjugate Beta prior onp; the second is pushed to the base estimator viaset_prior.
- model_log_density(model)[source]
Sum the Beta-prior log-density at
pand the base estimator’s term (ELBO global term).- Parameters:
model (OptionalDistribution)
- Return type:
- class OptionalDataEncoder(encoder, missing_value=None)[source]
Bases:
DataSequenceEncoder- Parameters:
encoder (DataSequenceEncoder)
missing_value (Any)
- OptionalAccumulator
alias of
OptionalEstimatorAccumulator
- OptionalAccumulatorFactory
alias of
OptionalEstimatorAccumulatorFactory
- class OptionalFisherView(dist)[source]
Bases:
EmpiricalMetricFixedFisherView- Parameters:
dist (Any)
- mean_statistics(stats=None, model=True, **kwargs)[source]
- fisher_information(stats=None, diagonal=False, ridge=1.0e-8, **kwargs)[source]
Empirical Fisher approximation from per-observation statistic vectors.
- fisher_vectors(stats=None, metric='diagonal', center=None, fisher=None, ridge=1.0e-8, **kwargs)[source]
Return centered/whitened sufficient-statistic vectors.
metric=’identity’ returns centered statistics, ‘diagonal’ divides by per-coordinate Fisher standard deviations, and ‘full’ applies an empirical full-matrix whitening transform.