mixle.stats.rankings.spearman_rho module¶
Create, estimate, and sample from a Spearman ranking distribution.
Defines the SpearmanRankingDistribution, SpearmanRankingSampler, SpearmanRankingAccumulatorFactory, SpearmanRankingAccumulator, SpearmanRankingEstimator, and the SpearmanRankingDataEncoder classes for use with mixle.
Data type: List[int] (Component-wise rank of K dimensional observation vector)
The Spearman ranking distribution with dimension K, has probability function
p_mat(x_k;rho, sigma) = exp(-rho * ||x_k-sigma||^2 ) / sum_{k=0}^{K-1} exp(-rho * ||x_k-sigma||^2 ), for k = 0,1,..,K-1
where x_k list of integers containing a permutation of the integers 0,1,2,…K-1. Note sigma is a list of floats with dimension equal to K representing the mean of the rank variables, and rho is a correlation coefficient.
- class SpearmanRankingDistribution(sigma, rho=1.0, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionSpearman ranking distribution over permutations of 0,…,K-1 with location sigma and decay rate rho.
Data type: List[int] (a permutation of the integers 0,1,…,K-1).
- classmethod compute_capabilities()[source]
- classmethod compute_declaration()[source]
- static backend_legacy_sufficient_statistics(x, params, engine)[source]
Return row-wise legacy sufficient statistics for resident reductions.
- static backend_log_density_from_params(x, sigma, rho, log_const, engine)[source]
Engine-neutral Spearman ranking log-density from fitted parameters.
- density(x)[source]
Density of Spearman ranking distribution at observation x.
See log_density() for details.
- log_density(x)[source]
Log-density of Spearman ranking distribution at observation x.
The log-density is given by
log(p(x; rho, sigma)) = -rho * ||x - sigma||^2 - log_const,
where log_const normalizes over all K! permutations.
- seq_log_density(x)[source]
Vectorized evaluation of log-density at sequence encoded input x.
- Parameters:
x (np.ndarray) – 2-d numpy array of N permutations with K columns.
- Returns:
Numpy array of log-density (float) of length N.
- Return type:
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded permutations.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked parameters for equal-dimensional Spearman ranking mixtures.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of Spearman ranking component log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return component-stacked legacy
(count, weighted_rank_sum)statistics.
- sampler(seed=None)[source]
Create a SpearmanRankingSampler object from parameters of SpearmanRankingDistribution instance.
- Parameters:
seed (Optional[int]) – Used to set seed in random sampler.
- Returns:
SpearmanRankingSampler object.
- Return type:
SpearmanRankingSampler
- estimator(pseudo_count=None)[source]
Create a SpearmanRankingEstimator with matching dimension and concentration rho.
- Parameters:
pseudo_count (Optional[float]) – Used to inflate sufficient statistics.
- Returns:
SpearmanRankingEstimator object.
- Return type:
SpearmanRankingEstimator
- dist_to_encoder()[source]
Returns a SpearmanRankingDataEncoder object for encoding sequences of data.
- Return type:
SpearmanRankingDataEncoder
- enumerator()[source]
Returns SpearmanRankingEnumerator iterating permutations in descending probability order.
- Return type:
SpearmanRankingEnumerator
- class SpearmanRankingEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate permutations in descending Spearman probability order, lazily.
The Spearman distance
sum_i (x_i - sigma_i)^2is a linear assignment cost (assigning value j to position i costs(j - sigma_i)^2), so descending probability is increasing assignment cost: Murty’s k-best assignment streams the permutations in order without materializing the K! support.- Parameters:
dist (SpearmanRankingDistribution)
- class SpearmanRankingSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for the SpearmanRankingDistribution. Draws permutations of 0,…,K-1.
- Parameters:
dist (SpearmanRankingDistribution)
seed (int | None)
- sample(size=None)[source]
Draw iid samples (permutations of 0,…,K-1) from the Spearman ranking distribution.
- class SpearmanRankingAccumulator(dim, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for the SpearmanRankingDistribution. Tracks the weighted sum of ranks and total weight.
- update(x, weight, estimate)[source]
Update sufficient statistics with a weighted observation.
- initialize(x, weight, rng)[source]
Initialize sufficient statistics with a weighted observation.
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from sequence encoded data.
- Parameters:
x (np.ndarray) – 2-d numpy array of N permutations with K columns.
weights (np.ndarray) – Weights for each of the N observations.
estimate (Optional[SpearmanRankingDistribution]) – Previous estimate (unused).
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of sufficient statistics from sequence encoded data.
- Parameters:
x (np.ndarray) – 2-d numpy array of N permutations with K columns.
weights (np.ndarray) – Weights for each of the N observations.
rng (RandomState) – Random number generator (unused).
- Return type:
None
- combine(suff_stat)[source]
Combine sufficient statistics from another accumulator into this one.
- Parameters:
suff_stat (Tuple[float, np.ndarray]) – Tuple of count and component-wise rank sums.
- Returns:
Self, with aggregated sufficient statistics.
- Return type:
SpearmanRankingAccumulator
- value()[source]
Returns sufficient statistics as a Tuple of count and component-wise rank sums.
- from_value(x)[source]
Set sufficient statistics of accumulator from value x.
- Parameters:
x (Tuple[float, np.ndarray]) – Tuple of count and component-wise rank sums.
- Returns:
Self, with sufficient statistics set to x.
- Return type:
SpearmanRankingAccumulator
- key_merge(stats_dict)[source]
Merge sufficient statistics of object instance with suff stats containing matching keys.
- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to shared sufficient statistics.
- Returns:
None.
- Return type:
None
- key_replace(stats_dict)[source]
Set sufficient statistics of object instance to suff stats with matching keys.
- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to shared sufficient statistics.
- Returns:
None.
- Return type:
None
- acc_to_encoder()[source]
Returns a SpearmanRankingDataEncoder object for encoding sequences of data.
- Return type:
SpearmanRankingDataEncoder
- class SpearmanRankingAccumulatorFactory(dim, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for creating SpearmanRankingAccumulator objects.
- make()[source]
Returns a new SpearmanRankingAccumulator object.
- Return type:
SpearmanRankingAccumulator
- class SpearmanRankingEstimator(dim, rho=None, pseudo_count=None, suff_stat=None, name=None, keys=None, max_rho=1.0e6)[source]
Bases:
ParameterEstimatorEstimator for the SpearmanRankingDistribution from aggregated sufficient statistics.
The consensus ranking sigma and, by default, the concentration rho are estimated by maximum likelihood. Pass a numeric rho to hold the concentration fixed.
- Parameters:
- accumulator_factory()[source]
Returns a SpearmanRankingAccumulatorFactory for creating SpearmanRankingAccumulator objects.
- Return type:
SpearmanRankingAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a SpearmanRankingDistribution from sufficient statistics.
The consensus ranking sigma is the maximum likelihood estimate, given by the rank order (argsort) of the component-wise rank sums. When rho is None, the concentration is the nonnegative MLE satisfying E_rho[||X-sigma||^2] = observed mean squared distance. If no data was observed, rho is set to 0.0.
- class SpearmanRankingDataEncoder[source]
Bases:
DataSequenceEncoderData encoder for sequences of rank vector (permutation) observations.