mixle.stats.combinator.zero_inflated module¶
Zero-inflated count models: a point mass at 0 mixed with any base count distribution.
A zero-inflated distribution adds a separate source of zeros to a base count distribution: with
probability pi the observation is a structural zero, and with probability 1 - pi it is drawn
from base. So
P(X = 0) = pi + (1 - pi) * P_base(0), P(X = k) = (1 - pi) * P_base(k) for k > 0.
This is the standard remedy for excess zeros (ecology, insurance, defaults). Because the base can
itself produce zeros, an observed 0 is ambiguous – it may be structural or a base zero – so the
model is fit by EM (a two-component mixture whose inflation component is a fixed PointMass(0)).
Wrapping any base gives the whole family at once: a Poisson base is the zero-inflated Poisson
(ZIP), a NegativeBinomial base is ZINB, a Binomial base is zero-inflated binomial, and so on.
- class ZeroInflatedDistribution(base, pi, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionA base count distribution with an extra point mass of probability
piat zero.- Parameters:
- density(x)[source]
Return the zero-inflated probability at
x.
- log_density(x)[source]
Return
log[(1-pi) p_base(x)]forx > 0, mixed withpiatx == 0.
- seq_log_density(x)[source]
Vectorized zero-inflated log-density for an encoded batch.
- sampler(seed=None)[source]
Return a ZeroInflatedSampler for this distribution.
- Parameters:
seed (int | None)
- Return type:
ZeroInflatedSampler
- estimator(pseudo_count=None)[source]
Return an estimator that fits
piand the base by EM over the latent zero source.- Parameters:
pseudo_count (float | None)
- Return type:
ZeroInflatedEstimator
- dist_to_encoder()[source]
Return the data encoder (base encoding + a boolean is-zero mask).
- Return type:
ZeroInflatedDataEncoder
- class ZeroInflatedSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw a structural zero with probability
pi, otherwise draw from the base.- Parameters:
dist (ZeroInflatedDistribution)
seed (int | None)
- class ZeroInflatedAccumulator(base_accumulator, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the base sufficient statistics plus the expected structural-zero count.
- Parameters:
base_accumulator (SequenceEncodableStatisticAccumulator)
keys (str | None)
- update(x, weight, estimate)[source]
- seq_update(x, weights, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (Any)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- scale(c)[source]
Scale linear sufficient statistics in-place by
c.The structural default is correct for ordinary weighted sums, nested tuples/lists/dicts, and numeric arrays. Families whose
value()payload includes non-linear metadata such as support bounds must override this method and leave that metadata unscaled.- Parameters:
c (float)
- Return type:
ZeroInflatedAccumulator
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
ZeroInflatedDataEncoder
- class ZeroInflatedAccumulatorFactory(base_factory, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for
ZeroInflatedAccumulator.- Parameters:
base_factory (StatisticAccumulatorFactory)
keys (str | None)
- make()[source]
- Return type:
ZeroInflatedAccumulator
- class ZeroInflatedEstimator(base_estimator, pseudo_count=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorEM estimator:
pi = (expected structural zeros) / N, base re-fit on the down-weighted data.- Parameters:
- accumulator_factory()[source]
- Return type:
ZeroInflatedAccumulatorFactory
- class ZeroInflatedDataEncoder(base_encoder)[source]
Bases:
MaskedBaseEncoderEncode observations via the base encoder, plus a boolean
x == 0mask.- Parameters:
base_encoder (DataSequenceEncoder)