ArticleslgStudy

computer science

Forward algorithm

Forward 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 algorithm rather than just read about it. In short: The forward algorithm, in the context of a hidden Markov model (HMM), is used to calculate a 'belief state': the probability of a state at a certain time, given the history of evidence. The process is also known as filtering.

Forward algorithm — main illustration
Forward algorithm — illustration

Key takeaways

  • Forward 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 algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Forward algorithm from memory before moving on to harder problems.

Reference excerpt

The forward algorithm, in the context of a hidden Markov model (HMM), is used to calculate a 'belief state': the probability of a state at a certain time, given the history of evidence. The process is also known as filtering. The forward algorithm is closely related to, but distinct from, the Viterbi algorithm.

Introduction The forward and backward algorithms should be placed within the context of probability as they appear to simply be names given to a set of standard mathematical procedures within a few fields. For example, neither "forward algorithm" nor "Viterbi" appear in the Cambridge encyclopedia of mathematics. The main observation to take away from these algorithms is how to organize Bayesian updates and inference to be computationally efficient in the context of directed graphs of variables (see sum-product networks). For an HMM such as this one:

this probability is written as p(xt|y1:t). Here x(t) is the hidden state which is abbreviated as xt and y1:t are the observations 1 to t. The backward algorithm complements the forward algorithm by taking into account the future history if one wanted to improve the estimate for past times. This is referred to as smoothing and the forward/backward algorithm computes p(xt|y1:T) for 1<t<T. Thus, the full forward/backward algorithm takes into account all evidence. Note that a belief state can be calculated at each time step, but doing this does not, in a strict sense, produce the most likely state sequence, but rather the most likely state at each time step, given the previous history. In order to achieve the most likely sequence, the Viterbi algorithm is required. It computes the most likely state sequence given the history of observations, that is, the state sequence that maximizes p(x0:t|y0:t).

Algorithm The goal of the forward algorithm is to compute the joint probability p(xt,y1:t), where for notational convenience we have abbreviated x(t) as xt and (y(1),y(2),...,y(t)) as y1:t. Once the joint probability p(xt,y1:t) is computed, the other probabilities p(xt|y1:t) and p(y1:t) are easily obtained. Both the state xt and observation yt are assumed to be discrete, finite random variables. The hidden Markov model's state transition probabilities p(xt|xt−1), observation/emission probabilities p(yt|xt), and initial prior probability p(x0) are assumed to be known. Furthermore, the sequence of observations y1:t are assumed to be given. Computing p(xt,y1:t) naively would require marginalizing over all possible state sequences {x1:t−1}, the number of which grows exponentially with t. Instead, the forward algorithm takes advantage of the conditional independence rules of the hidden Markov model (HMM) to perform the calculation recursively. To demonstrate the recursion, let

α(xt)=p(xt,y1:t)=∑xt−1p(xt,xt−1,y1:t). Using the chain rule to expand p(xt,xt−1,y1:t), we can then write

α(xt)=∑xt−1p(yt|xt,xt−1,y1:t−1)p(xt|xt−1,y1:t−1)p(xt−1,y1:t−1). Because yt is conditionally independent of everything but xt, and xt is conditionally independent of everything but xt−1, this simplifies to

α(xt)=p(yt|xt)∑xt−1p(xt|xt−1)α(xt−1). Thus, since p(yt|xt) and p(xt|xt−1) are given by the model's emission distributions and transition probabilities, which are assumed to be known, one can quickly calculate α(xt) from α(xt−1) and avoid incurring exponential computation time. The recursion formula given above can be written in a more compact form. Let aij=p(xt=i|xt−1=j) be the transition probabilities and bij=p(yt=i|xt=j) be the emission probabilities, then

𝜶t=𝐛tT⊙𝐀𝜶t−1 where 𝐀=[aij] is the transition probability matrix, 𝐛t is the i-th row of the emission probability matrix 𝐁=[bij] which corresponds to the actual observation yt=i at time t, and 𝜶t=[α(xt=1),…,α(xt=n)]T is the alpha vector. The ⊙ is the hadamard product between the transpose of 𝐛t and 𝐀𝜶t−1. The initial condition is set in accordance to the prior probability over x0 as

α(x0)=p(y0|x0)p(x0). Once the joint probability α(xt)=p(xt,y1:t) has been computed using the forward algorithm, we can easily obtain the related joint probability p(y1:t) as

p(y1:t)=∑xtp(xt,y1:t)=∑xtα(xt) and the required conditional probability p(xt|y1:t) as

p(xt|y1:t)=p(xt,y1:t)p(y1:t)=α(xt)∑xtα(xt). Once the conditional probability has been calculated, we can also find the point estimate of xt. For instance, the MAP estimate of xt is given by

x^tMAP=arg⁡maxxtp(xt|y1:t)=arg⁡maxxtα(xt), while the MMSE estimate of xt is given by

x^tMMSE=𝔼[xt|y1:t]=∑xtxtp(xt|y1:t)=∑xtxtα(xt)∑xtα(xt). The forward algorithm is easily modified to account for observations from variants of the hidden Markov model as well, such as the Markov jump linear system.

Pseudocode

Example This example on observing possible states of weather from the observed condition of seaweed. We have observations of seaweed for three consecutive days as dry, damp, and soggy in order. The possible states of weather can be sunny, cloudy, or rainy. In total, there can be 33=27 such weather sequences. Exploring all such possible state sequences is computationally very expensive. To reduce this complexity, Forward algorithm comes in handy, where the trick lies in using the conditional independence of the sequence steps to calculate partial probabilities, α(xt)=p(xt,y1:t)=p(yt|xt)∑xt−1p(xt|xt−1)α(xt−1) as shown in the above derivation. Hence, we can calculate the probabilities as the product of the appropriate observation/emission probability, p(yt|xt) ( probability of state yt seen at time t from previous observation) with the sum of probabilities of reaching that state at time t, calculated using transition probabilities. This reduces complexity of the problem from searching whole search space to just using previously computed α's and transition probabilities.

Complexity Complexity of Forward Algorithm is Θ(nm2), where m is the number of possible states for a latent variable (like the number of weather conditions in the example above), and n is the length of the observed sequence. This is a clear reduction from the ad hoc method of exploring all the possible states, which has a complexity of Θ(nmn).

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Forward algorithm

Start with the simplest possible case. Write down what Forward 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 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 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 algorithm

In research
Forward 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 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 algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Markov models, so understanding it makes those chapters shorter.
In everyday life
Look for Forward 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 algorithm” →

How to study Forward algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Forward 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 algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Forward algorithm in simple terms?

The forward algorithm, in the context of a hidden Markov model (HMM), is used to calculate a 'belief state': the probability of a state at a certain time, given the history of evidence. The process is also known as filtering.

Why does Forward 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 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 algorithm.

Tags

  • Markov models

Keep exploring