Skip to Content
Support Palestine 🍉  ⬩  Know what's happening

Lab 09: Forward Algorithm in HMMs

In this lab, we dive into the Forward Algorithm, a core component of probabilistic models—especially Hidden Markov Models (HMMs). The algorithm helps compute the probability of an observed sequence, given a model’s parameters. This is crucial in applications like speech recognition, bioinformatics, and NLP.

Introduction

Hidden Markov Models (HMMs) are statistical models where the system being modeled is assumed to be a Markov process with hidden states. The Forward Algorithm efficiently calculates the total probability of an observed sequence by summing over all possible hidden state sequences.

Problem Setup

Let:

  • NN: number of hidden states
  • TT: length of observation sequence
  • AA: state transition probabilities
  • BB: observation/emission probabilities
  • π\pi: initial state distribution
  • O=o1,o2,...,oTO = o_1, o_2, ..., o_T: sequence of observations

The goal is to compute:

P(Oλ)=overallstatesequencesP(O,statesequenceλ)P(O | λ) = ∑ over all state sequences P(O, state sequence | λ)

This is efficiently done using dynamic programming.

Algorithm Overview

Steps:

  1. Initialization
    α1(i)=πibi(o1)\alpha_1(i) = \pi_i \cdot b_i(o_1)

  2. Recursion
    αt(j)=i=1Nαt1(i)aijbj(ot)\alpha*t(j) = \sum*{i=1}^{N} \alpha*{t-1}(i) \cdot a*{ij} \cdot b_j(o_t)

  3. Termination
    P(Oλ)=_i=1NαT(i)P(O \mid \lambda) = \sum\_{i=1}^{N} \alpha_T(i)

Implementation

import numpy as np def forward_algorithm(A, B, pi, observations): N = A.shape[0] # Number of states T = len(observations) # Length of observation sequence alpha = np.zeros((T, N)) # Initialization alpha[0, :] = pi * B[:, observations[0]] # Recursion for t in range(1, T): for j in range(N): alpha[t, j] = np.sum(alpha[t - 1] * A[:, j]) * B[j, observations[t]] # Termination probability = np.sum(alpha[T - 1, :]) return probability, alpha

Example Usage

Click to expand

# Define model parameters A = np.array([[0.7, 0.3], [0.4, 0.6]]) B = np.array([[0.1, 0.4, 0.5], [0.6, 0.3, 0.1]]) pi = np.array([0.6, 0.4]) # Observations (coded as indices) observations = [0, 1, 2] # e.g., observation symbols could be ['H', 'T', 'H'] # Run the forward algorithm probability, alpha_matrix = forward_algorithm(A, B, pi, observations) print("Probability of observing sequence:", probability) print("Alpha matrix:\n", alpha_matrix)

Visualization

Click to expand

import seaborn as sns import matplotlib.pyplot as plt def visualize_alpha(alpha): plt.figure(figsize=(10, 4)) sns.heatmap(alpha, annot=True, fmt=".4f", cmap="Blues") plt.title("Forward Probabilities (Alpha Matrix)") plt.xlabel("States") plt.ylabel("Time Step") plt.show() visualize_alpha(alpha_matrix)

Time and Space Complexity

StepTime ComplexitySpace Complexity
InitializationO(N)O(N)O(NT)O(NT)
RecursionO(N2T)O(N^2T)O(NT)O(NT)
TerminationO(N)O(N)O(1)O(1)
  • NN: Number of states
  • TT: Length of observation sequence

Applications

  1. Speech Recognition: Predicting spoken words from audio input.
  2. Gene Prediction: Modeling DNA sequences.
  3. POS Tagging: Inferring grammatical structures from sentence tokens.
  4. Time Series Analysis: Anomaly detection in sensor data.

Conclusion

The Forward Algorithm is a foundational method in probabilistic modeling for sequences. It elegantly leverages dynamic programming to solve a problem that would otherwise require exponential time. Mastering this algorithm is essential for understanding more advanced HMM tasks like training and decoding.

References

  1. Rabiner, L. R.
    A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition, Proceedings of the IEEE.

  2. MIT 6.864 Lecture on HMMs

  3. YouTube: HMM Tutorial Video



💡

Some of my latest posts on LinkedIn

Last updated on