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).- phase(t, length)[source]
Return the phase index for position
tin a sequence oflength.
- to_dict()[source]
Serialize the schedule to a JSON-compatible dictionary.
- class Homogeneous[source]
Bases:
PhaseScheduleOne phase for everything – the ordinary time-homogeneous HMM.
- phase(t, length)[source]
Return the single homogeneous phase.
- class ByPosition(cap)[source]
Bases:
PhaseScheduleAbsolute position:
phi(t, L) = min(t, cap - 1)(positions pastcap-1share the last phase).- Parameters:
cap (int)
- phase(t, length)[source]
Return the absolute-position phase capped at the final phase.
- class ByRelativePosition(bins)[source]
Bases:
PhaseScheduleRelative position:
phi(t, L) = min(bins - 1, floor(bins * t / L))– progress through the sequence.- Parameters:
bins (int)
- phase(t, length)[source]
Return the relative-position phase for
t / length.
- 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])
- phase(t, length)[source]
Return the length-bucket phase for the sequence length.
- 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 likelihood of one scheduled HMM sequence.
- seq_log_density(x)[source]
Score a batch of scheduled HMM sequences.
- sampler(seed=None)[source]
Return a sampler for scheduled HMM sequences.
- Parameters:
seed (int | None)
- Return type:
ScheduledHMMSampler
- estimator(pseudo_count=None)[source]
Raise because emission and length estimators must be supplied explicitly.
- Parameters:
pseudo_count (float | None)
- Return type:
ScheduledHMMEstimator
- dist_to_encoder()[source]
Return the pass-through scheduled HMM encoder.
- Return type:
ScheduledHMMDataEncoder
- class ScheduledHMMSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler for scheduled HMM sequences.
- Parameters:
dist (ScheduledHiddenMarkovModelDistribution)
seed (int | None)
- class ScheduledHMMDataEncoder[source]
Bases:
DataSequenceEncoderPass-through encoder for scheduled HMM sequence observations.
- class ScheduledHMMAccumulator(n_states, schedule, emission_factory, len_factory=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulator for phase-pooled scheduled HMM EM sufficient statistics.
- Parameters:
n_states (int)
schedule (PhaseSchedule)
emission_factory (Any)
len_factory (Any)
- update(x, weight, estimate)[source]
Accumulate sufficient statistics from one weighted sequence.
- seq_update(x, weights, estimate)[source]
Accumulate weighted sufficient statistics from a batch.
- initialize(x, weight, rng)[source]
Initialize sufficient statistics with random soft state responsibilities.
- Parameters:
weight (float)
rng (RandomState)
- Return type:
None
- seq_initialize(x, weights, rng)[source]
Initialize sufficient statistics from a weighted batch.
- Parameters:
x (Any)
weights (ndarray)
rng (RandomState)
- Return type:
None
- combine(other)[source]
Merge serialized scheduled HMM sufficient statistics.
- Parameters:
other (Any)
- Return type:
ScheduledHMMAccumulator
- from_value(value)[source]
Restore accumulator state from serialized sufficient statistics.
- Parameters:
value (tuple)
- Return type:
ScheduledHMMAccumulator
- acc_to_encoder()[source]
Return the encoder associated with this accumulator.
- Return type:
ScheduledHMMDataEncoder
- class ScheduledHMMAccumulatorFactory(n_states, schedule, emission_estimator, len_estimator=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for scheduled HMM accumulators.
- Parameters:
n_states (int)
schedule (PhaseSchedule)
emission_estimator (Any)
len_estimator (Any)
- make()[source]
Create a fresh scheduled HMM accumulator.
- 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 the accumulator factory used by this estimator.
- Return type:
ScheduledHMMAccumulatorFactory