mixle.stats.rankings.ewens module¶
Ewens distribution over permutations – the canonical cycle-structure (random-permutation) law.
A permutation is weighted by its number of cycles:
p(sigma) = theta^{cycles(sigma)} / Z, Z = theta (theta + 1) … (theta + n - 1) = Gamma(theta+n)/Gamma(theta).
theta = 1 is uniform over the n! permutations; theta -> 0 concentrates on single n-cycles
(few cycles); large theta concentrates on the identity (every point a fixed cycle). This is the
permutation form of the Ewens sampling formula (population genetics, random-permutation theory); its
induced partition by cycle sizes is what ChineseRestaurantProcessDistribution models.
Sufficient statistic: the cycle count, computed from the shared Cayley kernel
(cycles = n - cayley_distance(sigma, identity)). Sampling is an exact numba Chinese-restaurant /
Feller construction; theta is fit by matching the mean cycle count E[cycles] = sum_i theta/(theta+i).
Data type: List[int] – a permutation of 0..n-1 read as a function i -> sigma[i] (cycles of
that map are what the model scores).
- class EwensDistribution(dim, theta=1.0, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionEwens distribution over permutations of
0..n-1with cycle-weight parametertheta > 0.- 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.
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
EwensSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
EwensEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
EwensDataEncoder
- class EwensSampler(dist, seed=None)[source]
Bases:
DistributionSamplerExact Ewens draws via the numba Chinese-restaurant construction.
- Parameters:
dist (EwensDistribution)
seed (int | None)
- sample(size=None)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=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 independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class EwensAccumulator(dim, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the total (weighted) cycle count and observation weight.
- 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]
- Return type:
EwensAccumulator
- value()[source]
- from_value(x)[source]
- Return type:
EwensAccumulator
- 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:
EwensDataEncoder
- class EwensAccumulatorFactory(dim, keys=None)[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- Return type:
EwensAccumulator
- class EwensEstimator(dim, theta=None, name=None, keys=None)[source]
Bases:
ParameterEstimatorFit
thetaby matching the mean cycle countE[cycles] = sum_i theta/(theta+i).- accumulator_factory()[source]
- Return type:
EwensAccumulatorFactory