mixle.stats.rankings.low_rank_permutation module

Low-rank assignment (first-order Fourier) model over permutations.

A permutation Gibbs model that scores an ordering by a low-rank item-by-rank score matrix:

p(sigma) = exp( sum_r S[sigma[r], r] ) / Z, S = U V^T (rank r << n), Z = permanent(exp(S)).

This is the maximum-entropy distribution matching first-order (item-at-rank) marginals – the first-order term of the Fourier / coset expansion on the symmetric group – and the low-rank factorization S = U V^T is the structured, O(n r)-parameter version of the full MatchingDistribution. The normalizer is a permanent (#P-hard), so it is computed exactly by a numba Ryser kernel for n <= max_exact and approximated by a numba Sinkhorn / Bethe permanent beyond that (documented approximation). Fitting is a Sinkhorn-marginal gradient ascent on U, V toward the empirical item-by-rank marginals.

Data type: List[int] – a full ordering, a permutation of 0..n-1 with x[r] the item at rank r (best first).

class LowRankPermutationDistribution(u, v, name=None, keys=None, max_exact=12, sinkhorn_iter=200)[source]

Bases: SequenceEncodableProbabilityDistribution

Permutation Gibbs model with a low-rank item-by-rank score matrix S = U V^T.

Parameters:
  • u (np.ndarray)

  • v (np.ndarray)

  • name (str | None)

  • keys (str | None)

  • max_exact (int)

  • sinkhorn_iter (int)

classmethod compute_capabilities()[source]
density(x)[source]

Return the probability density or mass at a single observation.

Concrete default: exponentiate log_density (the abstract method subclasses must provide). Leaves with a cheaper closed form may override this.

Parameters:

x (Sequence[int])

Return type:

float

log_density(x)[source]

Return the log-density or log-mass at a single observation.

Parameters:

x (Sequence[int])

Return type:

float

seq_log_density(x)[source]

Return vectorized log-density values for sequence-encoded observations.

Parameters:

x (ndarray)

Return type:

ndarray

marginals()[source]

Model first-order marginals P[item, rank] (Sinkhorn doubly-stochastic transport plan).

Return type:

ndarray

sampler(seed=None)[source]

Return a sampler for drawing observations from this distribution.

Parameters:

seed (int | None)

Return type:

LowRankPermutationSampler

estimator(pseudo_count=None)[source]

Return an estimator for fitting this distribution from data.

Parameters:

pseudo_count (float | None)

Return type:

LowRankPermutationEstimator

dist_to_encoder()[source]

Return the data encoder used by this distribution for vectorized methods.

Return type:

LowRankPermutationDataEncoder

class LowRankPermutationSampler(dist, seed=None, burn=2000, thin=20)[source]

Bases: DistributionSampler

Draw orderings via a numba Metropolis sampler on the assignment scores.

Parameters:
  • dist (LowRankPermutationDistribution)

  • seed (int | None)

  • burn (int)

  • thin (int)

sample(size=None)[source]

Draw observations.

Combinator samplers (mixture/sequence/…) accept batched. With batched=True (the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independent RandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path. batched=False forces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.

Parameters:

size (int | None)

Return type:

list[int] | list[list[int]]

class LowRankPermutationAccumulator(dim, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate the item-by-rank count matrix C[item, rank] – the first-order sufficient statistic.

Parameters:
update(x, weight, estimate)[source]
Parameters:
Return type:

None

initialize(x, weight, rng)[source]
Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]
Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]
Parameters:
Return type:

None

combine(suff_stat)[source]
Return type:

LowRankPermutationAccumulator

value()[source]
from_value(x)[source]
Return type:

LowRankPermutationAccumulator

key_merge(stats_dict)[source]

Pool this accumulator’s statistics into stats_dict under its merge key.

The structural default implements the common single-key pattern: store the accumulator under self.keys the first time the key is seen, else combine into the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. A keys of None (the default) is a no-op.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator’s statistics from the pooled stats_dict entry (see key_merge).

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]
Return type:

LowRankPermutationDataEncoder

class LowRankPermutationAccumulatorFactory(dim, keys=None)[source]

Bases: StatisticAccumulatorFactory

Parameters:
make()[source]
Return type:

LowRankPermutationAccumulator

class LowRankPermutationEstimator(dim, rank=2, max_exact=12, sinkhorn_iter=200, max_iter=300, lr=0.5, name=None, keys=None)[source]

Bases: ParameterEstimator

Fit U, V by Sinkhorn-marginal gradient ascent toward the empirical item-by-rank marginals.

Parameters:
accumulator_factory()[source]
Return type:

LowRankPermutationAccumulatorFactory

estimate(nobs, suff_stat)[source]
Parameters:

nobs (float | None)

Return type:

LowRankPermutationDistribution

class LowRankPermutationDataEncoder(dim=None)[source]

Bases: DataSequenceEncoder

Encode a sequence of orderings (permutations of 0,…,n-1) into an (N, n) integer array.

Parameters:

dim (int | None)

seq_encode(x)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

x (Sequence[Sequence[int]])

Return type:

ndarray