Forward algorithm

From HandWiki
Short description: Hidden Markov model algorithm


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.

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 efficient in the context of directed graphs of variables (see sum-product networks).

For an HMM such as this one:

Temporal evolution of a hidden Markov model

this probability is written as [math]\displaystyle{ p(x_t | y_{1:t} ) }[/math]. Here [math]\displaystyle{ x(t) }[/math] is the hidden state which is abbreviated as [math]\displaystyle{ x_t }[/math] and [math]\displaystyle{ y_{1:t} }[/math] are the observations [math]\displaystyle{ 1 }[/math] to [math]\displaystyle{ t }[/math].

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 [math]\displaystyle{ p(x_t | y_{1:T} ) }[/math] for [math]\displaystyle{ 1 \lt t \lt T }[/math]. 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 [math]\displaystyle{ p(x_{0:t}|y_{0:t}) }[/math].

History

The forward algorithm is one of the algorithms used to solve the decoding problem. Since the development of speech recognition[1] and pattern recognition and related fields like computational biology which use HMMs, the forward algorithm has gained popularity.

Algorithm

The goal of the forward algorithm is to compute the joint probability [math]\displaystyle{ p(x_t,y_{1:t}) }[/math], where for notational convenience we have abbreviated [math]\displaystyle{ x(t) }[/math] as [math]\displaystyle{ x_t }[/math] and [math]\displaystyle{ (y(1), y(2), ..., y(t)) }[/math] as [math]\displaystyle{ y_{1:t} }[/math]. Computing [math]\displaystyle{ p(x_t,y_{1:t}) }[/math] directly would require marginalizing over all possible state sequences [math]\displaystyle{ \{x_{1:t-1}\} }[/math], the number of which grows exponentially with [math]\displaystyle{ t }[/math]. 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

[math]\displaystyle{ \alpha_t(x_t) = p(x_t,y_{1:t}) = \sum_{x_{t-1}}p(x_t,x_{t-1},y_{1:t}) }[/math].

Using the chain rule to expand [math]\displaystyle{ p(x_t,x_{t-1},y_{1:t}) }[/math], we can then write

[math]\displaystyle{ \alpha_t(x_t) = \sum_{x_{t-1}}p(y_t|x_t,x_{t-1},y_{1:t-1})p(x_t|x_{t-1},y_{1:t-1})p(x_{t-1},y_{1:t-1}) }[/math].

Because [math]\displaystyle{ y_t }[/math] is conditionally independent of everything but [math]\displaystyle{ x_t }[/math], and [math]\displaystyle{ x_t }[/math] is conditionally independent of everything but [math]\displaystyle{ x_{t-1} }[/math], this simplifies to

[math]\displaystyle{ \alpha_t(x_t) = p(y_t|x_t)\sum_{x_{t-1}}p(x_t|x_{t-1})\alpha_{t-1}(x_{t-1}) }[/math].

Thus, since [math]\displaystyle{ p(y_t|x_t) }[/math] and [math]\displaystyle{ p(x_t|x_{t-1}) }[/math] are given by the model's emission distributions and transition probabilities, one can quickly calculate [math]\displaystyle{ \alpha_t(x_t) }[/math] from [math]\displaystyle{ \alpha_{t-1}(x_{t-1}) }[/math] and avoid incurring exponential computation time.

The initial condition is set as some prior probability over [math]\displaystyle{ x_0 }[/math] as

[math]\displaystyle{ \alpha_0(x_0) = p(x_0) }[/math] such that [math]\displaystyle{ \sum_{x_0} \alpha_0(x_0) = 1. }[/math]

Once the joint probability [math]\displaystyle{ \alpha_t(x_t) = p(x_t,y_{1:t}) }[/math] has been computed using the forward algorithm, we can easily obtain the related joint probability [math]\displaystyle{ p(y_{1:t}) }[/math] as

[math]\displaystyle{ \alpha_t = p(y_{1:t}) = \sum_{x_t} p(x_t, y_{1:t}) = \sum_{x_t} \alpha_t(x_t) }[/math]

and the required conditional probability [math]\displaystyle{ p(x_t|y_{1:t}) }[/math] as

