mixle.stats.bayes.dict_dirichlet module

Dirichlet distribution on dictionary-valued probability maps.

Observations are dicts {value: probability} whose probabilities are non-negative and sum to one (points on the simplex indexed by the dict keys). A DictDirichletDistribution with concentration parameters alpha = {k: a_k} has log-density

log f(x; alpha) = gammaln(sum_k a_k) + sum_k [(a_k - 1)*log(x_k) - gammaln(a_k)].

A single scalar alpha is treated as a symmetric Dirichlet whose dimension is inferred from each observation (is_unbounded).

This is the conjugate prior used by CategoricalDistribution (see its prior= argument). It is a parameter prior: it is scored on probability maps, not fit from data by EM. Ported from mixle.bstats.catdirichlet.

class DictDirichletDistribution(alpha, name=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Dirichlet distribution over probability maps keyed by arbitrary values; a scalar alpha denotes a symmetric Dirichlet of unspecified dimension.

Parameters:
get_parameters()[source]

Returns the concentration parameters (dict, or scalar if unbounded).

Return type:

dict | float

set_parameters(params)[source]

Set the concentration parameters.

Parameters:

params (dict[Any, float] | float) – Dict {value: a_k} of positive reals, or a positive scalar for a symmetric Dirichlet of unspecified dimension.

Return type:

None

density(x)[source]

Density at the probability map x (exp of log_density).

Parameters:

x (dict[Any, float])

Return type:

float

log_density(x)[source]

Log-density of the Dirichlet at the probability map x.

With scalar alpha the dimension is len(x); with dict alpha the observation is scored with the concentration entries matching its keys.

Parameters:

x (dict[Any, float])

Return type:

float

seq_log_density(x)[source]

Vectorized log-density at a sequence of probability maps.

Parameters:

x (list[dict[Any, float]])

Return type:

ndarray

cross_entropy(dist)[source]

Cross entropy -E_self[log dist(x)] for a DictDirichlet argument.

Parameters:

dist (DictDirichletDistribution)

Return type:

float

entropy()[source]

Returns the differential entropy in nats (dict alpha only).

Return type:

float

sampler(seed=None)[source]

Returns a DictDirichletSampler for this distribution.

Parameters:

seed (int | None)

Return type:

DictDirichletSampler

estimator(pseudo_count=None)[source]

DictDirichlet is a parameter prior and is not fit from data by EM.

Parameters:

pseudo_count (float | None)

Return type:

ParameterEstimator

dist_to_encoder()[source]

Returns a DictDirichletDataEncoder for encoding probability maps.

Return type:

DictDirichletDataEncoder

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

Bases: DistributionSampler

Draws probability maps from a DictDirichletDistribution with dict-valued concentration.

Parameters:
  • dist (DictDirichletDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw Dirichlet-distributed probability maps over the alpha keys (dict alpha only).

Parameters:

size (int | None)

Return type:

dict | list[dict]

class DictDirichletDataEncoder[source]

Bases: DataSequenceEncoder

Trivial encoder for sequences of probability maps (passes the list through).

seq_encode(x)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

x (Any)

Return type:

list