mixle.stats.combinator.weighted module

Evaluate, estimate, and sample from a weighted wrapper around a base distribution.

Defines the WeightedDistribution, WeightedSampler, WeightedAccumulator, WeightedAccumulatorFactory, WeightedEstimator, and the WeightedDataEncoder classes for use with mixle.

Data type: Tuple[D, float]: An observation is a pair (value, weight) where value has the data type D of the base distribution and weight is a non-negative score attached to the observation. The weight does not enter the likelihood; it only scales the observation’s contribution to the sufficient statistics during estimation. Likelihood evaluations delegate to the base distribution on the value alone, i.e.

P((x, w)) = P_base(x).

class WeightedDistribution(dist, name=None)[source]

Bases: SequenceEncodableProbabilityDistribution

WeightedDistribution object that attaches observation weights to a base distribution.

Parameters:
  • dist (SequenceEncodableProbabilityDistribution) – Base distribution for the observed values.

  • name (Optional[str]) – Set name for object instance.

dist

Base distribution for the observed values.

Type:

SequenceEncodableProbabilityDistribution

name

Name for object instance.

Type:

Optional[str]

compute_capabilities()[source]
compute_declaration()[source]
density(x)[source]

Density of the base distribution at observation value x.

Parameters:

x (D) – Observation value (weight excluded).

Returns:

Density of the base distribution at x.

Return type:

float

log_density(x)[source]

Log-density of the base distribution at observation value x.

The observation weight does not enter the likelihood, so this is simply the base distribution’s log-density evaluated on the value.

Parameters:

x (D) – Observation value (weight excluded).

Returns:

Log-density of the base distribution at x.

Return type:

float

seq_log_density(x)[source]

Vectorized log-density of the base distribution on encoded values.

Parameters:

x (Tuple[E, np.ndarray]) – Sequence encoded values and weights from WeightedDataEncoder.

Returns:

Numpy array of base-distribution log-densities.

Return type:

ndarray

backend_seq_log_density(x, engine)[source]

Engine-neutral vectorized log-density delegated to the value distribution.

Parameters:
Return type:

Any

classmethod backend_stacked_params(dists, engine)[source]

Return stacked child parameters for homogeneous weighted-wrapper mixtures.

Parameters:
Return type:

dict[str, Any]

classmethod backend_stacked_log_density(x, params, engine)[source]

Return an (n, k) matrix of child log densities, ignoring attached weights.

Parameters:
Return type:

Any

classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]

Return child legacy statistics with posterior weights scaled by observation weights.

Parameters:
Return type:

Any

dist_to_encoder()[source]

Returns a WeightedDataEncoder for encoding sequences of (value, weight) observations.

Return type:

WeightedDataEncoder

to_fisher(**kwargs)[source]

Fisher view for the weighted wrapper.

estimator(pseudo_count=None)[source]

Create a WeightedEstimator wrapping the base distribution’s estimator.

Parameters:

pseudo_count (Optional[float]) – Passed through to the base distribution’s estimator.

Returns:

WeightedEstimator object.

Return type:

WeightedEstimator

sampler(seed=None)[source]

Create a WeightedSampler producing (value, weight) pairs.

Parameters:

seed (Optional[int]) – Used to set seed in random sampler.

Returns:

WeightedSampler object.

Return type:

WeightedSampler

enumerator()[source]

Delegates to the base distribution’s enumerator (log_density is pure delegation).

Return type:

DistributionEnumerator

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

Bases: DistributionSampler

WeightedSampler object for drawing (value, weight) observations from a WeightedDistribution.

The likelihood does not model the weight, so samples carry the neutral weight 1.0: accumulating (value, 1.0) is equivalent to accumulating the bare value with the base distribution. Values are drawn from the base distribution’s sampler.

Parameters:
  • dist (WeightedDistribution) – WeightedDistribution to draw samples from.

  • seed (Optional[int]) – Seed to set for sampling with RandomState.

dist

WeightedDistribution to draw samples from.

Type:

WeightedDistribution

rng

Seeded RandomState for sampling.

Type:

RandomState

dist_sampler

Sampler for the base distribution.

Type:

DistributionSampler

sample(size=None)[source]

Draw iid (value, weight) samples, each with weight 1.0.

Parameters:

size (Optional[int]) – Number of iid samples to draw.

Returns:

A single (value, 1.0) tuple if size is None, else a list of size such tuples.

Return type:

tuple[Any, float] | Sequence[tuple[Any, float]]

class WeightedAccumulator(accumulator, name=None)[source]

Bases: SequenceEncodableStatisticAccumulator

WeightedAccumulator object that scales each observation’s weight by its attached score.

Parameters:
  • accumulator (SequenceEncodableStatisticAccumulator) – Accumulator for the base distribution.

  • name (Optional[str]) – Set name for object instance.

accumulator

Accumulator for the base distribution.

Type:

SequenceEncodableStatisticAccumulator

name

Name for object instance.

Type:

Optional[str]

initialize(x, weight, rng)[source]