[math]\displaystyle{ p(x_t|y_{1:t}) = \frac{p(x_t,y_{1:t})}{p(y_{1:t})} = \frac{\alpha_t(x_t)}{\alpha_t}. }[/math]

Once the conditional probability has been calculated, we can also find the point estimate of [math]\displaystyle{ x_t }[/math]. For instance, the MAP estimate of [math]\displaystyle{ x_t }[/math] is given by

[math]\displaystyle{ \widehat{x}_t^{MAP} = \arg \max_{x_t} \; p(x_t|y_{1:t}) = \arg \max_{x_t} \; \alpha_t(x_t), }[/math]

while the MMSE estimate of [math]\displaystyle{ x_t }[/math] is given by

[math]\displaystyle{ \widehat{x}_t^{MMSE} = \mathbb{E}[x_t|y_{1:t}] = \sum_{x_t} x_t p(x_t|y_{1:t}) = \frac{1}{\alpha_t}\sum_{x_t} x_t \alpha_t(x_t). }[/math]

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

  1. Initialize
    [math]\displaystyle{ t = 0 }[/math],
    transition probabilities, [math]\displaystyle{ p(x_t|x_{t-1}) }[/math],
    emission probabilities, [math]\displaystyle{ p(y_j|x_i) }[/math],
    observed sequence, [math]\displaystyle{ y_{1:T} }[/math]
    prior probability, [math]\displaystyle{ \alpha_0(x_0) }[/math]
  2. For [math]\displaystyle{ t = 1 }[/math] to [math]\displaystyle{ T }[/math]
    [math]\displaystyle{ \alpha_t(x_t) = p(y_t|x_t)\sum_{x_{t-1}}p(x_t|x_{t-1})\alpha_{t-1}(x_{t-1}) }[/math].
  3. Calculate [math]\displaystyle{ \alpha_T = \sum_{x_T} \alpha_T(x_T) }[/math]
  4. Return [math]\displaystyle{ p(x_T|y_{1:T})= \frac{\alpha_T(x_T)}{\alpha_T} }[/math]

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 [math]\displaystyle{ 3^3=27 }[/math] 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, [math]\displaystyle{ \alpha_t(x_t) = p(x_t,y_{1:t}) = p(y_t|x_t)\sum_{x_{t-1}}p(x_t|x_{t-1})\alpha_{t-1}(x_{t-1}) }[/math] as shown in the above derivation. Hence, we can calculate the probabilities as the product of the appropriate observation/emission probability, [math]\displaystyle{ p(y_t|x_t) }[/math] ( probability of state [math]\displaystyle{ y_t }[/math] 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 [math]\displaystyle{ \alpha }[/math]'s and transition probabilities.

Applications of the algorithm

The forward algorithm is mostly used in applications that need us to determine the probability of being in a specific state when we know about the sequence of observations. We first calculate the probabilities over the states computed for the previous observation and use them for the current observations, and then extend it out for the next step using the transition probability table. The approach basically caches all the intermediate state probabilities so they are computed only once. This helps us to compute a fixed state path. The process is also called posterior decoding. The algorithm computes probability much more efficiently than the naive approach, which very quickly ends up in a combinatorial explosion. Together, they can provide the probability of a given emission/observation at each position in the sequence of observations. It is from this information that a version of the most likely state path is computed ("posterior decoding"). The algorithm can be applied wherever we can train a model as we receive data using Baum-Welch[2] or any general EM algorithm. The Forward algorithm will then tell us about the probability of data with respect to what is expected from our model. One of the applications can be in the domain of Finance, where it can help decide on when to buy or sell tangible assets. It can have applications in all fields where we apply Hidden Markov Models. The popular ones include Natural language processing domains like tagging part-of-speech and speech recognition.[1] Recently it is also being used in the domain of Bioinformatics. Forward algorithm can also be applied to perform Weather speculations. We can have a HMM describing the weather and its relation to the state of observations for few consecutive days (some examples could be dry, damp, soggy, sunny, cloudy, rainy etc.). We can consider calculating the probability of observing any sequence of observations recursively given the HMM. We can then calculate the probability of reaching an intermediate state as the sum of all possible paths to that state. Thus the partial probabilities for the final observation will hold the probability of reaching those states going through all possible paths.

Variants of the algorithm

Hybrid Forward Algorithm:[3] A variant of the Forward Algorithm called Hybrid Forward Algorithm (HFA) can be used for the construction of radial basis function (RBF) neural networks with tunable nodes. The RBF neural network is constructed by the conventional subset selection algorithms. The network structure is determined by combining both the stepwise forward network configuration and the continuous RBF parameter optimization. It is used to efficiently and effectively produce a parsimonious RBF neural network that generalizes well. It is achieved through simultaneous network structure determination and parameter optimization on the continuous parameter space. HFA tackles the mixed integer hard problem using an integrated analytic framework, leading to improved network performance and reduced memory usage for the network construction.

Forward Algorithm for Optimal Control in Hybrid Systems:[4] This variant of Forward algorithm is motivated by the structure of manufacturing environments that integrate process and operations control. We derive a new property of the optimal state trajectory structure which holds under a modified condition on the cost function. This allows us to develop a low-complexity, scalable algorithm for explicitly determining the optimal controls, which can be more efficient than Forward Algorithm.

Continuous Forward Algorithm:[5] A continuous forward algorithm (CFA) can be used for nonlinear modelling and identification using radial basis function (RBF) neural networks. The proposed algorithm performs the two tasks of network construction and parameter optimization within an integrated analytic framework, and offers two important advantages. First, the model performance can be significantly improved through continuous parameter optimization. Secondly, the neural representation can be built without generating and storing all candidate regressors, leading to significantly reduced memory usage and computational complexity.

Complexity

Complexity of Forward Algorithm is [math]\displaystyle{ \Theta(nm^2) }[/math], where [math]\displaystyle{ m }[/math] is the number of hidden or latent variables, like weather in the example above, and [math]\displaystyle{ n }[/math] is the length of the sequence of the observed variable. This is clear reduction from the adhoc method of exploring all the possible states with a complexity of [math]\displaystyle{ \Theta(nm^n) }[/math].

See also

References

  1. 1.0 1.1 Lawrence R. Rabiner, "A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition". Proceedings of the IEEE, 77 (2), p. 257–286, February 1989. 10.1109/5.18626
  2. Zhang, Yanxue, Dongmei Zhao, and Jinxing Liu. "The Application of Baum-Welch Algorithm in Multistep Attack." The Scientific World Journal 2014.
  3. Peng, Jian-Xun, Kang Li, and De-Shuang Huang. "A hybrid forward algorithm for RBF neural network construction." Neural Networks, IEEE Transactions on 17.6 (2006): 1439-1451.
  4. Zhang, Ping, and Christos G. Cassandras. "An improved forward algorithm for optimal control of a class of hybrid systems." Automatic Control, IEEE Transactions on 47.10 (2002): 1735-1739.
  5. Peng, Jian-Xun, Kang Li, and George W. Irwin. "A novel continuous forward algorithm for RBF neural modelling." Automatic Control, IEEE Transactions on 52.1 (2007): 117-122.

Further reading

  • Russell and Norvig's Artificial Intelligence, a Modern Approach, starting on page 570 of the 2010 edition, provides a succinct exposition of this and related topics
  • Smyth, Padhraic, David Heckerman, and Michael I. Jordan. "Probabilistic independence networks for hidden Markov probability models." Neural computation 9.2 (1997): 227-269. [1]
  • Read, Jonathon. "Hidden Markov Models and Dynamic Programming." University of Oslo (2011). [2]
  • Kohlschein, Christian, An introduction to Hidden Markov Models [3]
  • Manganiello, Fabio, Mirco Marchetti, and Michele Colajanni. Multistep attack detection and alert correlation in intrusion detection systems. Information Security and Assurance. Springer Berlin Heidelberg, 2011. 101-110. [4]
  • Zhang, Ping, and Christos G. Cassandras. "An improved forward algorithm for optimal control of a class of hybrid systems." Automatic Control, IEEE Transactions on 47.10 (2002): 1735-1739.
  • Stratonovich, R. L. "Conditional markov processes". Theory of Probability & Its Applications 5, no. 2 (1960): 156178.

Softwares