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: SequenceEncodableProbabilityDistribution

Ewens distribution over permutations of 0..n-1 with cycle-weight parameter theta > 0.

Parameters:
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.

Parameters:

x (Sequence[int])

Return type:

float

log_density(x)[source]

Return the log-density or log-mass at a single observation.

Parameters:

x (Sequence[int])

Return type:

float

seq_log_density(x)[source]

Return vectorized log-density values for sequence-encoded observations.

Parameters:

x (ndarray)

Return type:

ndarray

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: DistributionSampler

Exact 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. With batched=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 independent RandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path. batched=False forces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.

Parameters:

size (int | None)

Return type:

list[int] | list[list[int]]

class EwensAccumulator(dim, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate the total (weighted) cycle count and observation weight.

Parameters:
update(x, weight, estimate)[source]
Parameters:
Return type:

None

initialize(x, weight, rng)[source]
Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]
Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]
Parameters:
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_dict under its merge key.

The structural default implements the common single-key pattern: store the accumulator under self.keys the first time the key is seen, else combine into 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. A keys of None (the default) is a no-op.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator’s statistics from the pooled stats_dict entry (see key_merge).

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]
Return type:

EwensDataEncoder

class EwensAccumulatorFactory(dim, keys=None)[source]

Bases: StatisticAccumulatorFactory

Parameters:
make()[source]
Return type:

EwensAccumulator

class EwensEstimator(dim, theta=None, name=None, keys=None)[source]

Bases: ParameterEstimator

Fit theta by matching the mean cycle count E[cycles] = sum_i theta/(theta+i).

Parameters:
accumulator_factory()[source]
Return type:

EwensAccumulatorFactory

estimate(nobs, suff_stat)[source]
Parameters:

nobs (float | None)

Return type:

EwensDistribution

class EwensDataEncoder(dim=None)[source]

Bases: DataSequenceEncoder

Encode a sequence of permutations of 0,…,n-1 into an (N, n) integer array.

Parameters:

dim (int | None)

seq_encode(x)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

x (Sequence[Sequence[int]])

Return type:

ndarray