mixle.stats.latent.semi_supervised_hidden_markov_model module

Semi-supervised hidden Markov model: each observation may carry a per-position state prior.

A SemiSupervisedHiddenMarkovModelDistribution is an HMM with shared emissions and transitions in which every observation can carry soft evidence (a prior) over the hidden state at each position of the sequence – not only the initial state. An observation is a pair (emission_seq, state_prior):

  • emission_seq: a length-T sequence of emissions (data type of the emission distributions).

  • state_prior: an optional T-by-S array of finite non-negative likelihood potentials. Row t is soft evidence over the S hidden states at position t. Rows are normalized to unit maximum, so their absolute scale is irrelevant and an all-ones row imposes no constraint. The HMM’s initial state law is uniform and is not replaced by the evidence at position 0.

The prior folds into the forward-backward as an extra multiplicative factor on the emission likelihood at every position, so it shapes both scoring (log_density) and the EM E-step. Only the transitions and emissions (and an optional length distribution) are learned; the priors are given side information. With every prior None the model is an ordinary HMM with a uniform initial state distribution.

Defines SemiSupervisedHiddenMarkovModelDistribution, SemiSupervisedHiddenMarkovSampler, SemiSupervisedHiddenMarkovEstimatorAccumulator, SemiSupervisedHiddenMarkovEstimatorAccumulatorFactory, SemiSupervisedHiddenMarkovEstimator, and SemiSupervisedHiddenMarkovDataEncoder.

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

Bases: DistributionSampler

Sample emission sequences from the HMM with a uniform initial state distribution.

Priors are external side information, so sampled observations carry None as their prior.

Parameters:

dist (SemiSupervisedHiddenMarkovModelDistribution)

sample(size=None, *, batched=True)[source]

Draw one observation or a list of observations with None state priors.

Parameters:

batched (bool)

class SemiSupervisedHiddenMarkovEstimatorAccumulator(accumulators, len_accumulator=None, keys=(None, None))[source]

Bases: SequenceEncodableStatisticAccumulator

Baum-Welch sufficient statistics for the semi-supervised HMM (transition + emission counts, length).

update(x, weight, estimate)[source]

Update Baum-Welch sufficient statistics from one weighted observation.

initialize(x, weight, rng)[source]

Initialize emission and transition statistics with random soft state assignments.

seq_update(x, weights, estimate)[source]

Update sufficient statistics from encoded observations and weights.

seq_initialize(x, weights, rng)[source]

Initialize sufficient statistics from encoded observations and weights.

combine(suff_stat)[source]

Merge transition, emission, and length sufficient statistics.

value()[source]

Return transition counts, per-state emission stats, and length stats.

from_value(x)[source]

Restore transition counts, per-state emission stats, and length stats.

key_merge(stats_dict)[source]

Merge transition, state, and child statistics into stats_dict.

key_replace(stats_dict)[source]

Replace transition, state, and child statistics from keyed entries when present.

acc_to_encoder()[source]

Return the encoder compatible with this accumulator.

class SemiSupervisedHiddenMarkovEstimatorAccumulatorFactory(factories, len_factory=None, keys=(None, None))[source]

Bases: StatisticAccumulatorFactory

Create accumulators for semi-supervised HMM Baum-Welch statistics.

make()[source]

Create an empty semi-supervised HMM accumulator.

class SemiSupervisedHiddenMarkovEstimator(estimators, len_estimator=None, pseudo_count=None, name=None, keys=(None, None), terminal_states=None)[source]

Bases: ParameterEstimator

Estimate transitions, emissions, and optional length from semi-supervised HMM statistics.

accumulator_factory()[source]

Return a factory for semi-supervised HMM sufficient-statistic accumulators.

estimate(nobs, suff_stat)[source]

Estimate the HMM transition matrix and child distributions from accumulated statistics.

class SemiSupervisedHiddenMarkovDataEncoder(emission_encoder, len_encoder=None)[source]

Bases: DataSequenceEncoder

Encode a sequence of (emission_seq, state_prior) observations for the semi-supervised HMM.

seq_encode(x)[source]

Encode observations as emission lists, priors, optional lengths, and raw lengths.

row_count(x)[source]

Return and validate the number of encoded semi-supervised sequences.

Return type:

int

class SemiSupervisedHiddenMarkovModelDistribution(topics, transitions, len_dist=None, name=None, keys=None, use_numba=None, terminal_states=None)[source]

Bases: SequenceEncodableProbabilityDistribution

HMM with shared emissions/transitions where each observation may carry a per-position state prior.

compute_capabilities()[source]

Declare the legacy NumPy execution path for semi-supervised HMM inference.

density(x)[source]

Return the probability of one semi-supervised HMM observation.

Return type:

float

log_density(x)[source]

Return the log-likelihood of one (emissions, state_prior) observation.

Return type:

float

seq_log_density(x)[source]

Return vectorized log-likelihoods for encoded semi-supervised HMM observations.

Return type:

ndarray

density_semantics()[source]

Return the joined density semantics of emission and optional length distributions.

sampler(seed=None)[source]

Return a sampler for emission sequences with no external state priors.

estimator(pseudo_count=None)[source]

Return a Baum-Welch estimator for transitions, emissions, and optional length.

dist_to_encoder()[source]

Return the encoder for emission sequences, priors, and optional lengths.

SemiSupervisedHiddenMarkovModelSampler

alias of SemiSupervisedHiddenMarkovSampler

SemiSupervisedHiddenMarkovModelEstimator

alias of SemiSupervisedHiddenMarkovEstimator

SemiSupervisedHiddenMarkovModelDataEncoder

alias of SemiSupervisedHiddenMarkovDataEncoder

SemiSupervisedHiddenMarkovModelAccumulator

alias of SemiSupervisedHiddenMarkovEstimatorAccumulator

SemiSupervisedHiddenMarkovModelAccumulatorFactory

alias of SemiSupervisedHiddenMarkovEstimatorAccumulatorFactory

SemiSupervisedHiddenMarkovAccumulator

alias of SemiSupervisedHiddenMarkovEstimatorAccumulator

SemiSupervisedHiddenMarkovAccumulatorFactory

alias of SemiSupervisedHiddenMarkovEstimatorAccumulatorFactory