mixle.stats.rankings.generalized_mallows module¶
Generalized Mallows distribution over permutations under a choice of distance metric.
The Mallows family concentrates probability around a central permutation sigma0 with dispersion
theta >= 0:
p(sigma) = exp(-theta * d(sigma, sigma0)) / Z(theta, n),
generalizing MallowsDistribution (Kendall-only) to any of the six
metrics in mixle.stats.rankings._permutation_kernels. The per-datum distance is the numba kernel;
this module supplies the metric-specific normalizer Z. Three metrics have a closed-form (fast-DP)
normalizer and moment E_theta[d]:
kendall Z = prod_{i=1}^{n-1} (1 - phi^{i+1}) / (1 - phi) (phi = e^{-theta}) cayley Z = prod_{i=1}^{n-1} (1 + i phi) hamming Z = sum_{m=0}^{n} C(n, m) D_m phi^m (D_m = subfactorial)
The other three are #P-hard and use an exact small-n normalizer with a numba Monte-Carlo fallback
beyond a size cap (the fallback is an approximation, controlled by n_mc / seed):
footrule Z = perm(phi^{|i-j|}) exact Ryser permanent for n <= max_exact (16), else MC spearman Z = perm(phi^{(i-j)^2}) exact Ryser permanent for n <= max_exact (16), else MC ulam Z = sum over the LIS-distance histogram, exact for n <= max_enum (9), else MC
Data type: List[int] – a full ordering, a permutation of 0..n-1 with x[r] the item at rank
r (best first).
- class GeneralizedMallowsDistribution(sigma0, theta=1.0, metric='kendall', name=None, keys=None, n_mc=20000, seed=0, max_exact=16, max_enum=9)[source]
Bases:
SequenceEncodableProbabilityDistributionMallows distribution under a configurable distance
metric(closed-form normalizer metrics).- Parameters:
- classmethod compute_capabilities()[source]
Declare the NumPy and numba execution path used by Mallows kernels.
- distance(x)[source]
Distance between ordering
xand the central permutation under this metric.
- density(x)[source]
Return the probability of one ordering under the Mallows model.
- log_density(x)[source]
Return the log-probability of one ordering.
- seq_log_density(x)[source]
Return vectorized log-probabilities for encoded orderings.
- sampler(seed=None)[source]
Return a sampler for this Mallows distribution.
- Parameters:
seed (int | None)
- Return type:
GeneralizedMallowsSampler
- estimator(pseudo_count=None)[source]
Return a Mallows estimator with this metric and item count.
- Parameters:
pseudo_count (float | None)
- Return type:
GeneralizedMallowsEstimator
- dist_to_encoder()[source]
Return the full-ranking encoder used by vectorized methods.
- Return type:
GeneralizedMallowsDataEncoder
- class GeneralizedMallowsSampler(dist, seed=None, burn=1000, thin=10)[source]
Bases:
DistributionSamplerDraw orderings via the exact RIM (Kendall) or a numba Metropolis sampler (other metrics).
- class GeneralizedMallowsAccumulator(dim, reservoir=10000, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the rank-count matrix, the precede matrix, and a bounded reservoir of orderings.
rank_count[item, rank]andprecede[a, b]give the consensus (Borda / Copeland); the reservoir supplies the empirical mean metric-distance to the fitted center (exact when the data fit in it).- update(x, weight, estimate)[source]
Update rank, precedence, and reservoir statistics from one ordering.
- initialize(x, weight, rng)[source]
Initialize statistics from one weighted ordering.
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
Update rank, precedence, and reservoir statistics from encoded orderings.
- seq_initialize(x, weights, rng)[source]
Initialize statistics from encoded orderings.
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge consensus statistics and reservoir samples from another accumulator.
- Return type:
GeneralizedMallowsAccumulator
- value()[source]
Return count, consensus matrices, and bounded reservoir contents.
- from_value(x)[source]
Restore accumulator state from
valueoutput.- Return type:
GeneralizedMallowsAccumulator
- key_merge(stats_dict)[source]
Merge this accumulator into
stats_dictunder its configured key.
- key_replace(stats_dict)[source]
Replace this accumulator’s state from keyed statistics when present.
- acc_to_encoder()[source]
Return the ranking encoder compatible with these sufficient statistics.
- Return type:
GeneralizedMallowsDataEncoder
- class GeneralizedMallowsAccumulatorFactory(dim, reservoir=10000, keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate accumulators for generalized Mallows consensus statistics.
- make()[source]
Create an empty generalized Mallows accumulator.
- Return type:
GeneralizedMallowsAccumulator
- class GeneralizedMallowsEstimator(dim, metric='kendall', theta=None, reservoir=10000, name=None, keys=None, n_mc=20000, seed=0, max_exact=16, max_enum=9)[source]
Bases:
ParameterEstimatorEstimate the central permutation (Copeland/Borda) and dispersion theta (moment match).
- Parameters:
- accumulator_factory()[source]
Return a factory for Mallows sufficient-statistic accumulators.
- Return type:
GeneralizedMallowsAccumulatorFactory