mixle.stats.univariate.discrete.bernoulli module¶
Bernoulli distributions over binary outcomes.
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]
Describe backend support for generated Bernoulli kernels.
- classmethod compute_declaration()[source]
Return the structured compute declaration for Bernoulli distributions.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Bernoulli sufficient statistics for generated scoring.
- static exp_family_base_measure(x, engine)[source]
Return the Bernoulli base measure:
0on{0, 1},-inffor any other count.The generated engine kernels score
x log p + (1 - x) log(1 - p)with no support of their own, so once the encoder admitted a count outside{0, 1}(Q01-F02) the numba-generated kernel scored-1as a POSITIVE log-probability, and an engine fit reported a finite validation objective the default route refuses (V01-F04). The support lives in the base measure, as the Poisson family’s already does.
- 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.
A boolean encoding holds only valid outcomes. An integer encoding is what the encoder emits when the batch also holds a count outside
{0, 1}, which this law gives probability zero: it scores -inf, as the scalar path does (P02-F03, Q01-F02).
- static backend_log_density_from_params(x, p, engine)[source]
Engine-neutral Bernoulli log-mass from explicit parameters (
-infoutside{0, 1}).
- 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)
- class BernoulliAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted success and observation counts.
- update(x, weight, estimate)[source]
Accumulate weighted success and count statistics for one observation.
- initialize(x, weight, rng)[source]
Initialize statistics from one observation.
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- supported_rows(x)[source]
Encoded rows this law can be fitted on: outcomes in
{0, 1}.The encoder admits an integer outside
{0, 1}so a mixture can encode one batch against every component (P02-F03); this is the predicate a latent model’s initialization consults before handing this component any responsibility. The encoder used to refuse such a count, so an integer-categorical-plus-Bernoulli mixture could not be encoded at all (Q01-F02).
- seq_update(x, weights, estimate)[source]
Accumulate weighted statistics from encoded observations.
A count outside
{0, 1}carrying weight is refused; with zero weight it contributes nothing. Before the encoder admitted such counts this summed the raw value, which never mattered because none could arrive.
- seq_initialize(x, weights, rng)[source]
Initialize statistics from encoded observations.
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge another Bernoulli sufficient-statistic tuple.
- from_value(x)[source]
Replace accumulator contents from a sufficient-statistic tuple.
- acc_to_encoder()[source]
Return the encoder used by this accumulator.
- Return type:
BernoulliDataEncoder
- class BernoulliAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for BernoulliAccumulator.
- make()[source]
Create a fresh Bernoulli accumulator.
- 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 an accumulator factory for Bernoulli sufficient statistics.
- 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:
- estimate(nobs, suff_stat)[source]
Estimate the Bernoulli success probability from weighted counts.
The distribution is defined on the open interval (0, 1), so a sample that is all successes or all failures – whose maximum-likelihood
pis exactly 1 or 0 – is clamped_P_BOUNDARY(1e-12) away from the endpoint. When that clamp binds it is reported on the returned model throughnumerical_repairs()asbernoulli-p-clamped(...), and so throughfit_provenance().repairs; an ordinary fit whosepis already interior records nothing.
- class BernoulliDataEncoder[source]
Bases:
DataSequenceEncoderEncode Bernoulli observations as a boolean numpy array.
- seq_encode(x)[source]
Encode Bernoulli observations as a boolean array, or as integers when a count lies off
{0, 1}.An integer outside
{0, 1}is an outcome this law gives probability zero – a probability question the scorer answers -inf – so it is admitted, and a mixture whose other component owns it can encode the whole batch (P02-F03, Q01-F02). Such a batch keeps its integers, since a boolean cast would turn2into a success. Anything that is not an integer stays refused.