mixle.stats.univariate.discrete.integer_categorical module¶
Create, estimate, and sample from a Categorical distribution defined on a range of integers starting a user defined minimum value.
Defines the IntegerCategoricalDistribution, IntegerCategoricalSampler, IntegerCategoricalAccumulatorFactory, IntegerCategoricalAccumulator, IntegerCategoricalEstimator, and the IntegerCategoricalDataEncoder classes for use with mixle.
Data type (int): The integer categorical distribution is defined through summary statistics min_val (int) and vector of probabilities p_vec (np.ndarray[float]) that sum to 1.0. The range of values is given by [min_val, min_val + len(p_vec) - ). The density is then,
P(x_mat=i) = p_vec[i]
for x in {min_val,min_val+1, …, min_val + length(p_vec) - 1}, else 0.0.
- class IntegerCategoricalFisherView(dist)[source]
Bases:
CategoricalFisherView- Parameters:
dist (Any)
- class IntegerCategoricalDistribution(min_val, p_vec=MISSING, name=None, prob_vec=MISSING, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionCategorical distribution over a bounded integer range.
- Parameters:
- classmethod compute_capabilities()[source]
- classmethod compute_declaration()[source]
- 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}] - log(1 + default_value). 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]
IntegerCategoricalSampler object for sampling from IntegerCategoricalDistribution instance.
- Parameters:
seed (Optional[int]) – Set seed for drawing random samples.
- Returns:
IntegerCategoricalSampler object.
- Return type:
IntegerCategoricalSampler
- estimator(pseudo_count=None)[source]
IntegerCategoricalEstimator object from instance of IntegerCategoricalDistribution object.
If pseudo_count is not None, pass min_val and p_vec as sufficient statistics for aggregated estimaton.
- Parameters:
pseudo_count (Optional[float]) – Used to re-weight sufficient statistics of IntegerCategoricalDistribution instance in estimation.
- Returns:
IntegerCategoricalEstimator object.
- Return type:
IntegerCategoricalEstimator
- dist_to_encoder()[source]
Return IntegerCategoricalDataEncoder object for encoding sequences of 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:
DistributionEnumerator- Parameters:
dist (IntegerCategoricalDistribution)
- class IntegerCategoricalSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (IntegerCategoricalDistribution)
seed (int | None)
- sample(size=None)[source]
Draw iid samples from IntegerCategoricalSampler object.
Note: If size is None, a single sample is returned as an integer. If size > 0, a List of integers with length equal to size is returned.
- class IntegerCategoricalAccumulator(min_val=None, max_val=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulator- update(x, weight, estimate)[source]
Update sufficient statistics for IntegerCategoricalAccumulator with one weighted observation.
If min_val and max_val are not set, count_vec is created. If x is larger than max_val of x is less than min_val a new value for max_val/min_val is set, and count_vec is increased to account for the new support range.
- initialize(x, weight, rng)[source]
Initialize IntegerCategoricalAccumulator object with weighted observation
Note: Just calls update().
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of IntegerCategoricalAccumulator sufficient statistics with weighted observations.
Note: Just calls 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]
- Combine aggregated sufficient statistics with sufficient statistics of IntegerCategoricalAccumulator
instance.
- Arg passed suff_stat is sufficient statistics a Tuple of length two containing:
suff_stat[0] (int): Minimum value of the integer categorical, suff_stat[1] (np.ndarray[float]): Numpy array containing probabilities for each integer value. This also
sets the support of integer categorical to have a maximum value of suff_stat[0] + len(suff_stat[0]) - 1.
Member variables min_val, max_val, and count_vec are set from suff_stat arg if count_vec is None. Else, suff_stat is combined with the values of min_val, max_val, and count_vec.
- value()[source]
- Returns member sufficient statistics Tuple[int, np.ndarray[float]] of IntegerCategoricalAccumulator
instance.
Entry 0 of returned value is the minimum value, and entry 1 is the probability weights for each integer value in the support.
- from_value(x)[source]
Sets IntegerCategoricalAccumulator instance sufficient statistic member variables to x.
- Arg passed x is sufficient statistics a Tuple of length two containing:
x[0] (int): Minimum value of the integer categorical, x[1] (np.ndarray[float]): Numpy array containing probabilities for each integer value. This also sets the
support of integer categorical to have a maximum value of x[0] + len(x[0]) - 1.
- scale(c)[source]
Scale count vector while preserving integer support metadata.
- Parameters:
c (float)
- Return type:
IntegerCategoricalAccumulator
- key_merge(stats_dict)[source]
Aggregate member sufficient statistics with sufficient statistics of objects with matching keys.
- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to corresponding sufficient stats.
- Returns:
None.
- Return type:
None
- key_replace(stats_dict)[source]
Set member sufficient statistics to suff stats with matching keys.
- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to corresponding sufficient stats.
- Returns:
None.
- Return type:
None
- acc_to_encoder()[source]
Return IntegerCategoricalDataEncoder object for encoding sequences of iid integer categorical observations.
- Return type:
IntegerCategoricalDataEncoder
- class IntegerCategoricalAccumulatorFactory(min_val=None, max_val=None, keys=None)[source]
Bases:
StatisticAccumulatorFactory- make()[source]
Returns IntegerCategoricalAccumulator object with min_val, max_val, and keys passed.
- Return type:
IntegerCategoricalAccumulator
- class IntegerCategoricalEstimator(min_val=None, max_val=None, pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimator- 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]
- Returns IntegerCategoricalAccumulatorFactory object from member sufficient statistics of
IntegerCategoricalEstimator.
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 IntegerCategoricalDistribution object from aggregating sufficient statistics.
- Arg ‘suff_stat’ is a Tuple of int and np.ndarray[float],
suff_stat[0] (int): Minimum value of the integer categorical distribution, suff_stat[1] (ndarray[float]): Probabilities for each integer observation in range [suff_stat[0], suff_stat[0] + len(suff_stat[1])-1).
Arg suff_stat is aggregated sufficient statistics obtained from observations of integer categorical data, that is used to estimate the integer categorical distribution. If pseudo_count is not None, the integer categorical is estimated by a combing arg suff_stat and a re-weighted member variable ‘suff_stat’.
- class IntegerCategoricalDataEncoder[source]
Bases:
DataSequenceEncoderIntegerCategoricalDataEncoder object for encoding sequences of iid integer categorical observations.