mixle.stats.rankings.matching module¶
Create, estimate, sample, and enumerate a weighted bipartite perfect-matching distribution.
Defines the MatchingDistribution, MatchingEnumerator, MatchingSampler, MatchingAccumulatorFactory, MatchingAccumulator, MatchingEstimator, and the MatchingDataEncoder classes for use with mixle.
Data type: a perfect matching of the complete bipartite graph K_{n,n} given as a permutation x of
0,…,n-1, where left node i is matched to right node x[i].
Each left-right edge (i, j) has a positive weight w[i, j]. A matching (permutation) sigma has
p(sigma) = prod_i w[i, sigma(i)] / Z, Z = sum over permutations of prod_i w[i, sigma(i)] = perm(W),
the matrix permanent. Unlike the Plackett-Luce model (a single worth vector over items), this scores a
full edge-weight matrix, so it is the natural assignment / matching law. The permanent is computed
exactly with Ryser’s formula, which is exponential in n, so the family targets small-to-moderate n
(default cap max_nodes = 12). Sampling draws each match in turn from the exact conditional
distribution (via permanents of the remaining submatrix); enumeration is lazy and streams matchings in
decreasing probability via Murty’s k-best assignment (no n! materialization). Estimation matches the empirical or symmetrically
smoothed assignment frequencies to the model edge marginals by projected gradient ascent on the
log-weights.
- class MatchingDistribution(weights, max_nodes=_DEFAULT_MAX_NODES, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionWeighted bipartite perfect-matching distribution over n left/right nodes (permanent-normalized).
Data type: a permutation x of 0,…,n-1 (left node i matched to right node x[i]).
- Parameters:
- classmethod compute_capabilities()[source]
- density(x)[source]
Return the probability of a matching x (a permutation).
- log_density(x)[source]
Return the log-probability of a matching x (left i matched to right x[i]).
- seq_log_density(x)[source]
Return vectorized log-probabilities for an (N, n) array of matchings.
- sampler(seed=None)[source]
Return a sampler for drawing matchings from this distribution.
- Parameters:
seed (int | None)
- Return type:
MatchingSampler
- enumerator()[source]
Return an exact finite enumerator over all matchings in decreasing probability order.
- Return type:
MatchingEnumerator
- estimator(pseudo_count=1.0)[source]
Return an estimator that keeps the node count fixed at this distribution’s n.
- Parameters:
pseudo_count (float | None)
- Return type:
MatchingEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
MatchingDataEncoder
- class MatchingEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate finite-probability perfect matchings in descending probability order.
Lazily, via Murty’s k-best assignment on the edge-cost matrix
-log(weights): decreasing probability is increasing assignment cost, and zero-weight edges become +inf costs (forbidden), so only positive-probability matchings are yielded. This streams the top matchings without materializing the n! permutation support.- Parameters:
dist (MatchingDistribution)
- class MatchingSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid matchings by sampling each left node’s match from the exact conditional permanent.
- Parameters:
dist (MatchingDistribution)
seed (int | None)
- class MatchingAccumulator(dim, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the weighted assignment-frequency matrix (the sufficient statistic for the weights).
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto 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. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
MatchingDataEncoder
- class MatchingAccumulatorFactory(dim, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for MatchingAccumulator.
- make()[source]
- Return type:
MatchingAccumulator
- class MatchingEstimator(dim, max_nodes=_DEFAULT_MAX_NODES, pseudo_count=1.0, max_steps=500, learning_rate=1.0, tol=1.0e-7, name=None, keys=None)[source]
Bases:
ParameterEstimatorMaximum-likelihood estimator for the edge weights (matches empirical and model edge marginals).
- Parameters:
- accumulator_factory()[source]
- Return type:
MatchingAccumulatorFactory