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 one partition or a list of independent partitions.

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]

Accumulate the weighted block count for one partition.

Parameters:
Return type:

None

initialize(x, weight, rng)[source]

Initialize the sufficient statistics with one weighted partition.

Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate weighted block counts for encoded partition label vectors.

Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize the sufficient statistics from encoded partitions.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge serialized block-count statistics into this accumulator.

Parameters:

suff_stat (tuple[float, float])

Return type:

ChineseRestaurantProcessAccumulator

value()[source]

Return the total weighted block count and observation weight.

Return type:

tuple[float, float]

from_value(x)[source]

Restore the accumulator from serialized block-count statistics.

Parameters:

x (tuple[float, float])

Return type:

ChineseRestaurantProcessAccumulator

key_merge(stats_dict)[source]

Merge this accumulator into a keyed statistics dictionary.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator from a keyed statistics dictionary.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Return an encoder for CRP partition label vectors.

Return type:

ChineseRestaurantProcessDataEncoder

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

Bases: StatisticAccumulatorFactory

Factory for ChineseRestaurantProcessAccumulator.

Parameters:
  • name (str | None)

  • keys (str | None)

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

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

Parameters:
accumulator_factory()[source]

Return a factory for CRP sufficient-statistic accumulators.

Return type:

ChineseRestaurantProcessAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate the CRP concentration from the observed mean block count.

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 partition label vectors as integer arrays without relabeling them.

Parameters:

x (Sequence[ndarray])

Return type:

list[ndarray]