top of page

Forward Algorithm (Hidden Markov Model)

About the Algorythm Recipe 🍰

Imagine you're trying to predict the weather, but you can't directly see it. Instead, you can see people's reactions, like whether they're carrying an umbrella or wearing a coat.

The forward algorithm in a hidden Markov model helps you figure out the likelihood of different sequences of hidden states (like sunny, rainy, or cloudy days) that could have led to the observed reactions (people with or without umbrellas). It does this by calculating the probability of being in each state at each time step, taking into account both the current observation and the probabilities of transitioning between states.

The forward algorithm calculates the probability of observing a sequence of events given a hidden Markov model, by considering the current observation and previous probabilities. It's like tracking the likelihood of a story unfolding step by step, accounting for all possible paths it could take.

Cookin' time! 🍳


import numpy as np

def forward_algorithm(emission_probs, initial_probs, transition_probs):
  """
  This function computes the forward probabilities for a given HMM.

  Args:
      emission_probs: A numpy array of shape (num_states, num_emissions) representing the emission probabilities.
      initial_probs: A numpy array of shape (num_states,) representing the initial state probabilities.
      transition_probs: A numpy array of shape (num_states, num_states) representing the transition probabilities.

  Returns:
      A numpy array of shape (num_states, sequence_length) representing the forward probabilities for each state in the sequence.
  """

  # Get the number of states, number of emissions, and sequence length
  num_states = emission_probs.shape[0]
  num_emissions = emission_probs.shape[1]
  sequence_length = len(emission_sequence)  # Assuming emission_sequence is defined elsewhere

  # Initialize the forward probabilities array
  forward_probs = np.zeros((num_states, sequence_length))

  # Set the initial probabilities
  forward_probs[:, 0] = initial_probs * emission_probs[:, emission_sequence[0]]

  # Calculate forward probabilities for the remaining time steps
  for t in range(1, sequence_length):
    for i in range(num_states):
      # Sum over all previous states
      forward_probs[i, t] = np.sum(forward_probs[:, t-1] * transition_probs[:, i]) * emission_probs[i, emission_sequence[t]]

  return forward_probs

# Example usage (replace with your actual data)
emission_sequence = [1, 0, 2]  # Example sequence of emissions
initial_probs = np.array([0.5, 0.5])
transition_probs = np.array([[0.7, 0.3], [0.2, 0.8]])
emission_probs = np.array([[0.4, 0.6], [0.2, 0.8], [0.7, 0.3]])  # Example emission probabilities

forward_probs = forward_algorithm(emission_probs, initial_probs, transition_probs)

print(forward_probs)

This code defines a function forward_algorithm that takes the emission probabilities, initial state probabilities, and transition probabilities as input. It then calculates the forward probabilities for each state in the sequence using a loop. We'll discuss this #algorythm at a later segment :)


Note:

  • This code snippet only implements the Forward Algorithm. There are other algorithms for HMMs like the Viterbi Algorithm and Backward Algorithm.

  • This is a basic example, and real-world implementations might involve additional functionalities and optimizations.

bottom of page