pub struct HiddenMarkovModel { /* private fields */ }Expand description
A discrete hidden Markov model: hidden states with start probabilities, a transition matrix and
an emission matrix over a finite alphabet of observation symbols (0..symbols).
Implementations§
Source§impl HiddenMarkovModel
impl HiddenMarkovModel
Sourcepub fn new(
initial: Vec<f64>,
transition: R2D,
emission: R2D,
) -> Result<Self, ErrorsJSL>
pub fn new( initial: Vec<f64>, transition: R2D, emission: R2D, ) -> Result<Self, ErrorsJSL>
initial[s], transition[[from, to]] and emission[[state, symbol]] must be probability
distributions (each row sums to one).
pub fn states(&self) -> usize
Sourcepub fn viterbi(
&self,
observations: &[usize],
) -> Result<(Vec<usize>, f64), ErrorsJSL>
pub fn viterbi( &self, observations: &[usize], ) -> Result<(Vec<usize>, f64), ErrorsJSL>
The most likely hidden state sequence for the observations and its log probability.
Source§impl HiddenMarkovModel
impl HiddenMarkovModel
pub fn initial(&self) -> &[f64]
pub fn transition(&self) -> &R2D
pub fn emission(&self) -> &R2D
Sourcepub fn random(
states: usize,
symbols: usize,
seed: u64,
) -> Result<Self, ErrorsJSL>
pub fn random( states: usize, symbols: usize, seed: u64, ) -> Result<Self, ErrorsJSL>
A random model with states states and symbols symbols (rows drawn uniformly from the
simplex and kept away from zero), as a starting point for HiddenMarkovModel::baum_welch.
Sourcepub fn sample(&self, steps: usize, seed: u64) -> (Vec<usize>, Vec<usize>)
pub fn sample(&self, steps: usize, seed: u64) -> (Vec<usize>, Vec<usize>)
Samples a hidden state path and its observations of the given length.
Sourcepub fn baum_welch(
&self,
sequences: &[Vec<usize>],
max_iterations: usize,
tolerance: f64,
) -> Result<BaumWelchResult, ErrorsJSL>
pub fn baum_welch( &self, sequences: &[Vec<usize>], max_iterations: usize, tolerance: f64, ) -> Result<BaumWelchResult, ErrorsJSL>
Fits the model to observation sequences by the Baum-Welch (expectation-maximisation)
algorithm, starting from self. Each iteration runs a scaled forward-backward pass per
sequence, accumulates expected transition and emission counts, and re-estimates the
parameters; the likelihood never decreases. Stops when the log-likelihood improves by less
than tolerance (relative) or after max_iterations. EM finds a local optimum, so several
random starts (HiddenMarkovModel::random) may be worth trying.