/// Execute the forward algorithm and return the forward probabilites as `LogProb` values
/// and the resulting forward probability.
///
/// ## Arguments
///
/// - `hmm` - the `Model` to run the forward algorithm on
/// - `observations` - a slice of observation values to use in the algorithm
///
/// ## Result
///
/// The resulting pair (*P*, *p*) is the forward probability table (`P[[s, o]]` is the entry
/// for state `s` and observation `o`) and the overall probability for `observations` (as
/// `LogProb`).
///
/// ## Type Parameters
///
/// - `O` - the observation type
/// - `M` - type `Model` type
Execute the forward algorithm and return the forward probabilites as `LogProb` values
and the resulting forward probability.
Execute the forward algorithm and return the forward probabilites as ` LogProb ` values and the resulting forward probability .
pub fn forward<O, M: Model<O>>(hmm: &M, observations: &[O]) -> (Array2<LogProb>, LogProb) {
    // The matrix with probabilities.
    let mut vals = Array2::<LogProb>::zeros((observations.len(), hmm.num_states()));
    // Compute matrix.
    for (i, o) in observations.iter().enumerate() {
        if i == 0 {
            // Initial column.
            for s in hmm.states() {
                vals[[0, *s]] = hmm.initial_prob(s) + hmm.observation_prob(s, o);
            }
        } else {
            // Subsequent columns.
            for j in hmm.states() {
                let xs = hmm
                    .states()
                    .map(|k| {
                        vals[[i - 1, *k]]
                            + hmm.transition_prob_idx(k, j, i)
                            + hmm.observation_prob(j, o)
                        // + maybe_initial
                    })
                    .collect::<Vec<LogProb>>();
                vals[[i, *j]] = LogProb::ln_sum_exp(&xs);
            }
        }
    }
    // Compute final probability.
    let prob_vec_final = hmm
        .states()
        .map(|k| vals[[observations.len() - 1, *k]] + hmm.end_prob(k))
        .collect::<Vec<LogProb>>();
    let prob = LogProb::ln_sum_exp(&prob_vec_final);
    // let prob = LogProb::ln_sum_exp(vals.row(observations.len() - 1).to_slice().unwrap());
    (vals, prob)
}
pub fn forward < O , M : Model < O > > ( hmm : & M , observations : & [ O ] ) -> ( Array2 < LogProb > , LogProb ) { // The matrix with probabilities. let mut vals = Array2 :: < LogProb > :: zeros ( ( observations . len ( ) , hmm . num_states ( ) ) ) ; // Compute matrix. for ( i , o ) in observations . iter ( ) . enumerate ( ) { if i == 0 { // Initial column. for s in hmm . states ( ) { vals [ [ 0 , * s ] ] = hmm . initial_prob ( s ) + hmm . observation_prob ( s , o ) ; } } else { // Subsequent columns. for j in hmm . states ( ) { let xs = hmm . states ( ) . map ( | k | { vals [ [ i - 1 , * k ] ] + hmm . transition_prob_idx ( k , j , i ) + hmm . observation_prob ( j , o ) // + maybe_initial } ) . collect :: < Vec < LogProb > > ( ) ; vals [ [ i , * j ] ] = LogProb :: ln_sum_exp ( & xs ) ; } } } // Compute final probability. let prob_vec_final = hmm . states ( ) . map ( | k | vals [ [ observations . len ( ) - 1 , * k ] ] + hmm . end_prob ( k ) ) . collect :: < Vec < LogProb > > ( ) ; let prob = LogProb :: ln_sum_exp ( & prob_vec_final ) ; // let prob = LogProb::ln_sum_exp(vals.row(observations.len() - 1).to_slice().unwrap()); ( vals , prob ) }
Execute the forward algorithm and return the forward probabilites as `LogProb` values
and the resulting forward probability.
Execute the forward algorithm and return the forward probabilites as ` LogProb ` values and the resulting forward probability .
hmm
&M
the `Model` to run the forward algorithm on
the ` Model ` to run the forward algorithm on
observations
&[O]
a slice of observation values to use in the algorithm
a slice of observation values to use in the algorithm
O
the observation type
the observation type
M
type `Model` type
type ` Model ` type
result
The resulting pair (*P*, *p*) is the forward probability table (`P[[s, o]]` is the entry
for state `s` and observation `o`) and the overall probability for `observations` (as
`LogProb`).
The resulting pair ( * P * * p * ) is the forward probability table ( ` P [[ s o ]] ` is the entry for state ` s ` and observation ` o ` ) and the overall probability for ` observations ` ( as ` LogProb ` ) .