2
$\begingroup$

I am learning Hidden Markov Model and its implementation for Stock Price Prediction. I am trying to implement the Forward Algorithm according to this paper.

enter image description here

Here I found an implementation of the Forward Algorithm in Python.

import pandas as pd
import numpy as np

V = np.array([0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 2]) 

# Transition Probabilities
a = np.array(((0.54, 0.46), (0.49, 0.51)))

# Emission Probabilities
b = np.array(((0.16, 0.26, 0.58), (0.25, 0.28, 0.47)))

# # Equal Probabilities for the initial distribution
pi = np.array((0.5, 0.5))

def forward(V, a, b, pi):
    alpha = np.zeros((V.shape[0], a.shape[0]))
    alpha[0, :] = initial_distribution * b[:, V[0]]

    for t in range(1, V.shape[0]):
        for j in range(a.shape[0]):
            alpha[t, j] = alpha[t - 1].dot(a[:, j]) * b[j, V[t]]

    return alpha

alpha = forward(V, a, b, pi)

But it seems to me that it does not include (c) and (d) steps from the algorithm. So I added them:

def forward(V, a, b, pi):
    p = 1
    alpha = np.zeros((V.shape[0], a.shape[0]))
    alpha[0, :] = pi * b[:, V[0]]

    for t in range(1, V.shape[0]):
        probability_of_observation = 0 #my code
        for j in range(a.shape[0]):
            alpha[t, j] = alpha[t - 1].dot(a[:, j]) * b[j, V[t]]
            probability_of_observation += alpha[t, j]  #my code
        p = p * probability_of_observation #my code

    return p #changed

p = forward(V, a, b, pi)  #changed

Does my code coincide with the given algorithm?

$\endgroup$
0
$\begingroup$

Maybe this python library could help you: hmmlearn

When I tried to build an hmm I used it and it worked well.

| improve this answer | |
$\endgroup$
  • $\begingroup$ thank you for your answer! I am reviewing research paper and I need to understand the algorithm how it works. For me it is hard to understand the algorithm as mathematical notation. That's why I am trying to implement it in Python. If I use the library, I do not understand how the algorithm works. $\endgroup$ – Joe Rakhimov May 13 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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