The forward–backward algorithm is an inference algorithm for hidden Markov models which computes the posterior marginals of all hidden state variables given a sequence of observations/emissions o1:T:=o1,…,oT, i.e. it computes, for all hidden state variables Xt∈{X1,…,XT}, the distribution P(Xt | o1:T). This inference task is usually called smoothing. The algorithm makes use of the principle of dynamic programming to efficiently compute the values that are required to obtain the posterior marginal distributions in two passes. The first pass goes forward in time while the second goes backward in time; hence the name forward–backward algorithm. The term forward–backward algorithm is also used to refer to any algorithm belonging to the general class of algorithms that operate on sequence models in a forward–backward manner. In this sense, the descriptions in the remainder of this article refer only to one specific instance of this class.
Overview In the first pass, the forward–backward algorithm computes a set of forward probabilities which provide, for all t∈{1,…,T}, the probability of ending up in any particular state given the first t observations in the sequence, i.e. P(Xt | o1:t). In the second pass, the algorithm computes a set of backward probabilities which provide the probability of observing the remaining observations given any starting point t, i.e. P(ot+1:T | Xt). These two sets of probability distributions can then be combined to obtain the distribution over states at any specific point in time given the entire observation sequence:
P(Xt | o1:T)=P(Xt | o1:t,ot+1:T)∝P(ot+1:T | Xt)P(Xt|o1:t) The last step follows from an application of the Bayes' rule and the conditional independence of ot+1:T and o1:t given Xt. As outlined above, the algorithm involves three steps:
computing forward probabilities computing backward probabilities computing smoothed values. The forward and backward steps may also be called "forward message pass" and "backward message pass" - these terms are due to the message-passing used in general belief propagation approaches. At each single observation in the sequence, probabilities to be used for calculations at the next observation are computed. The smoothing step can be calculated simultaneously during the backward pass. This step allows the algorithm to take into account any past observations of output for computing more accurate results. The forward–backward algorithm can be used to find the most likely state for any point in time. It cannot, however, be used to find the most likely sequence of states (see Viterbi algorithm).
Forward probabilities The following description will use matrices of probability values instead of probability distributions. However, the forward-backward algorithm can generally be applied to both continuous and discrete probability models. We transform the probability distributions related to a given hidden Markov model into matrix notation as follows. The transition probabilities 𝐏(Xt∣Xt−1) of a given random variable Xt representing all possible states in the hidden Markov model will be represented by the matrix 𝐓 where the column index j will represent the target state and the row index i represents the start state. A transition from row-vector state 𝝅𝐭 to the incremental row-vector state 𝝅𝐭+𝟏 is written as 𝝅𝐭+𝟏=𝝅𝐭𝐓. The example below represents a system where the probability of staying in the same state after each step is 70% and the probability of transitioning to the other state is 30%. The transition matrix is then:
𝐓=(0.70.30.30.7) In a typical Markov model, we would multiply a state vector by this matrix to obtain the probabilities for the subsequent state. In a hidden Markov model the state is unknown, and we instead observe events associated with the possible states. An event matrix of the form:
𝐁=(0.90.10.20.8) provides the probabilities for observing events given a particular state. In the above example, event 1 will be observed 90% of the time if we are in state 1 while event 2 has a 10% probability of occurring in this state. In contrast, event 1 will only be observed 20% of the time if we are in state 2 and event 2 has an 80% chance of occurring. Given an arbitrary row-vector describing the state of the system (𝝅), the probability of observing event j is then:
𝐏(O=j)=∑iπiBi,j The probability of a given state leading to the observed event j can be represented in matrix form by multiplying the state row-vector (𝝅) with an observation matrix (𝐎𝐣=diag(B∗,oj)) containing only diagonal entries. Continuing the above example, the observation matrix for event 1 would be:
𝐎𝟏=(0.90.00.00.2) This allows us to calculate the new unnormalized probabilities state vector 𝝅′ through Bayes rule, weighting by the likelihood that each element of 𝝅 generated event 1 as:
𝝅′=𝝅𝐎𝟏 We can now make this general procedure specific to our series of observations. Assuming an initial state vector 𝝅0, (which can be optimized as a parameter through repetitions of the forward-backward procedure), we begin with 𝐟𝟎:𝟎=𝝅0, then updating the state distribution and weighting by the likelihood of the first observation:
𝐟𝟎:𝟏=𝝅0𝐓𝐎𝐨𝟏 This process can be carried forward with additional observations using:
𝐟𝟎:𝐭=𝐟𝟎:𝐭−𝟏𝐓𝐎𝐨𝐭 This value is the forward unnormalized probability vector. The i'th entry of this vector provides:
𝐟𝟎:𝐭(i)=𝐏(o1,o2,…,ot,Xt=xi|𝝅0) Typically, we will normalize the probability vector at each step so that its entries sum to 1. A scaling factor is thus introduced at each step such that:
𝐟̂𝟎:𝐭=ct−1 𝐟̂𝟎:𝐭−𝟏𝐓𝐎𝐨𝐭 where 𝐟̂𝟎:𝐭−𝟏 represents the scaled vector from the previous step and ct represents the scaling factor that causes the resulting vector's entries to sum to 1. The product of the scaling factors is the total probability for observing the given events irrespective of the final states:
𝐏(o1,o2,…,ot|𝝅0)=∏s=1tcs This allows us to interpret the scaled probability vector as:
𝐟̂𝟎:𝐭(i)=𝐟𝟎:𝐭(i)∏s=1tcs=𝐏(o1,o2,…,ot,Xt=xi|𝝅0)𝐏(o1,o2,…,ot|𝝅0)=𝐏(Xt=xi|o1,o2,…,ot,𝝅0) We thus find that the product of the scaling factors provides us with the total probability for observing the given sequence up to time t and that the scaled probability vector provides us with the probability of being in each state at this time.
Backward probabilities A similar procedure can be constructed to find backward probabilities. These intend to provide the probabilities:
… excerpt ends here. Continue reading the full article.

