package aima.core.probability.hmm.exact;
import java.util.ArrayList;
import java.util.List;
import aima.core.probability.CategoricalDistribution;
import aima.core.probability.hmm.HiddenMarkovModel;
import aima.core.probability.proposition.AssignmentProposition;
import aima.core.probability.temporal.ForwardBackwardInference;
import aima.core.util.math.Matrix;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): page 576.
*
*
*
* function FORWARD-BACKWARD(ev, prior) returns a vector of probability distributions * inputs: ev, a vector of evidence values for steps 1,...,t * prior, the prior distribution on the initial state, P(X0) * local variables: fv, a vector of forward messages for steps 0,...,t * b, a representation of the backward message, initially all 1s * sv, a vector of smoothed estimates for steps 1,...,t * * fv[0] <- prior * for i = 1 to t do * fv[i] <- FORWARD(fv[i-1], ev[i]) * for i = t downto 1 do * sv[i] <- NORMALIZE(fv[i] * b) * b <- BACKWARD(b, ev[i]) * return sv ** * Figure 15.4 The forward-backward algorithm for smoothing: computing posterior * probabilities of a sequence of states given a sequence of observations. The * FORWARD and BACKWARD operators are defined by Equations (15.5) and (15.9), * respectively.
* f1:t+1 = αOt+1TTf1:t ** * @param f1_t * f1:t * @param O_tp1 * Ot+1 * @return f1:t+1 */ public Matrix forward(Matrix f1_t, Matrix O_tp1) { return hmm.normalize(O_tp1.times(hmm.getTransitionModel().transpose() .times(f1_t))); } /** * The backward equation (15.9) in Matrix form becomes (15.13):
* bk+1:t = TOk+1bk+2:t ** * @param b_kp2t * bk+2:t * @param O_kp1 * Ok+1 * @return bk+1:t */ public Matrix backward(Matrix b_kp2t, Matrix O_kp1) { return hmm.getTransitionModel().times(O_kp1).times(b_kp2t); } }