I want to know what the differences between the forward-backward algorithm and the Viterbi algorithm for inference in hidden Markov models (HMM) are.

  • 2
    $\begingroup$ Would descriptions of the algortihms (here and here) answer your question or are you looking for something else? Are you wondering when to use which algorithm? Looking for a discussion of their respective merits? $\endgroup$
    – MånsT
    Jul 6, 2012 at 8:05

5 Answers 5


A bit of background first maybe it clears things up a bit.

When talking about HMMs (Hidden Markov Models) there are generally 3 problems to be considered:

  1. Evaluation problem

    • Evaluation problem answers the question: what is the probability that a particular sequence of symbols is produced by a particular model?
    • For evaluation we use two algorithms: the forward algorithm or the backwards algorithm (DO NOT confuse them with the forward-backward algorithm).
  2. Decoding problem

    • Decoding problem answers the question: Given a sequence of symbols (your observations) and a model, what is the most likely sequence of states that produced the sequence.
    • For decoding we use the Viterbi algorithm.
  3. Training problem

    • Training problem answers the question: Given a model structure and a set of sequences, find the model that best fits the data.
    • For this problem we can use the following 3 algorithms:
      1. MLE (maximum likelihood estimation)
      2. Viterbi training(DO NOT confuse with Viterbi decoding)
      3. Baum Welch = forward-backward algorithm

To sum it up, you use the Viterbi algorithm for the decoding problem and Baum Welch/Forward-backward when you train your model on a set of sequences.

Baum Welch works in the following way.

For each sequence in the training set of sequences.

  1. Calculate forward probabilities with the forward algorithm
  2. Calculate backward probabilities with the backward algorithm
  3. Calculate the contributions of the current sequence to the transitions of the model, calculate the contributions of the current sequence to the emission probabilities of the model.
  4. Calculate the new model parameters (start probabilities, transition probabilities, emission probabilities)
  5. Calculate the new log likelihood of the model
  6. Stop when the change in log likelihood is smaller than a given threshold or when a maximum number of iterations is passed.

If you need a full description of the equations for Viterbi decoding and the training algorithm let me know and I can point you in the right direction.

  • 1
    $\begingroup$ Do you have any sources about Viterbi Training vs Viterbi Decoding? $\endgroup$ Jun 7, 2022 at 21:40

Forward-Backward gives marginal probability for each individual state, Viterbi gives probability of the most likely sequence of states. For instance if your HMM task is to predict sunny vs. rainy weather for each day, Forward Backward would tell you the probability of it being "sunny" for each day, Viterbi would give the most likely sequence of sunny/rainy days, and the probability of this sequence.


I find these two following slides from {2} to be really good to situate the forward-backward and Viterbi algorithms amongst all other typical algorithms used with HMM:

enter image description here

enter image description here


  • $x$ is the observed emission(s), $\pi$ are the parameters of the HMM.
  • path = a sequence of emissions
  • decoding = inference
  • learning = training = parameter estimation
  • Some papers (e.g., {1}) claim that Baum–Welch is the same as forward–backward algorithm, but I agree with Masterfool and Wikipedia: Baum–Welch is an expectation-maximization algorithm that uses the forward–backward algorithm. The two illustrations also distinguish Baum–Welch from the forward–backward algorithm.


  • $\begingroup$ There seem to be several errors in the notes below the figures. $\pi$ is a path (a sequence of hidden states), not a set of parameters. Similarly, a path is not a sequence of emissions, but a sequence of hidden states. The figures themselves are fantastic though. $\endgroup$
    – Mew
    Apr 13 at 23:44

Morat's answer is false on one point: Baum-Welch is an Expectation-Maximization algorithm, used to train an HMM's parameters. It uses the forward-backward algorithm during each iteration. The forward-backward algorithm really is just a combination of the forward and backward algorithms: one forward pass, one backward pass. On its own, the forward-backward algorithm is not used for training an HMM's parameters, but only for smoothing: computing the marginal likelihoods of a sequence of states.




@Yaroslav Bulatov had a precise answer. I would add one example of it to tell the differences between forward-backward and Viterbi algorithms.

Suppose we have an this HMM (from Wikipedia HMM page). Note, the model is already given, so there is no learning from data task here.

enter image description here

Suppose our data is a length 4 sequence. (Walk, Shop, Walk, Clean). Two algorithm will give different things.

  • Forward backward algorithm will give the probability of each hidden states. Here is an example. Note, each column in the table sum up to $1$.

enter image description here

  • Viterbi algorithm will give the most probable sequence of hidden states. Here is an example. Note, there is also a probability associated with this hidden state sequence. This sequence has max prob. over all other sequences (e.g., $2^4=16$ sequences from all Sunny to all Rainy).

enter image description here

Here is some R code for the demo

# in education setting,
# hidden state: Rainy and Sunny
# observation: Walk, Shop, Clean

# state transition
P <- as.matrix(rbind(c(0.7,0.3),

# emission prob
R <- as.matrix(rbind(c(0.1, 0.4, 0.5),
                     c(0.6,0.3, 0.1)))

hmm = initHMM(States=c("Rainy","Sunny"),
              Symbols=c("Walk","Shop", "Clean"),

obs=c("Walk","Shop","Walk", "Clean")
print(viterbi(hmm, obs))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.