0% found this document useful (0 votes)
42 views2 pages

Assignment 5

Uploaded by

nihal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Assignment 5

Uploaded by

nihal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

assignment-5

November 12, 2024

[1]: import numpy as np

class HiddenMarkovModel:
def __init__(self, transition_probs, emission_probs, initial_probs):
self.A = transition_probs # State transition probabilities
self.B = emission_probs # Observation probabilities
self.pi = initial_probs # Initial state probabilities
self.num_states = len(self.A)

def forward(self, observations):


T = len(observations)
alpha = np.zeros((T, self.num_states))
alpha[0, :] = self.pi * self.B[:, observations[0]]

for t in range(1, T):


for j in range(self.num_states):
alpha[t, j] = np.dot(alpha[t - 1, :], self.A[:, j]) * self.B[j,␣
↪observations[t]]

probability = np.sum(alpha[-1, :])


return alpha, probability

def viterbi(self, observations):


T = len(observations)
delta = np.zeros((T, self.num_states))
psi = np.zeros((T, self.num_states), dtype=int)
delta[0, :] = self.pi * self.B[:, observations[0]]

for t in range(1, T):


for j in range(self.num_states):
delta[t, j] = np.max(delta[t - 1, :] * self.A[:, j]) * self.
↪B[j, observations[t]]

psi[t, j] = np.argmax(delta[t - 1, :] * self.A[:, j])

states = np.zeros(T, dtype=int)


states[-1] = np.argmax(delta[-1, :])
for t in range(T - 2, -1, -1):

1
states[t] = psi[t + 1, states[t + 1]]

return states

# Example usage
transition_probs = np.array([[0.7, 0.3], [0.4, 0.6]])
emission_probs = np.array([[0.9, 0.1], [0.2, 0.8]])
initial_probs = np.array([0.6, 0.4])
observations = [0, 1, 0]

hmm = HiddenMarkovModel(transition_probs, emission_probs, initial_probs)


alpha, prob = hmm.forward(observations)
states = hmm.viterbi(observations)

print("Forward probability matrix:\n", alpha)


print("Probability of observation sequence:", prob)
print("Most likely states:", states)

Forward probability matrix:


[[0.54 0.08 ]
[0.041 0.168 ]
[0.08631 0.02262]]
Probability of observation sequence: 0.10893000000000003
Most likely states: [0 1 0]

[ ]:

You might also like