mixle.stats.latent.segmental_hidden_markov_model module¶
Hidden Markov model over arbitrary emission segments.
Each hidden state emits one segment object. The segment can be any data type
accepted by that state’s emission distribution: a scalar, tuple, set, sequence,
or another composable mixle.stats object. To model variable-length emissions,
use a SequenceDistribution (or any other distribution over sequences) as an
emission distribution.
Unlike HiddenMarkovModelDistribution, each state keeps its own encoder, so
emission distributions may use different distribution classes as long as they
can all score the same raw segment observations.
- class SegmentalHiddenMarkovModelDistribution(emissions, w=MISSING, transitions=MISSING, len_dist=NullDistribution(), name=None, weights=MISSING, terminal_states=None)[source]
Bases:
SequenceEncodableProbabilityDistributionHMM whose states emit arbitrary segment-valued distributions.
Observations are lists of segment objects. For example, with
SequenceDistribution(GaussianDistribution(...), len_dist=...)as an emission, each state emits a variable-length list of real values.- Parameters:
- compute_capabilities()[source]
- compute_declaration()[source]
- property topics: list[SequenceEncodableProbabilityDistribution]
Compatibility alias with HiddenMarkovModelDistribution terminology.
- density(x)[source]
Return the probability density or mass at a single observation.
- log_density(x)[source]
Return the log-density or log-mass at a single observation.
- seq_log_density(x)[source]
Return vectorized log-density values for sequence-encoded observations.
- backend_seq_log_density(x, engine)[source]
Engine-neutral segmental-HMM forward scoring for encoded segment sequences.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
SegmentalHiddenMarkovSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
SegmentalHiddenMarkovEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
SegmentalHiddenMarkovDataEncoder
- enumerator()[source]
Enumerate segment sequences in descending marginal probability order.
The segmental HMM has the standard HMM forward semantics – each position emits one segment from its state’s distribution, scored independently – so it reuses
HiddenMarkovModelEnumeratordirectly via its per-state emission (topics),log_w,log_transitions, andlen_dist. Each segment is drawn from the union of the per-state emission supports, so every emission distribution must itself support enumeration (and a length distribution must be modeled).- Return type:
DistributionEnumerator
- class SegmentalHiddenMarkovSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw iid segmental-HMM observations.
- Parameters:
dist (SegmentalHiddenMarkovModelDistribution)
seed (int | None)
- sample(size=None)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class SegmentalHiddenMarkovAccumulator(accumulators, len_accumulator=NullAccumulator(), keys=(None, None, None), name=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorBaum-Welch accumulator for segmental HMMs.
- Parameters:
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
weight (float)
rng (RandomState)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
- seq_update(x, weights, estimate)[source]
- seq_update_engine(x, weights, estimate, engine)[source]
Engine-resident Baum-Welch E-step (numpy or torch).
Reuses hmm_engine_forward_backward over the sequence-contiguous segment encoding, producing the same initial/state/transition/emission statistics as the host seq_update.
- combine(suff_stat)[source]
- from_value(x)[source]
- scale(c)[source]
Scale linear sufficient statistics in-place by
c.The structural default is correct for ordinary weighted sums, nested tuples/lists/dicts, and numeric arrays. Families whose
value()payload includes non-linear metadata such as support bounds must override this method and leave that metadata unscaled.- Parameters:
c (float)
- Return type:
SegmentalHiddenMarkovAccumulator
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
SegmentalHiddenMarkovDataEncoder
- class SegmentalHiddenMarkovAccumulatorFactory(factories, len_factory=NullAccumulatorFactory(), keys=(None, None, None), name=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for SegmentalHiddenMarkovAccumulator.
- Parameters:
- make()[source]
- Return type:
SegmentalHiddenMarkovAccumulator
- class SegmentalHiddenMarkovEstimator(estimators, len_estimator=NullEstimator(), pseudo_count=(None, None), name=None, keys=(None, None, None), terminal_states=None)[source]
Bases:
ParameterEstimatorBaum-Welch estimator for SegmentalHiddenMarkovModelDistribution.
- Parameters:
- accumulator_factory()[source]
- Return type:
SegmentalHiddenMarkovAccumulatorFactory
- class SegmentalHiddenMarkovDataEncoder(emission_encoders, len_encoder=NullDataEncoder())[source]
Bases:
DataSequenceEncoderEncode a batch of segment sequences for a segmental HMM.
- Parameters:
emission_encoders (Sequence[DataSequenceEncoder])
len_encoder (DataSequenceEncoder | None)
- SegmentalHiddenMarkovDistribution
alias of
SegmentalHiddenMarkovModelDistribution
- SegmentalHiddenMarkovModelAccumulator
alias of
SegmentalHiddenMarkovAccumulator
- SegmentalHiddenMarkovModelAccumulatorFactory
alias of
SegmentalHiddenMarkovAccumulatorFactory
- SegmentalHiddenMarkovModelDataEncoder
alias of
SegmentalHiddenMarkovDataEncoder
- SegmentalHiddenMarkovModelEstimator
alias of
SegmentalHiddenMarkovEstimator
- SegmentalHiddenMarkovModelSampler
alias of
SegmentalHiddenMarkovSampler