mixle.stats.processes.chinese_restaurant_process module

Chinese Restaurant Process – a Bayesian-nonparametric distribution over partitions.

The CRP with concentration alpha is the exchangeable distribution over partitions of n items induced by the sequential rule “item i joins an existing block of size m with probability m / (i - 1 + alpha) or starts a new block with probability alpha / (i - 1 + alpha)”. A partition with blocks of sizes n_1, ..., n_K has the Ewens probability

P = alpha^K * Gamma(alpha) / Gamma(alpha + n) * prod_k Gamma(n_k),

so larger alpha favours more, smaller blocks. It is the partition prior underlying Dirichlet-process mixtures; an observation here is a partition of n items given as a label vector (the labels are arbitrary – the density is relabeling-invariant). alpha is fit by maximum likelihood, the monotone solve alpha (psi(alpha + n) - psi(alpha)) = mean number of blocks.

Reference: Pitman, Combinatorial Stochastic Processes (Springer, 2006).

class ChineseRestaurantProcessDistribution(alpha, n, name=None, keys=None)[source]

Bases: SequenceEncodableProbabilityDistribution

CRP distribution over partitions of n items with concentration alpha > 0.

Parameters:
density(x)[source]

Return the probability of the partition encoded by label vector x.

Parameters:

x (ndarray)

Return type:

float

log_density(x)[source]

Return the Ewens log-probability of the partition that label vector x induces.

Parameters:

x (ndarray)

Return type:

float

seq_log_density(x)[source]

Return the Ewens log-probability for a list of partition label vectors.

Parameters:

x (list[ndarray])

Return type:

ndarray

sampler(seed=None)[source]

Return a sampler that draws partitions by the sequential CRP rule.

Parameters:

seed (int | None)

Return type:

ChineseRestaurantProcessSampler

estimator(pseudo_count=None)[source]

Return a maximum-likelihood estimator for the concentration alpha at fixed n.

Parameters:

pseudo_count (float | None)

Return type:

ChineseRestaurantProcessEstimator

dist_to_encoder()[source]

Return the data encoder (passes label vectors through).

Return type:

ChineseRestaurantProcessDataEncoder

class ChineseRestaurantProcessSampler(dist, seed=None)[source]

Bases: DistributionSampler

Draw partitions by the sequential CRP seating rule; returns first-appearance label vectors.

Parameters:
  • dist (ChineseRestaurantProcessDistribution)

  • 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)

class ChineseRestaurantProcessAccumulator(name=None, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate the total number of blocks and observation count (the CRP sufficient statistics).

Parameters:
  • name (str | None)

  • keys (str | None)

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

suff_stat (tuple[float, float])

Return type:

ChineseRestaurantProcessAccumulator

value()[source]
Return type:

tuple[float, float]

from_value(x)[source]
Parameters:

x (tuple[float, float])

Return type:

ChineseRestaurantProcessAccumulator

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:

ChineseRestaurantProcessDataEncoder

class ChineseRestaurantProcessAccumulatorFactory(name=None, keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for ChineseRestaurantProcessAccumulator.

Parameters:
  • name (str | None)

  • keys (str | None)

make()[source]
Return type:

ChineseRestaurantProcessAccumulator

class ChineseRestaurantProcessEstimator(n, alpha_min=1.0e-6, alpha_max=1.0e6, name=None, keys=None)[source]

Bases: ParameterEstimator

Maximum-likelihood estimator for the CRP concentration via the monotone expected-blocks equation.

Parameters:
accumulator_factory()[source]
Return type:

ChineseRestaurantProcessAccumulatorFactory

estimate(nobs, suff_stat)[source]
Parameters:
Return type:

ChineseRestaurantProcessDistribution

class ChineseRestaurantProcessDataEncoder[source]

Bases: DataSequenceEncoder

Encode a sequence of partition label vectors (passthrough as integer arrays).

seq_encode(x)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

x (Sequence[ndarray])

Return type:

list[ndarray]