mixle.stats.latent.hierarchical_mixture module¶
Create, estimate, and sample from a hierarchical mixture distribution with K components consisting of sequence mixture distribution with L topics shared across all K components.
Defines the HierarchicalMixtureDistribution, HierarchicalMixtureSampler, HierarchicalMixtureEstimatorAccumulatorFactory, HierarchicalMixtureEstimatorAccumulator, HierarchicalMixtureEstimator, and the HierarchicalMixtureDataEncoder classes for use with mixle.
Data type: Sequence[T], where ‘T’ is the type of the topic distributions.
Note that this is a mixture with K ‘outer-mixture’ components consisting of L topic distributions {f_l(theta_l)}_{l=1}^{L}, with ‘inner-mixture’ weights {tau_{k,l}}_{l=1}^{L} for each of the K components.
Sampling proceeds as follows. Each sample is a sequence of length ‘N’ (where can be modeled with a length distribution P_len()) from an outer-mixture component k with probability w_k. Sampling from mixture component ‘k’ consists of sampling from a mixture with topics {f_l(theta_l)}_{l=1}^{L} and ‘inner-mixture’ weights {tau_{k,l}}_{l=1}^{L}.
Example: Let x = (x_1, x_2, x_3, …., x_N) be an observation from a hierarchical mixture distribution of length ‘N’. Let Z and U be a random variables s.t. p_mat(Z=k) = w_k and p_mat(U=l | Z = k) = tau_{k,l}. Then
alpha_i = x_i | Z = k ~ sum_{l=1}^{L} f_l(theta_l)*tau_{k,l}, for i = 1,2,…,N.
Further,
alpha_i | U=l ~ f_l(theta_l), for i = 1,2,3,…,N.
- class HierarchicalMixtureDistribution(topics, mixture_weights, topic_weights, len_dist=NullDistribution(), name=None, keys=(None, None))[source]
Bases:
SequenceEncodableProbabilityDistributionHierarchicalMixtureDistribution object defining an outer mixture over sequence mixtures with shared topics.
Data type: Sequence[T], where T is the data type of the topic distributions.
- Parameters:
- compute_capabilities()[source]
- compute_declaration()[source]
- density(x)[source]
Evaluate the density of an observation from hierarchical mixture distribution.
- Parameters:
x (Sequence[T]) – A sequence of type data type T’s.
- Returns:
Density evaluated at x.
- Return type:
- log_density(x)[source]
Evaluate the log density of an observation from hierarchical mixture distribution.
Note: Observation is a sequence.
- Parameters:
x (Sequence[T]) – A sequence of type data type T’s.
- Returns:
Log-density evaluated at x.
- Return type:
- posterior(x)[source]
Compute the posterior over the mixture components for the outer-mixture at observed value x.
- Parameters:
x (Sequence[T]) – An observed sequence of data type T.
- Returns:
Numpy array of length ‘num_mixtures’.
- Return type:
- component_log_density(x)[source]
Evaluate the component-wise log-density for an observation from a hierarchical mixture model.
- Parameters:
x (Sequence[T]) – An observation from a hierarchical mixture model.
- Returns:
Numpy array length of ‘num_mixtures’.
- Return type:
- to_mixture()[source]
Returns a MixtureDistribution object created from object instance.
- Return type:
MixtureDistribution
- seq_component_log_density(x)[source]
Vectorized evaluation of the outer-mixture component-wise log-density for an encoded sequence x.
This returns a numpy array with shape (rv[0], ‘num_mixtures’).
Note: This density is a Mixture of Sequence of Mixture, so the data must be bin-counted as last step in code.
- Encoded sequence ‘x’ is a Tuple of length 5 containing:
x[0] (int): Number of independent observations. x[1] (ndarray[int]): Observation sequence index for each value in flattened x. x[2] (ndarray[int]): Length of each observation in x. x[3] (E): Encoded sequence of flattened observed values (has type E). x[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- seq_log_density(x)[source]
Vectorized evaluation of the log-density for an encoded sequence of observations in x.
- Encoded sequence ‘x’ is a Tuple of length 5 containing:
x[0] (int): Number of independent observations. x[1] (ndarray[int]): Observation sequence index for each value in flattened x. x[2] (ndarray[int]): Length of each observation in x. x[3] (E): Encoded sequence of flattened observed values (has type E). x[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- backend_seq_component_log_density(x, engine)[source]
Engine-neutral outer-component log densities for hierarchical-mixture encoded sequences.
- backend_seq_log_density(x, engine)[source]
Engine-neutral hierarchical-mixture log-density for encoded sequences.
- seq_posterior(x)[source]
Vectorized evaluation of the posterior over each outer-mixture component for an encoded sequence x.
- Encoded sequence ‘x’ is a Tuple of length 5 containing:
x[0] (int): Number of independent observations. x[1] (ndarray[int]): Observation sequence index for each value in flattened x. x[2] (ndarray[int]): Length of each observation in x. x[3] (E): Encoded sequence of flattened observed values (has type E). x[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- to_fisher(**kwargs)[source]
Reuse the equivalent flat mixture’s Fisher view.
- density_semantics()[source]
What
log_densityreturns relative to the true log-density (default: exact).Override to declare that this distribution’s
log_densityis a variational lower bound (ELBO), an upper bound, or an approximation rather than the exactlog p(x). This is surfaced as theExactDensitycapability and noted inmixle.describe(), so code that needs an exact likelihood canrequire(x, ExactDensity)instead of silently trusting a bound.
- sampler(seed=None)[source]
Return HierarchicalMixtureSampler object created from attribute variables.
- Parameters:
seed (int | None)
- Return type:
HierarchicalMixtureSampler
- estimator(pseudo_count=None)[source]
Create an HierarchicalMixtureEstimator object from attributes variables.
- Parameters:
pseudo_count (Optional[float]) – Re-weight sufficient statistics in estimation step of EM.
- Returns:
HierarchicalMixtureEstimator object.
- Return type:
HierarchicalMixtureEstimator
- dist_to_encoder()[source]
Return an HierarchicalMixtureDataEncoder object for encoding sequences of iid observations.
- Return type:
HierarchicalMixtureDataEncoder
- enumerator()[source]
Returns a HierarchicalMixtureEnumerator iterating sequences in descending probability order.
- Return type:
HierarchicalMixtureEnumerator
- class HierarchicalMixtureEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerates the support of a HierarchicalMixtureDistribution in descending probability order.
- Parameters:
dist (HierarchicalMixtureDistribution)
- class HierarchicalMixtureSampler(dist, seed=None)[source]
Bases:
DistributionSamplerHierarchicalMixtureSampler object for sampling sequences from a HierarchicalMixtureDistribution.
- Parameters:
dist (HierarchicalMixtureDistribution)
seed (int | None)
- class HierarchicalMixtureEstimatorAccumulator(accumulators, num_mixtures, len_accumulator=NullAccumulator(), keys=(None, None))[source]
Bases:
SequenceEncodableStatisticAccumulatorHierarchicalMixtureEstimatorAccumulator object for aggregating sufficient statistics of observed data.
- Parameters:
- update(x, weight, estimate)[source]
Update sufficient statistics with an observation x.
Encodes the single observation and delegates to seq_update() so that the scalar and vectorized estimation paths agree.
- Parameters:
x (Sequence[T]) – An observation from hierarchical mixture mode with data type T.
weight (float) – Observation weight.
estimate (HierarchicalMixtureDistribution) – Previous estimate from EM algorithm.
- Returns:
None.
- Return type:
None
- initialize(x, weight, rng)[source]
Initialize sufficient statistics with an observation x.
- Parameters:
x (Sequence[T]) – An observation from hierarchical mixture mode with data type T.
weight (float) – Observation weight.
rng (RandomState) – RandomState object for initializing sufficient statistics.
- Returns:
None.
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of sufficient statistics from an encoded sequence of observations in x.
Note: Calls _rng_initialize() to ensure equivalence between seq_initialize() and initialize().
- Encoded sequence ‘x’ is a Tuple of length 5 containing:
x[0] (int): Number of independent observations. x[1] (ndarray[int]): Observation sequence index for each value in flattened x. x[2] (ndarray[int]): Length of each observation in x. x[3] (E): Encoded sequence of flattened observed values (has type E). x[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from an encoded sequence x.
- Encoded sequence ‘x’ is a Tuple of length 5 containing:
x[0] (int): Number of independent observations. x[1] (ndarray[int]): Observation sequence index for each value in flattened x. x[2] (ndarray[int]): Length of each observation in x. x[3] (E): Encoded sequence of flattened observed values (has type E). x[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident E-step: topic scoring and the outer/topic posterior arithmetic run on the active engine (numpy or torch); component counts and per-item topic responsibilities are produced on the engine and fed to the child accumulators. Matches host seq_update.
- combine(suff_stat)[source]
Combine the sufficient statistics of ‘suff_stat; with attribute variables.
- Arg suff_stat is a Tuple of length 3 containing,
suff_stat[0] (ndarray[float]): Aggregated component counts with shape (num_mixtures, num_topics). suff_stat[1] (Tuple[SS1,…]): Tuple of ‘num_topics’ sufficient statistics for the topics. suff_stat[2] (Optional[SS2]): Optional sufficient statistic for length accumulator.
- Parameters:
suff_stat (Tuple[np.ndarray, Tuple[SS1, ...], Optional[SS2]]) – See above for details.
- Returns:
HierarchicalMixtureEstimatorAccumulator object.
- Return type:
HierarchicalMixtureEstimatorAccumulator
- value()[source]
Returns sufficient statistics of type Tuple[np.ndarray, Tuple[SS1,…], Optional[SS2]].
- from_value(x)[source]
Set the attribute variables for sufficient statistics to arg ‘x’.
- Arg ‘x’ is a Tuple of length 3 containing,
x[0] (ndarray[float]): Aggregated component counts with shape (num_mixtures, num_topics). x[1] (Tuple[SS1,…]): Tuple of ‘num_topics’ sufficient statistics for the topics. x[2] (Optional[SS2]): Optional sufficient statistic for length accumulator.
- Parameters:
x (Tuple[np.ndarray, Tuple[SS1, ...], Optional[SS2]]) – See above for details.
- Returns:
HierarchicalMixtureEstimatorAccumulator object.
- Return type:
HierarchicalMixtureEstimatorAccumulator
- scale(c)[source]
Scale linear counts and delegate child/length sufficient statistics.
- Parameters:
c (float)
- Return type:
HierarchicalMixtureEstimatorAccumulator
- key_merge(stats_dict)[source]
Merge sufficient statistics of object instance with matching keys in stats_dict.
Merges comp_counts if weight_key is set and found in stats_dict. Merges topic accumulators if ‘comp_key’ is set and found in stats_dict.
Calls key_merge() of the length accumulator.
- Parameters:
stats_dict (Dict[str, Any]) – Dictionary mapping keys to corresponding sufficient statistics.
- Returns:
None.
- Return type:
None
- key_replace(stats_dict)[source]
Replace the sufficient statistics of object instance with those containing matching keys in ‘stats_dict’.
Replaces comp_counts if weight_key is set and found in stats_dict. Replaces topic accumulators if ‘comp_key’ is set and found in stats_dict.
Calls key_replace() of the length accumulator.
- Parameters:
stats_dict (Dict[str, Any]) – Dictionary mapping keys to corresponding sufficient statistics.
- Returns:
None.
- Return type:
None
- acc_to_encoder()[source]
Return an HierarchicalMixtureDataEncoder object for encoding sequences of iid observations.
- Return type:
HierarchicalMixtureDataEncoder
- class HierarchicalMixtureEstimatorAccumulatorFactory(factories, num_mixtures, len_factory=NullAccumulatorFactory(), keys=(None, None))[source]
Bases:
StatisticAccumulatorFactoryHierarchicalMixtureEstimatorAccumulatorFactory object for creating HierarchicalMixtureEstimatorAccumulator objects.
- Parameters:
- make()[source]
Returns an HierarchicalMixtureEstimatorAccumulator object from attributes variables.
- Return type:
HierarchicalMixtureEstimatorAccumulator
- class HierarchicalMixtureEstimator(estimators, num_mixtures, len_estimator=NullEstimator(), len_dist=None, suff_stat=None, pseudo_count=None, name=None, keys=(None, None))[source]
Bases:
ParameterEstimatorHierarchicalMixtureEstimator object for estimating a HierarchicalMixtureDistribution from sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Create an HierarchicalMixtureEstimatorAccumulator from object instance.
- Return type:
HierarchicalMixtureEstimatorAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate HierarchicalMixtureDistribution from aggregated sufficient statistics.
- Arg suff_stat is a Tuple of length 3 containing,
suff_stat[0] (ndarray[float]): Aggregated component counts with shape (num_mixtures, num_topics). suff_stat[1] (Tuple[SS1,…]): Tuple of ‘num_topics’ sufficient statistics for the topics. suff_stat[2] (Optional[SS2]): Optional sufficient statistic for length accumulator.
- class HierarchicalMixtureDataEncoder(topic_encoder, len_encoder)[source]
Bases:
DataSequenceEncoderHierarchicalMixtureDataEncoder object for encoding sequences of iid hierarchical mixture observations.
- Parameters:
topic_encoder (DataSequenceEncoder)
len_encoder (DataSequenceEncoder)
- seq_encode(x)[source]
Encode a sequence of iid observations from a hierarchical mixture model.
- Returns ‘rv’ as a Tuple of length 5 containing:
rv[0] (int): Number of independent observations. rv[1] (ndarray[int]): Observation sequence index for each value in flattened x. rv[2] (ndarray[int]): Length of each observation in x. rv[3] (E): Encoded sequence of flattened observed values (has type E). rv[4] (Optional[E2]): Encoded sequence of lengths (has type E2).
- HierarchicalMixtureAccumulator
alias of
HierarchicalMixtureEstimatorAccumulator
- HierarchicalMixtureAccumulatorFactory
alias of
HierarchicalMixtureEstimatorAccumulatorFactory