I'm trying to figure out this problem. Hopefully someone can tell me how to complete this. I consulted the following pages, but I was unable to write a code in java/python that produces the correct output and passes all test cases. I'd appreciate any and all help.
Markov chain probability calculation - Python
Calculating Markov chain probabilities with values too large to exponentiate
Write a function answer(m) that takes an array of array of nonnegative ints representing how many times that state has gone to the next state and return an array of ints for each terminal state giving the exact probabilities of each terminal state, represented as the numerator for each state, then the denominator for all of them at the end and in simplest form. The matrix is at most 10 by 10. It is guaranteed that no matter which state the ore is in, there is a path from that state to a terminal state. That is, the processing will always eventually end in a stable state. The ore starts in state 0. The denominator will fit within a signed 32-bit integer during the calculation, as long as the fraction is simplified regularly. For example, consider the matrix m:
[
[0,1,0,0,0,1], # s0, the initial state, goes to s1 and s5 with equal probability
[4,0,0,3,2,0], # s1 can become s0, s3, or s4, but with different probabilities
[0,0,0,0,0,0], # s2 is terminal, and unreachable (never observed in practice)
[0,0,0,0,0,0], # s3 is terminal
[0,0,0,0,0,0], # s4 is terminal
[0,0,0,0,0,0], # s5 is terminal
]
So, we can consider different paths to terminal states, such as:
s0 -> s1 -> s3
s0 -> s1 -> s0 -> s1 -> s0 -> s1 -> s4
s0 -> s1 -> s0 -> s5
Tracing the probabilities of each, we find that
s2 has probability 0
s3 has probability 3/14
s4 has probability 1/7
s5 has probability 9/14
The absorbing states are A and B. Absorbing states in a transition diagram have no arrows pointing away from them. In a transition matrix, a row with a 1 on the diagonal and zeros everywhere else indicates an absorbing state. each non-absorbing state to at least one of the absorbing states.
The Markov chain X(t) is time-homogeneous if P(Xn+1 = j|Xn = i) = P(X1 = j|X0 = i), i.e. the transition probabilities do not depend on time n. If this is the case, we write pij = P(X1 = j|X0 = i) for the probability to go from i to j in one step, and P = (pij) for the transition matrix.
Definition: The state of a Markov chain at time t is the value of Xt. For example, if Xt = 6, we say the process is in state 6 at time t. Definition: The state space of a Markov chain, S, is the set of values that each Xt can take. For example, S = {1,2,3,4,5,6,7}.
I'm not sure what the results for the edge cases should be, but what I did for this problem is:
Side notes:
I know it's a bit old topic but maybe someone will be interested.
In my case, this PDF helped me a lot: https://math.dartmouth.edu/archive/m20x06/public_html/Lecture14.pdf
The algorithm is easy to implement.
As Ana said you need to sort matrix, remember to sort rows and columns at the same time to get proper results.
Regarding edge cases:
1x1 is always 100% if you start from the only one state and it must terminal as there is no other state.
if there is only one nonterminal state, then the result will be the same as this row. No calculation needed.
The last two edge cases from Ana's answer (which should be accepted in my opinion) are not the edge cases, to be frank, they are regular cases so you need to calculate the answer normally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With