I want to calculate the probability of several sequences in a Markov Chain. I got the Markov Chain ready, but I am not sure how to easily calculate specific sequence probabilities.
My pandas dataframe with A-E on the left as the index and A-E on the top as columns is called Markov, looks as follows:
A B C D E
A 0.3 0.2 0.5 0.0 0.2
B 0.2 0.4 0 0 0.4
C 0.5 0.4 0 0.1 0
D 0.2 0.2 0.2 0.2 0.2
E 0.6 0.1 0.1 0.1 0.1
let's assume I want to check the probability of the sequence called sequence: ['A', 'C', 'D']. Which would mean the transition A to C, C to D. It should result in 0.05.
I succeeded by using the pandas .at function:
markov.at[sequence[0], sequence[1]] * markov.at[sequence[1], sequence[2]].
However, I would like to build a function that when I hand it a table of sequences on each row which vary in length, it calculates the corresponding sequence probabilities. In my approach, I have to manually alter the code each time I want to check a specific sequence.
How could I achieve this? Am I overlooking a building feature of pandas to perform such calculations?