mixle.represent.posterior module

Posterior retrieval – nearest neighbours by what the MODEL believes, not by raw-feature cosine.

The differentiated half of model-based RAG: fit a mixture to heterogeneous records (mixle.propose() will happily do it), and retrieval similarity becomes posterior affinity – two records are close when the model’s field-restricted latent posteriors agree (per-field Bhattacharyya, the balanced affinity from mixle.utils.hvis), with the 1-nat evidence cap so a single wildly-different field can testify “these differ” but can never single-handedly veto a pair that every other field matches. Raw-feature cosine has neither property: it weights fields by their numeric scale, and one hot field dominates the dot product:

m = mixle.propose(records, fit=True)
r = PosteriorRetriever(m.fitted, records)          # any mixture over the records works
r.retrieve(query, k=5)                             # [(corpus index, log-affinity), ...]

Honest cost note: affinities are computed jointly over corpus + queries through the model’s per-field likelihoods – linear in rows for the model passes but quadratic for the affinity block, so this is built for corpora in the thousands, not millions. For big-corpus first-stage recall use mixle.represent.fit_embedder() and re-rank the shortlist here.

class PosteriorRetriever(model, corpus, *, evidence_cap=1.0, field_weights=None)[source]

Bases: object

Retrieve over raw heterogeneous records by the fitted mixture’s posterior affinity.

Parameters:
  • model (Any)

  • corpus (Any)

  • evidence_cap (float | None)

  • field_weights (Any)

affinity_matrix()[source]

The corpus’s dense (n, n) log-affinity matrix (diagonal -inf).

Return type:

ndarray

retrieve(query, k=5)[source]

Top-k corpus records for one query: [(corpus_index, log_affinity), ...] best first.

Parameters:
Return type:

list[tuple[int, float]]

retrieve_batch(queries, k=5)[source]

Top-k per query, computed in one joint pass over corpus + queries.

Parameters:
Return type:

list[list[tuple[int, float]]]