mixle.stats.latent.hidden_association module¶
Create, estimate, and sample from a hidden association model.
Defines the HiddenAssociationDistribution, HiddenAssociationSampler, HiddenAssociationAccumulatorFactory, HiddenAssociationAccumulator, HiddenAssociationEstimator, and the HiddenAssociationDataEncoder classes for use with mixle.
Consider a set of value V = {v_1,v_2,…,v_K} with data type T. Let the given density be discrete probability density over the values in V,
P_g(X_i = v_k) = p_g(k), for k = 1,2,….,K
where sum_k p_g(k) = 1.0. Consider M samples from P_g() denoted x = (x_1,x_2,…,x_M). We then introduce the latent variable U, where
p_k(x) = p_mat(U = v_k | x) = (# of x_1,…,x_M that are = to v_k) / M, for k = 1,2,…,K.
We then draw N a positive integer N from distribution P_len(), then draw N samples from the density above to get z = (z_1, z_2, …., z_N). Last we sample from the conditional distribution defined for P_c(Y = v_k | z_i) to obtain y = (y_1,…,y_N).
The log_density is given by,
log(p_mat(x,y)) = sum_{i=1}^{N} log(sum_{k=1}^{K} p_k(x)*P_c(y_i|v_k)) + log(P_g(x)) + log(P_len(N)).
Note: That in this model we consider grouped-counts. So the given data type is
x: Tuple[List[Tuple[T, float]], List[Tuple[T, float]]] = [x[0], x[1]],
where x[0] = [(value, count)] for the unique values of x_mat = (X_1,X_2,…,X_M) in V, and x[1] = [(value, count)] for the unique values of Y = (Y_1,…,Y_N) in V as well.
- class HiddenAssociationDistribution(cond_dist, given_dist=NullDistribution(), len_dist=NullDistribution(), name=None, keys=(None, None))[source]
Bases:
SequenceEncodableProbabilityDistributionHidden association model: values of a second set are emitted conditionally on values drawn from a first set.
- Parameters:
- compute_capabilities()[source]
Return backend capability metadata for this concrete hidden association model.
- compute_declaration()[source]
- log_density(x)[source]
Log-density of the hidden association model at observation x.
For each emitted value in x[1], marginalizes the conditional emission density over the given values in x[0] weighted by their counts, then adds the log-density of the given set under given_dist and of the total emission count under len_dist.
- seq_log_density(x)[source]
Evaluation of log-density at sequence encoded input x (loops over log_density).
- backend_seq_log_density(x, engine)[source]
Evaluate encoded log-densities through distribution-owned backend composition.
- emission_mixture(s1)[source]
The per-emission distribution
q(.|S1)as a mixture, or None for an empty/degenerate S1.q(emitted|S1) = sum_u (c_u/n1) P(emitted|u)is a finite mixture of the conditional emission distributionscond_dist.dmap[u]weighted by the given-bag’s normalized counts – enumerable whenever those component distributions are. Requirescond_distto be aConditionalDistribution(so the per-given components are available).
- enumerator()[source]
Enumerate
(S1, S2)observations in descending probability order.The model factors as
given_dist(S1) * [prod_e q(e|S1)^{c_e}] * P_len(n): the emitted bag S2 is drawn iid from the per-given mixtureq(.|S1)(seeemission_mixture()). Enumeration is a conditional product – the outer stream enumerates S1 fromgiven_distand, for each S1, the inner stream enumerates S2 as a multiset best-first search overq(.|S1)’s own enumeration underlen_dist, merged by descending total score withgiven_dist(S1)as the frontier bound. Requires an enumerable non-nullgiven_distand a ConditionalDistributioncond_dist.- Return type:
DistributionEnumerator
- sampler(seed=None)[source]
Create a HiddenAssociationSampler object from this distribution.
Requires non-null given_dist and len_dist.
- Parameters:
seed (Optional[int]) – Used to set seed in random sampler.
- Returns:
HiddenAssociationSampler object.
- Return type:
HiddenAssociationSampler
- estimator(pseudo_count=None)[source]
Create a HiddenAssociationEstimator from the component distributions’ estimators.
- Parameters:
pseudo_count (Optional[float]) – Unused (kept for protocol consistency).
- Returns:
HiddenAssociationEstimator object.
- Return type:
HiddenAssociationEstimator
- dist_to_encoder()[source]
Returns a HiddenAssociationDataEncoder object for encoding sequences of data.
- Return type:
HiddenAssociationDataEncoder
- class HiddenAssociationEnumerator(dist)[source]
Bases:
DistributionEnumerator- Parameters:
dist (HiddenAssociationDistribution)
- class HiddenAssociationSampler(dist, seed=None)[source]
Bases:
DistributionSamplerHiddenAssociationSampler object for drawing grouped-count set pairs from a HiddenAssociationDistribution.
- Parameters:
dist (HiddenAssociationDistribution)
seed (int | None)
- sample(size=None)[source]
Draw iid grouped-count observations from the hidden association model.
- Parameters:
size (Optional[int]) – Number of observations to draw. If None, a single observation is returned.
- Returns:
A ([(given value, count)], [(emitted value, count)]) tuple if size is None, else a list of such tuples of length size.
- Return type:
Sequence[tuple[list[tuple[Any, float]], list[tuple[Any, float]]]] | tuple[list[tuple[Any, float]], list[tuple[Any, float]]]
- class HiddenAssociationAccumulator(cond_acc, given_acc=NullAccumulator(), size_acc=NullAccumulator(), name=None, keys=(None, None))[source]
Bases:
SequenceEncodableStatisticAccumulatorHiddenAssociationAccumulator object for accumulating sufficient statistics from observed set pairs.
- Parameters:
- update(x, weight, estimate)[source]
Update sufficient statistics with the posterior assignment of emitted values to given values.
- initialize(x, weight, rng)[source]
Initialize sufficient statistics with random (Dirichlet) assignments of emitted to given values.
- seq_initialize(x, weights, rng)[source]
Initialize sufficient statistics from sequence encoded observations (loops over initialize()).
- seq_update(x, weights, estimate)[source]
Update sufficient statistics from sequence encoded observations (loops over update()).
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident E-step for the hidden association model.
Builds the batched cross product of (given, emitted) pairs across the whole minibatch, scores them through the conditional distribution’s engine kernel, and forms the per-emitted posterior over given values with a segmented softmax (global-max shift +
index_add) on the active engine. The engine-computed posterior weights are fed to the conditional accumulator; the given/size accumulators are updated per observation. Mirrorsupdate.
- combine(suff_stat)[source]
Merge sufficient statistics of suff_stat into this accumulator.
- Parameters:
suff_stat (Tuple[SS1, Optional[SS2], Optional[SS3]]) – Conditional, given, and size suff stats.
- Returns:
This HiddenAssociationAccumulator.
- Return type:
HiddenAssociationAccumulator
- value()[source]
Returns the sufficient statistics: (conditional, given, size) accumulator values.
- from_value(x)[source]
Set the sufficient statistics of this accumulator from x.
- Parameters:
x (Tuple[SS1, Optional[SS2], Optional[SS3]]) – Conditional, given, and size suff stats.
- Returns:
This HiddenAssociationAccumulator.
- Return type:
HiddenAssociationAccumulator
- scale(c)[source]
Scale sufficient statistics by delegating to child accumulators.
- Parameters:
c (float)
- Return type:
HiddenAssociationAccumulator
- key_merge(stats_dict)[source]
Merge keyed statistics of the conditional, given, and size accumulators into stats_dict.
- Parameters:
stats_dict (Dict[str, Any]) – Maps keys to merged sufficient statistics.
- Return type:
None
- key_replace(stats_dict)[source]
Replace keyed statistics of the conditional, given, and size accumulators with those in stats_dict.
- Parameters:
stats_dict (Dict[str, Any]) – Maps keys to merged sufficient statistics.
- Return type:
None
- acc_to_encoder()[source]
Returns a HiddenAssociationDataEncoder object for encoding sequences of data.
- Return type:
HiddenAssociationDataEncoder
- class HiddenAssociationAccumulatorFactory(cond_factory, given_factory=NullAccumulatorFactory(), len_factory=NullAccumulatorFactory(), name=None, keys=(None, None))[source]
Bases:
StatisticAccumulatorFactoryHiddenAssociationAccumulatorFactory object for creating HiddenAssociationAccumulator objects.
- Parameters:
- make()[source]
Returns a new HiddenAssociationAccumulator object.
- Return type:
HiddenAssociationAccumulator
- class HiddenAssociationEstimator(cond_estimator, given_estimator=NullEstimator(), len_estimator=NullEstimator(), pseudo_count=None, name=None, keys=(None, None))[source]
Bases:
ParameterEstimatorHiddenAssociationEstimator object for estimating a HiddenAssociationDistribution from aggregated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Returns a HiddenAssociationAccumulatorFactory for creating HiddenAssociationAccumulator objects.
- Return type:
HiddenAssociationAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a HiddenAssociationDistribution from aggregated sufficient statistics.
- Parameters:
nobs (Optional[float]) – Number of observations, passed to the given and length estimators.
suff_stat (Tuple[SS1, Optional[SS2], Optional[SS3]]) – Conditional, given, and size suff stats.
- Returns:
HiddenAssociationDistribution object.
- Return type:
HiddenAssociationDistribution
- class HiddenAssociationDataEncoder[source]
Bases:
DataSequenceEncoderHiddenAssociationDataEncoder object for encoding sequences of iid grouped-count set pair observations.
- seq_encode(x)[source]
Encode a sequence of iid grouped-count observations (identity encoding).
- Parameters:
x (Sequence[Tuple[List[Tuple[T, float]], List[Tuple[T, float]]]]) – Sequence of iid ([(given value, count)], [(emitted value, count)]) observations.
- Returns:
The observations unchanged (seq_log_density and seq_update loop over them).
- Return type:
Sequence[tuple[list[tuple[T, float]], list[tuple[T, float]]]]