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:
SequenceEncodableProbabilityDistributionCRP distribution over partitions of
nitems with concentrationalpha > 0.- density(x)[source]
Return the probability of the partition encoded by label vector
x.
- log_density(x)[source]
Return the Ewens log-probability of the partition that label vector
xinduces.
- seq_log_density(x)[source]
Return the Ewens log-probability for a list of partition label vectors.
- 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
alphaat fixedn.- 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:
DistributionSamplerDraw 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. 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.- Parameters:
size (int | None)
- class ChineseRestaurantProcessAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate the total number of blocks and observation count (the CRP sufficient statistics).
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (ndarray)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- from_value(x)[source]
- 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:
ChineseRestaurantProcessDataEncoder
- class ChineseRestaurantProcessAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for ChineseRestaurantProcessAccumulator.
- make()[source]
- Return type:
ChineseRestaurantProcessAccumulator
- class ChineseRestaurantProcessEstimator(n, alpha_min=1.0e-6, alpha_max=1.0e6, name=None, keys=None)[source]
Bases:
ParameterEstimatorMaximum-likelihood estimator for the CRP concentration via the monotone expected-blocks equation.
- accumulator_factory()[source]
- Return type:
ChineseRestaurantProcessAccumulatorFactory
- class ChineseRestaurantProcessDataEncoder[source]
Bases:
DataSequenceEncoderEncode a sequence of partition label vectors (passthrough as integer arrays).