Initialize the base accumulator with observation x[0] weighted by weight*x[1].

Parameters:
  • x (Tuple[D, float]) – Observation (value, weight) pair.

  • weight (float) – External weight on the observation.

  • rng (RandomState) – Random number generator for initialization.

Return type:

None

update(x, weight, estimate)[source]

Update the base accumulator with observation x[0] weighted by weight*x[1].

Parameters:
  • x (Tuple[D, float]) – Observation (value, weight) pair.

  • weight (float) – External weight on the observation.

  • estimate (WeightedDistribution) – Previous estimate of the weighted distribution.

Return type:

None

seq_update(x, weights, estimate)[source]

Vectorized update of the base accumulator with weights scaled by the observation weights.

Parameters:
  • x (Tuple[E, np.ndarray]) – Sequence encoded values and weights from WeightedDataEncoder.

  • weights (np.ndarray) – External weights on the observations.

  • estimate (WeightedDistribution) – Previous estimate of the weighted distribution.

Return type:

None

seq_update_engine(x, weights, estimate, engine)[source]

Engine-resident E-step: per-observation weights are scaled on the active engine and the base accumulator is routed through the engine. Matches seq_update.

Parameters:
  • weights (Any)

  • estimate (WeightedDistribution)

  • engine (Any)

Return type:

None

seq_initialize(x, weights, rng)[source]

Vectorized initialization of the base accumulator with scaled weights.

Parameters:
  • x (Tuple[E, np.ndarray]) – Sequence encoded values and weights from WeightedDataEncoder.

  • weights (np.ndarray) – External weights on the observations.

  • rng (RandomState) – Random number generator for initialization.

Return type:

None

combine(suff_stat)[source]

Combine the base accumulator’s sufficient statistics with suff_stat.

Parameters:

suff_stat (SS) – Sufficient statistics of the base accumulator.

Returns:

This WeightedAccumulator.

Return type:

WeightedAccumulator

from_value(x)[source]

Set the base accumulator’s sufficient statistics from x.

Parameters:

x (SS) – Sufficient statistics of the base accumulator.

Returns:

This WeightedAccumulator.

Return type:

WeightedAccumulator

value()[source]

Returns the base accumulator’s sufficient statistics.

Return type:

Any

scale(c)[source]

Scale the child accumulator through its family-specific protocol.

Parameters:

c (float)

Return type:

WeightedAccumulator

key_merge(stats_dict)[source]

Merge keyed sufficient statistics of the base accumulator into stats_dict.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace keyed sufficient statistics of the base accumulator from stats_dict.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Returns a WeightedDataEncoder for encoding sequences of (value, weight) observations.

Return type:

WeightedDataEncoder

class WeightedAccumulatorFactory(factory, name=None)[source]

Bases: StatisticAccumulatorFactory

WeightedAccumulatorFactory object for creating WeightedAccumulator objects.

Parameters:
  • factory (StatisticAccumulatorFactory) – Accumulator factory for the base distribution.

  • name (Optional[str]) – Set name for object instance.

factory

Accumulator factory for the base distribution.

Type:

StatisticAccumulatorFactory

name

Name for object instance.

Type:

Optional[str]

make()[source]

Returns a new WeightedAccumulator wrapping a fresh base accumulator.

Return type:

WeightedAccumulator

class WeightedEstimator(estimator, name=None)[source]

Bases: ParameterEstimator

WeightedEstimator object for estimating a WeightedDistribution from weighted observations.

Parameters:
  • estimator (ParameterEstimator) – Estimator for the base distribution.

  • name (Optional[str]) – Set name for object instance.

estimator

Estimator for the base distribution.

Type:

ParameterEstimator

name

Name for object instance.

Type:

Optional[str]

accumulator_factory()[source]

Returns a WeightedAccumulatorFactory wrapping the base estimator’s factory.

Return type:

WeightedAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate a WeightedDistribution from the base distribution’s sufficient statistics.

Parameters:
  • nobs (Optional[float]) – Weighted number of observations.

  • suff_stat (SS) – Sufficient statistics of the base accumulator.

Returns:

WeightedDistribution wrapping the estimated base distribution.

Return type:

WeightedDistribution

class WeightedDataEncoder(encoder)[source]

Bases: DataSequenceEncoder

WeightedDataEncoder object for encoding sequences of iid (value, weight) observations.

Parameters:

encoder (DataSequenceEncoder) – Encoder for the base distribution’s values.

encoder

Encoder for the base distribution’s values.

Type:

DataSequenceEncoder

seq_encode(x)[source]

Encode a sequence of (value, weight) observations for vectorized use.

Parameters:

x (Sequence[Tuple[D, float]]) – Sequence of iid (value, weight) observations.

Returns:

Tuple of base-encoded values and a numpy array of weights.

Return type:

tuple[Any, ndarray]

class WeightedFisherView(dist)[source]

Bases: FixedFisherView

Parameters:

dist (Any)

score_center(stats=None, **kwargs)[source]
Parameters:
Return type:

ndarray