mixle.stats.univariate.discrete package¶
Discrete univariate distribution namespace.
Every class implemented under this package is re-exported here so the namespace exposes the API it
actually contains (MXR-080-1231). Before this, the package exposed only its submodule names, so
from mixle.stats.univariate.discrete import BernoulliAccumulator failed while from mixle.stats import BernoulliAccumulator
worked – the subpackage advertised modules and hid the classes inside them.
mixle.stats remains the consolidated public API and re-exports these same objects; this package
is the narrower namespace for callers who want only discrete univariate families.
- 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 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.
- 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 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 BernoulliSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid Bernoulli observations.
- Parameters:
dist (BernoulliDistribution)
seed (int | None)
- class BetaBinomialAccumulator(n, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted first and second moments for beta-binomial estimation.
- update(x, weight, estimate)[source]
Accumulate weighted first and second moments for one count.
- initialize(x, weight, rng)[source]
Initialize statistics from one count.
- Parameters:
x (int)
weight (float)
rng (RandomState | None)
- Return type:
None
- supported_rows(x)[source]
Encoded rows this law can be fitted on: counts in
{0, ..., n}.The encoder admits a count outside that range so a mixture can encode one batch against every component (P02-F03); this is the predicate that keeps it out of the moments and the one a latent model’s initialization consults before handing this component any responsibility (Q01-F02).
- Return type:
- seq_update(x, weights, estimate)[source]
Accumulate weighted moments from encoded counts.
A count outside
{0, ..., n}carrying weight is refused; with zero weight it contributes nothing.
- seq_initialize(x, weights, rng)[source]
Initialize statistics from encoded counts.
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge another beta-binomial sufficient-statistic tuple.
- value()[source]
Return accumulated sum, second moment sum, and count.
- 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:
BetaBinomialDataEncoder
- class BetaBinomialAccumulatorFactory(n, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for BetaBinomialAccumulator.
- make()[source]
Create a fresh beta-binomial accumulator.
- Return type:
BetaBinomialAccumulator
- class BetaBinomialDataEncoder(n)[source]
Bases:
DataSequenceEncoderEncode beta-binomial counts as a float array.
- Parameters:
n (int)
- seq_encode(x)[source]
Encode exact counts, admitting counts outside the support
{0, ..., n}.Being an exact integer is a type contract and stays enforced; being inside
{0, ..., n}is a probability question the scorer answers -inf, so a mixture whose other component owns such a count can encode the whole batch (P02-F03, Q01-F02).
- class BetaBinomialDistribution(n, a, b, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionBeta-binomial distribution over
{0, ..., n}with shape parametersa, b > 0.- density(x)[source]
Return the probability mass at a single count
x.
- log_density(x)[source]
Return the log probability mass at
x(-infoutside{0, ..., n}or non-integer – a fractional count is not a valid beta-binomial outcome).
- seq_log_density(x)[source]
Return vectorized log-mass for an array of counts (
-infoutside{0, ..., n}or non-integer – a fractional count is not a valid beta-binomial outcome).
- cdf(x)[source]
Cumulative distribution function P(X <= x): a log-space partial sum of the pmf over {0..n}.
- quantile(q)[source]
Inverse CDF F^{-1}(q) over {0..n}: the smallest count whose cdf reaches
q(search on the cdf).
- enumerator()[source]
Returns BetaBinomialEnumerator iterating the support in descending probability order.
- Return type:
BetaBinomialEnumerator
- sampler(seed=None)[source]
Return a sampler for drawing counts from this distribution.
- Parameters:
seed (int | None)
- Return type:
BetaBinomialSampler
- estimator(pseudo_count=None)[source]
Return a method-of-moments estimator for
a, bat the fixed number of trialsn.- Parameters:
pseudo_count (float | None)
- Return type:
BetaBinomialEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
BetaBinomialDataEncoder
- class BetaBinomialEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate the bounded beta-binomial support {0, …, n} in descending probability order.
Unlike the binomial, the beta-binomial pmf is not always unimodal (it is U-shaped for
a < 1, b < 1), so a two-pointer walk from a mode is not safe here; the finite support is materialized and sorted instead (the CategoricalEnumerator pattern), with value-order tie-breaks.- Parameters:
dist (BetaBinomialDistribution)
- class BetaBinomialEstimator(n, pseudo_count=None, suff_stat=None, min_conc=1.0e-6, max_conc=1.0e8, name=None, keys=None)[source]
Bases:
ParameterEstimatorMethod-of-moments estimator for the beta-binomial shape parameters.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for beta-binomial moments.
- Return type:
BetaBinomialAccumulatorFactory
- class BetaBinomialSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw counts as
p ~ Beta(a, b)thenk ~ Binomial(n, p).- Parameters:
dist (BetaBinomialDistribution)
seed (int | None)
- class BinomialAccumulator(max_val=None, min_val=None, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted Binomial count, sum, and observed support bounds.
- update(x, weight, estimate)[source]
Accumulates Binomial sufficient statistics for weighted single observation.
Add x*weight to attribute sum, and increase the count by weight.
- initialize(x, weight, rng)[source]
Initialize BinomialAccumulator sufficient statistics for one weighted observation.
- supported_rows(x)[source]
Encoded rows this accumulator can be fitted on: counts inside its DECLARED bounds.
The encoder admits counts outside
[min_val, max_val]so a mixture can encode one batch against every component, andseq_updaterefuses such a count only when it carries weight. Initialization draws responsibilities before any model exists, so without this predicate a mixture handed this component a share of the rows its bounds exclude and the refusal blamed the data – an integer-categorical-plus-binomial fit failed with “observations with positive weight must be at most 10” (Q01-F02). The bounds are the declared ones, never the observed running range, for the reason__init__records.
- seq_update(x, weights, estimate)[source]
Accumulates Binomial sufficient statistics for encoded sequence.
- Parameters:
x (E) – Encoded sequence of observations.
weights (np.ndarray) – Numpy array of floats for weighting each observation.
estimate (Optional[BinomialDistribution]) – Previous estimate of BinomialDistribution obtained from prior data.:
- Returns:
None
- Return type:
None
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident accumulation of count/sum statistics (numpy or torch).
The weighted sum and count reductions run on the active engine; the scalar min/max support bounds remain host bookkeeping. Matches seq_update.
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of BinomialAccumulator sufficient statistics with weights.
Calls seq_update().
- Parameters:
x (E) – Encoded sequence of observations.
weights (np.ndarray) – Numpy array of floats for weighting each observation.
rng (Optional[RandomState]) – RandomState not needed. No randomness in initialization.
- Returns:
None
- Return type:
None
- combine(suff_stat)[source]
Combine the sufficient statistics of BinomialAccumulator with suff_stat.
- value()[source]
Returns the sufficient statistics, and member variables min_val and max_val.
- from_value(x)[source]
Set BinomialAccumulator suff stats and member variables from suff_stat tuple defined in value().
Takes tuple of (count, sum, min_val, max_val) for setting values of BinomialAccumulator.
- scale(c)[source]
Scale linear count/sum statistics while preserving support bounds.
- Parameters:
c (float)
- Return type:
BinomialAccumulator
- acc_to_encoder()[source]
Create the encoder for binomial observations.
Note: Used for seq_initialize.
- Returns:
BinomialDataEncoder()
- Return type:
BinomialDataEncoder
- class BinomialAccumulatorFactory(max_val=None, min_val=0, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Binomial accumulators with configured support-bound metadata.
- make()[source]
Create a fresh binomial accumulator.
- Returns:
BinomialAccumulator.
- Return type:
BinomialAccumulator
- class BinomialDataEncoder(min_val=None, max_val=None)[source]
Bases:
DataSequenceEncoderData encoder for iid integer binomial observations.
- encoding_signature()[source]
The part of this encoder’s identity that determines the encoded column layout: none of it.
min_val/max_valare acceptance bounds –seq_encode()passes them toexact_integer_observationsasminimum/maximumand nothing else. The columns it emits carry the observed range of the data itself, not the encoder’s bounds, so twoBinomialDataEncoders differing only in bounds encode any mutually-acceptable data identically.That distinction matters to consumers that hold an already-encoded batch and need to know whether a different encoder instance describes the same layout – the compiled fused-kernel path being the one in tree. Judging that by
__eq__rejected an ordinary EM step, becauseBinomialEstimator’s M-step re-derives(n, min_val)from the data and so legitimately produces an encoder with different bounds each iteration.__eq__stays strict, since “same encoder” is a different (and still useful) question from “same encoding”.- Return type:
- row_count(x)[source]
Return the number of observations represented by the indexed payload.
- seq_encode(x)[source]
Encode List[int] for vectorized seq calls in Accumulator and Distribution.
- class BinomialDistribution(p, n, min_val=None, name=None, keys=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionBinomial distribution over
min_val + {0, ..., n}with success probabilityp.- Parameters:
- classmethod compute_capabilities()[source]
Declare backend support for generated Binomial density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Binomial distribution.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Binomial sufficient statistics from encoded observations.
The parameter-dependent support shift is handled by
exp_family_sufficient_statistics_from_paramswhen generated scoring supplies the declaration-stacked parameter bundle.
- static exp_family_sufficient_statistics_from_params(x, params, engine)[source]
Return Binomial sufficient statistics for generated scoring.
- static exp_family_natural_parameters(params, engine)[source]
Return Binomial natural parameters for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return Binomial log partition for generated scoring.
- static exp_family_base_measure(x, engine)[source]
Return the observation-only Binomial base measure.
- static exp_family_base_measure_from_params(x, params, engine)[source]
Return Binomial support/base measure 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(E[log p], E[log(1-p)]) = (digamma(a) - digamma(a+b), digamma(b) - digamma(a+b))so thatexpected_log_densityevaluates the variational Bayes expectationE_q[log p(x | p)]. 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.Uses the cached digamma expectations of
log pandlog(1-p); falls back to the plug-inlog_density(x)when no conjugate prior is attached.
- seq_expected_log_density(x)[source]
Vectorized
expected_log_densityover sequence-encoded observations.
- density(x)[source]
Returns the probability mass of integer value x.
If x is not an integer between [0,n) or [min_val, n-1-min_val), density is 0.0.
- log_density(x)[source]
Returns the log-probability mass of integer value x.
If x is not an integer between [0,n) or [min_val, n-1-min_val), log-density is -inf.
- seq_log_density(x)[source]
Vectorized evaluation of log-density for sequence encoded data.
Input value x must be obtained from a call to BinomialDataEncoder.seq_encode(data). Returns numpy array of log-density evaluated at all observations contained in encoded data x.
- static backend_log_density_from_params(vals, n, p, min_val, engine)[source]
Engine-neutral binomial log-density 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 binomial parameters for a homogeneous mixture kernel.
A stacked binomial mixture requires components to share the same trial count and shifted support. Mixtures with heterogeneous supports fall back to the generic kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of binomial log densities.
- classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]
Return stacked Binomial sufficient statistics using estimator-owned support bounds.
- to_fisher(**kwargs)[source]
Return the Binomial’s count-family Fisher view.
- cdf(x)[source]
Cumulative distribution function P(X <= x) over min_val + {0..n}.
- quantile(q)[source]
Inverse CDF
F^{-1}(q)overmin_val + {0..n}(via scipy binom).The endpoints are the support’s own bounds –
min_valatq = 0andmin_val + natq = 1– rather than scipy’s-1, which is outside the support this law scores (P01-F12).
- sampler(seed=None)[source]
Returns BinomialSampler for generating samples from BinomialDistribution(n,p,min_val).
- Parameters:
Optional[int] (seed) – Used to set seed on random number generator for sampling.
seed (int | None)
- Returns:
BinomialSampler for BinomialDistribution with seed.
- Return type:
BinomialSampler
- estimator(pseudo_count=None)[source]
Creates a BinomialEstimator for estimating parameters of BinomialDistribution.
- Parameters:
pseudo_count (Optional[float]) – If set, inflates counts for currently set sufficient statistic (p).
- Returns:
BinomialEstimator object.
- Return type:
BinomialEstimator
- dist_to_encoder()[source]
Create the encoder for sequence-encoded binomial observations.
- Returns:
BinomialDataEncoder object.
- Return type:
BinomialDataEncoder
- enumerator()[source]
Returns BinomialEnumerator iterating the support in descending probability order.
- Return type:
BinomialEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index by walking the binomial mode outward.
- class BinomialEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate bounded Binomial support values in descending probability order.
- Parameters:
dist (BinomialDistribution)
- class BinomialEstimator(max_val=None, min_val=0, pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimate Binomial probability and support parameters from sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Create a BinomialAccumulatorFactory from estimator member variables.
- Returns:
BinomialAccumulatorFactory
- Return type:
BinomialAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model’s success probability under the Beta prior (ELBO global term).
- Parameters:
model (BinomialDistribution)
- Return type:
- estimate(nobs, suff_stat)[source]
Estimate a binomial distribution from accumulated sufficient statistics.
nobsis accepted for estimator API consistency but is not used. The sufficient statistic is(count, sum, min_val, max_val). When a prior statistic andpseudo_countare present, they shrink the empirical success probability toward the estimator’s prior probability.
- class BinomialSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
BinomialDistribution.- Parameters:
dist (BinomialDistribution)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw samples from BinomialSampler.
- class CategoricalAccumulator(keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for weighted categorical label counts.
- Parameters:
keys (str | None)
- update(x, weight, estimate)[source]
Adds weight to the category_count for category x.
If x is new Category label, a new key in the dict count_map is created and the count is incremented by weight.
- Parameters:
x (Any) – Category label.
weight (float) – Weight for the observation x.
estimate (Optional['CategoricalDistribution']) – Kept for consistency with update method in SequenceEncodableStatisticAccumulator.
- Returns:
None, updates sufficient_stat of Accumulator, count_map.
- Return type:
None
- initialize(x, weight, rng)[source]
Initializes the CategoricalAccumulator sufficient statistics one observation at a time.
This delegates to
update(), since initialization has no random component.- Parameters:
x (Any) – Category label.
weight (float) – Weight incrementing suff stat count_map counts for the observation x.
rng (Optional[RandomState]) – Kept for consistency with update method in SequenceEncodableStatisticAccumulator.
- Returns:
None, initializes sufficient_stat of Accumulator, count_map.
- Return type:
None
- get_seq_lambda()[source]
Return the sequence-update kernel used by generated accumulation code.
- seq_update(x, weights, estimate)[source]
Vectorized accumulation of Categorical sufficient statistics from encoded sequence of data.
Requires data as encoded sequence from CategoricalDataEncoder.seq_encode(data).
- Parameters:
x (Tuple[np.ndarray,np.ndarray]) – Tuple of numpy indices for unique categories, and numpy array unique objects that index xs maps to.
weights (np.ndarray) – weights for each observation in encoded data set.
estimate (Optional['CategoricalDistribution']) – Kept for consistency with update method in SequenceEncodableStatisticAccumulator.
- Returns:
None
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of Categorical sufficient statistics from encoded sequence of data.
Requires data as encoded sequence from CategoricalDataEncoder.seq_encode(data). This delegates to
seq_update(), since initialization has no random component.- Parameters:
x (Tuple[np.ndarray,np.ndarray]) – Tuple of numpy indices for unique categories, and numpy array unique objects that index xs maps to.
weights (np.ndarray) – weights for each observation in encoded data set.
rng (Optional[RandomState]) – Kept for consistency with update method in SequenceEncodableStatisticAccumulator.
- Returns:
None
- Return type:
None
- combine(suff_stat)[source]
Combine the sufficient statistics of CategoricalAccumulator with suff_stat.
- Parameters:
suff_stat (Dict[Any, float]) – Prior data observations aggregated into dictionary with category levels as keys and counts as values.
- Returns:
None, updates the count_map of CategoricalAccumulator.
- Return type:
CategoricalAccumulator
- value()[source]
Returns sufficient statistic of CategoricalAccumulator.
Sufficient statistic value is a dictionary with category as keys and counts of categories as values.
- from_value(x)[source]
Set CategoricalAccumulator sufficient statistics and member variables from suff_stat dict defined in value().
Takes sufficient statistic value from dictionary with category as keys and counts of categories as values. Sets count_map to the passed value x.
- Parameters:
x (Dict[Any, float]) – Dictionary with category as keys and counts of categories as values
- Returns:
CategoricalAccumulator with member variable sufficient statistics set to x.
- Return type:
CategoricalAccumulator
- acc_to_encoder()[source]
Return an encoder compatible with categorical observations.
- Return type:
CategoricalDataEncoder
- class CategoricalAccumulatorFactory(keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for categorical count accumulators.
- Parameters:
keys (str | None)
- make()[source]
Return a fresh categorical accumulator.
- Return type:
CategoricalAccumulator
- class CategoricalDataEncoder[source]
Bases:
DataSequenceEncoderSequence encoder for categorical observations used by vectorized
seq_*methods.- seq_encode(x)[source]
Encode a list of category labels for vectorized
seq_*methods.
- class CategoricalDistribution(pmap=MISSING, default_value=0.0, name=None, keys=None, prob_map=MISSING, prior=None, scoring_only=False)[source]
Bases:
SequenceEncodableProbabilityDistributionCategorical distribution over hashable labels.
- Parameters:
- classmethod compute_capabilities()[source]
Return compute-backend metadata for categorical scoring.
- classmethod compute_declaration()[source]
Return the symbolic declaration for categorical probability maps.
- static exp_family_sufficient_statistics(x, engine)[source]
Return a shape-only fallback; category-aware statistics come from
..._from_params.
- static exp_family_sufficient_statistics_from_params(x, params, engine)[source]
Return the one-hot label indicator
T(x)of shape(n, K)(zeros for off-support labels).Categories are ordered canonically by
sorted(pmap, key=repr)so the columns line up withexp_family_natural_parameters().
- static exp_family_natural_parameters(params, engine)[source]
Return the natural parameter
eta = log(pmap)over categories in canonical key order.
- static exp_family_log_partition(params, engine)[source]
Return the log partition
A = 0(normalization is carried byeta = log p).
- static exp_family_base_measure_from_params(x, params, engine)[source]
Return
log h(x) = 0on the support (a key ofpmap) and-inffor off-support labels.
- get_prior()[source]
Return the conjugate parameter prior over the category-probability simplex (or None).
- Return type:
SequenceEncodableProbabilityDistribution | None
- set_prior(prior)[source]
Attach a parameter prior and precompute conjugate-prior expectations.
With a DictDirichlet(alpha) prior over the category probabilities this caches the variational expected log-probabilities E[log p_k] = digamma(alpha_k) - digamma(sum_k alpha_k) for each key of
pmapso thatexpected_log_density(x) = E[log p_x] - log(1 + default_value). A scalar alpha is treated as a symmetric Dirichlet of dimensionlen(pmap). 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)] under the DictDirichlet 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]
Density evaluation of CategoricalDistribution.
p_mat(x) = p_i, if x in pmap.keys(), else p_mat(x) = default_value.
With
default_value > 0and apmapthat does not cover the full label space, this is a smoothed scoring function, not a normalized probability measure – see thedefault_valuenote on__init__().- Parameters:
x (Any) – Evaluate CategoricalDistribution density value at x.
- Returns:
float density value at x
- Return type:
- density_semantics()[source]
Report what
log_densityactually returns, derived from the parameters.This keys off
is_normalized_probability, so an object whose numbers do not describe a law does not claim to be one (MXR-080-1841). Three shapes reportLIKELIHOOD_FACTOR:scoring_only, the author’s declaration that this is an evaluation-only factor,default_value != 0open-world smoothing, which spreads mass over every unseen label and so has no finite total,a pmap that does not sum to one – including the empty pmap, whose total is zero.
An earlier revision returned
EXACTfor the last two, because_owned_generative_componentsrefusesLIKELIHOOD_FACTORand mixtures, HMMs and segment models in this repository are all built from categoricals of exactly those shapes: a component that won zero responsibility this iteration, the explicit no-evidence state fitted on all-empty sequences, and deliberately unnormalized components. Labelling them honestly made them non-composable, so the label was bent instead. That was the wrong half to bend: composition inconvenience cannot turn a non-law into an exact law.The composition layer now decides admissibility for itself, by asking a factor whether it differs from a law only by scale (see
composable_as_componentand_owned_generative_components), so both questions get a truthful answer.
- composable_as_component()[source]
Whether a latent model may use this factor as a component (MXR-080-1841).
A mixture, HMM or segment model normalizes responsibilities across components, so a component that differs from a law by a positive constant scale is absorbed into its own mixing weight and leaves the E-step unchanged; a zero-mass component simply wins no responsibility. A categorical is a lookup over a discrete support, so an unnormalized or empty pmap is exactly such a scaled law and stays admissible.
scoring_onlyis not: it is an author’s declaration that the object is not generative at all, and a declaration is not something the composition layer may reason its way around.- Return type:
- log_density(x)[source]
Log-Density evaluation of CategoricalDistribution.
log(p_mat(x)) = log(p_i), if x in pmap.keys(), else log(p_mat(x)) = log(default_value).
With
default_value > 0and apmapthat does not cover the full label space, this is a smoothed scoring function, not a normalized probability measure – see thedefault_valuenote on__init__().- Parameters:
x (Any) – Evaluate CategoricalDistribution density value at x.
- Returns:
Log-density of Categorical distribution evaluated at x.
- Return type:
- seq_log_density(x)[source]
Vectorized evaluation of log-density for sequence encoded data.
Input value x must be obtained from a call to CategoricalDataEncoder.seq_encode(data). Returns numpy array of log-density evaluated at all observations contained in encoded data x.
- backend_seq_log_density(x, engine)[source]
Engine-neutral log-density for encoded object categories.
The object-to-index lookup remains Python-side at the encoding boundary; the selected log-probability vector is an engine tensor, so simplex-map parameters can still participate in autograd.
- 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 categorical probabilities for shared finite supports.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of categorical log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return per-component legacy count maps from engine-resident posterior weights.
- to_fisher(**kwargs)[source]
Return the categorical’s one-hot Fisher view (generic fallback for default-augmented maps).
- sampler(seed=None)[source]
Return a sampler for iid draws from this distribution.
- Parameters:
seed (int | None) – Optional random seed.
- Returns:
A configured
CategoricalSampler.- Return type:
CategoricalSampler
- estimator(pseudo_count=None)[source]
Return an estimator initialized from this distribution’s support.
- Parameters:
pseudo_count (float | None) – Optional smoothing count applied to current probabilities.
- Returns:
A
CategoricalEstimator.- Return type:
CategoricalEstimator
- dist_to_encoder()[source]
Return an encoder for categorical observations.
- Return type:
CategoricalDataEncoder
- enumerator()[source]
Return an enumerator over support labels in descending probability order.
- Return type:
CategoricalEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index directly from the finite support map.
- class CategoricalEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerator over finite categorical support in descending probability order.
- Parameters:
dist (CategoricalDistribution)
- class CategoricalEstimator(pseudo_count=None, suff_stat=None, default_value=False, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimator for categorical probability maps from weighted label counts.
- Parameters:
- get_prior()[source]
Return the conjugate parameter prior over the category-probability simplex (or None).
- Return type:
SequenceEncodableProbabilityDistribution | None
- set_prior(prior)[source]
Set the conjugate parameter prior over the category-probability simplex.
- Parameters:
prior (SequenceEncodableProbabilityDistribution | None)
- Return type:
None
- model_log_density(model)[source]
Log-density of the model probability map under the DictDirichlet prior (ELBO global term).
- Parameters:
model (CategoricalDistribution)
- Return type:
- accumulator_factory()[source]
Return an accumulator factory matching this estimator.
- Return type:
CategoricalAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a CategoricalDistribution from suff_stat value.
If default_value is True, we estimate a default value from the suff_stat counts. Else, it is set to 0.0.
pseudo_count is used to averaged over the number of levels and added to the corresponding counts.
If suff_stat member value is None, estimate for CategoricalDistribution is formed from the suff_stat passed. Otherwise, the suff_stat member value is combined with the suff_stat values passed to estimate.
- Parameters:
- Returns:
- CategoricalDistribution estimated from passed in suff_stat value and sufficient statistic member variable
(if it is not None).
- Return type:
CategoricalDistribution
- class CategoricalFisherView(dist, keys, probs)[source]
Bases:
FixedFisherViewFisher view for categorical one-hot sufficient statistics.
- class CategoricalSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for categorical labels according to the configured probability map.
- Parameters:
dist (CategoricalDistribution)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw iid samples from the categorical distribution.
- Parameters:
size (int | None) – Number of iid samples to draw.
Nonereturns a scalar label. Must be an exact non-negative integer –int()would have readTrueas one draw, truncated2.7to two, and turned a negative count into an empty “successful” sample, which is the contractBackoffSampler.samplealready enforces (MXR-080-1845/1904).batched (bool) – Whether to draw the whole batch in one vectorized call (the default) or one draw at a time. Both yield iid samples from the same law and differ only in how the random stream is consumed, so a fixed seed gives different – individually valid – sequences. This argument was previously declared and then ignored entirely: passing
batched=Falsedid nothing at all and silently returned the batched result (MXR-080-1904).
- Returns:
A scalar label when
sizeisNone; otherwise a list of labels.- Return type:
- class GeometricAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum statistics for Geometric estimation.
- update(x, weight, estimate)[source]
Update sufficient statistics for GeometricAccumulator with one weighted observation.
- supported_rows(x)[source]
Encoded rows this law can be fitted on: finite and at least one.
The encoder admits out-of-support observations so that a mixture can encode a whole batch against every component (P02-F03); this is the predicate that keeps them out of the sufficient statistics, and the one a latent model’s initialization consults before it hands this component any responsibility. Without it the initializer could not tell that this component does not own a row, seeded it from one anyway, and the refusal below then blamed the DATA for a mixture the model can express – a Poisson-plus-Geometric fit on counts with zeros was refused outright (Q01-F02/Q02-F02).
- Return type:
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from encoded sequence x.
sum increased by sum of weighted observations. count increased by sum of weights.
- Parameters:
x (ndarray) – Numpy array of positive integers.
weights (ndarray) – Numpy array of positive floats.
estimate (Optional[GeometricDistribution]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- initialize(x, weight, rng)[source]
Initialize sufficient statistics of GeometricAccumulator with weighted observation.
This delegates to
update().
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of GeometricAccumulator sufficient statistics with weighted observations.
This delegates to
seq_update().- Parameters:
x (ndarray) – Numpy array of positive integers.
weights (ndarray) – Numpy array of positive floats.
rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- combine(suff_stat)[source]
Combine aggregated sufficient statistics with sufficient statistics of GeometricAccumulator instance.
- Input suff_stat is Tuple[float, float] with:
suff_stat[0] (float): sum of observation weights, suff_stat[1] (float): weighted sum of observations.
- value()[source]
Returns sufficient statistics Tuple[float, float] of GeometricAccumulator instance.
- from_value(x)[source]
Sets GeometricAccumulator instance sufficient statistic member variables to x.
- acc_to_encoder()[source]
Return the encoder associated with this accumulator.
- Return type:
GeometricDataEncoder
- class GeometricAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Geometric accumulators with shared name and merge-key metadata.
- make()[source]
Return GeometricAccumulator with name and keys passed.
- Return type:
GeometricAccumulator
- class GeometricDataEncoder[source]
Bases:
DataSequenceEncoderData encoder for iid positive-integer geometric observations.
- seq_encode(x)[source]
Encode iid sequence of geometric observations for vectorized
seq_function calls.Note: x should be list of numpy array of positive integers.
- class GeometricDistribution(p, name=None, keys=None, prior=None)[source]
Bases:
CachedParameterLaw,SequenceEncodableProbabilityDistributionGeometric distribution on
{1, 2, ...}with success probabilityp.- Parameters:
- classmethod compute_capabilities()[source]
Declare backend support for generated Geometric density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Geometric distribution.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Geometric sufficient statistics in accumulator order.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Geometric sufficient statistic
T(x) = (x,)(support x = 1, 2, …).
- static exp_family_base_measure(x, engine)[source]
Return the Geometric base measure:
0forx >= 1,-infbelow.Without it the generated engine kernels scored an admitted zero or negative count with the closed form
log p + (x - 1) log(1 - p)– a finite log-probability for an impossible value, where every numpy route returns-inf(V01-F04). The encoder guarantees integers.
- static exp_family_natural_parameters(params, engine)[source]
Return Geometric natural parameter
eta = log(1 - p).
- static exp_family_log_partition(params, engine)[source]
Return Geometric log partition
A = log(1 - p) - log(p).
- static exp_family_from_natural(eta)[source]
Return the Geometric with natural parameter
eta = log(1 - p).- Parameters:
eta (Any)
- Return type:
GeometricDistribution
- 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(digamma(a), digamma(b), digamma(a+b))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.Uses the cached digamma expectations of
log pandlog(1-p); falls back to the plug-inlog_density(x)when no conjugate prior is attached.
- seq_expected_log_density(x)[source]
Vectorized
expected_log_densityover sequence-encoded observations.
- density(x)[source]
Density of geometric distribution evaluated at x.
P(x=k) = (k-1)*log(1-p) + log(p), for x = 1,2,…, else 0.0.
- log_density(x)[source]
Log-density of geometric distribution evaluated at x.
See density() for details.
- seq_log_density(x)[source]
Vectorized log-density evaluated on sequence encoded x.
- static backend_log_density_from_params(x, p, engine)[source]
Engine-neutral geometric log-density 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 geometric parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of geometric log densities.
- to_fisher(**kwargs)[source]
Return the Geometric’s count-family Fisher view.
- cdf(x)[source]
Cumulative distribution function P(X <= x) = 1 - (1-p)^floor(x), support x >= 1.
- quantile(q)[source]
Inverse CDF
F^{-1}(q), supportk >= 1(via scipy geom).The endpoints are the support’s own bounds –
1atq = 0andinfatq = 1; scipy’s0atq = 0is outside the support this law scores (P01-F12).
- sampler(seed=None)[source]
Create a sampler from this geometric distribution.
- Parameters:
seed (Optional[int]) – Used to set seed on random number generator.
- Returns:
GeometricSampler configured from this distribution.
- Return type:
GeometricSampler
- estimator(pseudo_count=None)[source]
Create an estimator for a geometric distribution.
- Parameters:
pseudo_count (Optional[float]) – Regularize empirical summary statistics.
- Returns:
GeometricEstimator configured with this distribution’s prior and name.
- Return type:
GeometricEstimator
- dist_to_encoder()[source]
Return the encoder for geometric observations.
- Return type:
GeometricDataEncoder
- enumerator()[source]
Returns GeometricEnumerator iterating the support {1, 2, …} in descending probability order.
- Return type:
GeometricEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index directly from the geometric tail formula.
- class GeometricEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate geometric support values in descending probability order.
- Parameters:
dist (GeometricDistribution)
- class GeometricEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimate Geometric success probabilities from accumulated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Create a geometric accumulator factory with this estimator’s name and keys.
- Return type:
GeometricAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model’s success probability under the Beta prior (ELBO global term).
- Parameters:
model (GeometricDistribution)
- Return type:
- estimate(nobs, suff_stat)[source]
Estimate a geometric distribution from weighted count and sum statistics.
nobsis accepted for estimator API consistency but is not used.suff_statis(count, sum). Without a pseudo-count, the estimate isp = count / sum; with a pseudo-count, the empirical statistic is shrunk toward the estimator’s prior statistic when available.
- class GeometricSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
GeometricDistribution.- Parameters:
dist (GeometricDistribution)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Generate iid samples from geometric distribution.
Generates a single geometric sample (int) if size is None, else a numpy array of integers of length size, iid samples, from the geometric distribution.
- class IntegerCategoricalAccumulator(min_val=None, max_val=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for weighted counts over a bounded integer support.
- update(x, weight, estimate)[source]
Update sufficient statistics with one weighted observation.
If the observed value falls outside the current support, the count vector is expanded and existing counts are realigned.
- initialize(x, weight, rng)[source]
Initialize sufficient statistics with one weighted observation.
- Parameters:
x (int) – Integer observation.
weight (float) – Observation weight.
rng (RandomState) – Accepted for accumulator API consistency.
- Returns:
None.
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of IntegerCategoricalAccumulator sufficient statistics with weighted observations.
This delegates to
seq_update().- Parameters:
x (np.ndarray[int]) – Sequence encoded iid observations of integer categorical distribution.
weights (ndarray) – Numpy array of positive floats.
rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- seq_update(x, weights, estimate)[source]
- Vectorized update of IntegerCategoricalAccumulator sufficient statistics with sequence encoded iid
observations x.
Note: Determines the range (support) of integer categorical from the sequence encoded data.
- Parameters:
x (np.ndarray[int]) – Sequence encoded iid observations of integer categorical distribution.
weights (ndarray) – Numpy array of positive floats.
estimate (Optional[IntegerCategoricalDistribution]) – Previous estimate of IntegerCategoricalDistribution.
- Returns:
None.
- Return type:
None
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident accumulation: the weighted value histogram is reduced on the active engine (numpy or torch); the dynamic support range is host bookkeeping. Matches seq_update.
- combine(suff_stat)[source]
Merge another
(min_val, count_vec)sufficient statistic.Supports are aligned before counts are added, so accumulators built on different observed ranges can be combined safely.
- from_value(x)[source]
Replace accumulator state from a
(min_val, count_vec)statistic.
- scale(c)[source]
Scale count vector while preserving integer support metadata.
- Parameters:
c (float)
- Return type:
IntegerCategoricalAccumulator
- acc_to_encoder()[source]
Return the encoder associated with this accumulator.
- Return type:
IntegerCategoricalDataEncoder
- class IntegerCategoricalAccumulatorFactory(min_val=None, max_val=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for integer-categorical count accumulators.
- make()[source]
Return a fresh integer categorical accumulator with this factory’s bounds and keys.
- Return type:
IntegerCategoricalAccumulator
- class IntegerCategoricalDataEncoder[source]
Bases:
DataSequenceEncoderData encoder for iid integer-categorical observations.
- class IntegerCategoricalDistribution(min_val, p_vec=MISSING, name=None, keys=None, prob_vec=MISSING, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionCategorical distribution over a bounded integer range.
- Parameters:
- classmethod compute_capabilities()[source]
Return compute-backend metadata for integer-categorical scoring.
- classmethod compute_declaration()[source]
Return the symbolic declaration for bounded integer-categorical probabilities.
- static exp_family_sufficient_statistics(x, engine)[source]
Return raw values; category-aware one-hot statistics come from
..._from_params.
- static exp_family_sufficient_statistics_from_params(x, params, engine)[source]
Return the one-hot category indicator
T(x)of shape(n, K)(zeros off support).
- static exp_family_natural_parameters(params, engine)[source]
Return the natural parameter
eta = log(p_vec)(one entry per category).
- static exp_family_log_partition(params, engine)[source]
Return the log partition
A = 0(normalization is carried byeta = log p).
- static exp_family_base_measure_from_params(x, params, engine)[source]
Return
log h(x) = 0on the support[min_val, min_val+K)and-infoutside it.
- get_parameters()[source]
Return the probability vector p_vec (lets it be scored by a Dirichlet conjugate prior).
- Return type:
- get_prior()[source]
Return the conjugate parameter prior over the probability vector (or None).
- Return type:
SequenceEncodableProbabilityDistribution | None
- set_prior(prior)[source]
Attach a parameter prior and precompute conjugate-prior expectations.
With a Dirichlet(alpha) (or SymmetricDirichlet(alpha)) prior over the probability vector this caches the variational expected log-probabilities E[log p_k] = digamma(alpha_k) - digamma(sum_k alpha_k) so that
expected_log_density(x) = E[log p_{x - min_val}]. 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)] under the (symmetric) Dirichlet 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]
Evaluate the density of the integer categorical at observation x.
p_mat(x_mat=x) = p_vec[x] if x in support [min_val, max_val], else 0.0.
- log_density(x)[source]
Evaluate the log-density of the integer categorical at observation x.
log_p(x_mat=x) = log_p_vec[x] if x in support [min_val, max_val], else -np.inf.
- seq_log_density(x)[source]
Vectorized evaluation of IntegerCategorical log_density() for sequence encoded iid observations x.
- static backend_log_density_from_params(x, min_val, log_p_vec, engine)[source]
Engine-neutral integer-categorical log-density 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 integer-categorical parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of integer-categorical log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return component-stacked legacy
(min_val, count_vec)statistics.
- to_fisher(**kwargs)[source]
Return the integer-categorical one-hot Fisher view.
- sampler(seed=None)[source]
Return a sampler for iid draws from this distribution.
- Parameters:
seed (int | None) – Optional random seed.
- Return type:
IntegerCategoricalSampler
- estimator(pseudo_count=None)[source]
Return an estimator initialized from this distribution.
When
pseudo_countis provided, the distribution’s probabilities are used as prior sufficient statistics during estimation.- Parameters:
pseudo_count (float | None) – Weight assigned to the distribution’s current probability vector during estimation.
- Return type:
IntegerCategoricalEstimator
- dist_to_encoder()[source]
Return the encoder for iid integer categorical observations.
- Return type:
IntegerCategoricalDataEncoder
- enumerator()[source]
Return IntegerCategoricalEnumerator iterating the support in descending probability order.
- Return type:
IntegerCategoricalEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index directly from the finite integer support.
- class IntegerCategoricalEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerator over bounded integer support in descending probability order.
- Parameters:
dist (IntegerCategoricalDistribution)
- class IntegerCategoricalEstimator(min_val=None, max_val=None, pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimator for bounded integer-categorical probability vectors.
- Parameters:
- get_prior()[source]
Return the conjugate parameter prior over the probability vector (or None).
- Return type:
SequenceEncodableProbabilityDistribution | None
- set_prior(prior)[source]
Set the conjugate parameter prior over the probability vector.
- Parameters:
prior (SequenceEncodableProbabilityDistribution | None)
- Return type:
None
- model_log_density(model)[source]
Log-density of the model probability vector under the (symmetric) Dirichlet prior.
- Parameters:
model (IntegerCategoricalDistribution)
- Return type:
- accumulator_factory()[source]
Return an accumulator factory configured from this estimator’s support and keys.
Note: If min_val and max_val are BOTH not None, these values are passed to IntegerCategoricalAccumulatorFactory. Else, they are obtained from member variable suff_stat. One of these conditions must be satisfied.
Returns:
- Return type:
IntegerCategoricalAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate an integer-categorical distribution from weighted counts.
nobsis accepted for estimator API consistency but is not used.suff_statis(min_val, count_vec). Whenpseudo_countand a prior statistic are present, the estimate combines observed counts with the weighted prior probability vector.
- class IntegerCategoricalFisherView(dist)[source]
Bases:
CategoricalFisherViewFisher view for bounded integer-categorical one-hot statistics.
- Parameters:
dist (Any)
- class IntegerCategoricalSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for bounded integer-categorical values.
- Parameters:
dist (IntegerCategoricalDistribution)
seed (int | None)
- class IntegerUniformSpikeAccumulator(min_val, max_val, keys=None, name=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for weighted integer counts over a growing range.
- min_val
Smallest integer observed (or configured) so far.
- Type:
Optional[int]
- max_val
Largest integer observed (or configured) so far.
- Type:
Optional[int]
- count_vec
Weighted counts for each integer in [min_val, max_val].
- Type:
Optional[np.ndarray]
- count
Total weighted observation count.
- Type:
- key
Key for merging sufficient statistics across accumulators.
- Type:
Optional[str]
- name
Optional accumulator name.
- Type:
Optional[str]
- update(x, weight, estimate)[source]
Add weight to the count for integer x, growing the count vector if x is out of range.
- initialize(x, weight, rng)[source]
Initialize the accumulator with observation x and weight (delegates to update).
- Parameters:
x (int)
weight (float)
rng (RandomState)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialization from encoded observations x (delegates to seq_update).
- seq_update(x, weights, estimate)[source]
Vectorized accumulation of weighted counts from encoded observations x.
- Parameters:
x (np.ndarray) – Sequence encoded integer observations.
weights (np.ndarray) – Weights on the observations.
estimate (Optional[IntegerUniformSpikeDistribution]) – Unused previous estimate.
- Return type:
None
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident accumulation: the weighted value histogram is reduced on the active engine (numpy or torch); the dynamic support range is host bookkeeping. Matches seq_update.
- combine(suff_stat)[source]
Combine sufficient statistics (min_val, count_vec) with this accumulator, aligning ranges.
- Parameters:
suff_stat (Tuple[int, np.ndarray]) – Minimum value and count vector of another accumulator.
- Returns:
This IntegerUniformSpikeAccumulator.
- Return type:
IntegerUniformSpikeAccumulator
- value()[source]
Returns sufficient statistics as a tuple (min_val, count_vec).
- from_value(x)[source]
Set sufficient statistics from a (min_val, count_vec) tuple.
- Parameters:
x (Tuple[int, np.ndarray]) – Minimum value and count vector.
- Returns:
This IntegerUniformSpikeAccumulator.
- Return type:
IntegerUniformSpikeAccumulator
- scale(c)[source]
Scale linear counts while preserving the integer support offset.
- Parameters:
c (float)
- Return type:
IntegerUniformSpikeAccumulator
- acc_to_encoder()[source]
Returns an IntegerUniformSpikeDataEncoder for encoding sequences of iid integer observations.
- Return type:
IntegerUniformSpikeDataEncoder
- class IntegerUniformSpikeAccumulatorFactory(min_val=None, max_val=None, keys=None, name=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for integer-uniform-spike accumulators.
- Parameters:
- min_val
Smallest integer value in the range, if known.
- Type:
Optional[int]
- max_val
Largest integer value in the range, if known.
- Type:
Optional[int]
- keys
Key for merging sufficient statistics.
- Type:
Optional[str]
- name
Optional name assigned to created accumulators.
- Type:
Optional[str]
- make()[source]
Return a fresh integer-uniform-spike accumulator.
- Return type:
IntegerUniformSpikeAccumulator
- class IntegerUniformSpikeDataEncoder[source]
Bases:
DataSequenceEncoderData encoder for iid integer-uniform-spike observations.
- class IntegerUniformSpikeDistribution(k, num_vals, p, min_val=0, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionUniform integer distribution with extra probability mass at one value.
- Parameters:
- classmethod compute_capabilities()[source]
Declare backend support for integer-uniform-spike generated kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the integer uniform spike.
- density(x)[source]
Density of the integer uniform spike distribution at observation x.
See log_density() for details.
- log_density(x)[source]
Log-density of the integer uniform spike distribution at observation x.
Returns log(p) if x equals the spike value k, log((1-p)/(num_vals-1)) for any other integer in [min_val, max_val], and -inf outside the range.
- seq_log_density(x)[source]
Vectorized evaluation of log-density at sequence encoded input x.
- Parameters:
x (np.ndarray) – Numpy array of integer observations.
- Returns:
Numpy array of log-density (float) of len(x).
- Return type:
- backend_seq_log_density(x, engine)[source]
Engine-neutral log-density for encoded integer spike observations.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked integer-uniform-spike parameters for a shared support.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of integer-uniform-spike log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return component-stacked legacy
(min_val, count_vec)statistics.
- sampler(seed=None)[source]
Create an IntegerUniformSpikeSampler from parameters of this distribution.
- Parameters:
seed (Optional[int]) – Used to set seed in random sampler.
- Returns:
IntegerUniformSpikeSampler object.
- Return type:
IntegerUniformSpikeSampler
- estimator(pseudo_count=None)[source]
Create an IntegerUniformSpikeEstimator for the current integer range.
- Parameters:
pseudo_count (Optional[float]) – Used to inflate sufficient statistics.
- Returns:
IntegerUniformSpikeEstimator object.
- Return type:
IntegerUniformSpikeEstimator
- dist_to_encoder()[source]
Returns an IntegerUniformSpikeDataEncoder for encoding sequences of iid integer observations.
- Return type:
IntegerUniformSpikeDataEncoder
- enumerator()[source]
Returns an IntegerUniformSpikeEnumerator iterating the support in descending probability order.
- Return type:
IntegerUniformSpikeEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index directly from the finite integer support.
- class IntegerUniformSpikeEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerates the support [min_val, max_val] in descending probability order.
The spike value k is yielded first when p >= (1-p)/(num_vals-1), otherwise last; the remaining values share the same probability and are yielded in ascending integer order. Zero-probability values are skipped.
- Parameters:
dist (IntegerUniformSpikeDistribution)
- class IntegerUniformSpikeEstimator(min_val=None, max_val=None, pseudo_count=None, suff_stat=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimator for integer-uniform-spike distributions from weighted counts.
- Parameters:
- accumulator_factory()[source]
Returns an IntegerUniformSpikeAccumulatorFactory consistent with this estimator.
- Return type:
IntegerUniformSpikeAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate an IntegerUniformSpikeDistribution by maximizing the spike location and weight.
The spike location k is chosen to maximize the likelihood of the accumulated counts (with optional pseudo_count regularization from the estimator configuration).
- class IntegerUniformSpikeSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for an integer-uniform-spike distribution.
- Parameters:
dist (IntegerUniformSpikeDistribution)
seed (int | None)
- dist
Distribution to sample from.
- Type:
IntegerUniformSpikeDistribution
- rng
Seeded RandomState for sampling.
- Type:
RandomState
- non_k
Integers of the support excluding the spike value k.
- Type:
np.ndarray
- class LogSeriesAccumulator(keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum for log-series estimation.
- Parameters:
keys (str | None)
- update(x, weight, estimate)[source]
Accumulate weighted count and total for one positive integer.
- initialize(x, weight, rng)[source]
Initialize statistics from one observation.
- Parameters:
x (int)
weight (float)
rng (RandomState | None)
- Return type:
None
- supported_rows(x)[source]
Encoded rows this law can be fitted on: finite and at least one.
The encoder admits out-of-support observations so that a mixture can encode a whole batch against every component (P02-F03); this is the predicate that keeps them out of the sufficient statistics, and the one a latent model’s initialization consults before it hands this component any responsibility. Without it the initializer could not tell that this component does not own a row, seeded it from one anyway, and the refusal below then blamed the DATA for a mixture the model can express – a Poisson-plus-Geometric fit on counts with zeros was refused outright (Q01-F02/Q02-F02).
- Return type:
- seq_update(x, weights, estimate)[source]
Accumulate weighted count and total from encoded observations.
- seq_initialize(x, weights, rng)[source]
Initialize statistics from encoded observations.
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge another log-series 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:
LogSeriesDataEncoder
- class LogSeriesAccumulatorFactory(keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for LogSeriesAccumulator.
- Parameters:
keys (str | None)
- make()[source]
Create a fresh log-series accumulator.
- Return type:
LogSeriesAccumulator
- class LogSeriesDataEncoder[source]
Bases:
DataSequenceEncoderEncode log-series observations as (k, log k) pairs.
- class LogSeriesDistribution(p, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionLogarithmic (log-series) distribution on k = 1, 2, … with shape parameter p in (0, 1).
- classmethod compute_capabilities()[source]
Describe backend support for generated log-series kernels.
- classmethod compute_declaration()[source]
Return the structured compute declaration for log-series distributions.
- static backend_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row (count, k) sufficient statistics in accumulator order.
- static exp_family_sufficient_statistics(x, engine)[source]
Return the log-series sufficient statistic
T(k) = (k,).
- static exp_family_natural_parameters(params, engine)[source]
Return the log-series natural parameter
eta = log(p).
- static exp_family_log_partition(params, engine)[source]
Return the log-series log partition
A = log(-log(1 - p)).
- static exp_family_base_measure(x, engine)[source]
Return the log-series base measure
log h(k) = -log(k)fork >= 1, and-infbelow.-log(k)alone is+infat an admittedk = 0and NaN at a negative count, so the generated engine kernels returned+infand NaN where every numpy route returns-inf(V01-F04). The encoder guarantees integers.
- static exp_family_from_natural(eta)[source]
Return the log-series with natural parameter
eta = log(p)(sop = exp(eta)).- Parameters:
eta (Any)
- Return type:
LogSeriesDistribution
- static backend_log_density_from_params(k, log_k, log_p, log_norm, engine)[source]
Engine-neutral log-series log-mass from explicit parameters (
-inffork < 1).Linear in
kandlog k, which is+infat an admittedk = 0and NaN at a negative count, so the torch kernel returned both where the numpy scorer returns-inf(V01-F04).
- density(x)[source]
Return the probability mass at a single observation.
- log_density(x)[source]
Return the log-mass at a single positive integer (or -inf off support).
- seq_log_density(x)[source]
Return vectorized log-mass values for sequence-encoded (k, log k) observations.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-mass for encoded data.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of log-series log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked sufficient statistics using engine-resident arrays.
- entropy()[source]
Shannon entropy in nats, by exact summation of the standard series.
The log-series entropy
-sum_k p_k log p_khas no closed form (Fisher, Corbet & Williams, 1943). The series is summed fromk = 1up to this distribution’s own quantile at1 - 1e-16(plus a safety margin), beyond which the remaining tail mass is far below double rounding.- Return type:
- cdf(x)[source]
Cumulative distribution function
P(X <= x), supportx >= 1.Summed directly in bounded blocks (the series has no closed form); past
_SERIES_CAPterms one minus the Euler-Maclaurin tail mass stands in, exact to double rounding by then and never below the partial sum at the cap, so the result is monotone inxand within[0, 1].
- quantile(q)[source]
Inverse CDF
F^{-1}(q): the smallestkwithP(X <= k) >= q.Summed directly in bounded blocks, never through scipy’s generic discrete
ppf. That search brackets the answer by doubling an upper bound untilcdf(bound) >= q, and the summed CDF can saturate one ULP short of aqthis close to one (numpy’s SIMDexpandloground the last bit differently per CPU: the sum reachedq = 1 - 1e-16atk = 5210on arm64 and emulated x86-64 and never did on AVX-512 hosted CI runners), so the bound doubled without limit and the vectorized CDF sums grew until the 16 GB runner was OOM-killed – throughentropy(), which calls this at exactly thatq. Here a block whose terms no longer move the running sum, or the term cap, ends the scan, and the Euler-Maclaurin tail mass answers for whatever lies beyond: the smallestkpast the scan whose tail mass is at most1 - q.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
LogSeriesSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
LogSeriesEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
LogSeriesDataEncoder
- enumerator()[source]
Return an enumerator over k = 1, 2, … in descending probability order.
- Return type:
LogSeriesEnumerator
- class LogSeriesEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate log-series support values in descending probability order.
The log-series pmf
p^k / (k * -log(1 - p))is strictly decreasing ink(each step multiplies byp * k / (k + 1) < 1), so value order 1, 2, 3, … IS descending-probability order. The iterator is infinite.- Parameters:
dist (LogSeriesDistribution)
- class LogSeriesEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorMaximum-likelihood estimator for the log-series shape p (inverts the mean -> p relation).
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for log-series count statistics.
- Return type:
LogSeriesAccumulatorFactory
- class LogSeriesSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid log-series observations.
- Parameters:
dist (LogSeriesDistribution)
seed (int | None)
- class NegativeBinomialAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum statistics plus the count histogram.
The dispersion
rhas no finite-dimensional sufficient statistic: its MLE needssum_i w_i digamma(x_i + r), which cannot be reduced to count/sum. We therefore accumulate the weighted histogram{x: weight}soNegativeBinomialEstimator.estimate()can solve the score equation forr.- update(x, weight, estimate)[source]
Accumulate weighted statistics for one non-negative integer count.
- initialize(x, weight, rng)[source]
Initialize statistics from one observation.
- Parameters:
x (int)
weight (float)
rng (RandomState | None)
- Return type:
None
- supported_rows(x)[source]
Encoded rows this law can be fitted on: non-negative counts.
The encoder admits a negative count so a mixture can encode one batch against every component (P02-F03); this is the predicate that keeps it out of the statistics and the one a latent model’s initialization consults before handing this component any responsibility. The encoder used to refuse it outright, so an integer-categorical-plus-negative-binomial mixture could not be encoded on data the categorical owns (Q01-F02).
- Return type:
- seq_update(x, weights, estimate)[source]
Accumulate weighted statistics from encoded observations.
A negative count carrying weight is refused. One with zero weight – a row EM gave another component – contributes nothing and is kept out of the histogram, whose keys the dispersion solve reads.
- seq_initialize(x, weights, rng)[source]
Initialize statistics from encoded observations.
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge another negative-binomial sufficient-statistic tuple.
- value()[source]
Return count, sum, and histogram statistics.
- 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:
NegativeBinomialDataEncoder
- class NegativeBinomialAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for NegativeBinomialAccumulator.
- make()[source]
Create a fresh negative-binomial accumulator.
- Return type:
NegativeBinomialAccumulator
- class NegativeBinomialDataEncoder[source]
Bases:
DataSequenceEncoderEncode count observations with precomputed log-factorials.
- seq_encode(x)[source]
Encode counts with precomputed
log(x!)values, admitting negative counts.Being an exact integer is a type contract and stays enforced; being non-negative is a probability question, answered -inf by the scorer, so a negative count is admitted and a mixture whose other component owns it can encode the whole batch (P02-F03, Q01-F02).
- class NegativeBinomialDistribution(r, p, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionNegative binomial distribution over non-negative integer counts.
- classmethod compute_capabilities()[source]
Describe backend support for generated negative-binomial kernels.
- classmethod compute_declaration()[source]
Return the structured compute declaration for negative binomial distributions.
- static backend_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row negative-binomial sufficient statistics in accumulator order.
The accumulator’s
NegativeBinomialAccumulator.value()returns(count, sum, histogram); the third row stat carries the per-row counts so the declaration’skind="histogram"reducer can fold them into the weighted count histogram the dispersion (r) solve needs.
- static exp_family_sufficient_statistics(x, engine)[source]
Return the NegativeBinomial sufficient statistic
T(x) = (x,)(rfixed).
- static exp_family_natural_parameters(params, engine)[source]
Return the NegativeBinomial natural parameter
eta = log(1 - p)(rfixed).
- static exp_family_log_partition(params, engine)[source]
Return the NegativeBinomial log partition
A = -r * log(p)(rfixed).
- static exp_family_base_measure_from_params(x, params, engine)[source]
Return the NegativeBinomial base measure
log h(x) = lgamma(x+r) - lgamma(r) - log(x!).The base measure carries the binomial-coefficient term and depends on the fixed shape
r; invalid (non-integer / negative) counts are mapped to-inf.
- 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 (
-infoff support).The encoder admits a negative count so a mixture can encode one batch against every component (P02-F03); the scalar path already scored it -inf, and so does the engine path below.
- static backend_log_density_from_params(vals, log_fact, r, p, engine)[source]
Engine-neutral negative-binomial log-density 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 negative-binomial parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of negative-binomial log densities.
- to_fisher(**kwargs)[source]
Return the NegativeBinomial’s count-family Fisher view.
- entropy()[source]
Shannon entropy in nats, by summation of the standard series in bounded memory.
The negative-binomial entropy
-sum_k p_k log p_khas no closed form (Johnson, Kemp & Kotz, Univariate Discrete Distributions, ch. 5). The series is summed over the support up to this distribution’s own quantile at1 - 1e-16(plus a safety margin), beyond which the tail mass is far below double rounding – driven by the exact CDF (viabetainc) rather than a Gaussian-tail heuristic, since the negative binomial can be heavy-tailed. The sum is taken one bounded block at a time and, once the support is wider thanENTROPY_TERM_CAPcounts, the remainder is the Euler-Maclaurin integral of the smooth extension:r = 1e8used to allocate a single 3.2 GB array here (P01-F02), and the quantile that bounded the scan is itself NaN at extreme parameters (P01-F11), which the tail branch now absorbs instead of propagating.- Return type:
- cdf(x)[source]
Cumulative distribution function P(X <= x) = I_p(r, floor(x)+1).
- quantile(q)[source]
Inverse CDF
F^{-1}(q)(via scipy nbinom).The endpoints are the support’s own bounds –
0atq = 0andinfatq = 1– rather than scipy’s-1, which is outside the support this law scores (P01-F12).
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
NegativeBinomialSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
NegativeBinomialEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
NegativeBinomialDataEncoder
- enumerator()[source]
Return an enumerator over the distribution support when available.
- Return type:
NegativeBinomialEnumerator
- class NegativeBinomialEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate the infinite support in descending probability order.
- Parameters:
dist (NegativeBinomialDistribution)
- class NegativeBinomialEstimator(r=1.0, pseudo_count=None, suff_stat=None, estimate_r=True, threshold=1.0e-8, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimate
randpfor a negative binomial distribution.By default the dispersion
ris recovered by a 1-D solve of the digamma score equation (pprofiled out), thenpfollows in closed form. Passestimate_r=Falseto holdrfixed at the constructor value (the historical geometric/fixed-shape M-step). Therargument is the initial value / fallback whenestimate_r=True.- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for negative-binomial statistics.
- Return type:
NegativeBinomialAccumulatorFactory
- resident_accumulation_supported()[source]
Dispersion estimation needs the full count histogram, not fixed-width resident stats.
- Return type:
- static estimate_dispersion(histogram, r_init, threshold)[source]
Solve the negative-binomial dispersion MLE from a weighted count histogram.
With
pprofiled to its MLEp = r / (r + xbar), the score forrisg(r) = sum_k h(k) [digamma(k + r) - digamma(r)] - N * log(1 + xbar / r),
which is strictly decreasing and crosses zero exactly when the data are over-dispersed (sample variance > mean). Under equi/under-dispersion the MLE runs off to the Poisson limit, so
ris capped at_MAX_NB_SHAPE.
- class NegativeBinomialSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid negative binomial observations.
- Parameters:
dist (NegativeBinomialDistribution)
seed (int | None)
- class PointMassAccumulator(value, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for a fixed point mass; no parameters are learned.
- update(x, weight, estimate)[source]
Ignore one observation because the atom is fixed.
- seq_update(x, weights, estimate)[source]
Ignore encoded observations because the atom is fixed.
- seq_update_engine(x, weights, estimate, engine)[source]
Ignore engine-resident observations because the atom is fixed.
- initialize(x, weight, rng)[source]
Ignore initialization observations because the atom is fixed.
- Parameters:
x (Any)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Ignore encoded initialization observations because the atom is fixed.
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Return this accumulator because fixed atoms have no sufficient statistics.
- Parameters:
suff_stat (Any)
- Return type:
PointMassAccumulator
- value()[source]
Return
Nonebecause fixed atoms have no sufficient statistics.- Return type:
None
- from_value(x)[source]
Return this accumulator because there is no state to restore.
- Parameters:
x (Any)
- Return type:
PointMassAccumulator
- key_merge(stats_dict)[source]
Register the key with empty statistics when configured.
- key_replace(stats_dict)[source]
Ignore keyed replacements because there is no accumulator state.
- acc_to_encoder()[source]
Return the equality-mask encoder for this atom.
- Return type:
PointMassDataEncoder
- class PointMassAccumulatorFactory(value, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for PointMassAccumulator.
- make()[source]
Create an empty point-mass accumulator.
- Return type:
PointMassAccumulator
- class PointMassDataEncoder(value)[source]
Bases:
DataSequenceEncoderEncode observations as a boolean equality mask against the fixed atom.
- Parameters:
value (Any)
- class PointMassDistribution(value, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionFixed Dirac/point-mass distribution assigning all mass to one value.
- classmethod compute_capabilities()[source]
Declare generic NumPy/Torch scoring support for fixed atoms.
- classmethod compute_declaration()[source]
Return the declaration for a fixed non-estimated atom.
- property value: Any
Return an independent copy so callers cannot mutate the model’s atom.
- 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.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density from encoded equality flags.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked point-mass parameters for identical fixed atoms.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of point-mass log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return per-component empty statistics for fixed point masses.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
PointMassSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
PointMassEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
PointMassDataEncoder
- enumerator()[source]
Return an enumerator over the distribution support when available.
- Return type:
PointMassEnumerator
- class PointMassEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate the single atom of a PointMassDistribution.
- Parameters:
dist (PointMassDistribution)
- class PointMassEstimator(value, pseudo_count=None, suff_stat=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimator that always returns the configured point mass.
- Parameters:
- accumulator_factory()[source]
Return a factory for point-mass no-op accumulators.
- Return type:
PointMassAccumulatorFactory
- class PointMassSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler returning the fixed atom.
- Parameters:
dist (PointMassDistribution)
seed (int | None)
- class PoissonAccumulator(keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum statistics for Poisson estimation.
- Parameters:
keys (str | None)
- initialize(x, weight, rng=None)[source]
Initialize sufficient statistics with one weighted observation.
This method delegates to
update.- Parameters:
x (int) – Observation from a Poisson distribution.
weight (float) – Observation weight.
rng (RandomState | None) – Unused; accepted for the accumulator interface.
- Return type:
None
- seq_initialize(x, weights, rng=None)[source]
Vectorized initialization of PoissonAccumulator sufficient statistics with weighted observations.
This delegates to
seq_update().Arg value x (Tuple[np.ndarray[int], np.ndarray[float]]) is seq_encoded Poisson data from PoissonDataEncoder.seq_encode(), containing
x[0] (np.ndarray[int]): Non-negative integer valued Poisson iid observations, x[1] (np.ndarray[float]): np.log(Gamma(x[0]+1.0)), Gamma is the gamma function.
- update(x, weight, estimate=None)[source]
Update sufficient statistics for PoissonAccumulator with one weighted observation.
- supported_rows(x)[source]
Encoded rows this law can be fitted on: finite and non-negative.
The encoder admits out-of-support observations so that a mixture can encode a whole batch against every component (P02-F03); this is the predicate that keeps them out of the sufficient statistics, and the one a latent model’s initialization consults before it hands this component any responsibility. Without it the initializer could not tell that this component does not own a row, seeded it from one anyway, and the refusal below then blamed the DATA for a mixture the model can express – a Poisson-plus-Geometric fit on counts with zeros was refused outright (Q01-F02/Q02-F02).
- Return type:
- seq_update(x, weights, estimate=None)[source]
Vectorized update of PoissonAccumulator sufficient statistics with weighted observations.
Arg value x (Tuple[np.ndarray[int], np.ndarray[float]]) is seq_encoded Poisson data from PoissonDataEncoder.seq_encode(), containing
x[0] (np.ndarray[int]): Non-negative integer valued Poisson iid observations, x[1] (np.ndarray[float]): np.log(Gamma(x[0]+1.0)), Gamma is the gamma function.
- combine(suff_stat)[source]
Merge aggregated Poisson sufficient statistics into this accumulator.
The tuple is interpreted as
(count, sum).
- from_value(x)[source]
Replace this accumulator’s sufficient statistics.
- acc_to_encoder()[source]
Return an encoder compatible with Poisson observations.
- Return type:
PoissonDataEncoder
- class PoissonAccumulatorFactory(keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Poisson accumulators with a shared optional merge key.
- Parameters:
keys (str | None)
- make()[source]
Return a fresh Poisson accumulator.
- Return type:
PoissonAccumulator
- class PoissonDataEncoder[source]
Bases:
DataSequenceEncoderEncode iid non-negative integer Poisson observations with log-factorials.
- seq_encode(x)[source]
Encode iid sequence of Poisson observations for vectorized
seq_function calls.Data type must be int. Values must be non-negative integers. Returns the integer observations and
log(Gamma(x + 1))values used by the vectorized scorer.
- class PoissonDistribution(lam, name=None, keys=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionPoisson distribution over non-negative integer counts with rate
lam.- Parameters:
- classmethod compute_capabilities()[source]
Declare backend support for generated Poisson density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Poisson distribution.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Poisson sufficient statistics for generated scoring.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Poisson sufficient statistics in accumulator order.
- static exp_family_natural_parameters(params, engine)[source]
Return Poisson natural parameters for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return Poisson log partition for generated scoring.
- static exp_family_base_measure(x, engine)[source]
Return Poisson base measure for generated scoring.
- set_prior(prior)[source]
Attach a parameter prior and cache the conjugate Gamma expectations.
With a Gamma(k, theta) prior over the rate
lamthis caches (k, theta) so thatexpected_log_density(x) = (psi(k) + ln theta)*x - k*theta - gammaln(x+1)(the VB E-step term using E[ln lam] = psi(k) + ln theta and E[lam] = k*theta). 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 | lam)] under the Gamma 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]
Evaluate the density of Poisson distribution at observation x.
Calls np.exp(log_density(x)). See log_density() for details.
- log_density(x)[source]
Log-density of Poisson distribution evaluated at x.
Log-density given by,
log(p_mat(x_mat=x; lam) = x*log(lam) - log(x!) - lam, for x in {0,1,2,…}
and -np.inf else.
Note: log(Gamma(x+1.0)) = log(x!), where Gamma is the gamma function.
- seq_log_density(x)[source]
Vectorized log-density evaluated on sequence encoded x.
Arg value x (Tuple[np.ndarray[int], np.ndarray[float]]) is seq_encoded Poisson data from PoissonDataEncoder.seq_encode(), containing
x[0] (np.ndarray[int]): Non-negative integer valued Poisson iid observations, x[1] (np.ndarray[float]): np.log(Gamma(x[0]+1.0)), Gamma is the gamma function.
- static backend_log_density_from_params(vals, log_fact, lam, engine)[source]
Engine-neutral Poisson log-density 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 Poisson parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of Poisson log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked Poisson sufficient statistics using engine-resident arrays.
- to_fisher(**kwargs)[source]
Return the Poisson’s count-family Fisher view.
- cdf(x)[source]
Cumulative distribution function P(X <= x) = Q(floor(x)+1, lam).
- entropy()[source]
Shannon entropy in nats, by summation of the standard series in bounded memory.
The Poisson entropy
-sum_k p_k log p_khas no closed form; the series is summed over the effective supportk <= lam + 40 sqrt(lam) + 40, beyond which the remaining terms are far below double rounding (the tail mass decays super-geometrically). The sum is taken one bounded block at a time, and fromlam = 1e3upward the classical asymptotic series stands in for it exactly:lam = 1e8used to allocate a single 3.2 GB array here andlam = 1e15a 7 PiB one (P01-F02).- Return type:
- quantile(q)[source]
Inverse CDF
F^{-1}(q): the smallestkwithP(X <= k) >= q.The endpoints are the support’s own bounds –
0atq = 0andinfatq = 1– rather than scipy’s-1, which is outside the support this law scores (P01-F12). Pastlam ~ 1e12scipy’s generic discreteppfreturns NaN for mid-rangeqwhile the CDF keeps working; there the answer is bracketed from the normal approximation and bisected on this law’s own CDF (P01-F11).
- sampler(seed=None)[source]
Return a sampler for iid draws from this distribution.
- Parameters:
seed (int | None) – Optional seed for the sampler’s random state.
- Returns:
A configured
PoissonSampler.- Return type:
PoissonSampler
- estimator(pseudo_count=None)[source]
Return an estimator initialized from this distribution’s shape.
- Parameters:
pseudo_count (float | None) – Optional smoothing count applied to the current rate.
- Returns:
A
PoissonEstimator.- Return type:
PoissonEstimator
- dist_to_encoder()[source]
Return an encoder for iid Poisson observations.
- Return type:
PoissonDataEncoder
- enumerator()[source]
Return an enumerator over nonnegative counts in descending probability order.
- Return type:
PoissonEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index by walking the Poisson mode outward.
- class PoissonEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate Poisson support values in descending probability order.
- Parameters:
dist (PoissonDistribution)
- class PoissonEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimate Poisson rate parameters from accumulated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory matching this estimator.
- Return type:
PoissonAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model’s rate under the Gamma prior (ELBO global term).
- Parameters:
model (PoissonDistribution)
- Return type:
- estimate(nobs, suff_stat)[source]
Estimate a Poisson distribution from aggregated sufficient statistics.
The tuple is interpreted as
(count, sum).
- class PoissonSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
PoissonDistribution.- Parameters:
dist (PoissonDistribution)
seed (int | None)
- class SkellamAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the weighted count, sum, and sum-of-squares needed for the moment fit.
- update(x, weight, estimate)[source]
Accumulate count, sum, and squared sum for one integer observation.
- initialize(x, weight, rng)[source]
Initialize statistics from one observation.
- Parameters:
x (int)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
Accumulate count, sum, and squared sum from encoded observations.
- 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 Skellam sufficient-statistic tuple.
- from_value(x)[source]
Replace accumulator contents from a sufficient-statistic tuple.
- scale(c)[source]
Scale all weight-linear sufficient statistics by
c.- Parameters:
c (float)
- Return type:
SkellamAccumulator
- acc_to_encoder()[source]
Return the encoder used by this accumulator.
- Return type:
SkellamDataEncoder
- class SkellamAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for SkellamAccumulator.
- make()[source]
Create a fresh Skellam accumulator.
- Return type:
SkellamAccumulator
- class SkellamDataEncoder[source]
Bases:
DataSequenceEncoderEncode sequences of iid Skellam observations (integer data type, any sign).
- class SkellamDistribution(mu1, mu2, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionSkellam distribution:
K = N1 - N2for independentN1 ~ Poisson(mu1),N2 ~ Poisson(mu2).- density(x)[source]
Probability mass at integer
x(seelog_density).
- log_density(x)[source]
Stable Skellam log-mass at integer
x(-inffor non-integer input).
- seq_log_density(x)[source]
Vectorized Skellam log-mass at sequence-encoded integer counts
x.
- entropy()[source]
Shannon entropy in nats, by exact two-sided summation of the standard series.
The Skellam entropy
-sum_k p_k log p_khas no closed form. The sum runs over the support between this distribution’s own quantiles at1e-16and1 - 1e-16(plus a safety margin on each side), beyond which the remaining tail mass on both sides is far below double rounding. Terms where the scaled Bessel term underflows to exactly zero (mass indistinguishable from 0 at double precision) are dropped, matching thep log p -> 0limit.- Return type:
- classmethod compute_capabilities()[source]
Describe backend support for generated Skellam scoring kernels.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized Skellam log-mass for encoded data (see class backend note).
The series yields the UNSCALED
log I_k(notlog ive = log I_k - z), so the constant here is-(mu1 + mu2)— the legacy path’s-sqrt_diff_sqplus its implicit-z.
- cdf(x)[source]
Cumulative distribution function P(X <= x) (via scipy skellam).
- quantile(q)[source]
Inverse CDF F^{-1}(q) (via scipy skellam).
- sampler(seed=None)[source]
Return a SkellamSampler for this distribution.
- Parameters:
seed (int | None)
- Return type:
SkellamSampler
- estimator(pseudo_count=None)[source]
Return a SkellamEstimator (method of moments).
- Parameters:
pseudo_count (float | None)
- Return type:
SkellamEstimator
- dist_to_encoder()[source]
Return the encoder for Skellam observations.
- Return type:
SkellamDataEncoder
- enumerator()[source]
Return an enumerator over all integers in descending probability order.
- Return type:
SkellamEnumerator
- class SkellamEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate the two-sided Skellam support in descending probability order.
The Skellam pmf is log-concave (the difference of two independent Poisson counts, each log-concave on the integers), hence unimodal: enumeration hill-climbs from
round(mu1 - mu2)to the mode and walks outward with the standard two-pointer merge over the two tails, emitting the larger side each step. Values whose mass underflows to zero are skipped per the enumerator contract, so the iterator ends when both tails are exhausted at float precision.- Parameters:
dist (SkellamDistribution)
- class SkellamEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimate
(mu1, mu2)by the (exact, closed-form) method of moments.- Parameters:
- accumulator_factory()[source]
Return an accumulator factory for Skellam moment statistics.
- Return type:
SkellamAccumulatorFactory
- class SkellamSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid Skellam observations as the difference of two independent Poisson draws.
- Parameters:
dist (SkellamDistribution)
seed (int | None)
Submodules¶
- mixle.stats.univariate.discrete.bernoulli module
- mixle.stats.univariate.discrete.beta_binomial module
- mixle.stats.univariate.discrete.binomial module
- mixle.stats.univariate.discrete.categorical module
- mixle.stats.univariate.discrete.geometric module
- mixle.stats.univariate.discrete.integer_categorical module
- mixle.stats.univariate.discrete.integer_uniform_spike module
- mixle.stats.univariate.discrete.logseries module
- mixle.stats.univariate.discrete.negative_binomial module
- mixle.stats.univariate.discrete.point_mass module
- mixle.stats.univariate.discrete.poisson module
- mixle.stats.univariate.discrete.skellam module