mixle.stats.latent.dirac_length module¶
Create, estimate, and sample from a mixture over a dirac delta at v and a length distribution.
Defines the DiracLengthMixtureDistribution, DiracLengthMixtureSampler, DiracLengthMixtureAccumulatorFactory, DiracLengthMixtureAccumulator, DiracLengthMixtureEstimator, and the DiracLengthMixtureDataEncoder classes for use with mixle.
The DiracLengthMixtureDistribution is defined by the density of the form,
P(Y) = p*P_1(Y) + (1-p)*Delta_{v}(Y),
where P_1() is a length distribution with support on non-negative integers, or a subset of them, and Delta_{v}(x) = 1 if x = v, else 0.
- class DiracLengthMixtureDistribution(len_dist, p, v=0, name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionDiracLengthMixtureDistribution object defined by a length distribution, choice of dirac value, and p.
- Parameters:
- p
Probability of being drawn from length distribution. Must be between 0 and 1.
- Type:
- len_dist
Distribution with support on non-negative integers.
- Type:
SequenceEncodableProbabilityDistribution
- name
Name for object instance.
- Type:
Optional[str]
- compute_capabilities()[source]
- compute_declaration()[source]
- density(x)[source]
Evaluate density of length Dirac mixture distribution at observation x.
See log_density() for details.
- log_density(x)[source]
Evaluate the log-density of length Dirac mixture distribution at observation x.
log(P(x)) = log( p*P_1(x) + (1-p)*Delta_{v}(x) ),
- component_log_density(x)[source]
Log-density of each mixture component (length distribution, dirac at v) at x.
- posterior(x)[source]
Posterior probability of each mixture component given observation x.
- seq_component_log_density(x)[source]
Vectorized component log-densities at sequence encoded input x.
- Parameters:
x (E) – Sequence encoded data from DiracLengthMixtureDataEncoder.
- Returns:
Numpy array of shape (len(x), 2) of component log-densities.
- Return type:
- seq_log_density(x)[source]
Vectorized evaluation of the mixture log-density at sequence encoded input x.
- Parameters:
x (E) – Sequence encoded data from DiracLengthMixtureDataEncoder.
- Returns:
Numpy array of log-density (float) of len(x).
- Return type:
- backend_seq_component_log_density(x, engine)[source]
Engine-neutral component log densities for encoded length/dirac mixtures.
- backend_seq_log_density(x, engine)[source]
Engine-neutral mixture log-density for encoded length/dirac observations.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked parameters for shared-dirac length mixtures.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of length/dirac mixture log densities.
- classmethod backend_stacked_sufficient_statistics_with_estimator(x, weights, params, engine, estimator)[source]
Return per-component legacy
(component_counts, length_stat)statistics.
- seq_posterior(x)[source]
Vectorized component posterior probabilities at sequence encoded input x.
- Parameters:
x (E) – Sequence encoded data from DiracLengthMixtureDataEncoder.
- Returns:
Numpy array of shape (len(x), 2) of component posteriors.
- Return type:
- sampler(seed=None)[source]
Create a DiracLengthMixtureSampler from parameters of this distribution.
- Parameters:
seed (Optional[int]) – Used to set seed in random sampler.
- Returns:
DiracLengthMixtureSampler object.
- Return type:
DiracLengthMixtureSampler
- estimator(pseudo_count=None)[source]
Create a DiracLengthMixtureEstimator with matching dirac value v.
- Parameters:
pseudo_count (Optional[float]) – Used to inflate sufficient statistics.
- Returns:
DiracLengthMixtureEstimator object.
- Return type:
DiracLengthMixtureEstimator
- dist_to_encoder()[source]
Returns a DiracLengthMixtureDataEncoder for encoding sequences of iid integer observations.
- Return type:
DiracLengthMixtureDataEncoder
- enumerator()[source]
Returns a DiracLengthMixtureEnumerator iterating the union of the length-distribution support and the dirac point v in descending probability order.
- Return type:
DiracLengthMixtureEnumerator
- class DiracLengthMixtureEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerates the union of the length-distribution support and the dirac point v.
The model is a two-component mixture: the length distribution with weight p and a dirac delta at v with weight 1-p. The dirac component contributes the trivial single-point stream [(v, 0.0)]. Supports may overlap (the length distribution can also emit v), so candidates are de-duplicated and re-scored exactly with the mixture log-density.
- Parameters:
dist (DiracLengthMixtureDistribution)
- class DiracLengthMixtureSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDiracLengthMixtureSampler object for sampling from a DiracLengthMixtureDistribution.
- Parameters:
dist (DiracLengthMixtureDistribution)
seed (int | None)
- class DiracLengthMixtureAccumulator(accumulator, v=0, keys=(None, None), name=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorDiracLengthMixtureAccumulator object for accumulating component counts and length-distribution statistics.
- Parameters:
- accumulator
Accumulator for the length distribution.
- Type:
SequenceEncodableStatisticAccumulator
- comp_counts
Posterior-weighted counts for the two components.
- Type:
np.ndarray
- weight_key
Key for merging mixture weight counts.
- Type:
Optional[str]
- comp_key
Key for merging component sufficient statistics.
- Type:
Optional[str]
- v
Dirac location.
- Type:
- name
Name for object instance.
- Type:
Optional[str]
- seq_update(x, weights, estimate)[source]
Vectorized accumulation of posterior-weighted statistics from encoded observations x.
- Parameters:
x (E) – Sequence encoded data from DiracLengthMixtureDataEncoder.
weights (np.ndarray) – Weights on the observations.
estimate (DiracLengthMixtureDistribution) – Previous estimate used for posteriors.
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident E-step: the length-distribution scoring (the heavy term) runs through the active engine; the cheap two-component (length vs. Dirac) responsibility bookkeeping mirrors the host seq_update exactly.
- update(x, weight, estimate)[source]
Add one observation’s posterior-weighted contribution to the sufficient statistics.
- initialize(x, weight, rng)[source]
Initialize the accumulator with observation x, randomly splitting weight at the dirac point.
- seq_initialize(x, weights, rng)[source]
Vectorized initialization from encoded observations x with random splits at the dirac point.
- Parameters:
x (E) – Sequence encoded data from DiracLengthMixtureDataEncoder.
weights (np.ndarray) – Weights on the observations.
rng (RandomState) – Random number generator for initialization.
- Return type:
None
- combine(suff_stat)[source]
Combine sufficient statistics (component counts, length-dist stats) with this accumulator.
- Parameters:
suff_stat (Tuple[np.ndarray, SS0]) – Component counts and length-distribution statistics.
- Returns:
This DiracLengthMixtureAccumulator.
- Return type:
DiracLengthMixtureAccumulator
- value()[source]
Returns sufficient statistics as a tuple (component counts, length-distribution statistics).
- from_value(x)[source]
Set sufficient statistics from a (component counts, length-distribution statistics) tuple.
- Parameters:
x (Tuple[np.ndarray, SS0]) – Component counts and length-distribution statistics.
- Returns:
This DiracLengthMixtureAccumulator.
- Return type:
DiracLengthMixtureAccumulator
- key_merge(stats_dict)[source]
Merge keyed sufficient statistics into stats_dict under the weight and component keys.
- key_replace(stats_dict)[source]
Replace keyed sufficient statistics from stats_dict under the weight and component keys.
- acc_to_encoder()[source]
Returns a DiracLengthMixtureDataEncoder for encoding sequences of iid integer observations.
- Return type:
DiracLengthMixtureDataEncoder
- class DiracLengthMixtureAccumulatorFactory(factory, v=0, keys=(None, None), name=None)[source]
Bases:
StatisticAccumulatorFactoryDiracLengthMixtureAccumulatorFactory object for creating DiracLengthMixtureAccumulator objects.
- Parameters:
- factory
Accumulator factory for the length distribution.
- Type:
StatisticAccumulatorFactory
- v
Dirac location.
- Type:
- keys
Keys for the mixture weights and component statistics.
- name
Name for object instance.
- Type:
Optional[str]
- make()[source]
Returns a new DiracLengthMixtureAccumulator wrapping a fresh length-distribution accumulator.
- Return type:
DiracLengthMixtureAccumulator
- class DiracLengthMixtureEstimator(estimator, v=0, fixed_p=None, suff_stat=None, pseudo_count=None, name=None, keys=(None, None))[source]
Bases:
ParameterEstimatorDiracLengthMixtureEstimator object for estimating DiracLengthMixtureDistribution objects.
- Parameters:
estimator (ParameterEstimator) – Estimator for the length distribution.
v (int) – Dirac location.
fixed_p (Optional[float]) – Hold the length-distribution weight p fixed at this value.
suff_stat (Optional[float]) – Prior value of p used with pseudo_count for regularization.
pseudo_count (Optional[float]) – Used to inflate the component count statistics.
name (Optional[str]) – Set name for object instance.
keys (Tuple[Optional[str], Optional[str]]) – Keys for the mixture weights and component statistics.
- estimator
Estimator for the length distribution.
- Type:
ParameterEstimator
- v
Dirac location.
- Type:
- pseudo_count
Used to inflate the component count statistics.
- Type:
Optional[float]
- suff_stat
Prior value of p used with pseudo_count for regularization.
- Type:
Optional[float]
- keys
Keys for the mixture weights and component statistics.
- name
Name for object instance.
- Type:
Optional[str]
- fixed_p_vec
Fixed component weights [p, 1-p] when fixed_p is given.
- Type:
Optional[np.ndarray]
- accumulator_factory()[source]
Returns a DiracLengthMixtureAccumulatorFactory consistent with this estimator.
- Return type:
DiracLengthMixtureAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a DiracLengthMixtureDistribution from accumulated sufficient statistics.
- Parameters:
nobs (Optional[float]) – Weighted number of observations.
suff_stat (Tuple[np.ndarray, SS0]) – Component counts and length-distribution statistics.
- Returns:
DiracLengthMixtureDistribution object.
- Return type:
DiracLengthMixtureDistribution
- class DiracLengthMixtureDataEncoder(encoder, v=0)[source]
Bases:
DataSequenceEncoderDiracLengthMixtureDataEncoder object for encoding sequences of iid integer observations.
- Parameters:
encoder (DataSequenceEncoder) – Encoder for the length distribution.
v (int) – Dirac location.
- encoder
Encoder for the length distribution.
- Type:
DataSequenceEncoder
- v
Dirac location.
- Type:
- seq_encode(x)[source]
Encode a sequence of iid integer observations for vectorized use.