↑ arvau

Wikipedia / Forward–backward algorithm

The forward–backward algorithm is an inference algorithm for hidden Markov models which computes the posterior marginals. 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

. 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:

The last step follows from an application of the Bayes' rule and the conditional independence.

As outlined above, the algorithm involves three steps:

  1. computing forward probabilities

  2. computing backward probabilities

  3. 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. 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:

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:

), the probability of observing event j is then:

) containing only diagonal entries. Continuing the above example, the observation matrix for event 1 would be:

generated event 1 as:

, then updating the state distribution and weighting by the likelihood of the first observation:

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:

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:

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:

This allows us to interpret the scaled probability vector as:

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:

), and we are now interested in the probability of observing all future events from this state. Since the initial state is assumed as given (i.e. the prior probability of this state = 100%), we begin with:

Notice that we are now using a column vector while the forward probabilities used row vectors. We can then work backwards using:

is not scaled, but subsequent operations use:

represents the previous, scaled vector. This result is that the scaled probability vector is related to the backward probabilities by:

This is useful because it allows us to find the total probability of being in each state at a given time, t, by multiplying these values:

. These values are sometimes called the "smoothed values" as they combine the forward and backward probabilities to compute a final probability.

. The most probable sequence of states that produced an observation sequence can be found using the Viterbi algorithm.

Example

This example takes as its basis the umbrella world in Russell & Norvig 2010 Chapter 15 pp. 567 in which we would like to infer the weather given observation of another person either carrying or not carrying an umbrella. We assume two possible states for the weather: state 1 = rain, state 2 = no rain. We assume that the weather has a 70% chance of staying the same each day and a 30% chance of changing. The transition probabilities are then:

We also assume each state generates one of two possible events: event 1 = umbrella, event 2 = no umbrella. The conditional probabilities for these occurring in each state are given by the probability matrix:

We then observe the following sequence of events: {umbrella, umbrella, no umbrella, umbrella, umbrella} which we will represent in our calculations as:

differs from the others because of the "no umbrella" observation.

In computing the forward probabilities we begin with:

which is our prior state vector indicating that we don't know which state the weather is in before our observations. While a state vector should be given as a row vector, we will use the transpose of the matrix so that the calculations below are easier to read. Our calculations are then written in the form:

instead of:

Notice that the transformation matrix is also transposed, but in our example the transpose is equal to the original matrix. Performing these calculations and normalizing the results provides:

For the backward probabilities, we start with:

We are then able to compute (using the observations in reverse order and normalizing with different constants):

's found earlier. The backward probability vectors above thus actually represent the likelihood of each state at time t given the future observations. Because these vectors are proportional to the actual backward probabilities, the result has to be scaled an additional time.

needs to be combined with the initial state vector to find the most likely initial state. We thus find that the forward probabilities by themselves are sufficient to calculate the most likely final state. Similarly, the backward probabilities can be combined with the initial state vector to provide the most probable initial state given the observations. The forward and backward probabilities need only be combined to infer the most probable states between the initial and final points.

quantifies our knowledge of the state vector at the end of the observation sequence. We can then use this to predict the probability of the various weather states tomorrow as well as the probability of observing an umbrella.

Performance

is the number of symbols in the state alphabet. by recomputing values at each step. For comparison, a brute-force procedure state sequences and calculate the joint probability of each state sequence with the observed series of events, which would have time complexity. Brute force is intractable for realistic problems, as the number of possible hidden node sequences typically is extremely high.

An enhancement to the general forward-backward algorithm, called the Island algorithm time algorithm, although the inverted process may not exist or be ill-conditioned.

efficiently through online smoothing such as the fixed-lag smoothing (FLS) algorithm.

Pseudocode

algorithm forward_backward is
    input: guessState
           int sequenceIndex
    output: result

    if sequenceIndex is past the end of the sequence then
        return 1
    if (guessState, sequenceIndex) has been seen before then
        return saved result

    result := 0

    for each neighboring state n:
        result := result + (transition probability from guessState to 
                            n given observation element at sequenceIndex)
                            × Backward(n, sequenceIndex + 1)

    save result for (guessState, sequenceIndex)

    return result

Python example

Given HMM (just like in Viterbi algorithm) represented in the Python programming language:

We can write the implementation of the forward-backward algorithm like this:

The function fwd_bkw takes the following arguments: x is the sequence of observations, e.g. ['normal', 'cold', 'dizzy']; states is the set of hidden states; a_0 is the start probability; a are the transition probabilities; and e are the emission probabilities.

For simplicity of code, we assume that the observation sequence x is non-empty and that a[i][j] and e[i][j] is defined for all states i,j.

In the running example, the forward-backward algorithm is used as follows:

See also

References