[英]Markov Chain: Finding terminal state calculation
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.我查阅了以下页面,但我无法在 java/python 中编写产生正确输出并通过所有测试用例的代码。 I'd appreciate any and all help.我很感激任何和所有的帮助。
Markov chain probability calculation - Python 马尔可夫链概率计算 - 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.编写一个函数 answer(m) ,它接受一个非负整数数组,表示该状态进入下一个状态的次数,并为每个终止状态返回一个整数数组,给出每个终止状态的确切概率,表示为每个状态的分子,然后是所有状态最后和最简单形式的分母。 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.矩阵最多为 10 x 10。可以保证无论矿石处于哪种状态,都存在从该状态到终端状态的路径。 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.矿石从状态 0 开始。在计算过程中,分母将适合一个有符号的 32 位整数,只要分数被有规律地简化。 For example, consider the matrix m:例如,考虑矩阵 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
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就我而言,这个 PDF 对我帮助很大: 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.正如 Ana 所说,您需要对矩阵进行排序,请记住同时对行和列进行排序以获得正确的结果。
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.如果您从唯一的一个状态开始,1x1 总是 100%,并且它必须终止,因为没有其他状态。
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. Ana 的回答中的最后两个边缘情况(我认为应该接受)不是边缘情况,坦率地说,它们是常规情况,因此您需要正常计算答案。
As an alternative approach, one may consider Engel's algorithm for absorbing Markov chains to compute absorption probabilities.作为一种替代方法,可以考虑使用恩格尔吸收马尔可夫链的算法来计算吸收概率。 This requires no matrix inversion / linear system solution, and hence there is no need in rational number arithmetic.这不需要矩阵求逆/线性系统解决方案,因此不需要有理数算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.