mixle.enumeration.quantization.seek module¶
The quantized seek / unrank index over an exact probability-ordered enumeration.
QuantizedEnumerationIndex precomputes how many support values fall in each quantized
log-probability bin, so an arbitrary rank can be resolved and unranked without walking the prefix;
LazyQuantizedEnumerationIndex builds it on demand and QuantizedCrossIndex indexes the product
of two such enumerations. This is the finite/materialized seek index; its exponential-support
counterpart – the structural count-budget index – lives in mixle.enumeration.quantization.core.
- 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.
- quantized_index(enum, max_bits, bin_width_bits=1.0)[source]
Convenience wrapper for QuantizedEnumerationIndex.from_enumerator.
- 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.