mixle.stats.rankings.spearman_rho module¶
Spearman-rho distribution over full item orderings.
Observations use the ranking convention shared by the other ranking families:
x[rank] = item. The location parameter is a (possibly fractional) mean
rank vector, sigma[item] = expected rank. Use ItemOrdering and
RankVector when a boundary needs to make that distinction explicit.
For an ordering x, let r be its inverse rank vector. Then
p(x) = exp(-rho * sum_item (r[item] - sigma[item])**2) / Z.
The normalizer is the permanent of the rank-by-item log-weight matrix. It is computed by exact subset dynamic programming under an explicit dimension budget; no factorial permutation array is materialized or globally cached. Sampling uses exact conditional permanents and enumeration uses lazy k-best assignment search.
- class ItemOrdering(values)[source]
-
A permutation whose entry at each rank is the item occupying that rank.
- to_rank_vector()[source]
Return the inverse permutation, indexed by item.
- Return type:
RankVector
- class RankVector(values)[source]
-
A permutation whose entry for each item is that item’s rank.
- to_item_ordering()[source]
Return the inverse permutation, indexed by rank.
- Return type:
ItemOrdering
- class SpearmanRankingFitDiagnostics(concentration_algorithm, converged, iterations, boundary_solution, regularized, pseudo_count)[source]
Bases:
objectConcentration-fit and regularization evidence attached to an estimate.
- class SpearmanRankingDistribution(sigma, rho=1.0, name=None, keys=None, max_dim=_DEFAULT_MAX_DIM, fit_diagnostics=None, *, _sigma_already_centered=False, _sigma_raw_scale_hint=None)[source]
Bases:
SequenceEncodableProbabilityDistributionExact finite Spearman law over item orderings under a dimension budget.
Known limitation: direct construction from a raw location vector (this class’s
sigmaargument – see the module docstring’s “possibly fractional mean rank vector”) whose entries share an extremely large common offset – beyond_TRUSTED_SIGMA_RAW_SCALE_CEILINGinspearman_rho.py, roughly the range of a 32-bit integer (~2.1e9) – succeeds here, but the one-time float64 cancellation__init__performs while centering it can leave a residual that a later restore (__pysp_setstate__) cannot always distinguish from genuine state corruption, and may then reject with aValueErroreven though it was built validly. The original, pre-centering magnitude is not recoverable from the restored, already-centered state, so this is an accepted, documented limit rather than a bug to silently work around – see the comment above_validate_locationin this module for the full reasoning and exact boundary.- Parameters:
- classmethod compute_capabilities()[source]
Declare generated NumPy and Torch scoring support.
- classmethod compute_declaration()[source]
Return the generated-compute declaration.
- static backend_legacy_sufficient_statistics(x, params, engine)[source]
Return row-wise count and item-indexed rank-vector statistics.
- static backend_log_density_from_params(x, sigma, rho, log_const, engine)[source]
Score item orderings after explicit conversion to rank vectors.
Callers arrive under two shape conventions.
backend_seq_log_densitypasses the natural ones – ranks(n, d)against a singlesigma(d,). The generated scorer inmixle.stats.compute.declarationspre-aligns both sides instead: data picks up a component axis ((n, 1, d)) and every parameter a leading row axis ((1, k, d)), so the two already broadcast. Inserting a component axis unconditionally, as this did, double-counts it on that path and returns an(n, 1, k)score where the contract is(n, k). Only add the axis when the ranks really are still two-dimensional.
- property sigma: ndarray
Return an ownership-safe copy of the canonical mean rank vector.
- density(x)[source]
Return the probability of an item ordering.
- log_density(x)[source]
Return the log-probability of one
x[rank] = itemordering.
- seq_log_density(x)[source]
Return vectorized log-probabilities for item orderings.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized scoring for encoded item orderings.
- classmethod backend_stacked_params(dists, engine)[source]
Stack equal-dimensional Spearman parameters.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an observation-by-component score matrix.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return component-stacked item-indexed rank-vector statistics.
- sampler(seed=None)[source]
Return an exact conditional-permanent sampler.
- Parameters:
seed (int | None)
- Return type:
SpearmanRankingSampler
- estimator(pseudo_count=None)[source]
Return an estimator preserving the dimension and resource budget.
- Parameters:
pseudo_count (float | None)
- Return type:
SpearmanRankingEstimator
- dist_to_encoder()[source]
Return the item-ordering encoder.
- Return type:
SpearmanRankingDataEncoder
- enumerator()[source]
Return a lazy descending-probability enumerator.
- Return type:
SpearmanRankingEnumerator
- class SpearmanRankingEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate item orderings by increasing rank-to-item assignment cost.
- Parameters:
dist (SpearmanRankingDistribution)
- class SpearmanRankingSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw exact Spearman item orderings using conditional permanents.
- Parameters:
dist (SpearmanRankingDistribution)
seed (int | None)
- class SpearmanRankingAccumulator(dim, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate item-indexed rank-vector sums from item orderings.
- update(x, weight, estimate)[source]
Update from one item ordering.
- initialize(x, weight, rng)[source]
Initialize from one item ordering.
- Parameters:
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
Update from encoded item orderings.
- seq_initialize(x, weights, rng)[source]
Initialize from encoded item orderings.
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge validated rank-vector statistics.
- value()[source]
Return an ownership-safe sufficient-statistic snapshot.
- from_value(x)[source]
Restore validated rank-vector statistics.
- acc_to_encoder()[source]
Return an encoder with the matching item count.
- Return type:
SpearmanRankingDataEncoder
- class SpearmanRankingAccumulatorFactory(dim, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Spearman ranking accumulators.
- make()[source]
Return a fresh accumulator.
- Return type:
SpearmanRankingAccumulator
- class SpearmanRankingEstimator(dim, rho=None, pseudo_count=None, suff_stat=None, name=None, keys=None, max_rho=1.0e6, max_dim=_DEFAULT_MAX_DIM)[source]
Bases:
ParameterEstimatorEstimate consensus mean ranks and nonnegative Spearman concentration.
- Parameters:
- accumulator_factory()[source]
Return a factory for item-ordering sufficient statistics.
- Return type:
SpearmanRankingAccumulatorFactory