mixle.enumeration.algorithms module¶
Back-compat facade for the smart-enumeration machinery.
The implementation was split by concern (it had grown into one ~1100-line module mixing three distinct things):
generic stream primitives ->
mixle.enumeration.streamsbest-first / product search ->
mixle.enumeration.best_firstthe quantized seek / unrank index ->
mixle.enumeration.quantization.seek
This module re-exports them so from mixle.enumeration.algorithms import X keeps working unchanged.
New code can import from the modules above directly. See
mixle.stats.compute.pdist.DistributionEnumerator for the enumeration contract.
- class BufferedStream(it)[source]
Bases:
objectRandom access by rank into a lazy stream of (value, log_prob) pairs.
get(i) extends an internal buffer as needed and returns the i-th item, or None if the stream has fewer than i+1 items. The underlying stream is consumed at most once regardless of how many consumers share this object.
- freeze(x)[source]
Return a canonical hashable key for x, for de-duplicating support values.
Lists/tuples freeze element-wise to tuples, dicts to frozensets of (key, value) pairs, sets to frozensets, numpy arrays to (shape, bytes), numpy scalars to their python equivalents, and NaN to a shared sentinel (so nan == nan for dedup purposes). Raises TypeError for values that cannot be canonicalized.
- merge_enumerators(streams, offsets)[source]
Lazy k-way merge of sorted (value, log_prob) streams with per-stream offsets.
Stream k’s log probs are shifted by offsets[k]. Correct only when the streams have pairwise disjoint supports (no de-duplication or re-scoring is performed).
- supports_enumeration(dist)[source]
Return True if dist.enumerator() can be constructed.
- Return type:
- class ProductEnumerator(streams, combine=tuple, offset=0.0)[source]
Bases:
objectBest-first enumeration of the Cartesian product of sorted child streams.
Yields (combine(values), log_prob) with log_prob = offset + sum of child log probs, in non-increasing order. Standard k-best lattice search: a max-heap over index tuples, with successors advancing one coordinate; correctness follows from each child stream being sorted (coordinate-wise monotonicity).
Duplicate-free successor rule: each heap entry carries the lowest coordinate it is allowed to advance, and a node only spawns successors for coordinates at or above that index. Every tuple is then reached by exactly one path – the one that increments coordinates in non-decreasing index order – so no
visitedset (and no O(n) per-successor tuple hashing) is needed. Best-first order is preserved because each successor’s score is <= its generating node’s (a sorted coordinate only decreases when advanced), so canonical edges are score-monotone.
- class LengthFrontierMerge(len_stream, make_stream)[source]
Bases:
objectMerge per-length sorted streams, instantiating lengths lazily from a sorted length stream.
len_stream yields (length, log_prob_of_length) in descending order. make_stream(length, lp_len) returns a sorted iterator of (value, log_prob) whose log probs already include lp_len and never exceed it (true whenever per-element contributions are log probs <= 0). The next un-instantiated length’s lp_len is then a valid upper bound on anything its stream could produce, so lengths are instantiated only when they can beat the best instantiated head. Supports of distinct lengths must be disjoint (no de-duplication).
- frontier_merge(outer_stream, make_stream)[source]
Merge per-outer-key inner streams, instantiating outer keys lazily from a sorted outer stream.
The general form of
LengthFrontierMergefor arbitrary (not necessarily integer) outer keys:outer_streamyields(key, lp_key)in descendinglp_keyorder, andmake_stream(key, lp_key)returns a sorted(value, log_prob)iterator whose log-probs are<= lp_key(true whenever the inner contribution is<= 0andlp_keyis folded in as an offset). The next un-instantiated key’slp_keyis then an upper bound on anything its stream could produce, so keys are instantiated only when they can beat the best instantiated head. Supports of distinct keys must be disjoint (no de-duplication). Used for conditional products where the inner distribution depends on the outer value (e.g. hidden-association S2 given S1).
- best_first_union(streams, log_offsets, exact_log_density, tol=1.0e-10)[source]
Enumerate the union of sorted streams with overlapping supports.
Candidate values are pulled from the streams (stream k shifted by log_offsets[k]), de-duplicated via freeze, re-scored exactly with exact_log_density, and buffered until their exact score is at least the upper bound on any not-yet-seen value: logsumexp_k(log_offsets[k] + head_lp_k). This is the mixture algorithm: any unseen x satisfies p_k(x) <= head_k for every k, hence sum_k w_k p_k(x) <= exp(bound).
- best_first_union_max(streams, log_offsets, exact_log_density, tol=1.0e-10)[source]
Like best_first_union, but for a max-scored union (bound = max over heads).
Used to enumerate a deduped symbol pool ordered by max-over-states emission probability for markov/HMM enumerators.
- bounded_best_first_union_index(streams, log_offsets, exact_log_density, max_bits, bin_width_bits=1.0, tol=1.0e-10, component_log_density=None)[source]
Build a quantized index from a globally bounded best-first union.
This is the index-building counterpart of
best_first_union. It is useful for mixtures: if each stream head bounds one component contribution, then the log-sum of live heads bounds every unseen value. Once that global frontier is below the requested probability threshold, all remaining qualifying values must already be in the exact-score buffer, so the index can stop without enumerating a looser per-component candidate prefix.
- sound_top_k(dist, k, start=0, budget_bits=40.0, oversample=8, bin_width_bits=1.0, max_budget_bits=512.0, total_mass=1.0, tol=1.0e-12)[source]
Exact true-descending observations ranked
[start, start+k)for ANY normalized model.Mass-threshold certificate, correct regardless of the seek stream’s ordering (so it holds for deep/nested mixtures and HMMs where the tropical order is badly displaced): pull distinct
(value, log_prob)from the count-budget index, accumulate exact probability mass, and keep the beststart+kby probability. Since every unpulled item’s probability is at most the remaining masstotal_mass - accumulated, onceremainingdrops below the (start+k)-th best probability the heap is exactly the true topstart+k; returnsorted_desc[start:start+k]. If the in-budget stream is exhausted first, the budget is doubled (up tomax_budget_bits).total_mass(default 1.0) may be a known upper bound for sub-normalized models.Cost is
O(number pulled)– small for peaked distributions, large for flat ones, so for top-k from rank 0dist.enumerator()is usually leaner; this adds the soundness certificate and the arbitrary start offset the sequential enumerator lacks.
- class QuantizedEnumerationIndex(bins, bin_width_bits, max_bits, truncated)[source]
Bases:
objectBounded, indexable view of an exact probability-ordered enumeration.
Items are grouped into log-probability bins measured in bits:
bits(x) = -log2(p(x)) bin(x) = floor(bits(x) / bin_width_bits)
Only items whose bit cost is at most max_bits are indexed. The returned (value, log_prob) pairs always carry the original exact log probability; the quantized bins are used only for counting and rank lookup.
The generic implementation can build from an exact enumerator stream, so it works for every currently enumerable discrete stats model. Simple distributions may also build directly from scored support items, and distribution-specific dynamic programs can produce the same bin layout without relying on an exact global stream.
- Parameters:
- static bin_for_log_prob(log_prob, bin_width_bits=1.0)[source]
Return the quantized bit bin for a log probability.
- classmethod from_enumerator(enum, max_bits, bin_width_bits=1.0)[source]
Build an index from an exact non-increasing-probability enumerator.
- Parameters:
- Returns:
QuantizedEnumerationIndex over the bounded prefix/domain slice.
- Return type:
QuantizedEnumerationIndex
- classmethod from_items(items, max_bits, bin_width_bits=1.0, sorted_items=False, truncated=None)[source]
Build an index from known support items and exact log probabilities.
- Parameters:
items (Sequence[tuple[Any, float]]) – Finite collection of (value, log_prob) pairs. Zero-probability items with log_prob == -inf are ignored.
max_bits (float) – Include only values with -log2(p) <= max_bits.
bin_width_bits (float) – Width of each quantized probability bin in bits.
sorted_items (bool) – If True, preserve the input order. Otherwise, stable-sort included items by descending log probability.
truncated (bool | None) – Optional explicit truncation flag. If omitted, it is True when any finite-probability item was outside the bit bound.
- Returns:
QuantizedEnumerationIndex over the bounded finite item set.
- Return type:
QuantizedEnumerationIndex
- bin_for_index(index)[source]
Return (bin_id, offset_within_bin) for a bounded quantized rank.
- get(index)[source]
Return the indexed (value, exact_log_prob) pair.
- slice(start, k)[source]
Return up to k indexed pairs starting at start.
- iter_from(start=0)[source]
Iterate indexed pairs from start to the end of the bounded index.
- bin_items(bin_id)[source]
Return all indexed items in a quantized probability bin.
- class LazyQuantizedEnumerationIndex(counts, bin_width_bits, max_bits, truncated, getter)[source]
Bases:
QuantizedEnumerationIndexQuantized index whose bin counts are precomputed but items are unranked lazily.
This supports compositional distributions where a dynamic program can count how many values fall in each quantized log-density bin without materializing every Cartesian-product value. The getter receives a bin id and offset within that bin and returns the exact (value, log_prob) pair for that quantized rank.
- Parameters:
- bin_for_index(index)[source]
Return (bin_id, offset_within_bin) for a bounded quantized rank.
- get(index)[source]
Return the indexed (value, exact_log_prob) pair.
- iter_from(start=0)[source]
Iterate indexed pairs from start to the end of the bounded index.
- class QuantizedCrossIndex(items, max_bits, bin_width_bits, truncated=False)[source]
Bases:
objectAligned support rows for multiple distributions under quantized bit bounds.
Each row is (value, log_probs), where log_probs[j] is the exact log-density of value under component j, or -inf when that component assigns zero mass. Rows are included when any component’s bit cost is within its requested bound. The joint bin counts expose the cross-bin alignment that mixtures need and marginal bin counts cannot provide.
- Parameters:
- static bits_for_log_prob(log_prob)[source]
Return -log2(p), using inf for zero probability.
- classmethod from_items(items, max_bits, bin_width_bits=1.0, truncated=False)[source]
Build a cross index from exact aligned support rows.
- iter_items()[source]
Iterate aligned support rows.