mixle.stats.univariate.discrete.bernoulli module¶
Create, estimate, enumerate, and sample from a Bernoulli distribution.
Data type: bool or values in {0, 1}. The distribution has success probability p and log-density log(p) for True/1 and log(1-p) for False/0.
Reference: Johnson, Kemp & Kotz, Univariate Discrete Distributions (3rd ed., Wiley, 2005).
- class BernoulliDistribution(p, name=None, keys=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionBernoulli distribution over {False, True} with success probability p.
- Parameters:
- classmethod compute_capabilities()[source]
- classmethod compute_declaration()[source]
- static exp_family_sufficient_statistics(x, engine)[source]
Return Bernoulli sufficient statistics for generated scoring.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Bernoulli sufficient statistics in accumulator order.
- static exp_family_natural_parameters(params, engine)[source]
Return Bernoulli natural parameters for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return Bernoulli log partition for generated scoring.
- set_prior(prior)[source]
Attach a Beta parameter prior and precompute conjugate-prior expectations.
With a Beta(a, b) prior on the success probability
pthis caches the digamma terms so thatexpected_log_densityevaluates the variational Bayes expectationE_q[log p(x | p)]viaE[log p] = digamma(a) - digamma(a+b)andE[log(1-p)] = digamma(b) - digamma(a+b). Any other prior (includingNone) leaves the distribution a plain point model.- Parameters:
prior (SequenceEncodableProbabilityDistribution | None)
- Return type:
None
- expected_log_density(x)[source]
Variational expectation
E_q[log p(x | p)]under the Beta prior.Falls back to the plug-in
log_density(x)when no conjugate prior is attached.
- seq_expected_log_density(x)[source]
Vectorized
expected_log_densityover sequence-encoded observations.
- density(x)[source]
Return the probability density or mass at a single observation.
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- static backend_log_density_from_params(x, p, engine)[source]
Engine-neutral Bernoulli log-mass from explicit parameters.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded data.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked Bernoulli parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of Bernoulli log masses.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked Bernoulli sufficient statistics using engine-resident arrays.
- to_fisher(**kwargs)[source]
Return the Bernoulli’s count-family Fisher view.
- cdf(x)[source]
Cumulative distribution function P(X <= x) over {0, 1}.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
BernoulliSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
BernoulliEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
BernoulliDataEncoder
- enumerator()[source]
Return an enumerator over the distribution support when available.
- Return type:
BernoulliEnumerator
- class BernoulliEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate False/True in descending probability order.
- Parameters:
dist (BernoulliDistribution)
- class BernoulliSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid Bernoulli observations.
- Parameters:
dist (BernoulliDistribution)
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 BernoulliAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted success and observation counts.
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- 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:
BernoulliDataEncoder
- class BernoulliAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for BernoulliAccumulator.
- make()[source]
- Return type:
BernoulliAccumulator
- class BernoulliEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimate a Bernoulli distribution from weighted success counts.
- Parameters:
- accumulator_factory()[source]
- Return type:
BernoulliAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model’s success probability under the Beta prior (ELBO global term).
- Parameters:
model (BernoulliDistribution)
- Return type: