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)
- 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]
Accumulate the weighted block count for one partition.
- initialize(x, weight, rng)[source]
Initialize the sufficient statistics with one weighted partition.
- Parameters:
x (ndarray)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
Accumulate weighted block counts for encoded partition label vectors.
- seq_initialize(x, weights, rng)[source]
Initialize the sufficient statistics from encoded partitions.
- Parameters:
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
Merge serialized block-count statistics into this accumulator.
- value()[source]
Return the total weighted block count and observation weight.
- from_value(x)[source]
Restore the accumulator from serialized block-count statistics.
- key_merge(stats_dict)[source]
Merge this accumulator into a keyed statistics dictionary.
- key_replace(stats_dict)[source]
Replace this accumulator from a keyed statistics dictionary.
- acc_to_encoder()[source]
Return an encoder for CRP partition label vectors.
- Return type:
ChineseRestaurantProcessDataEncoder
- class ChineseRestaurantProcessAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for ChineseRestaurantProcessAccumulator.
- make()[source]
Create an empty CRP accumulator.
- 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 a factory for CRP sufficient-statistic accumulators.
- Return type:
ChineseRestaurantProcessAccumulatorFactory
- class ChineseRestaurantProcessDataEncoder[source]
Bases:
DataSequenceEncoderEncode a sequence of partition label vectors (passthrough as integer arrays).