mixle.models.dirichlet_process_mixture moduleΒΆ

Dirichlet-process mixture experiment helpers.

This module keeps nonparametric-mixture logic in the model layer. It exposes small stick-breaking utilities and a dependency-free truncated variational mixture loop over ordinary mixle.stats component estimators.

class TruncatedDirichletProcessMixtureFitResult(model, responsibilities, history)[source]

Bases: object

Fitted truncated DPM plus variational responsibilities and history.

Parameters:
  • model (TruncatedDirichletProcessMixtureModel)

  • responsibilities (ndarray)

  • history (list[float])

model: TruncatedDirichletProcessMixtureModel
responsibilities: ndarray
history: list[float]
class TruncatedDirichletProcessMixtureModel(components, alpha=1.0, gamma=None, weights=None, name=None)[source]

Bases: object

Truncated stick-breaking mixture over existing mixle component models.

Parameters:
  • components (Sequence[SequenceEncodableProbabilityDistribution])

  • alpha (float)

  • gamma (Any | None)

  • weights (Any | None)

  • name (str | None)

property expected_log_weights: ndarray

Return E_q[log pi_k] under the variational stick posteriors.

component_log_density(x)[source]

Return component log densities for one observation.

Parameters:

x (Any)

Return type:

ndarray

log_density(x)[source]

Return the finite-truncation mixture log density for one observation.

Parameters:

x (Any)

Return type:

float

density(x)[source]

Return the finite-truncation mixture density for one observation.

Parameters:

x (Any)

Return type:

float

responsibilities(data, expected=True)[source]

Return posterior component probabilities for observations.

Parameters:
Return type:

ndarray

effective_components(threshold=0.01)[source]

Count components with posterior mean stick weight above threshold.

Parameters:

threshold (float)

Return type:

int

sample(size=None, seed=None)[source]

Draw observations from the finite truncation.

Parameters:
  • size (int | None)

  • seed (int | None)

Return type:

Any | list[Any]

stick_breaking_weights(stick_fractions, residual=True)[source]

Convert stick fractions into mixture weights.

When residual is true, the returned vector has one extra final entry containing the remaining stick mass. This is the usual finite truncation.

Parameters:
  • stick_fractions (Any)

  • residual (bool)

Return type:

ndarray

expected_log_stick_weights(gamma)[source]

Return E_q[log pi_k] for truncated Beta stick posteriors.

Parameters:

gamma (Any)

Return type:

ndarray

mean_stick_weights(gamma)[source]

Return E_q[pi_k] under independent Beta stick posteriors.

Parameters:

gamma (Any)

Return type:

ndarray

sample_crp_assignments(num_obs, alpha, seed=None)[source]

Sample Chinese-restaurant-process assignments and table counts.

Parameters:
Return type:

tuple[ndarray, ndarray]

fit_truncated_dpm(data, initial_components, component_estimator, alpha=1.0, max_its=50, tol=1.0e-8, sort_components=True, name=None)[source]

Fit a truncated DP mixture by coordinate-ascent variational updates.

The component M-steps are delegated to ordinary mixle.stats estimators. This keeps component likelihood math and sufficient statistics in their distribution modules.

Parameters:
  • data (Sequence[Any])

  • initial_components (Sequence[SequenceEncodableProbabilityDistribution])

  • component_estimator (ParameterEstimator | Sequence[ParameterEstimator])

  • alpha (float)

  • max_its (int)

  • tol (float | None)

  • sort_components (bool)

  • name (str | None)

Return type:

TruncatedDirichletProcessMixtureFitResult