mixle.stats.latent.scheduled_hidden_markov_model module¶
A length- and position-conditional (“scheduled”) hidden Markov model.
A standard HMM is time-homogeneous: the same initial distribution, transition matrix, and emissions apply at
every position, and (with a len_dist) the length is drawn independently of the path – it only sets the
count. This family makes the dynamics depend on where you are in the sequence and how long it is, through a
serializable PhaseSchedule phi(t, L) that maps position t in a length-L sequence to a phase.
Each phase has its own initial / transition / emission parameters; EM pools sufficient statistics by phase.
One mechanism covers every reasonable “length-conditional” model:
Homogeneous–phi(t, L) = 0– the ordinary HMM (one phase).ByLength–phi(t, L) = bucket(L)– a length-conditional HMM: short and long sequences use different dynamics (constant within a sequence).ByRelativePosition–phi(t, L) = floor(B * t / L)– relative position: the chain knows how far through the sequence it is (e.g. winds down toward the end), regardless of absolute length.ByPosition–phi(t, L) = min(t, cap-1)– absolute position (non-homogeneous in time).
The length itself is still drawn from len_dist (it remains a random variable); the schedule adds the
conditioning of the content on length/position that the homogeneous model lacks. Emissions are per-phase too,
so length/position can shape emissions, not just transitions.
This is a deliberately lean, numpy-only implementation (no numba / enumeration / terminal-state integration –
those live on HiddenMarkovModelDistribution). It reuses the emission
families’ own estimators for the M-step.
- class PhaseSchedule[source]
Bases:
objectMaps a position
tin a length-Lsequence to a phase index in[0, n_phases).- n_phases: int = 1
- class Homogeneous[source]
Bases:
PhaseScheduleOne phase for everything – the ordinary time-homogeneous HMM.
- n_phases: int = 1
- class ByPosition(cap)[source]
Bases:
PhaseScheduleAbsolute position:
phi(t, L) = min(t, cap - 1)(positions pastcap-1share the last phase).- Parameters:
cap (int)
- class ByRelativePosition(bins)[source]
Bases:
PhaseScheduleRelative position:
phi(t, L) = min(bins - 1, floor(bins * t / L))– progress through the sequence.- Parameters:
bins (int)
- class ByLength(boundaries)[source]
Bases:
PhaseScheduleLength-conditional: phase is the bucket of
Lagainst sortedboundaries(constant within a seq).With
boundaries = [5, 10]there are three phases:L <= 5,5 < L <= 10,L > 10.- Parameters:
boundaries (Sequence[int])
- class ScheduledHiddenMarkovModelDistribution(inits, transitions, emissions, schedule, len_dist=None, name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionPhase-indexed (length-/position-conditional) HMM. See the module docstring for the modeling story.
- Parameters:
- 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.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
ScheduledHMMSampler
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
ScheduledHMMEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
ScheduledHMMDataEncoder
- class ScheduledHMMSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (ScheduledHiddenMarkovModelDistribution)
seed (int | None)
- sample(size=None, *, batched=True)[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 ScheduledHMMDataEncoder[source]
Bases:
DataSequenceEncoder
- class ScheduledHMMAccumulator(n_states, schedule, emission_factory, len_factory=None)[source]
Bases:
SequenceEncodableStatisticAccumulator- Parameters:
n_states (int)
schedule (PhaseSchedule)
emission_factory (Any)
len_factory (Any)
- update(x, weight, estimate)[source]
- seq_update(x, weights, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
weight (float)
rng (RandomState)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (Any)
weights (ndarray)
rng (RandomState)
- Return type:
None
- acc_to_encoder()[source]
- Return type:
ScheduledHMMDataEncoder
- class ScheduledHMMAccumulatorFactory(n_states, schedule, emission_estimator, len_estimator=None)[source]
Bases:
StatisticAccumulatorFactory- Parameters:
n_states (int)
schedule (PhaseSchedule)
emission_estimator (Any)
len_estimator (Any)
- make()[source]
- Return type:
ScheduledHMMAccumulator
- class ScheduledHMMEstimator(n_states, schedule, emission_estimator, len_estimator=None, pseudo_count=1e-8, name=None)[source]
Bases:
ParameterEstimatorEM estimator for a
ScheduledHiddenMarkovModelDistributionwith a fixed schedule.emission_estimatoris the estimator for ONE emission distribution (reused for every phase x state);len_estimator(optional) estimates the length distribution. The schedule is fixed (it defines the parameter sharing); only the per-phase parameters are learned.- Parameters:
- accumulator_factory()[source]
- Return type:
ScheduledHMMAccumulatorFactory