pub struct HiddenMarkovModel<S> { /* private fields */ }Expand description
A finite hidden Markov model with inspectable transition and emission rows.
Implementations§
Source§impl<S: Eq + Clone> HiddenMarkovModel<S>
impl<S: Eq + Clone> HiddenMarkovModel<S>
Sourcepub fn discrete(
states: Vec<S>,
initial: Vec<f64>,
transitions: Vec<Vec<f64>>,
emissions: Vec<Vec<f64>>,
) -> Result<Self, HmmError>
pub fn discrete( states: Vec<S>, initial: Vec<f64>, transitions: Vec<Vec<f64>>, emissions: Vec<Vec<f64>>, ) -> Result<Self, HmmError>
Builds a model with categorical observations indexed from zero.
Examples found in repository?
examples/bounded_sequence_inference.rs (lines 24-29)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let policy = QuantilePolicy::new(0.02, 8, 256)?;
8 let mut left = QuantileSketch::new(policy.clone())?;
9 let mut right = QuantileSketch::new(policy)?;
10 for value in 0..100 {
11 if value % 2 == 0 {
12 left.insert(value as f64)?;
13 } else {
14 right.insert(value as f64)?;
15 }
16 }
17 left.merge(&right)?;
18 let median = left.estimate(0.5)?;
19 println!(
20 "quantile value={} rank=[{:.3},{:.3}] retained={} exact={}",
21 median.value, median.rank_lower, median.rank_upper, median.retained_entries, median.exact
22 );
23
24 let model = HiddenMarkovModel::discrete(
25 vec!["quiet", "active"],
26 vec![0.6, 0.4],
27 vec![vec![0.8, 0.2], vec![0.3, 0.7]],
28 vec![vec![0.9, 0.1], vec![0.2, 0.8]],
29 )?;
30 let observations = [0, 0, 1, 1];
31 let inference = forward_backward(&model, &observations)?;
32 let path = viterbi(&model, &observations)?;
33 println!(
34 "inference log-likelihood={:.6} path={:?} repairs={}",
35 inference.evidence.log_likelihood, path.states, inference.evidence.numerical_repairs
36 );
37
38 let data = [
39 Sequence::Discrete(vec![0, 0, 1, 1, 1, 0]),
40 Sequence::Discrete(vec![0, 1, 1, 0, 0, 0]),
41 ];
42 let report = fit_hmm(
43 &data,
44 HmmSpec::Discrete {
45 states: 2,
46 symbols: 2,
47 additive_smoothing: 1.0e-6,
48 },
49 HmmFitControl::new(23, 6, 1.0e-7, 10_000, 1.0e-12)?,
50 )?;
51 println!(
52 "fit likelihood={:.6} iterations={} converged={} repairs={} work={} termination={:?} seed={}",
53 report.evidence.log_likelihood,
54 report.evidence.iterations,
55 report.evidence.converged,
56 report.evidence.numerical_repairs,
57 report.evidence.work,
58 report.evidence.termination,
59 report.evidence.seed
60 );
61 Ok(())
62}Sourcepub fn gaussian(
states: Vec<S>,
initial: Vec<f64>,
transitions: Vec<Vec<f64>>,
means: Vec<f64>,
variances: Vec<f64>,
variance_floor: f64,
) -> Result<Self, HmmError>
pub fn gaussian( states: Vec<S>, initial: Vec<f64>, transitions: Vec<Vec<f64>>, means: Vec<f64>, variances: Vec<f64>, variance_floor: f64, ) -> Result<Self, HmmError>
Builds a model with scalar Gaussian observations.
Sourcepub fn from_transition_matrix(
initial: Vec<f64>,
transitions: FiniteTransitionMatrix<S>,
emissions: EmissionModel,
) -> Result<Self, HmmError>
pub fn from_transition_matrix( initial: Vec<f64>, transitions: FiniteTransitionMatrix<S>, emissions: EmissionModel, ) -> Result<Self, HmmError>
Builds a model from the same finite transition representation exposed
by crate::MarkovModel::transition_matrix.
Sourcepub fn initial_probabilities(&self) -> &[f64]
pub fn initial_probabilities(&self) -> &[f64]
Returns the normalized initial-state probabilities.
Sourcepub fn transitions(&self) -> &FiniteTransitionMatrix<S>
pub fn transitions(&self) -> &FiniteTransitionMatrix<S>
Returns the shared finite transition representation.
Sourcepub fn emissions(&self) -> &EmissionModel
pub fn emissions(&self) -> &EmissionModel
Returns the categorical or Gaussian emission representation.
Trait Implementations§
Source§impl<S: Clone> Clone for HiddenMarkovModel<S>
impl<S: Clone> Clone for HiddenMarkovModel<S>
Source§fn clone(&self) -> HiddenMarkovModel<S>
fn clone(&self) -> HiddenMarkovModel<S>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<S: Debug> Debug for HiddenMarkovModel<S>
impl<S: Debug> Debug for HiddenMarkovModel<S>
Source§impl<S: PartialEq> PartialEq for HiddenMarkovModel<S>
impl<S: PartialEq> PartialEq for HiddenMarkovModel<S>
impl<S: PartialEq> StructuralPartialEq for HiddenMarkovModel<S>
Auto Trait Implementations§
impl<S> Freeze for HiddenMarkovModel<S>
impl<S> RefUnwindSafe for HiddenMarkovModel<S>where
S: RefUnwindSafe,
impl<S> Send for HiddenMarkovModel<S>where
S: Send,
impl<S> Sync for HiddenMarkovModel<S>where
S: Sync,
impl<S> Unpin for HiddenMarkovModel<S>where
S: Unpin,
impl<S> UnsafeUnpin for HiddenMarkovModel<S>
impl<S> UnwindSafe for HiddenMarkovModel<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more