ArticleslgStudy

computer science

Forward–backward algorithm

Forward–backward algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Forward–backward algorithm rather than just read about it. In short: 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.

Forward–backward algorithm — main illustration
Forward–backward algorithm — illustration

Key takeaways

  • Forward–backward algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Forward–backward algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Forward–backward algorithm from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

Forward–backward algorithm: Forward Algorithm - Hidden Markov Model
Forward Algorithm - Hidden Markov Model

Worked examples

Example 1 — a first encounter with Forward–backward algorithm

Start with the simplest possible case. Write down what Forward–backward algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Forward–backward algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Forward–backward algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Forward–backward algorithm

In research
Forward–backward algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Forward–backward algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Forward–backward algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Dynamic programming, Error detection and correction, Machine learning algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Forward–backward algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Forward–backward algorithm” →

How to study Forward–backward algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Forward–backward algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Forward–backward algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Forward–backward algorithm in simple terms?

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…

Why does Forward–backward algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Forward–backward algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Forward–backward algorithm.

Tags

  • Dynamic programming
  • Error detection and correction
  • Machine learning algorithms
  • Markov models

Keep exploring