Markov chain probability calculation - Python
Asked Answered
L

1

0

I have a Python dictionary with state transition probabilities of a Markov-chain model.

dict_m = {('E', 'F'): 0.29032258064516131, ('D', 'F'): 0.39726027397260272, ('D', 'D'): 0.30136986301369861, ('E', 'D'): 0.32258064516129031, ('E', 'E'): 0.38709677419354838, ('D', 'E'): 0.30136986301369861, ('F', 'F'): 0.68152866242038213, ('F', 'E'): 0.10191082802547771, ('F', 'D'): 0.19108280254777071, ('F', 'H'): 0.025477707006369428, ('H', 'F'): 1.0}

Suppose I have a data sequence like following. State transitions are from D to E, E to F, F to E..etc.

s = ['D','E','F','E','E','F','H','F']

Now I need to calculate the probability value by multiplying the probabilities in dict_m. In this case probability of all state transition is

probability = 0.301370*0.290323*0.101911*0.387097*0.290323*0.025478*1.000000

Can anybody help me to formulate this.

print zip(s,s[1:])
[('D', 'E'), ('E', 'F'), ('F', 'E'), ('E', 'E'), ('E', 'F'), ('F', 'H'), ('H', 'F')]
Loco answered 13/8, 2014 at 9:15 Comment(1)
What exactly is the question here? You've figured out how to get the list of keys for your dictionary, what are you stuck on? This isn't a code-writing service; please clarify exactly what the problem is.Aniseed
A
2

You have to use the tuples from the zip to index the dictionary and then multiply all the numbers.

from operator import mul
print reduce(mul, (dict_m[t] for t in zip(s, s[1:])))
Adapa answered 13/8, 2014 at 9:24 Comment(1)
I was stuck on multiplying the probabilities in the dictionary. Thanks very much!.Loco

© 2022 - 2024 — McMap. All rights reserved.